metadata.pl 20 KB

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