metadata.pl 20 KB

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