package-metadata.pl 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. #!/usr/bin/env perl
  2. use FindBin;
  3. use lib "$FindBin::Bin";
  4. use strict;
  5. use metadata;
  6. use Getopt::Long;
  7. use Time::Piece;
  8. use JSON::PP;
  9. my %board;
  10. sub version_to_num($) {
  11. my $str = shift;
  12. my $num = 0;
  13. if (defined($str) && $str =~ /^\d+(?:\.\d+)+$/)
  14. {
  15. my @n = (split(/\./, $str), 0, 0, 0, 0);
  16. $num = ($n[0] << 24) | ($n[1] << 16) | ($n[2] << 8) | $n[3];
  17. }
  18. return $num;
  19. }
  20. sub version_filter_list(@) {
  21. my $cmpver = version_to_num(shift @_);
  22. my @items;
  23. foreach my $item (@_)
  24. {
  25. if ($item =~ s/@(lt|le|gt|ge|eq|ne)(\d+(?:\.\d+)+)\b//)
  26. {
  27. my $op = $1;
  28. my $symver = version_to_num($2);
  29. if ($symver > 0 && $cmpver > 0)
  30. {
  31. next unless (($op eq 'lt' && $cmpver < $symver) ||
  32. ($op eq 'le' && $cmpver <= $symver) ||
  33. ($op eq 'gt' && $cmpver > $symver) ||
  34. ($op eq 'ge' && $cmpver >= $symver) ||
  35. ($op eq 'eq' && $cmpver == $symver) ||
  36. ($op eq 'ne' && $cmpver != $symver));
  37. }
  38. }
  39. push @items, $item;
  40. }
  41. return @items;
  42. }
  43. sub gen_kconfig_overrides() {
  44. my %config;
  45. my %kconfig;
  46. my $package;
  47. my $pkginfo = shift @ARGV;
  48. my $cfgfile = shift @ARGV;
  49. my $patchver = shift @ARGV;
  50. # parameter 2: build system config
  51. open FILE, "<$cfgfile" or return;
  52. while (<FILE>) {
  53. /^(CONFIG_.+?)=(.+)$/ and $config{$1} = 1;
  54. }
  55. close FILE;
  56. # parameter 1: package metadata
  57. open FILE, "<$pkginfo" or return;
  58. while (<FILE>) {
  59. /^Package:\s*(.+?)\s*$/ and $package = $1;
  60. /^Kernel-Config:\s*(.+?)\s*$/ and do {
  61. my @config = split /\s+/, $1;
  62. foreach my $config (version_filter_list($patchver, @config)) {
  63. my $val = 'm';
  64. my $override;
  65. if ($config =~ /^(.+?)=(.+)$/) {
  66. $config = $1;
  67. $override = 1;
  68. $val = $2;
  69. }
  70. if ($config{"CONFIG_PACKAGE_$package"} and ($config ne 'n')) {
  71. next if $kconfig{$config} eq 'y';
  72. $kconfig{$config} = $val;
  73. } elsif (!$override) {
  74. $kconfig{$config} or $kconfig{$config} = 'n';
  75. }
  76. }
  77. };
  78. };
  79. close FILE;
  80. foreach my $kconfig (sort keys %kconfig) {
  81. if ($kconfig{$kconfig} eq 'n') {
  82. print "# $kconfig is not set\n";
  83. } else {
  84. print "$kconfig=$kconfig{$kconfig}\n";
  85. }
  86. }
  87. }
  88. my %dep_check;
  89. sub __find_package_dep($$) {
  90. my $pkg = shift;
  91. my $name = shift;
  92. my $deps = $pkg->{depends};
  93. return 0 unless defined $deps;
  94. foreach my $vpkg (@{$deps}) {
  95. foreach my $dep (@{$vpackage{$vpkg}}) {
  96. next if $dep_check{$dep->{name}};
  97. $dep_check{$dep->{name}} = 1;
  98. return 1 if $dep->{name} eq $name;
  99. return 1 if (__find_package_dep($dep, $name) == 1);
  100. }
  101. }
  102. return 0;
  103. }
  104. # wrapper to avoid infinite recursion
  105. sub find_package_dep($$) {
  106. my $pkg = shift;
  107. my $name = shift;
  108. %dep_check = ();
  109. return __find_package_dep($pkg, $name);
  110. }
  111. sub package_depends($$) {
  112. my $a = shift;
  113. my $b = shift;
  114. my $ret;
  115. return 0 if ($a->{submenu} ne $b->{submenu});
  116. if (find_package_dep($a, $b->{name}) == 1) {
  117. $ret = 1;
  118. } elsif (find_package_dep($b, $a->{name}) == 1) {
  119. $ret = -1;
  120. } else {
  121. return 0;
  122. }
  123. return $ret;
  124. }
  125. sub mconf_depends {
  126. my $pkgname = shift;
  127. my $depends = shift;
  128. my $only_dep = shift;
  129. my $res;
  130. my $dep = shift;
  131. my $seen = shift;
  132. my $parent_condition = shift;
  133. $dep or $dep = {};
  134. $seen or $seen = {};
  135. my @t_depends;
  136. $depends or return;
  137. my @depends = @$depends;
  138. foreach my $depend (@depends) {
  139. my $m = "depends on";
  140. my $flags = "";
  141. $depend =~ s/^([@\+]+)// and $flags = $1;
  142. my $condition = $parent_condition;
  143. next if $condition eq $depend;
  144. next if $seen->{"$parent_condition:$depend"};
  145. next if $seen->{":$depend"};
  146. $seen->{"$parent_condition:$depend"} = 1;
  147. if ($depend =~ /^(.+):(.+)$/) {
  148. if ($1 ne "PACKAGE_$pkgname") {
  149. if ($condition) {
  150. $condition = "$condition && $1";
  151. } else {
  152. $condition = $1;
  153. }
  154. }
  155. $depend = $2;
  156. }
  157. if ($flags =~ /\+/) {
  158. my $vdep = $vpackage{$depend};
  159. if ($vdep) {
  160. my @vdeps;
  161. foreach my $v (@$vdep) {
  162. next if $v->{buildonly};
  163. if ($v->{variant_default}) {
  164. unshift @vdeps, $v->{name};
  165. } else {
  166. push @vdeps, $v->{name};
  167. }
  168. }
  169. $depend = shift @vdeps;
  170. if (@vdeps > 1) {
  171. $condition = ($condition ? "$condition && " : '') . join("&&", map { "PACKAGE_$_<PACKAGE_$pkgname" } @vdeps);
  172. } elsif (@vdeps > 0) {
  173. $condition = ($condition ? "$condition && " : '') . "PACKAGE_${vdeps[0]}<PACKAGE_$pkgname";
  174. }
  175. }
  176. # Menuconfig will not treat 'select FOO' as a real dependency
  177. # thus if FOO depends on other config options, these dependencies
  178. # will not be checked. To fix this, we simply emit all of FOO's
  179. # depends here as well.
  180. $package{$depend} and push @t_depends, [ $package{$depend}->{depends}, $condition ];
  181. $m = "select";
  182. next if $only_dep;
  183. $flags =~ /@/ or $depend = "PACKAGE_$depend";
  184. } else {
  185. my $vdep = $vpackage{$depend};
  186. if ($vdep && @$vdep > 0) {
  187. $depend = join("||", map { "PACKAGE_".$_->{name} } @$vdep);
  188. } else {
  189. $flags =~ /@/ or $depend = "PACKAGE_$depend";
  190. }
  191. }
  192. if ($condition) {
  193. if ($m =~ /select/) {
  194. next if $depend eq $condition;
  195. $depend = "$depend if $condition";
  196. } else {
  197. next if $dep->{"$depend if $condition"};
  198. $depend = "!($condition) || $depend" unless $dep->{$condition} eq 'select';
  199. }
  200. }
  201. $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
  202. }
  203. foreach my $tdep (@t_depends) {
  204. mconf_depends($pkgname, $tdep->[0], 1, $dep, $seen, $tdep->[1]);
  205. }
  206. foreach my $depend (keys %$dep) {
  207. my $m = $dep->{$depend};
  208. $res .= "\t\t$m $depend\n";
  209. }
  210. return $res;
  211. }
  212. sub mconf_conflicts {
  213. my $pkgname = shift;
  214. my $depends = shift;
  215. my $res = "";
  216. foreach my $depend (@$depends) {
  217. next unless $package{$depend};
  218. $res .= "\t\tdepends on m || (PACKAGE_$depend != y)\n";
  219. }
  220. return $res;
  221. }
  222. sub print_package_config_category($) {
  223. my $cat = shift;
  224. my %menus;
  225. my %menu_dep;
  226. return unless $category{$cat};
  227. print "menu \"$cat\"\n\n";
  228. my %spkg = %{$category{$cat}};
  229. foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
  230. foreach my $pkg (@{$spkg{$spkg}}) {
  231. next if $pkg->{buildonly};
  232. my $menu = $pkg->{submenu};
  233. if ($menu) {
  234. $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
  235. } else {
  236. $menu = 'undef';
  237. }
  238. $menus{$menu} or $menus{$menu} = [];
  239. push @{$menus{$menu}}, $pkg;
  240. }
  241. }
  242. my @menus = sort {
  243. ($a eq 'undef' ? 1 : 0) or
  244. ($b eq 'undef' ? -1 : 0) or
  245. ($a cmp $b)
  246. } keys %menus;
  247. foreach my $menu (@menus) {
  248. my @pkgs = sort {
  249. package_depends($a, $b) or
  250. ($a->{name} cmp $b->{name})
  251. } @{$menus{$menu}};
  252. if ($menu ne 'undef') {
  253. $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
  254. print "menu \"$menu\"\n";
  255. }
  256. foreach my $pkg (@pkgs) {
  257. next if $pkg->{src}{ignore};
  258. my $title = $pkg->{name};
  259. my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
  260. if ($c > 0) {
  261. $title .= ("." x $c). " ". $pkg->{title};
  262. }
  263. $title = "\"$title\"";
  264. print "\t";
  265. $pkg->{menu} and print "menu";
  266. print "config PACKAGE_".$pkg->{name}."\n";
  267. $pkg->{hidden} and $title = "";
  268. print "\t\t".($pkg->{tristate} ? 'tristate' : 'bool')." $title\n";
  269. print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
  270. unless ($pkg->{hidden}) {
  271. my @def = ("ALL");
  272. if (!exists($pkg->{repository})) {
  273. push @def, "ALL_NONSHARED";
  274. }
  275. if ($pkg->{name} =~ /^kmod-/) {
  276. push @def, "ALL_KMODS";
  277. }
  278. $pkg->{default} ||= "m if " . join("||", @def);
  279. }
  280. if ($pkg->{default}) {
  281. foreach my $default (split /\s*,\s*/, $pkg->{default}) {
  282. print "\t\tdefault $default\n";
  283. }
  284. }
  285. print mconf_depends($pkg->{name}, $pkg->{depends}, 0);
  286. print mconf_depends($pkg->{name}, $pkg->{mdepends}, 0);
  287. print mconf_conflicts($pkg->{name}, $pkg->{conflicts});
  288. print "\t\thelp\n";
  289. print $pkg->{description};
  290. print "\n";
  291. $pkg->{config} and print $pkg->{config}."\n";
  292. }
  293. if ($menu ne 'undef') {
  294. print "endmenu\n";
  295. $menu_dep{$menu} and print "endif\n";
  296. }
  297. }
  298. print "endmenu\n\n";
  299. undef $category{$cat};
  300. }
  301. sub print_package_overrides() {
  302. keys %overrides > 0 or return;
  303. print "\tconfig OVERRIDE_PKGS\n";
  304. print "\t\tstring\n";
  305. print "\t\tdefault \"".join(" ", sort keys %overrides)."\"\n\n";
  306. }
  307. sub gen_package_config() {
  308. parse_package_metadata($ARGV[0]) or exit 1;
  309. print "menuconfig IMAGEOPT\n\tbool \"Image configuration\"\n\tdefault n\n";
  310. print "source \"package/*/image-config.in\"\n";
  311. if (scalar glob "package/feeds/*/*/image-config.in") {
  312. print "source \"package/feeds/*/*/image-config.in\"\n";
  313. }
  314. print_package_config_category 'Base system';
  315. foreach my $cat (sort {uc($a) cmp uc($b)} keys %category) {
  316. print_package_config_category $cat;
  317. }
  318. print_package_overrides();
  319. }
  320. sub and_condition($) {
  321. my $condition = shift;
  322. my @spl_and = split('\&\&', $condition);
  323. if (@spl_and == 1) {
  324. return "\$(CONFIG_$spl_and[0])";
  325. }
  326. return "\$(and " . join (',', map("\$(CONFIG_$_)", @spl_and)) . ")";
  327. }
  328. sub gen_condition ($) {
  329. my $condition = shift;
  330. # remove '!()', just as include/package-ipkg.mk does
  331. $condition =~ s/[()!]//g;
  332. return join("", map(and_condition($_), split('\|\|', $condition)));
  333. }
  334. sub get_conditional_dep($$) {
  335. my $condition = shift;
  336. my $depstr = shift;
  337. if ($condition) {
  338. if ($condition =~ /^!(.+)/) {
  339. return "\$(if " . gen_condition($1) . ",,$depstr)";
  340. } else {
  341. return "\$(if " . gen_condition($condition) . ",$depstr)";
  342. }
  343. } else {
  344. return $depstr;
  345. }
  346. }
  347. sub gen_package_mk() {
  348. my $line;
  349. parse_package_metadata($ARGV[0]) or exit 1;
  350. foreach my $srcname (sort {uc($a) cmp uc($b)} keys %srcpackage) {
  351. my $src = $srcpackage{$srcname};
  352. my $variant_default;
  353. my %deplines = ('' => {});
  354. foreach my $pkg (@{$src->{packages}}) {
  355. foreach my $dep (@{$pkg->{depends}}) {
  356. next if ($dep =~ /@/);
  357. my $condition;
  358. $dep =~ s/\+//g;
  359. if ($dep =~ /^(.+):(.+)/) {
  360. $condition = $1;
  361. $dep = $2;
  362. }
  363. my $vpkg_dep = $vpackage{$dep};
  364. unless (defined $vpkg_dep) {
  365. warn sprintf "WARNING: Makefile '%s' has a dependency on '%s', which does not exist\n",
  366. $src->{makefile}, $dep;
  367. next;
  368. }
  369. # Filter out self-depends
  370. my @vdeps = grep { $srcname ne $_->{src}{name} } @{$vpkg_dep};
  371. foreach my $vdep (@vdeps) {
  372. my $depstr = sprintf '$(curdir)/%s/compile', $vdep->{src}{path};
  373. if (@vdeps > 1) {
  374. $depstr = sprintf '$(if $(CONFIG_PACKAGE_%s),%s)', $vdep->{name}, $depstr;
  375. }
  376. my $depline = get_conditional_dep($condition, $depstr);
  377. if ($depline) {
  378. $deplines{''}{$depline}++;
  379. }
  380. }
  381. }
  382. my $config = '';
  383. $config = sprintf '$(CONFIG_PACKAGE_%s)', $pkg->{name} unless $pkg->{buildonly};
  384. $pkg->{prereq} and printf "prereq-%s += %s\n", $config, $src->{path};
  385. next if $pkg->{buildonly};
  386. printf "package-%s += %s\n", $config, $src->{path};
  387. if ($pkg->{variant}) {
  388. if (!defined($variant_default) or $pkg->{variant_default}) {
  389. $variant_default = $pkg->{variant};
  390. }
  391. printf "\$(curdir)/%s/variants += \$(if %s,%s)\n", $src->{path}, $config, $pkg->{variant};
  392. }
  393. }
  394. if (defined($variant_default)) {
  395. printf "\$(curdir)/%s/default-variant := %s\n", $src->{path}, $variant_default;
  396. }
  397. unless (grep {!$_->{buildonly}} @{$src->{packages}}) {
  398. printf "package- += %s\n", $src->{path};
  399. }
  400. if (@{$src->{buildtypes}} > 0) {
  401. printf "buildtypes-%s = %s\n", $src->{path}, join(' ', @{$src->{buildtypes}});
  402. }
  403. foreach my $type ('', @{$src->{buildtypes}}) {
  404. my $suffix = '';
  405. $suffix = "/$type" if $type;
  406. next unless $src->{"builddepends$suffix"};
  407. defined $deplines{$suffix} or $deplines{$suffix} = {};
  408. foreach my $dep (@{$src->{"builddepends$suffix"}}) {
  409. my $depsuffix = "";
  410. my $deptype = "";
  411. my $condition;
  412. if ($dep =~ /^(.+):(.+)/) {
  413. $condition = $1;
  414. $dep = $2;
  415. }
  416. if ($dep =~ /^(.+)\/(.+)/) {
  417. $dep = $1;
  418. $deptype = $2;
  419. $depsuffix = "/$2";
  420. }
  421. next if $srcname.$suffix eq $dep.$depsuffix;
  422. my $src_dep = $srcpackage{$dep};
  423. unless (defined($src_dep) && (!$deptype || grep { $_ eq $deptype } @{$src_dep->{buildtypes}})) {
  424. warn sprintf "WARNING: Makefile '%s' has a build dependency on '%s', which does not exist\n",
  425. $src->{makefile}, $dep.$depsuffix;
  426. next;
  427. }
  428. my $depstr = sprintf '$(curdir)/%s/compile', $src_dep->{path}.$depsuffix;
  429. my $depline = get_conditional_dep($condition, $depstr);
  430. if ($depline) {
  431. $deplines{$suffix}{$depline}++;
  432. }
  433. }
  434. }
  435. foreach my $suffix (sort keys %deplines) {
  436. my $depline = join(" ", sort keys %{$deplines{$suffix}});
  437. if ($depline) {
  438. $line .= sprintf "\$(curdir)/%s/compile += %s\n", $src->{path}.$suffix, $depline;
  439. }
  440. }
  441. }
  442. if ($line ne "") {
  443. print "\n$line";
  444. }
  445. }
  446. sub gen_package_source() {
  447. parse_package_metadata($ARGV[0]) or exit 1;
  448. foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
  449. my $pkg = $package{$name};
  450. if ($pkg->{name} && $pkg->{source}) {
  451. print "$pkg->{name}: ";
  452. print "$pkg->{source}\n";
  453. }
  454. }
  455. }
  456. sub gen_package_auxiliary() {
  457. parse_package_metadata($ARGV[0]) or exit 1;
  458. foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
  459. my $pkg = $package{$name};
  460. if ($pkg->{name} && $pkg->{repository}) {
  461. print "Package/$name/subdir = $pkg->{repository}\n";
  462. }
  463. my %depends;
  464. foreach my $dep (@{$pkg->{depends} || []}) {
  465. if ($dep =~ m!^\+?(?:[^:]+:)?([^@]+)$!) {
  466. $depends{$1}++;
  467. }
  468. }
  469. my @depends = sort keys %depends;
  470. if (@depends > 0) {
  471. foreach my $n (@{$pkg->{provides}}) {
  472. print "Package/$n/depends = @depends\n";
  473. }
  474. }
  475. }
  476. }
  477. sub gen_package_license($) {
  478. my $level = shift;
  479. parse_package_metadata($ARGV[0]) or exit 1;
  480. foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
  481. my $pkg = $package{$name};
  482. if ($pkg->{name}) {
  483. if ($pkg->{license}) {
  484. print "$pkg->{name}: ";
  485. print "$pkg->{license}\n";
  486. if ($pkg->{licensefiles} && $level == 0) {
  487. print "\tFiles: $pkg->{licensefiles}\n";
  488. }
  489. } else {
  490. if ($level == 1) {
  491. print "$pkg->{name}: Missing license! ";
  492. print "Please fix $pkg->{src}{makefile}\n";
  493. }
  494. }
  495. }
  496. }
  497. }
  498. sub gen_version_filtered_list() {
  499. foreach my $item (version_filter_list(@ARGV)) {
  500. print "$item\n";
  501. }
  502. }
  503. sub gen_usergroup_list() {
  504. parse_package_metadata($ARGV[0]) or exit 1;
  505. for my $name (keys %usernames) {
  506. print "user $name $usernames{$name}{id} $usernames{$name}{makefile}\n";
  507. }
  508. for my $name (keys %groupnames) {
  509. print "group $name $groupnames{$name}{id} $groupnames{$name}{makefile}\n";
  510. }
  511. }
  512. sub gen_package_manifest_json() {
  513. my $json;
  514. parse_package_metadata($ARGV[0]) or exit 1;
  515. foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
  516. my %depends;
  517. my $pkg = $package{$name};
  518. foreach my $dep (@{$pkg->{depends} || []}) {
  519. if ($dep =~ m!^\+?(?:[^:]+:)?([^@]+)$!) {
  520. $depends{$1}++;
  521. }
  522. }
  523. my @depends = sort keys %depends;
  524. my $pkg_deps = join ' ', map { qq/"$_",/ } @depends;
  525. $pkg_deps =~ s/\,$//;
  526. my $pkg_maintainer = join ' ', map { qq/"$_",/ } @{$pkg->{maintainer} || []};
  527. $pkg_maintainer =~ s/\,$//;
  528. $json = <<"END_JSON";
  529. ${json}{
  530. "name":"$name",
  531. "version":"$pkg->{version}",
  532. "category":"$pkg->{category}",
  533. "license":"$pkg->{license}",
  534. "cpe_id":"$pkg->{cpe_id}",
  535. "maintainer": [$pkg_maintainer],
  536. "depends":[$pkg_deps]},
  537. END_JSON
  538. }
  539. $json =~ s/[\n\r]//g;
  540. $json =~ s/\,$//;
  541. print "[$json]";
  542. }
  543. sub image_manifest_packages($)
  544. {
  545. my %packages;
  546. my $imgmanifest = shift;
  547. open FILE, "<$imgmanifest" or return;
  548. while (<FILE>) {
  549. /^(.+?) - (.+)$/ and $packages{$1} = $2;
  550. }
  551. close FILE;
  552. return %packages;
  553. }
  554. sub dump_cyclonedxsbom_json {
  555. my (@components) = @_;
  556. my $uuid = sprintf(
  557. "%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
  558. rand(0xffff), rand(0xffff), rand(0xffff),
  559. rand(0x0fff) | 0x4000,
  560. rand(0x3fff) | 0x8000,
  561. rand(0xffff), rand(0xffff), rand(0xffff)
  562. );
  563. my $cyclonedx = {
  564. bomFormat => "CycloneDX",
  565. specVersion => "1.4",
  566. serialNumber => "urn:uuid:$uuid",
  567. version => 1,
  568. metadata => {
  569. timestamp => gmtime->datetime,
  570. },
  571. "components" => [@components],
  572. };
  573. return encode_json($cyclonedx);
  574. }
  575. sub gen_image_cyclonedxsbom() {
  576. my $pkginfo = shift @ARGV;
  577. my $imgmanifest = shift @ARGV;
  578. my @components;
  579. my %image_packages;
  580. %image_packages = image_manifest_packages($imgmanifest);
  581. %image_packages or exit 1;
  582. parse_package_metadata($pkginfo) or exit 1;
  583. $package{"kernel"} = {
  584. license => "GPL-2.0",
  585. cpe_id => "cpe:/o:linux:linux_kernel",
  586. name => "kernel",
  587. };
  588. my %abimap;
  589. my @abipkgs = grep { defined $package{$_}->{abi_version} } keys %package;
  590. foreach my $name (@abipkgs) {
  591. my $pkg = $package{$name};
  592. my $abipkg = $name . $pkg->{abi_version};
  593. $abimap{$abipkg} = $name;
  594. }
  595. foreach my $name (sort {uc($a) cmp uc($b)} keys %image_packages) {
  596. my $pkg = $package{$name};
  597. if (!$pkg) {
  598. $pkg = $package{$abimap{$name}};
  599. next if !$pkg;
  600. }
  601. my @licenses;
  602. my @license = split(/\s+/, $pkg->{license});
  603. foreach my $lic (@license) {
  604. push @licenses, (
  605. { "license" => { "name" => $lic } }
  606. );
  607. }
  608. my $type;
  609. if ($pkg->{category}) {
  610. my $category = $pkg->{category};
  611. my %cat_type = (
  612. "Firmware" => "firmware",
  613. "Libraries" => "library"
  614. );
  615. if ($cat_type{$category}) {
  616. $type = $cat_type{$category};
  617. } else {
  618. $type = "application";
  619. }
  620. }
  621. my $version = $pkg->{version};
  622. if ($image_packages{$name}) {
  623. $version = $image_packages{$name};
  624. }
  625. $version =~ s/-\d+$// if $version;
  626. if ($name =~ /^(kernel|kmod-)/ and $version =~ /^(\d+\.\d+\.\d+)/) {
  627. $version = $1;
  628. }
  629. push @components, {
  630. name => $pkg->{name},
  631. version => $version,
  632. @licenses > 0 ? (licenses => [ @licenses ]) : (),
  633. $pkg->{cpe_id} ? (cpe => $pkg->{cpe_id}.":".$version) : (),
  634. $type ? (type => $type) : (),
  635. $version ? (version => $version) : (),
  636. };
  637. }
  638. print dump_cyclonedxsbom_json(@components);
  639. }
  640. sub gen_package_cyclonedxsbom() {
  641. my $pkgmanifest = shift @ARGV;
  642. my @components;
  643. my %mpkgs;
  644. %mpkgs = parse_package_manifest_metadata($pkgmanifest);
  645. %mpkgs or exit 1;
  646. foreach my $name (sort {uc($a) cmp uc($b)} keys %mpkgs) {
  647. my $pkg = $mpkgs{$name};
  648. my @licenses;
  649. my @license = split(/\s+/, $pkg->{license});
  650. foreach my $lic (@license) {
  651. push @licenses, (
  652. { "license" => { "name" => $lic } }
  653. );
  654. }
  655. my $type;
  656. if ($pkg->{section}) {
  657. my $section = $pkg->{section};
  658. my %section_type = (
  659. "firmware" => "firmware",
  660. "libs" => "library"
  661. );
  662. if ($section_type{$section}) {
  663. $type = $section_type{$section};
  664. } else {
  665. $type = "application";
  666. }
  667. }
  668. my $version = $pkg->{version};
  669. $version =~ s/-\d+$// if $version;
  670. if ($name =~ /^(kernel|kmod-)/ and $version =~ /^(\d+\.\d+\.\d+)/) {
  671. $version = $1;
  672. }
  673. push @components, {
  674. name => $name,
  675. version => $version,
  676. @licenses > 0 ? (licenses => [ @licenses ]) : (),
  677. $pkg->{cpe_id} ? (cpe => $pkg->{cpe_id}.":".$version) : (),
  678. $type ? (type => $type) : (),
  679. $version ? (version => $version) : (),
  680. };
  681. }
  682. print dump_cyclonedxsbom_json(@components);
  683. }
  684. sub parse_command() {
  685. GetOptions("ignore=s", \@ignore);
  686. my $cmd = shift @ARGV;
  687. for ($cmd) {
  688. /^mk$/ and return gen_package_mk();
  689. /^config$/ and return gen_package_config();
  690. /^kconfig/ and return gen_kconfig_overrides();
  691. /^source$/ and return gen_package_source();
  692. /^pkgaux$/ and return gen_package_auxiliary();
  693. /^pkgmanifestjson$/ and return gen_package_manifest_json();
  694. /^imgcyclonedxsbom$/ and return gen_image_cyclonedxsbom();
  695. /^pkgcyclonedxsbom$/ and return gen_package_cyclonedxsbom();
  696. /^license$/ and return gen_package_license(0);
  697. /^licensefull$/ and return gen_package_license(1);
  698. /^usergroup$/ and return gen_usergroup_list();
  699. /^version_filter$/ and return gen_version_filtered_list();
  700. }
  701. die <<EOF
  702. Available Commands:
  703. $0 mk [file] Package metadata in makefile format
  704. $0 config [file] Package metadata in Kconfig format
  705. $0 kconfig [file] [config] [patchver] Kernel config overrides
  706. $0 source [file] Package source file information
  707. $0 pkgaux [file] Package auxiliary variables in makefile format
  708. $0 pkgmanifestjson [file] Package manifests in JSON format
  709. $0 imgcyclonedxsbom <file> [manifest] Image package manifest in CycloneDX SBOM JSON format
  710. $0 pkgcyclonedxsbom <file> Package manifest in CycloneDX SBOM JSON format
  711. $0 license [file] Package license information
  712. $0 licensefull [file] Package license information (full list)
  713. $0 usergroup [file] Package usergroup allocation list
  714. $0 version_filter [patchver] [list...] Filter list of version tagged strings
  715. Options:
  716. --ignore <name> Ignore the source package <name>
  717. EOF
  718. }
  719. parse_command();