2
0

metadata.pl 20 KB

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