metadata.pl 20 KB

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