metadata.pl 20 KB

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