3 use lib "$FindBin::Bin";
10 sub version_to_num($) {
14 if (defined($str) && $str =~ /^\d+(?:\.\d+)+$/)
16 my @n = (split(/\./, $str), 0, 0, 0, 0);
17 $num = ($n[0] << 24) | ($n[1] << 16) | ($n[2] << 8) | $n[3];
23 sub version_filter_list(@) {
24 my $cmpver = version_to_num(shift @_);
29 if ($item =~ s/@(lt|le|gt|ge|eq|ne)(\d+(?:\.\d+)+)\b//)
32 my $symver = version_to_num($2);
34 if ($symver > 0 && $cmpver > 0)
36 next unless (($op eq 'lt' && $cmpver < $symver) ||
37 ($op eq 'le' && $cmpver <= $symver) ||
38 ($op eq 'gt' && $cmpver > $symver) ||
39 ($op eq 'ge' && $cmpver >= $symver) ||
40 ($op eq 'eq' && $cmpver == $symver) ||
41 ($op eq 'ne' && $cmpver != $symver));
51 sub gen_kconfig_overrides() {
55 my $pkginfo = shift @ARGV;
56 my $cfgfile = shift @ARGV;
57 my $patchver = shift @ARGV;
59 # parameter 2: build system config
60 open FILE, "<$cfgfile" or return;
62 /^(CONFIG_.+?)=(.+)$/ and $config{$1} = 1;
66 # parameter 1: package metadata
67 open FILE, "<$pkginfo" or return;
69 /^Package:\s*(.+?)\s*$/ and $package = $1;
70 /^Kernel-Config:\s*(.+?)\s*$/ and do {
71 my @config = split /\s+/, $1;
72 foreach my $config (version_filter_list($patchver, @config)) {
75 if ($config =~ /^(.+?)=(.+)$/) {
80 if ($config{"CONFIG_PACKAGE_$package"} and ($config ne 'n')) {
81 next if $kconfig{$config} eq 'y';
82 $kconfig{$config} = $val;
83 } elsif (!$override) {
84 $kconfig{$config} or $kconfig{$config} = 'n';
91 foreach my $kconfig (sort keys %kconfig) {
92 if ($kconfig{$kconfig} eq 'n') {
93 print "# $kconfig is not set\n";
95 print "$kconfig=$kconfig{$kconfig}\n";
101 sub __find_package_dep($$) {
104 my $deps = $pkg->{depends};
106 return 0 unless defined $deps;
107 foreach my $vpkg (@{$deps}) {
108 foreach my $dep (@{$vpackage{$vpkg}}) {
109 next if $dep_check{$dep->{name}};
110 $dep_check{$dep->{name}} = 1;
111 return 1 if $dep->{name} eq $name;
112 return 1 if (__find_package_dep($dep, $name) == 1);
118 # wrapper to avoid infinite recursion
119 sub find_package_dep($$) {
124 return __find_package_dep($pkg, $name);
127 sub package_depends($$) {
132 return 0 if ($a->{submenu} ne $b->{submenu});
133 if (find_package_dep($a, $b->{name}) == 1) {
135 } elsif (find_package_dep($b, $a->{name}) == 1) {
146 my $only_dep = shift;
150 my $parent_condition = shift;
156 my @depends = @$depends;
157 foreach my $depend (@depends) {
158 my $m = "depends on";
160 $depend =~ s/^([@\+]+)// and $flags = $1;
161 my $condition = $parent_condition;
163 next if $condition eq $depend;
164 next if $seen->{"$parent_condition:$depend"};
165 next if $seen->{":$depend"};
166 $seen->{"$parent_condition:$depend"} = 1;
167 if ($depend =~ /^(.+):(.+)$/) {
168 if ($1 ne "PACKAGE_$pkgname") {
170 $condition = "$condition && $1";
177 if ($flags =~ /\+/) {
178 my $vdep = $vpackage{$depend};
182 foreach my $v (@$vdep) {
183 next if $v->{buildonly};
184 if ($v->{variant_default}) {
185 unshift @vdeps, $v->{name};
187 push @vdeps, $v->{name};
191 $depend = shift @vdeps;
194 $condition = ($condition ? "$condition && " : '') . join("&&", map { "PACKAGE_$_<PACKAGE_$pkgname" } @vdeps);
195 } elsif (@vdeps > 0) {
196 $condition = ($condition ? "$condition && " : '') . "PACKAGE_${vdeps[0]}<PACKAGE_$pkgname";
200 # Menuconfig will not treat 'select FOO' as a real dependency
201 # thus if FOO depends on other config options, these dependencies
202 # will not be checked. To fix this, we simply emit all of FOO's
203 # depends here as well.
204 $package{$depend} and push @t_depends, [ $package{$depend}->{depends}, $condition ];
209 $flags =~ /@/ or $depend = "PACKAGE_$depend";
211 my $vdep = $vpackage{$depend};
212 if ($vdep && @$vdep > 0) {
213 $depend = join("||", map { "PACKAGE_".$_->{name} } @$vdep);
215 $flags =~ /@/ or $depend = "PACKAGE_$depend";
220 if ($m =~ /select/) {
221 next if $depend eq $condition;
222 $depend = "$depend if $condition";
224 next if $dep->{"$depend if $condition"};
225 $depend = "!($condition) || $depend" unless $dep->{$condition} eq 'select';
228 $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
231 foreach my $tdep (@t_depends) {
232 mconf_depends($pkgname, $tdep->[0], 1, $dep, $seen, $tdep->[1]);
235 foreach my $depend (keys %$dep) {
236 my $m = $dep->{$depend};
237 $res .= "\t\t$m $depend\n";
242 sub mconf_conflicts {
247 foreach my $depend (@$depends) {
248 next unless $package{$depend};
249 $res .= "\t\tdepends on m || (PACKAGE_$depend != y)\n";
254 sub print_package_config_category($) {
259 return unless $category{$cat};
261 print "menu \"$cat\"\n\n";
262 my %spkg = %{$category{$cat}};
264 foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
265 foreach my $pkg (@{$spkg{$spkg}}) {
266 next if $pkg->{buildonly};
267 my $menu = $pkg->{submenu};
269 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
273 $menus{$menu} or $menus{$menu} = [];
274 push @{$menus{$menu}}, $pkg;
278 ($a eq 'undef' ? 1 : 0) or
279 ($b eq 'undef' ? -1 : 0) or
283 foreach my $menu (@menus) {
285 package_depends($a, $b) or
286 ($a->{name} cmp $b->{name})
288 if ($menu ne 'undef') {
289 $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
290 print "menu \"$menu\"\n";
292 foreach my $pkg (@pkgs) {
293 next if $pkg->{src}{ignore};
294 my $title = $pkg->{name};
295 my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
297 $title .= ("." x $c). " ". $pkg->{title};
299 $title = "\"$title\"";
301 $pkg->{menu} and print "menu";
302 print "config PACKAGE_".$pkg->{name}."\n";
303 $pkg->{hidden} and $title = "";
304 print "\t\t".($pkg->{tristate} ? 'tristate' : 'bool')." $title\n";
305 print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
306 unless ($pkg->{hidden}) {
308 if (!exists($pkg->{repository})) {
309 push @def, "ALL_NONSHARED";
311 if ($pkg->{name} =~ /^kmod-/) {
312 push @def, "ALL_KMODS";
314 $pkg->{default} ||= "m if " . join("||", @def);
316 if ($pkg->{default}) {
317 foreach my $default (split /\s*,\s*/, $pkg->{default}) {
318 print "\t\tdefault $default\n";
321 print mconf_depends($pkg->{name}, $pkg->{depends}, 0);
322 print mconf_depends($pkg->{name}, $pkg->{mdepends}, 0);
323 print mconf_conflicts($pkg->{name}, $pkg->{conflicts});
325 print $pkg->{description};
328 $pkg->{config} and print $pkg->{config}."\n";
330 if ($menu ne 'undef') {
332 $menu_dep{$menu} and print "endif\n";
337 undef $category{$cat};
340 sub print_package_overrides() {
341 keys %overrides > 0 or return;
342 print "\tconfig OVERRIDE_PKGS\n";
343 print "\t\tstring\n";
344 print "\t\tdefault \"".join(" ", sort keys %overrides)."\"\n\n";
347 sub gen_package_config() {
348 parse_package_metadata($ARGV[0]) or exit 1;
349 print "menuconfig IMAGEOPT\n\tbool \"Image configuration\"\n\tdefault n\n";
350 print "source \"package/*/image-config.in\"\n";
351 if (scalar glob "package/feeds/*/*/image-config.in") {
352 print "source \"package/feeds/*/*/image-config.in\"\n";
354 print_package_config_category 'Base system';
355 foreach my $cat (sort {uc($a) cmp uc($b)} keys %category) {
356 print_package_config_category $cat;
358 print_package_overrides();
361 sub and_condition($) {
362 my $condition = shift;
363 my @spl_and = split('\&\&', $condition);
365 return "\$(CONFIG_$spl_and[0])";
367 return "\$(and " . join (',', map("\$(CONFIG_$_)", @spl_and)) . ")";
370 sub gen_condition ($) {
371 my $condition = shift;
372 # remove '!()', just as include/package-ipkg.mk does
373 $condition =~ s/[()!]//g;
374 return join("", map(and_condition($_), split('\|\|', $condition)));
377 sub get_conditional_dep($$) {
378 my $condition = shift;
381 if ($condition =~ /^!(.+)/) {
382 return "\$(if " . gen_condition($1) . ",,$depstr)";
384 return "\$(if " . gen_condition($condition) . ",$depstr)";
391 sub gen_package_mk() {
394 parse_package_metadata($ARGV[0]) or exit 1;
395 foreach my $srcname (sort {uc($a) cmp uc($b)} keys %srcpackage) {
396 my $src = $srcpackage{$srcname};
398 my %deplines = ('' => {});
400 foreach my $pkg (@{$src->{packages}}) {
401 foreach my $dep (@{$pkg->{depends}}) {
402 next if ($dep =~ /@/);
407 if ($dep =~ /^(.+):(.+)/) {
412 my $vpkg_dep = $vpackage{$dep};
413 unless (defined $vpkg_dep) {
414 warn sprintf "WARNING: Makefile '%s' has a dependency on '%s', which does not exist\n",
415 $src->{makefile}, $dep;
419 # Filter out self-depends
420 my @vdeps = grep { $srcname ne $_->{src}{name} } @{$vpkg_dep};
422 foreach my $vdep (@vdeps) {
423 my $depstr = sprintf '$(curdir)/%s/compile', $vdep->{src}{path};
425 $depstr = sprintf '$(if $(CONFIG_PACKAGE_%s),%s)', $vdep->{name}, $depstr;
427 my $depline = get_conditional_dep($condition, $depstr);
429 $deplines{''}{$depline}++;
435 $config = sprintf '$(CONFIG_PACKAGE_%s)', $pkg->{name} unless $pkg->{buildonly};
437 $pkg->{prereq} and printf "prereq-%s += %s\n", $config, $src->{path};
439 next if $pkg->{buildonly};
441 printf "package-%s += %s\n", $config, $src->{path};
443 if ($pkg->{variant}) {
444 if (!defined($variant_default) or $pkg->{variant_default}) {
445 $variant_default = $pkg->{variant};
447 printf "\$(curdir)/%s/variants += \$(if %s,%s)\n", $src->{path}, $config, $pkg->{variant};
451 if (defined($variant_default)) {
452 printf "\$(curdir)/%s/default-variant := %s\n", $src->{path}, $variant_default;
455 unless (grep {!$_->{buildonly}} @{$src->{packages}}) {
456 printf "package- += %s\n", $src->{path};
459 if (@{$src->{buildtypes}} > 0) {
460 printf "buildtypes-%s = %s\n", $src->{path}, join(' ', @{$src->{buildtypes}});
463 foreach my $type ('', @{$src->{buildtypes}}) {
466 $suffix = "/$type" if $type;
468 next unless $src->{"builddepends$suffix"};
470 defined $deplines{$suffix} or $deplines{$suffix} = {};
472 foreach my $dep (@{$src->{"builddepends$suffix"}}) {
477 if ($dep =~ /^(.+):(.+)/) {
481 if ($dep =~ /^(.+)\/(.+)/) {
487 next if $srcname.$suffix eq $dep.$depsuffix;
489 my $src_dep = $srcpackage{$dep};
490 unless (defined($src_dep) && (!$deptype || grep { $_ eq $deptype } @{$src_dep->{buildtypes}})) {
491 warn sprintf "WARNING: Makefile '%s' has a build dependency on '%s', which does not exist\n",
492 $src->{makefile}, $dep.$depsuffix;
496 my $depstr = sprintf '$(curdir)/%s/compile', $src_dep->{path}.$depsuffix;
497 my $depline = get_conditional_dep($condition, $depstr);
499 $deplines{$suffix}{$depline}++;
504 foreach my $suffix (sort keys %deplines) {
505 my $depline = join(" ", sort keys %{$deplines{$suffix}});
507 $line .= sprintf "\$(curdir)/%s/compile += %s\n", $src->{path}.$suffix, $depline;
517 sub gen_package_source() {
518 parse_package_metadata($ARGV[0]) or exit 1;
519 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
520 my $pkg = $package{$name};
521 if ($pkg->{name} && $pkg->{source}) {
522 print "$pkg->{name}: ";
523 print "$pkg->{source}\n";
528 sub gen_package_auxiliary() {
529 parse_package_metadata($ARGV[0]) or exit 1;
530 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
531 my $pkg = $package{$name};
532 if ($pkg->{name} && $pkg->{repository}) {
533 print "Package/$name/subdir = $pkg->{repository}\n";
535 if ($pkg->{name} && defined($pkg->{abiversion}) && length($pkg->{abiversion})) {
538 if ($pkg->{abiversion} =~ m!^(\d{4})-(\d{2})-(\d{2})-[0-9a-f]{7,40}$!) {
539 print STDERR "WARNING: Reducing ABI version '$pkg->{abiversion}' of package '$name' to '$1$2$3'\n";
543 $abiv = $pkg->{abiversion};
546 foreach my $n (@{$pkg->{provides}}) {
547 print "Package/$n/abiversion = $abiv\n";
551 foreach my $dep (@{$pkg->{depends} || []}) {
552 if ($dep =~ m!^\+?(?:[^:]+:)?([^@]+)$!) {
556 my @depends = sort keys %depends;
558 foreach my $n (@{$pkg->{provides}}) {
559 print "Package/$n/depends = @depends\n";
565 sub gen_package_license($) {
567 parse_package_metadata($ARGV[0]) or exit 1;
568 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
569 my $pkg = $package{$name};
571 if ($pkg->{license}) {
572 print "$pkg->{name}: ";
573 print "$pkg->{license}\n";
574 if ($pkg->{licensefiles} && $level == 0) {
575 print "\tFiles: $pkg->{licensefiles}\n";
579 print "$pkg->{name}: Missing license! ";
580 print "Please fix $pkg->{src}{makefile}\n";
587 sub gen_version_filtered_list() {
588 foreach my $item (version_filter_list(@ARGV)) {
593 sub gen_usergroup_list() {
594 parse_package_metadata($ARGV[0]) or exit 1;
595 for my $name (keys %usernames) {
596 print "user $name $usernames{$name}{id} $usernames{$name}{makefile}\n";
598 for my $name (keys %groupnames) {
599 print "group $name $groupnames{$name}{id} $groupnames{$name}{makefile}\n";
603 sub parse_command() {
604 GetOptions("ignore=s", \@ignore);
605 my $cmd = shift @ARGV;
607 /^mk$/ and return gen_package_mk();
608 /^config$/ and return gen_package_config();
609 /^kconfig/ and return gen_kconfig_overrides();
610 /^source$/ and return gen_package_source();
611 /^pkgaux$/ and return gen_package_auxiliary();
612 /^license$/ and return gen_package_license(0);
613 /^licensefull$/ and return gen_package_license(1);
614 /^usergroup$/ and return gen_usergroup_list();
615 /^version_filter$/ and return gen_version_filtered_list();
619 $0 mk [file] Package metadata in makefile format
620 $0 config [file] Package metadata in Kconfig format
621 $0 kconfig [file] [config] [patchver] Kernel config overrides
622 $0 source [file] Package source file information
623 $0 pkgaux [file] Package auxiliary variables in makefile format
624 $0 license [file] Package license information
625 $0 licensefull [file] Package license information (full list)
626 $0 usergroup [file] Package usergroup allocation list
627 $0 version_filter [patchver] [list...] Filter list of version tagged strings
630 --ignore <name> Ignore the source package <name>