metadata.pl 21 KB

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