metadata.pl 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. #!/usr/bin/env perl
  2. use FindBin;
  3. use lib "$FindBin::Bin";
  4. use strict;
  5. use metadata;
  6. my %board;
  7. sub confstr($) {
  8. my $conf = shift;
  9. $conf =~ tr#/\.\-/#___#;
  10. return $conf;
  11. }
  12. sub parse_target_metadata() {
  13. my $file = shift @ARGV;
  14. my ($target, @target, $profile);
  15. my %target;
  16. open FILE, "<$file" or do {
  17. warn "Can't open file '$file': $!\n";
  18. return;
  19. };
  20. while (<FILE>) {
  21. chomp;
  22. /^Target:\s*(.+)\s*$/ and do {
  23. my $name = $1;
  24. $target = {
  25. id => $name,
  26. board => $name,
  27. boardconf => confstr($name),
  28. conf => confstr($name),
  29. profiles => [],
  30. features => [],
  31. depends => [],
  32. subtargets => []
  33. };
  34. push @target, $target;
  35. $target{$name} = $target;
  36. if ($name =~ /([^\/]+)\/([^\/]+)/) {
  37. push @{$target{$1}->{subtargets}}, $2;
  38. $target->{board} = $1;
  39. $target->{boardconf} = confstr($1);
  40. $target->{subtarget} = 1;
  41. $target->{parent} = $target{$1};
  42. }
  43. };
  44. /^Target-Name:\s*(.+)\s*$/ and $target->{name} = $1;
  45. /^Target-Path:\s*(.+)\s*$/ and $target->{path} = $1;
  46. /^Target-Arch:\s*(.+)\s*$/ and $target->{arch} = $1;
  47. /^Target-Arch-Packages:\s*(.+)\s*$/ and $target->{arch_packages} = $1;
  48. /^Target-Features:\s*(.+)\s*$/ and $target->{features} = [ split(/\s+/, $1) ];
  49. /^Target-Depends:\s*(.+)\s*$/ and $target->{depends} = [ split(/\s+/, $1) ];
  50. /^Target-Description:/ and $target->{desc} = get_multiline(*FILE);
  51. /^Target-Optimization:\s*(.+)\s*$/ and $target->{cflags} = $1;
  52. /^CPU-Type:\s*(.+)\s*$/ and $target->{cputype} = $1;
  53. /^Linux-Version:\s*(.+)\s*$/ and $target->{version} = $1;
  54. /^Linux-Release:\s*(.+)\s*$/ and $target->{release} = $1;
  55. /^Linux-Kernel-Arch:\s*(.+)\s*$/ and $target->{karch} = $1;
  56. /^Default-Subtarget:\s*(.+)\s*$/ and $target->{def_subtarget} = $1;
  57. /^Default-Packages:\s*(.+)\s*$/ and $target->{packages} = [ split(/\s+/, $1) ];
  58. /^Target-Profile:\s*(.+)\s*$/ and do {
  59. $profile = {
  60. id => $1,
  61. name => $1,
  62. packages => []
  63. };
  64. push @{$target->{profiles}}, $profile;
  65. };
  66. /^Target-Profile-Name:\s*(.+)\s*$/ and $profile->{name} = $1;
  67. /^Target-Profile-Packages:\s*(.*)\s*$/ and $profile->{packages} = [ split(/\s+/, $1) ];
  68. /^Target-Profile-Description:\s*(.*)\s*/ and $profile->{desc} = get_multiline(*FILE);
  69. /^Target-Profile-Config:/ and $profile->{config} = get_multiline(*FILE, "\t");
  70. /^Target-Profile-Kconfig:/ and $profile->{kconfig} = 1;
  71. }
  72. close FILE;
  73. foreach my $target (@target) {
  74. if (@{$target->{subtargets}} > 0) {
  75. $target->{profiles} = [];
  76. next;
  77. }
  78. @{$target->{profiles}} > 0 or $target->{profiles} = [
  79. {
  80. id => 'Default',
  81. name => 'Default',
  82. packages => []
  83. }
  84. ];
  85. }
  86. return @target;
  87. }
  88. sub gen_kconfig_overrides() {
  89. my %config;
  90. my %kconfig;
  91. my $package;
  92. my $pkginfo = shift @ARGV;
  93. my $cfgfile = shift @ARGV;
  94. # parameter 2: build system config
  95. open FILE, "<$cfgfile" or return;
  96. while (<FILE>) {
  97. /^(CONFIG_.+?)=(.+)$/ and $config{$1} = 1;
  98. }
  99. close FILE;
  100. # parameter 1: package metadata
  101. open FILE, "<$pkginfo" or return;
  102. while (<FILE>) {
  103. /^Package:\s*(.+?)\s*$/ and $package = $1;
  104. /^Kernel-Config:\s*(.+?)\s*$/ and do {
  105. my @config = split /\s+/, $1;
  106. foreach my $config (@config) {
  107. my $val = 'm';
  108. my $override;
  109. if ($config =~ /^(.+?)=(.+)$/) {
  110. $config = $1;
  111. $override = 1;
  112. $val = $2;
  113. }
  114. if ($config{"CONFIG_PACKAGE_$package"} and ($config ne 'n')) {
  115. next if $kconfig{$config} eq 'y';
  116. $kconfig{$config} = $val;
  117. } elsif (!$override) {
  118. $kconfig{$config} or $kconfig{$config} = 'n';
  119. }
  120. }
  121. };
  122. };
  123. close FILE;
  124. foreach my $kconfig (sort keys %kconfig) {
  125. if ($kconfig{$kconfig} eq 'n') {
  126. print "# $kconfig is not set\n";
  127. } else {
  128. print "$kconfig=$kconfig{$kconfig}\n";
  129. }
  130. }
  131. }
  132. sub merge_package_lists($$) {
  133. my $list1 = shift;
  134. my $list2 = shift;
  135. my @l = ();
  136. my %pkgs;
  137. foreach my $pkg (@$list1, @$list2) {
  138. $pkgs{$pkg} = 1;
  139. }
  140. foreach my $pkg (keys %pkgs) {
  141. push @l, $pkg unless ($pkg =~ /^-/ or $pkgs{"-$pkg"});
  142. }
  143. return sort(@l);
  144. }
  145. sub target_config_features(@) {
  146. my $ret;
  147. while ($_ = shift @_) {
  148. /arm_v(\w+)/ and $ret .= "\tselect arm_v$1\n";
  149. /broken/ and $ret .= "\tdepends on BROKEN\n";
  150. /audio/ and $ret .= "\tselect AUDIO_SUPPORT\n";
  151. /display/ and $ret .= "\tselect DISPLAY_SUPPORT\n";
  152. /dt/ and $ret .= "\tselect USES_DEVICETREE\n";
  153. /gpio/ and $ret .= "\tselect GPIO_SUPPORT\n";
  154. /pci/ and $ret .= "\tselect PCI_SUPPORT\n";
  155. /pcie/ and $ret .= "\tselect PCIE_SUPPORT\n";
  156. /usb/ and $ret .= "\tselect USB_SUPPORT\n";
  157. /usbgadget/ and $ret .= "\tselect USB_GADGET_SUPPORT\n";
  158. /pcmcia/ and $ret .= "\tselect PCMCIA_SUPPORT\n";
  159. /rtc/ and $ret .= "\tselect RTC_SUPPORT\n";
  160. /squashfs/ and $ret .= "\tselect USES_SQUASHFS\n";
  161. /jffs2$/ and $ret .= "\tselect USES_JFFS2\n";
  162. /jffs2_nand/ and $ret .= "\tselect USES_JFFS2_NAND\n";
  163. /ext4/ and $ret .= "\tselect USES_EXT4\n";
  164. /targz/ and $ret .= "\tselect USES_TARGZ\n";
  165. /cpiogz/ and $ret .= "\tselect USES_CPIOGZ\n";
  166. /ubifs/ and $ret .= "\tselect USES_UBIFS\n";
  167. /fpu/ and $ret .= "\tselect HAS_FPU\n";
  168. /spe_fpu/ and $ret .= "\tselect HAS_SPE_FPU\n";
  169. /ramdisk/ and $ret .= "\tselect USES_INITRAMFS\n";
  170. /powerpc64/ and $ret .= "\tselect powerpc64\n";
  171. /nommu/ and $ret .= "\tselect NOMMU\n";
  172. /mips16/ and $ret .= "\tselect HAS_MIPS16\n";
  173. /rfkill/ and $ret .= "\tselect RFKILL_SUPPORT\n";
  174. }
  175. return $ret;
  176. }
  177. sub target_name($) {
  178. my $target = shift;
  179. my $parent = $target->{parent};
  180. if ($parent) {
  181. return $target->{parent}->{name}." - ".$target->{name};
  182. } else {
  183. return $target->{name};
  184. }
  185. }
  186. sub kver($) {
  187. my $v = shift;
  188. $v =~ tr/\./_/;
  189. if (substr($v,0,2) eq "2_") {
  190. $v =~ /(\d+_\d+_\d+)(_\d+)?/ and $v = $1;
  191. } else {
  192. $v =~ /(\d+_\d+)(_\d+)?/ and $v = $1;
  193. }
  194. return $v;
  195. }
  196. sub print_target($) {
  197. my $target = shift;
  198. my $features = target_config_features(@{$target->{features}});
  199. my $help = $target->{desc};
  200. my $confstr;
  201. chomp $features;
  202. $features .= "\n";
  203. if ($help =~ /\w+/) {
  204. $help =~ s/^\s*/\t /mg;
  205. $help = "\thelp\n$help";
  206. } else {
  207. undef $help;
  208. }
  209. my $v = kver($target->{version});
  210. if (@{$target->{subtargets}} == 0) {
  211. $confstr = <<EOF;
  212. config TARGET_$target->{conf}
  213. bool "$target->{name}"
  214. select LINUX_$v
  215. EOF
  216. }
  217. else {
  218. $confstr = <<EOF;
  219. config TARGET_$target->{conf}
  220. bool "$target->{name}"
  221. EOF
  222. }
  223. if ($target->{subtarget}) {
  224. $confstr .= "\tdepends on TARGET_$target->{boardconf}\n";
  225. }
  226. if (@{$target->{subtargets}} > 0) {
  227. $confstr .= "\tselect HAS_SUBTARGETS\n";
  228. grep { /broken/ } @{$target->{features}} and $confstr .= "\tdepends on BROKEN\n";
  229. } else {
  230. $confstr .= $features;
  231. }
  232. if ($target->{arch} =~ /\w/) {
  233. $confstr .= "\tselect $target->{arch}\n";
  234. }
  235. foreach my $dep (@{$target->{depends}}) {
  236. my $mode = "depends on";
  237. my $flags;
  238. my $name;
  239. $dep =~ /^([@\+\-]+)(.+)$/;
  240. $flags = $1;
  241. $name = $2;
  242. next if $name =~ /:/;
  243. $flags =~ /-/ and $mode = "deselect";
  244. $flags =~ /\+/ and $mode = "select";
  245. $flags =~ /@/ and $confstr .= "\t$mode $name\n";
  246. }
  247. $confstr .= "$help\n\n";
  248. print $confstr;
  249. }
  250. sub gen_target_config() {
  251. my @target = parse_target_metadata();
  252. my %defaults;
  253. my @target_sort = sort {
  254. target_name($a) cmp target_name($b);
  255. } @target;
  256. print <<EOF;
  257. choice
  258. prompt "Target System"
  259. default TARGET_ar71xx
  260. reset if !DEVEL
  261. EOF
  262. foreach my $target (@target_sort) {
  263. next if $target->{subtarget};
  264. print_target($target);
  265. }
  266. print <<EOF;
  267. endchoice
  268. choice
  269. prompt "Subtarget" if HAS_SUBTARGETS
  270. EOF
  271. foreach my $target (@target) {
  272. next unless $target->{def_subtarget};
  273. print <<EOF;
  274. default TARGET_$target->{conf}_$target->{def_subtarget} if TARGET_$target->{conf}
  275. EOF
  276. }
  277. print <<EOF;
  278. EOF
  279. foreach my $target (@target) {
  280. next unless $target->{subtarget};
  281. print_target($target);
  282. }
  283. print <<EOF;
  284. endchoice
  285. choice
  286. prompt "Target Profile"
  287. EOF
  288. foreach my $target (@target) {
  289. my $profiles = $target->{profiles};
  290. foreach my $profile (@$profiles) {
  291. print <<EOF;
  292. config TARGET_$target->{conf}_$profile->{id}
  293. bool "$profile->{name}"
  294. depends on TARGET_$target->{conf}
  295. $profile->{config}
  296. EOF
  297. $profile->{kconfig} and print "\tselect PROFILE_KCONFIG\n";
  298. my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
  299. foreach my $pkg (@pkglist) {
  300. print "\tselect DEFAULT_$pkg\n";
  301. $defaults{$pkg} = 1;
  302. }
  303. my $help = $profile->{desc};
  304. if ($help =~ /\w+/) {
  305. $help =~ s/^\s*/\t /mg;
  306. $help = "\thelp\n$help";
  307. } else {
  308. undef $help;
  309. }
  310. print "$help\n";
  311. }
  312. }
  313. print <<EOF;
  314. endchoice
  315. config HAS_SUBTARGETS
  316. bool
  317. config TARGET_BOARD
  318. string
  319. EOF
  320. foreach my $target (@target) {
  321. $target->{subtarget} or print "\t\tdefault \"".$target->{board}."\" if TARGET_".$target->{conf}."\n";
  322. }
  323. print <<EOF;
  324. config TARGET_ARCH_PACKAGES
  325. string
  326. EOF
  327. foreach my $target (@target) {
  328. next if @{$target->{subtargets}} > 0;
  329. print "\t\tdefault \"".($target->{arch_packages} || $target->{board})."\" if TARGET_".$target->{conf}."\n";
  330. }
  331. print <<EOF;
  332. config DEFAULT_TARGET_OPTIMIZATION
  333. string
  334. EOF
  335. foreach my $target (@target) {
  336. next if @{$target->{subtargets}} > 0;
  337. print "\tdefault \"".$target->{cflags}."\" if TARGET_".$target->{conf}."\n";
  338. }
  339. print "\tdefault \"-Os -pipe -funit-at-a-time\"\n";
  340. print <<EOF;
  341. config CPU_TYPE
  342. string
  343. EOF
  344. foreach my $target (@target) {
  345. next if @{$target->{subtargets}} > 0;
  346. print "\tdefault \"".$target->{cputype}."\" if TARGET_".$target->{conf}."\n";
  347. }
  348. print "\tdefault \"\"\n";
  349. my %kver;
  350. foreach my $target (@target) {
  351. my $v = kver($target->{version});
  352. next if $kver{$v};
  353. $kver{$v} = 1;
  354. print <<EOF;
  355. config LINUX_$v
  356. bool
  357. EOF
  358. }
  359. foreach my $def (sort keys %defaults) {
  360. print "\tconfig DEFAULT_".$def."\n";
  361. print "\t\tbool\n\n";
  362. }
  363. }
  364. my %dep_check;
  365. sub __find_package_dep($$) {
  366. my $pkg = shift;
  367. my $name = shift;
  368. my $deps = ($pkg->{vdepends} or $pkg->{depends});
  369. return 0 unless defined $deps;
  370. foreach my $dep (@{$deps}) {
  371. next if $dep_check{$dep};
  372. $dep_check{$dep} = 1;
  373. return 1 if $dep eq $name;
  374. return 1 if ($package{$dep} and (__find_package_dep($package{$dep},$name) == 1));
  375. }
  376. return 0;
  377. }
  378. # wrapper to avoid infinite recursion
  379. sub find_package_dep($$) {
  380. my $pkg = shift;
  381. my $name = shift;
  382. %dep_check = ();
  383. return __find_package_dep($pkg, $name);
  384. }
  385. sub package_depends($$) {
  386. my $a = shift;
  387. my $b = shift;
  388. my $ret;
  389. return 0 if ($a->{submenu} ne $b->{submenu});
  390. if (find_package_dep($a, $b->{name}) == 1) {
  391. $ret = 1;
  392. } elsif (find_package_dep($b, $a->{name}) == 1) {
  393. $ret = -1;
  394. } else {
  395. return 0;
  396. }
  397. return $ret;
  398. }
  399. sub mconf_depends {
  400. my $pkgname = shift;
  401. my $depends = shift;
  402. my $only_dep = shift;
  403. my $res;
  404. my $dep = shift;
  405. my $seen = shift;
  406. my $parent_condition = shift;
  407. $dep or $dep = {};
  408. $seen or $seen = {};
  409. my @t_depends;
  410. $depends or return;
  411. my @depends = @$depends;
  412. foreach my $depend (@depends) {
  413. my $m = "depends on";
  414. my $flags = "";
  415. $depend =~ s/^([@\+]+)// and $flags = $1;
  416. my $vdep;
  417. my $condition = $parent_condition;
  418. next if $condition eq $depend;
  419. next if $seen->{"$parent_condition:$depend"};
  420. next if $seen->{":$depend"};
  421. $seen->{"$parent_condition:$depend"} = 1;
  422. if ($depend =~ /^(.+):(.+)$/) {
  423. if ($1 ne "PACKAGE_$pkgname") {
  424. if ($condition) {
  425. $condition = "$condition && $1";
  426. } else {
  427. $condition = $1;
  428. }
  429. }
  430. $depend = $2;
  431. }
  432. next if $package{$depend} and $package{$depend}->{buildonly};
  433. if ($vdep = $package{$depend}->{vdepends}) {
  434. $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
  435. } else {
  436. $flags =~ /\+/ and do {
  437. # Menuconfig will not treat 'select FOO' as a real dependency
  438. # thus if FOO depends on other config options, these dependencies
  439. # will not be checked. To fix this, we simply emit all of FOO's
  440. # depends here as well.
  441. $package{$depend} and push @t_depends, [ $package{$depend}->{depends}, $condition ];
  442. $m = "select";
  443. next if $only_dep;
  444. };
  445. $flags =~ /@/ or $depend = "PACKAGE_$depend";
  446. if ($condition) {
  447. if ($m =~ /select/) {
  448. next if $depend eq $condition;
  449. $depend = "$depend if $condition";
  450. } else {
  451. $depend = "!($condition) || $depend";
  452. }
  453. }
  454. }
  455. $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
  456. }
  457. foreach my $tdep (@t_depends) {
  458. mconf_depends($pkgname, $tdep->[0], 1, $dep, $seen, $tdep->[1]);
  459. }
  460. foreach my $depend (keys %$dep) {
  461. my $m = $dep->{$depend};
  462. $res .= "\t\t$m $depend\n";
  463. }
  464. return $res;
  465. }
  466. sub print_package_config_category($) {
  467. my $cat = shift;
  468. my %menus;
  469. my %menu_dep;
  470. return unless $category{$cat};
  471. print "menu \"$cat\"\n\n";
  472. my %spkg = %{$category{$cat}};
  473. foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
  474. foreach my $pkg (@{$spkg{$spkg}}) {
  475. next if $pkg->{buildonly};
  476. my $menu = $pkg->{submenu};
  477. if ($menu) {
  478. $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
  479. } else {
  480. $menu = 'undef';
  481. }
  482. $menus{$menu} or $menus{$menu} = [];
  483. push @{$menus{$menu}}, $pkg;
  484. }
  485. }
  486. my @menus = sort {
  487. ($a eq 'undef' ? 1 : 0) or
  488. ($b eq 'undef' ? -1 : 0) or
  489. ($a cmp $b)
  490. } keys %menus;
  491. foreach my $menu (@menus) {
  492. my @pkgs = sort {
  493. package_depends($a, $b) or
  494. ($a->{name} cmp $b->{name})
  495. } @{$menus{$menu}};
  496. if ($menu ne 'undef') {
  497. $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
  498. print "menu \"$menu\"\n";
  499. }
  500. foreach my $pkg (@pkgs) {
  501. my $title = $pkg->{name};
  502. my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
  503. if ($c > 0) {
  504. $title .= ("." x $c). " ". $pkg->{title};
  505. }
  506. $title = "\"$title\"";
  507. print "\t";
  508. $pkg->{menu} and print "menu";
  509. print "config PACKAGE_".$pkg->{name}."\n";
  510. $pkg->{hidden} and $title = "";
  511. print "\t\t".($pkg->{tristate} ? 'tristate' : 'bool')." $title\n";
  512. print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
  513. unless ($pkg->{hidden}) {
  514. $pkg->{default} ||= "m if ALL";
  515. }
  516. if ($pkg->{default}) {
  517. foreach my $default (split /\s*,\s*/, $pkg->{default}) {
  518. print "\t\tdefault $default\n";
  519. }
  520. }
  521. print mconf_depends($pkg->{name}, $pkg->{depends}, 0);
  522. print mconf_depends($pkg->{name}, $pkg->{mdepends}, 0);
  523. print "\t\thelp\n";
  524. print $pkg->{description};
  525. print "\n";
  526. $pkg->{config} and print $pkg->{config}."\n";
  527. }
  528. if ($menu ne 'undef') {
  529. print "endmenu\n";
  530. $menu_dep{$menu} and print "endif\n";
  531. }
  532. }
  533. print "endmenu\n\n";
  534. undef $category{$cat};
  535. }
  536. sub print_package_features() {
  537. keys %features > 0 or return;
  538. print "menu \"Package features\"\n";
  539. foreach my $n (keys %features) {
  540. my @features = sort { $b->{priority} <=> $a->{priority} or $a->{title} cmp $b->{title} } @{$features{$n}};
  541. print <<EOF;
  542. choice
  543. prompt "$features[0]->{target_title}"
  544. default FEATURE_$features[0]->{name}
  545. EOF
  546. foreach my $feature (@features) {
  547. print <<EOF;
  548. config FEATURE_$feature->{name}
  549. bool "$feature->{title}"
  550. EOF
  551. $feature->{description} =~ /\w/ and do {
  552. print "\t\thelp\n".$feature->{description}."\n";
  553. };
  554. }
  555. print "endchoice\n"
  556. }
  557. print "endmenu\n\n";
  558. }
  559. sub gen_package_config() {
  560. parse_package_metadata($ARGV[0]) or exit 1;
  561. print "menuconfig IMAGEOPT\n\tbool \"Image configuration\"\n\tdefault n\n";
  562. foreach my $preconfig (keys %preconfig) {
  563. foreach my $cfg (keys %{$preconfig{$preconfig}}) {
  564. my $conf = $preconfig{$preconfig}->{$cfg}->{id};
  565. $conf =~ tr/\.-/__/;
  566. print <<EOF
  567. config UCI_PRECONFIG_$conf
  568. string "$preconfig{$preconfig}->{$cfg}->{label}" if IMAGEOPT
  569. depends on PACKAGE_$preconfig
  570. default "$preconfig{$preconfig}->{$cfg}->{default}"
  571. EOF
  572. }
  573. }
  574. print "source \"package/*/image-config.in\"\n";
  575. if (scalar glob "package/feeds/*/*/image-config.in") {
  576. print "source \"package/feeds/*/*/image-config.in\"\n";
  577. }
  578. print_package_features();
  579. print_package_config_category 'Base system';
  580. foreach my $cat (sort {uc($a) cmp uc($b)} keys %category) {
  581. print_package_config_category $cat;
  582. }
  583. }
  584. sub get_conditional_dep($$) {
  585. my $condition = shift;
  586. my $depstr = shift;
  587. if ($condition) {
  588. if ($condition =~ /^!(.+)/) {
  589. return "\$(if \$(CONFIG_$1),,$depstr)";
  590. } else {
  591. return "\$(if \$(CONFIG_$condition),$depstr)";
  592. }
  593. } else {
  594. return $depstr;
  595. }
  596. }
  597. sub gen_package_mk() {
  598. my %conf;
  599. my %dep;
  600. my %done;
  601. my $line;
  602. parse_package_metadata($ARGV[0]) or exit 1;
  603. foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
  604. my $config;
  605. my $pkg = $package{$name};
  606. my @srcdeps;
  607. next if defined $pkg->{vdepends};
  608. $config = "\$(CONFIG_PACKAGE_$name)";
  609. if ($config) {
  610. $pkg->{buildonly} and $config = "";
  611. print "package-$config += $pkg->{subdir}$pkg->{src}\n";
  612. if ($pkg->{variant}) {
  613. if (!defined($done{$pkg->{src}})) {
  614. print "\$(curdir)/$pkg->{subdir}$pkg->{src}/default-variant := $pkg->{variant}\n";
  615. }
  616. print "\$(curdir)/$pkg->{subdir}$pkg->{src}/variants += \$(if $config,$pkg->{variant})\n"
  617. }
  618. $pkg->{prereq} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
  619. }
  620. next if $done{$pkg->{src}};
  621. $done{$pkg->{src}} = 1;
  622. if (@{$pkg->{buildtypes}} > 0) {
  623. print "buildtypes-$pkg->{subdir}$pkg->{src} = ".join(' ', @{$pkg->{buildtypes}})."\n";
  624. }
  625. foreach my $spkg (@{$srcpackage{$pkg->{src}}}) {
  626. foreach my $dep (@{$spkg->{depends}}, @{$spkg->{builddepends}}) {
  627. $dep =~ /@/ or do {
  628. $dep =~ s/\+//g;
  629. push @srcdeps, $dep;
  630. };
  631. }
  632. }
  633. foreach my $type (@{$pkg->{buildtypes}}) {
  634. my @extra_deps;
  635. my %deplines;
  636. next unless $pkg->{"builddepends/$type"};
  637. foreach my $dep (@{$pkg->{"builddepends/$type"}}) {
  638. my $suffix = "";
  639. my $condition;
  640. if ($dep =~ /^(.+):(.+)/) {
  641. $condition = $1;
  642. $dep = $2;
  643. }
  644. if ($dep =~ /^(.+)(\/.+)/) {
  645. $dep = $1;
  646. $suffix = $2;
  647. }
  648. my $idx = "";
  649. my $pkg_dep = $package{$dep};
  650. if (defined($pkg_dep) && defined($pkg_dep->{src})) {
  651. $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
  652. } elsif (defined($srcpackage{$dep})) {
  653. $idx = $subdir{$dep}.$dep;
  654. } else {
  655. next;
  656. }
  657. my $depstr = "\$(curdir)/$idx$suffix/compile";
  658. my $depline = get_conditional_dep($condition, $depstr);
  659. if ($depline) {
  660. $deplines{$depline}++;
  661. }
  662. }
  663. my $depline = join(" ", sort keys %deplines);
  664. if ($depline) {
  665. $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/$type/compile += $depline\n";
  666. }
  667. }
  668. my $hasdeps = 0;
  669. my %deplines;
  670. foreach my $deps (@srcdeps) {
  671. my $idx;
  672. my $condition;
  673. my $prefix = "";
  674. my $suffix = "";
  675. if ($deps =~ /^(.+):(.+)/) {
  676. $condition = $1;
  677. $deps = $2;
  678. }
  679. if ($deps =~ /^(.+)(\/.+)/) {
  680. $deps = $1;
  681. $suffix = $2;
  682. }
  683. my $pkg_dep = $package{$deps};
  684. my @deps;
  685. if ($pkg_dep->{vdepends}) {
  686. @deps = @{$pkg_dep->{vdepends}};
  687. } else {
  688. @deps = ($deps);
  689. }
  690. foreach my $dep (@deps) {
  691. $pkg_dep = $package{$deps};
  692. if (defined $pkg_dep->{src}) {
  693. ($pkg->{src} ne $pkg_dep->{src}.$suffix) and $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
  694. } elsif (defined($srcpackage{$dep})) {
  695. $idx = $subdir{$dep}.$dep;
  696. }
  697. undef $idx if $idx eq 'base-files';
  698. if ($idx) {
  699. $idx .= $suffix;
  700. my $depline;
  701. next if $pkg->{src} eq $pkg_dep->{src}.$suffix;
  702. next if $dep{$condition.":".$pkg->{src}."->".$idx};
  703. next if $dep{$pkg->{src}."->($dep)".$idx} and $pkg_dep->{vdepends};
  704. my $depstr;
  705. if ($pkg_dep->{vdepends}) {
  706. $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
  707. $dep{$pkg->{src}."->($dep)".$idx} = 1;
  708. } else {
  709. $depstr = "\$(curdir)/$idx/compile";
  710. $dep{$pkg->{src}."->".$idx} = 1;
  711. }
  712. $depline = get_conditional_dep($condition, $depstr);
  713. if ($depline) {
  714. $deplines{$depline}++;
  715. }
  716. }
  717. }
  718. }
  719. my $depline = join(" ", sort keys %deplines);
  720. if ($depline) {
  721. $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/compile += $depline\n";
  722. }
  723. }
  724. if ($line ne "") {
  725. print "\n$line";
  726. }
  727. foreach my $preconfig (keys %preconfig) {
  728. my $cmds;
  729. foreach my $cfg (keys %{$preconfig{$preconfig}}) {
  730. my $conf = $preconfig{$preconfig}->{$cfg}->{id};
  731. $conf =~ tr/\.-/__/;
  732. $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
  733. }
  734. next unless $cmds;
  735. print <<EOF
  736. ifndef DUMP_TARGET_DB
  737. \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
  738. ( \\
  739. $cmds \\
  740. ) > \$@
  741. ifneq (\$(IMAGEOPT)\$(CONFIG_IMAGEOPT),)
  742. package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
  743. endif
  744. endif
  745. EOF
  746. }
  747. }
  748. sub gen_package_source() {
  749. parse_package_metadata($ARGV[0]) or exit 1;
  750. foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
  751. my $pkg = $package{$name};
  752. if ($pkg->{name} && $pkg->{source}) {
  753. print "$pkg->{name}: ";
  754. print "$pkg->{source}\n";
  755. }
  756. }
  757. }
  758. sub parse_command() {
  759. my $cmd = shift @ARGV;
  760. for ($cmd) {
  761. /^target_config$/ and return gen_target_config();
  762. /^package_mk$/ and return gen_package_mk();
  763. /^package_config$/ and return gen_package_config();
  764. /^kconfig/ and return gen_kconfig_overrides();
  765. /^package_source$/ and return gen_package_source();
  766. }
  767. print <<EOF
  768. Available Commands:
  769. $0 target_config [file] Target metadata in Kconfig format
  770. $0 package_mk [file] Package metadata in makefile format
  771. $0 package_config [file] Package metadata in Kconfig format
  772. $0 kconfig [file] [config] Kernel config overrides
  773. $0 package_source [file] Package source file information
  774. EOF
  775. }
  776. parse_command();