2
0

package-metadata.pl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. #!/usr/bin/env perl
  2. use FindBin;
  3. use lib "$FindBin::Bin";
  4. use strict;
  5. use metadata;
  6. use Getopt::Long;
  7. my %board;
  8. sub version_to_num($) {
  9. my $str = shift;
  10. my $num = 0;
  11. if (defined($str) && $str =~ /^\d+(?:\.\d+)+$/)
  12. {
  13. my @n = (split(/\./, $str), 0, 0, 0, 0);
  14. $num = ($n[0] << 24) | ($n[1] << 16) | ($n[2] << 8) | $n[3];
  15. }
  16. return $num;
  17. }
  18. sub version_filter_list(@) {
  19. my $cmpver = version_to_num(shift @_);
  20. my @items;
  21. foreach my $item (@_)
  22. {
  23. if ($item =~ s/@(lt|le|gt|ge|eq|ne)(\d+(?:\.\d+)+)\b//)
  24. {
  25. my $op = $1;
  26. my $symver = version_to_num($2);
  27. if ($symver > 0 && $cmpver > 0)
  28. {
  29. next unless (($op eq 'lt' && $cmpver < $symver) ||
  30. ($op eq 'le' && $cmpver <= $symver) ||
  31. ($op eq 'gt' && $cmpver > $symver) ||
  32. ($op eq 'ge' && $cmpver >= $symver) ||
  33. ($op eq 'eq' && $cmpver == $symver) ||
  34. ($op eq 'ne' && $cmpver != $symver));
  35. }
  36. }
  37. push @items, $item;
  38. }
  39. return @items;
  40. }
  41. sub gen_kconfig_overrides() {
  42. my %config;
  43. my %kconfig;
  44. my $package;
  45. my $pkginfo = shift @ARGV;
  46. my $cfgfile = shift @ARGV;
  47. my $patchver = shift @ARGV;
  48. # parameter 2: build system config
  49. open FILE, "<$cfgfile" or return;
  50. while (<FILE>) {
  51. /^(CONFIG_.+?)=(.+)$/ and $config{$1} = 1;
  52. }
  53. close FILE;
  54. # parameter 1: package metadata
  55. open FILE, "<$pkginfo" or return;
  56. while (<FILE>) {
  57. /^Package:\s*(.+?)\s*$/ and $package = $1;
  58. /^Kernel-Config:\s*(.+?)\s*$/ and do {
  59. my @config = split /\s+/, $1;
  60. foreach my $config (version_filter_list($patchver, @config)) {
  61. my $val = 'm';
  62. my $override;
  63. if ($config =~ /^(.+?)=(.+)$/) {
  64. $config = $1;
  65. $override = 1;
  66. $val = $2;
  67. }
  68. if ($config{"CONFIG_PACKAGE_$package"} and ($config ne 'n')) {
  69. next if $kconfig{$config} eq 'y';
  70. $kconfig{$config} = $val;
  71. } elsif (!$override) {
  72. $kconfig{$config} or $kconfig{$config} = 'n';
  73. }
  74. }
  75. };
  76. };
  77. close FILE;
  78. foreach my $kconfig (sort keys %kconfig) {
  79. if ($kconfig{$kconfig} eq 'n') {
  80. print "# $kconfig is not set\n";
  81. } else {
  82. print "$kconfig=$kconfig{$kconfig}\n";
  83. }
  84. }
  85. }
  86. my %dep_check;
  87. sub __find_package_dep($$) {
  88. my $pkg = shift;
  89. my $name = shift;
  90. my $deps = $pkg->{depends};
  91. return 0 unless defined $deps;
  92. foreach my $vpkg (@{$deps}) {
  93. foreach my $dep (@{$vpackage{$vpkg}}) {
  94. next if $dep_check{$dep->{name}};
  95. $dep_check{$dep->{name}} = 1;
  96. return 1 if $dep->{name} eq $name;
  97. return 1 if (__find_package_dep($dep, $name) == 1);
  98. }
  99. }
  100. return 0;
  101. }
  102. # wrapper to avoid infinite recursion
  103. sub find_package_dep($$) {
  104. my $pkg = shift;
  105. my $name = shift;
  106. %dep_check = ();
  107. return __find_package_dep($pkg, $name);
  108. }
  109. sub package_depends($$) {
  110. my $a = shift;
  111. my $b = shift;
  112. my $ret;
  113. return 0 if ($a->{submenu} ne $b->{submenu});
  114. if (find_package_dep($a, $b->{name}) == 1) {
  115. $ret = 1;
  116. } elsif (find_package_dep($b, $a->{name}) == 1) {
  117. $ret = -1;
  118. } else {
  119. return 0;
  120. }
  121. return $ret;
  122. }
  123. sub mconf_depends {
  124. my $pkgname = shift;
  125. my $depends = shift;
  126. my $only_dep = shift;
  127. my $res;
  128. my $dep = shift;
  129. my $seen = shift;
  130. my $parent_condition = shift;
  131. $dep or $dep = {};
  132. $seen or $seen = {};
  133. my @t_depends;
  134. $depends or return;
  135. my @depends = @$depends;
  136. foreach my $depend (@depends) {
  137. my $m = "depends on";
  138. my $flags = "";
  139. $depend =~ s/^([@\+]+)// and $flags = $1;
  140. my $condition = $parent_condition;
  141. next if $condition eq $depend;
  142. next if $seen->{"$parent_condition:$depend"};
  143. next if $seen->{":$depend"};
  144. $seen->{"$parent_condition:$depend"} = 1;
  145. if ($depend =~ /^(.+):(.+)$/) {
  146. if ($1 ne "PACKAGE_$pkgname") {
  147. if ($condition) {
  148. $condition = "$condition && $1";
  149. } else {
  150. $condition = $1;
  151. }
  152. }
  153. $depend = $2;
  154. }
  155. if ($flags =~ /\+/) {
  156. my $vdep = $vpackage{$depend};
  157. if ($vdep) {
  158. my @vdeps;
  159. foreach my $v (@$vdep) {
  160. next if $v->{buildonly};
  161. if ($v->{variant_default}) {
  162. unshift @vdeps, $v->{name};
  163. } else {
  164. push @vdeps, $v->{name};
  165. }
  166. }
  167. $depend = shift @vdeps;
  168. if (@vdeps > 1) {
  169. $condition = ($condition ? "$condition && " : '') . join("&&", map { "PACKAGE_$_<PACKAGE_$pkgname" } @vdeps);
  170. } elsif (@vdeps > 0) {
  171. $condition = ($condition ? "$condition && " : '') . "PACKAGE_${vdeps[0]}<PACKAGE_$pkgname";
  172. }
  173. }
  174. # Menuconfig will not treat 'select FOO' as a real dependency
  175. # thus if FOO depends on other config options, these dependencies
  176. # will not be checked. To fix this, we simply emit all of FOO's
  177. # depends here as well.
  178. $package{$depend} and push @t_depends, [ $package{$depend}->{depends}, $condition ];
  179. $m = "select";
  180. next if $only_dep;
  181. $flags =~ /@/ or $depend = "PACKAGE_$depend";
  182. } else {
  183. my $vdep = $vpackage{$depend};
  184. if ($vdep && @$vdep > 0) {
  185. $depend = join("||", map { "PACKAGE_".$_->{name} } @$vdep);
  186. } else {
  187. $flags =~ /@/ or $depend = "PACKAGE_$depend";
  188. }
  189. }
  190. if ($condition) {
  191. if ($m =~ /select/) {
  192. next if $depend eq $condition;
  193. $depend = "$depend if $condition";
  194. } else {
  195. next if $dep->{"$depend if $condition"};
  196. $depend = "!($condition) || $depend" unless $dep->{$condition} eq 'select';
  197. }
  198. }
  199. $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
  200. }
  201. foreach my $tdep (@t_depends) {
  202. mconf_depends($pkgname, $tdep->[0], 1, $dep, $seen, $tdep->[1]);
  203. }
  204. foreach my $depend (keys %$dep) {
  205. my $m = $dep->{$depend};
  206. $res .= "\t\t$m $depend\n";
  207. }
  208. return $res;
  209. }
  210. sub mconf_conflicts {
  211. my $pkgname = shift;
  212. my $depends = shift;
  213. my $res = "";
  214. foreach my $depend (@$depends) {
  215. next unless $package{$depend};
  216. $res .= "\t\tdepends on m || (PACKAGE_$depend != y)\n";
  217. }
  218. return $res;
  219. }
  220. sub print_package_config_category($) {
  221. my $cat = shift;
  222. my %menus;
  223. my %menu_dep;
  224. return unless $category{$cat};
  225. print "menu \"$cat\"\n\n";
  226. my %spkg = %{$category{$cat}};
  227. foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
  228. foreach my $pkg (@{$spkg{$spkg}}) {
  229. next if $pkg->{buildonly};
  230. my $menu = $pkg->{submenu};
  231. if ($menu) {
  232. $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
  233. } else {
  234. $menu = 'undef';
  235. }
  236. $menus{$menu} or $menus{$menu} = [];
  237. push @{$menus{$menu}}, $pkg;
  238. }
  239. }
  240. my @menus = sort {
  241. ($a eq 'undef' ? 1 : 0) or
  242. ($b eq 'undef' ? -1 : 0) or
  243. ($a cmp $b)
  244. } keys %menus;
  245. foreach my $menu (@menus) {
  246. my @pkgs = sort {
  247. package_depends($a, $b) or
  248. ($a->{name} cmp $b->{name})
  249. } @{$menus{$menu}};
  250. if ($menu ne 'undef') {
  251. $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
  252. print "menu \"$menu\"\n";
  253. }
  254. foreach my $pkg (@pkgs) {
  255. next if $pkg->{src}{ignore};
  256. my $title = $pkg->{name};
  257. my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
  258. if ($c > 0) {
  259. $title .= ("." x $c). " ". $pkg->{title};
  260. }
  261. $title = "\"$title\"";
  262. print "\t";
  263. $pkg->{menu} and print "menu";
  264. print "config PACKAGE_".$pkg->{name}."\n";
  265. $pkg->{hidden} and $title = "";
  266. print "\t\t".($pkg->{tristate} ? 'tristate' : 'bool')." $title\n";
  267. print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
  268. unless ($pkg->{hidden}) {
  269. my @def = ("ALL");
  270. if (!exists($pkg->{repository})) {
  271. push @def, "ALL_NONSHARED";
  272. }
  273. if ($pkg->{name} =~ /^kmod-/) {
  274. push @def, "ALL_KMODS";
  275. }
  276. $pkg->{default} ||= "m if " . join("||", @def);
  277. }
  278. if ($pkg->{default}) {
  279. foreach my $default (split /\s*,\s*/, $pkg->{default}) {
  280. print "\t\tdefault $default\n";
  281. }
  282. }
  283. print mconf_depends($pkg->{name}, $pkg->{depends}, 0);
  284. print mconf_depends($pkg->{name}, $pkg->{mdepends}, 0);
  285. print mconf_conflicts($pkg->{name}, $pkg->{conflicts});
  286. print "\t\thelp\n";
  287. print $pkg->{description};
  288. print "\n";
  289. $pkg->{config} and print $pkg->{config}."\n";
  290. }
  291. if ($menu ne 'undef') {
  292. print "endmenu\n";
  293. $menu_dep{$menu} and print "endif\n";
  294. }
  295. }
  296. print "endmenu\n\n";
  297. undef $category{$cat};
  298. }
  299. sub print_package_overrides() {
  300. keys %overrides > 0 or return;
  301. print "\tconfig OVERRIDE_PKGS\n";
  302. print "\t\tstring\n";
  303. print "\t\tdefault \"".join(" ", sort keys %overrides)."\"\n\n";
  304. }
  305. sub gen_package_config() {
  306. parse_package_metadata($ARGV[0]) or exit 1;
  307. print "menuconfig IMAGEOPT\n\tbool \"Image configuration\"\n\tdefault n\n";
  308. print "source \"package/*/image-config.in\"\n";
  309. if (scalar glob "package/feeds/*/*/image-config.in") {
  310. print "source \"package/feeds/*/*/image-config.in\"\n";
  311. }
  312. print_package_config_category 'Base system';
  313. foreach my $cat (sort {uc($a) cmp uc($b)} keys %category) {
  314. print_package_config_category $cat;
  315. }
  316. print_package_overrides();
  317. }
  318. sub and_condition($) {
  319. my $condition = shift;
  320. my @spl_and = split('\&\&', $condition);
  321. if (@spl_and == 1) {
  322. return "\$(CONFIG_$spl_and[0])";
  323. }
  324. return "\$(and " . join (',', map("\$(CONFIG_$_)", @spl_and)) . ")";
  325. }
  326. sub gen_condition ($) {
  327. my $condition = shift;
  328. # remove '!()', just as include/package-ipkg.mk does
  329. $condition =~ s/[()!]//g;
  330. return join("", map(and_condition($_), split('\|\|', $condition)));
  331. }
  332. sub get_conditional_dep($$) {
  333. my $condition = shift;
  334. my $depstr = shift;
  335. if ($condition) {
  336. if ($condition =~ /^!(.+)/) {
  337. return "\$(if " . gen_condition($1) . ",,$depstr)";
  338. } else {
  339. return "\$(if " . gen_condition($condition) . ",$depstr)";
  340. }
  341. } else {
  342. return $depstr;
  343. }
  344. }
  345. sub gen_package_mk() {
  346. my $line;
  347. parse_package_metadata($ARGV[0]) or exit 1;
  348. foreach my $srcname (sort {uc($a) cmp uc($b)} keys %srcpackage) {
  349. my $src = $srcpackage{$srcname};
  350. my $variant_default;
  351. my %deplines = ('' => {});
  352. foreach my $pkg (@{$src->{packages}}) {
  353. foreach my $dep (@{$pkg->{depends}}) {
  354. next if ($dep =~ /@/);
  355. my $condition;
  356. $dep =~ s/\+//g;
  357. if ($dep =~ /^(.+):(.+)/) {
  358. $condition = $1;
  359. $dep = $2;
  360. }
  361. my $vpkg_dep = $vpackage{$dep};
  362. unless (defined $vpkg_dep) {
  363. warn sprintf "WARNING: Makefile '%s' has a dependency on '%s', which does not exist\n",
  364. $src->{makefile}, $dep;
  365. next;
  366. }
  367. # Filter out self-depends
  368. my @vdeps = grep { $srcname ne $_->{src}{name} } @{$vpkg_dep};
  369. foreach my $vdep (@vdeps) {
  370. my $depstr = sprintf '$(curdir)/%s/compile', $vdep->{src}{path};
  371. if (@vdeps > 1) {
  372. $depstr = sprintf '$(if $(CONFIG_PACKAGE_%s),%s)', $vdep->{name}, $depstr;
  373. }
  374. my $depline = get_conditional_dep($condition, $depstr);
  375. if ($depline) {
  376. $deplines{''}{$depline}++;
  377. }
  378. }
  379. }
  380. my $config = '';
  381. $config = sprintf '$(CONFIG_PACKAGE_%s)', $pkg->{name} unless $pkg->{buildonly};
  382. $pkg->{prereq} and printf "prereq-%s += %s\n", $config, $src->{path};
  383. next if $pkg->{buildonly};
  384. printf "package-%s += %s\n", $config, $src->{path};
  385. if ($pkg->{variant}) {
  386. if (!defined($variant_default) or $pkg->{variant_default}) {
  387. $variant_default = $pkg->{variant};
  388. }
  389. printf "\$(curdir)/%s/variants += \$(if %s,%s)\n", $src->{path}, $config, $pkg->{variant};
  390. }
  391. }
  392. if (defined($variant_default)) {
  393. printf "\$(curdir)/%s/default-variant := %s\n", $src->{path}, $variant_default;
  394. }
  395. unless (grep {!$_->{buildonly}} @{$src->{packages}}) {
  396. printf "package- += %s\n", $src->{path};
  397. }
  398. if (@{$src->{buildtypes}} > 0) {
  399. printf "buildtypes-%s = %s\n", $src->{path}, join(' ', @{$src->{buildtypes}});
  400. }
  401. foreach my $type ('', @{$src->{buildtypes}}) {
  402. my $suffix = '';
  403. $suffix = "/$type" if $type;
  404. next unless $src->{"builddepends$suffix"};
  405. defined $deplines{$suffix} or $deplines{$suffix} = {};
  406. foreach my $dep (@{$src->{"builddepends$suffix"}}) {
  407. my $depsuffix = "";
  408. my $deptype = "";
  409. my $condition;
  410. if ($dep =~ /^(.+):(.+)/) {
  411. $condition = $1;
  412. $dep = $2;
  413. }
  414. if ($dep =~ /^(.+)\/(.+)/) {
  415. $dep = $1;
  416. $deptype = $2;
  417. $depsuffix = "/$2";
  418. }
  419. next if $srcname.$suffix eq $dep.$depsuffix;
  420. my $src_dep = $srcpackage{$dep};
  421. unless (defined($src_dep) && (!$deptype || grep { $_ eq $deptype } @{$src_dep->{buildtypes}})) {
  422. warn sprintf "WARNING: Makefile '%s' has a build dependency on '%s', which does not exist\n",
  423. $src->{makefile}, $dep.$depsuffix;
  424. next;
  425. }
  426. my $depstr = sprintf '$(curdir)/%s/compile', $src_dep->{path}.$depsuffix;
  427. my $depline = get_conditional_dep($condition, $depstr);
  428. if ($depline) {
  429. $deplines{$suffix}{$depline}++;
  430. }
  431. }
  432. }
  433. foreach my $suffix (sort keys %deplines) {
  434. my $depline = join(" ", sort keys %{$deplines{$suffix}});
  435. if ($depline) {
  436. $line .= sprintf "\$(curdir)/%s/compile += %s\n", $src->{path}.$suffix, $depline;
  437. }
  438. }
  439. }
  440. if ($line ne "") {
  441. print "\n$line";
  442. }
  443. }
  444. sub gen_package_source() {
  445. parse_package_metadata($ARGV[0]) or exit 1;
  446. foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
  447. my $pkg = $package{$name};
  448. if ($pkg->{name} && $pkg->{source}) {
  449. print "$pkg->{name}: ";
  450. print "$pkg->{source}\n";
  451. }
  452. }
  453. }
  454. sub gen_package_auxiliary() {
  455. parse_package_metadata($ARGV[0]) or exit 1;
  456. foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
  457. my $pkg = $package{$name};
  458. if ($pkg->{name} && $pkg->{repository}) {
  459. print "Package/$name/subdir = $pkg->{repository}\n";
  460. }
  461. if ($pkg->{name} && defined($pkg->{abiversion}) && length($pkg->{abiversion})) {
  462. my $abiv;
  463. if ($pkg->{abiversion} =~ m!^(\d{4})-(\d{2})-(\d{2})-[0-9a-f]{7,40}$!) {
  464. print STDERR "WARNING: Reducing ABI version '$pkg->{abiversion}' of package '$name' to '$1$2$3'\n";
  465. $abiv = "$1$2$3";
  466. }
  467. else {
  468. $abiv = $pkg->{abiversion};
  469. }
  470. foreach my $n (@{$pkg->{provides}}) {
  471. print "Package/$n/abiversion = $abiv\n";
  472. }
  473. }
  474. my %depends;
  475. foreach my $dep (@{$pkg->{depends} || []}) {
  476. if ($dep =~ m!^\+?(?:[^:]+:)?([^@]+)$!) {
  477. $depends{$1}++;
  478. }
  479. }
  480. my @depends = sort keys %depends;
  481. if (@depends > 0) {
  482. foreach my $n (@{$pkg->{provides}}) {
  483. print "Package/$n/depends = @depends\n";
  484. }
  485. }
  486. }
  487. }
  488. sub gen_package_license($) {
  489. my $level = shift;
  490. parse_package_metadata($ARGV[0]) or exit 1;
  491. foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
  492. my $pkg = $package{$name};
  493. if ($pkg->{name}) {
  494. if ($pkg->{license}) {
  495. print "$pkg->{name}: ";
  496. print "$pkg->{license}\n";
  497. if ($pkg->{licensefiles} && $level == 0) {
  498. print "\tFiles: $pkg->{licensefiles}\n";
  499. }
  500. } else {
  501. if ($level == 1) {
  502. print "$pkg->{name}: Missing license! ";
  503. print "Please fix $pkg->{src}{makefile}\n";
  504. }
  505. }
  506. }
  507. }
  508. }
  509. sub gen_version_filtered_list() {
  510. foreach my $item (version_filter_list(@ARGV)) {
  511. print "$item\n";
  512. }
  513. }
  514. sub gen_usergroup_list() {
  515. parse_package_metadata($ARGV[0]) or exit 1;
  516. for my $name (keys %usernames) {
  517. print "user $name $usernames{$name}{id} $usernames{$name}{makefile}\n";
  518. }
  519. for my $name (keys %groupnames) {
  520. print "group $name $groupnames{$name}{id} $groupnames{$name}{makefile}\n";
  521. }
  522. }
  523. sub parse_command() {
  524. GetOptions("ignore=s", \@ignore);
  525. my $cmd = shift @ARGV;
  526. for ($cmd) {
  527. /^mk$/ and return gen_package_mk();
  528. /^config$/ and return gen_package_config();
  529. /^kconfig/ and return gen_kconfig_overrides();
  530. /^source$/ and return gen_package_source();
  531. /^pkgaux$/ and return gen_package_auxiliary();
  532. /^license$/ and return gen_package_license(0);
  533. /^licensefull$/ and return gen_package_license(1);
  534. /^usergroup$/ and return gen_usergroup_list();
  535. /^version_filter$/ and return gen_version_filtered_list();
  536. }
  537. die <<EOF
  538. Available Commands:
  539. $0 mk [file] Package metadata in makefile format
  540. $0 config [file] Package metadata in Kconfig format
  541. $0 kconfig [file] [config] [patchver] Kernel config overrides
  542. $0 source [file] Package source file information
  543. $0 pkgaux [file] Package auxiliary variables in makefile format
  544. $0 license [file] Package license information
  545. $0 licensefull [file] Package license information (full list)
  546. $0 usergroup [file] Package usergroup allocation list
  547. $0 version_filter [patchver] [list...] Filter list of version tagged strings
  548. Options:
  549. --ignore <name> Ignore the source package <name>
  550. EOF
  551. }
  552. parse_command();