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->{vdepends
} or $pkg->{depends
});
106 return 0 unless defined $deps;
107 foreach my $dep (@
{$deps}) {
108 next if $dep_check{$dep};
109 $dep_check{$dep} = 1;
110 return 1 if $dep eq $name;
111 return 1 if ($package{$dep} and (__find_package_dep
($package{$dep},$name) == 1));
116 # wrapper to avoid infinite recursion
117 sub find_package_dep
($$) {
122 return __find_package_dep
($pkg, $name);
125 sub package_depends
($$) {
130 return 0 if ($a->{submenu
} ne $b->{submenu
});
131 if (find_package_dep
($a, $b->{name
}) == 1) {
133 } elsif (find_package_dep
($b, $a->{name
}) == 1) {
144 my $only_dep = shift;
148 my $parent_condition = shift;
154 my @depends = @
$depends;
155 foreach my $depend (@depends) {
156 my $m = "depends on";
158 $depend =~ s/^([@\+]+)// and $flags = $1;
160 my $condition = $parent_condition;
162 next if $condition eq $depend;
163 next if $seen->{"$parent_condition:$depend"};
164 next if $seen->{":$depend"};
165 $seen->{"$parent_condition:$depend"} = 1;
166 if ($depend =~ /^(.+):(.+)$/) {
167 if ($1 ne "PACKAGE_$pkgname") {
169 $condition = "$condition && $1";
176 next if $package{$depend} and $package{$depend}->{buildonly
};
177 if ($flags =~ /\+/) {
178 if ($vdep = $package{$depend}->{vdepends
}) {
182 foreach my $v (@
$vdep) {
183 if ($package{$v} && $package{$v}->{variant_default
}) {
191 $depend = shift @vdeps;
195 $condition = ($condition ?
"$condition && " : '') . '!('.join("||", map { "PACKAGE_".$_ } @vdeps).')';
196 } elsif (@vdeps > 0) {
197 $condition = ($condition ?
"$condition && " : '') . '!PACKAGE_'.$vdeps[0];
201 # Menuconfig will not treat 'select FOO' as a real dependency
202 # thus if FOO depends on other config options, these dependencies
203 # will not be checked. To fix this, we simply emit all of FOO's
204 # depends here as well.
205 $package{$depend} and push @t_depends, [ $package{$depend}->{depends
}, $condition ];
210 $flags =~ /@/ or $depend = "PACKAGE_$depend";
212 if ($vdep = $package{$depend}->{vdepends
}) {
213 $depend = join("||", map { "PACKAGE_".$_ } @
$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_features
() {
341 keys %features > 0 or return;
342 print "menu \"Package features\"\n";
343 foreach my $n (keys %features) {
344 my @features = sort { $b->{priority
} <=> $a->{priority
} or $a->{title
} cmp $b->{title
} } @
{$features{$n}};
347 prompt "$features[0]->{target_title}"
348 default FEATURE_$features[0]->{name}
351 foreach my $feature (@features) {
353 config FEATURE_$feature->{name}
354 bool "$feature->{title}"
356 $feature->{description
} =~ /\w/ and do {
357 print "\t\thelp\n".$feature->{description
}."\n";
365 sub print_package_overrides
() {
366 keys %overrides > 0 or return;
367 print "\tconfig OVERRIDE_PKGS\n";
368 print "\t\tstring\n";
369 print "\t\tdefault \"".join(" ", sort keys %overrides)."\"\n\n";
372 sub gen_package_config
() {
373 parse_package_metadata
($ARGV[0]) or exit 1;
374 print "menuconfig IMAGEOPT\n\tbool \"Image configuration\"\n\tdefault n\n";
375 foreach my $preconfig (keys %preconfig) {
376 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
377 my $conf = $preconfig{$preconfig}->{$cfg}->{id
};
380 config UCI_PRECONFIG_$conf
381 string "$preconfig{$preconfig}->{$cfg}->{label}" if IMAGEOPT
382 depends on PACKAGE_$preconfig
383 default "$preconfig{$preconfig}->{$cfg}->{default}"
388 print "source \"package/*/image-config.in\"\n";
389 if (scalar glob "package/feeds/*/*/image-config.in") {
390 print "source \"package/feeds/*/*/image-config.in\"\n";
392 print_package_features
();
393 print_package_config_category
'Base system';
394 foreach my $cat (sort {uc($a) cmp uc($b)} keys %category) {
395 print_package_config_category
$cat;
397 print_package_overrides
();
400 sub get_conditional_dep
($$) {
401 my $condition = shift;
404 if ($condition =~ /^!(.+)/) {
405 return "\$(if \$(CONFIG_$1),,$depstr)";
407 return "\$(if \$(CONFIG_$condition),$depstr)";
414 sub gen_package_mk
() {
419 parse_package_metadata
($ARGV[0]) or exit 1;
420 foreach my $srcname (sort {uc($a) cmp uc($b)} keys %srcpackage) {
421 my $src = $srcpackage{$srcname};
425 foreach my $pkg (@
{$src->{packages
}}) {
426 next if defined $pkg->{vdepends
};
429 $config = "\$(CONFIG_PACKAGE_$pkg->{name})" unless $pkg->{buildonly
};
431 $pkg->{prereq
} and print "prereq-$config += $src->{path}\n";
433 next if $pkg->{buildonly
};
435 print "package-$config += $src->{path}\n";
437 if ($pkg->{variant
}) {
438 if (!defined($variant_default) or $pkg->{variant_default
}) {
439 $variant_default = $pkg->{variant
};
441 print "\$(curdir)/$src->{path}/variants += \$(if $config,$pkg->{variant})\n";
445 if (defined($variant_default)) {
446 print "\$(curdir)/$src->{path}/default-variant := $variant_default\n";
449 unless (grep {!$_->{buildonly
}} @
{$src->{packages
}}) {
450 print "package- += $src->{path}\n";
453 if (@
{$src->{buildtypes
}} > 0) {
454 print "buildtypes-$src->{path} = ".join(' ', @
{$src->{buildtypes
}})."\n";
457 foreach my $dep (@
{$src->{builddepends
}}, map { @
{$_->{depends
}} } @
{$src->{packages
}}) {
463 foreach my $type (@
{$src->{buildtypes
}}) {
467 next unless $src->{"builddepends/$type"};
468 foreach my $dep (@
{$src->{"builddepends/$type"}}) {
473 if ($dep =~ /^(.+):(.+)/) {
477 if ($dep =~ /^(.+)\/(.+)/) {
484 my $pkg_dep = $package{$dep};
485 if (defined($pkg_dep) && defined($pkg_dep->{src
})) {
486 unless (!$deptype || grep { $_ eq $deptype } @
{$pkg_dep->{src
}{buildtypes
}}) {
487 warn sprintf "WARNING: Makefile '%s' has a %s build dependency on '%s/%s' but '%s' does not implement a '%s' build type\n",
488 $src->{makefile
}, $type, $pkg_dep->{src
}{name
}, $deptype, $pkg_dep->{src
}{makefile
}, $deptype;
491 $idx = $pkg_dep->{src
}{path
};
492 } elsif (defined($srcpackage{$dep})) {
493 $idx = $srcpackage{$dep}{path
};
497 my $depstr = "\$(curdir)/$idx$suffix/compile";
498 my $depline = get_conditional_dep
($condition, $depstr);
500 $deplines{$depline}++;
503 my $depline = join(" ", sort keys %deplines);
505 $line .= "\$(curdir)/$src->{path}/$type/compile += $depline\n";
511 foreach my $deps (@srcdeps) {
518 if ($deps =~ /^(.+):(.+)/) {
522 if ($deps =~ /^(.+)\/(.+)/) {
528 my $pkg_dep = $package{$deps};
531 if ($pkg_dep->{vdepends
}) {
532 @deps = @
{$pkg_dep->{vdepends
}};
537 foreach my $dep (@deps) {
538 $pkg_dep = $package{$deps};
539 if (defined $pkg_dep->{src
}) {
540 unless (!$deptype || grep { $_ eq $deptype } @
{$pkg_dep->{src
}{buildtypes
}}) {
541 warn sprintf "WARNING: Makefile '%s' has a build dependency on '%s/%s' but '%s' does not implement a '%s' build type\n",
542 $src->{makefile
}, $pkg_dep->{src
}{name
}, $deptype, $pkg_dep->{src
}{makefile
}, $deptype;
545 $idx = $pkg_dep->{src
}{path
};
546 } elsif (defined($srcpackage{$dep})) {
547 $idx = $srcpackage{$dep}{path
};
554 next if $srcname eq $pkg_dep->{src
}{name
}.$suffix;
555 next if $dep{$condition.":".$srcname."->".$idx};
556 next if $dep{$srcname."->($dep)".$idx} and $pkg_dep->{vdepends
};
559 if ($pkg_dep->{vdepends
}) {
560 $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
561 $dep{$srcname."->($dep)".$idx} = 1;
563 $depstr = "\$(curdir)/$idx/compile";
564 $dep{$srcname."->".$idx} = 1;
566 $depline = get_conditional_dep
($condition, $depstr);
568 $deplines{$depline}++;
573 my $depline = join(" ", sort keys %deplines);
575 $line .= "\$(curdir)/$src->{path}/compile += $depline\n";
582 foreach my $preconfig (keys %preconfig) {
584 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
585 my $conf = $preconfig{$preconfig}->{$cfg}->{id
};
587 $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
592 ifndef DUMP_TARGET_DB
593 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
598 ifneq (\$(IMAGEOPT)\$(CONFIG_IMAGEOPT),)
599 package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
607 sub gen_package_source
() {
608 parse_package_metadata
($ARGV[0]) or exit 1;
609 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
610 my $pkg = $package{$name};
611 if ($pkg->{name
} && $pkg->{source
}) {
612 print "$pkg->{name}: ";
613 print "$pkg->{source}\n";
618 sub gen_package_subdirs
() {
619 parse_package_metadata
($ARGV[0]) or exit 1;
620 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
621 my $pkg = $package{$name};
622 if ($pkg->{name
} && $pkg->{repository
}) {
623 print "Package/$name/subdir = $pkg->{repository}\n";
628 sub gen_package_license
($) {
630 parse_package_metadata
($ARGV[0]) or exit 1;
631 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
632 my $pkg = $package{$name};
634 if ($pkg->{license
}) {
635 print "$pkg->{name}: ";
636 print "$pkg->{license}\n";
637 if ($pkg->{licensefiles
} && $level == 0) {
638 print "\tFiles: $pkg->{licensefiles}\n";
642 print "$pkg->{name}: Missing license! ";
643 print "Please fix $pkg->{src}{makefile}\n";
650 sub gen_version_filtered_list
() {
651 foreach my $item (version_filter_list
(@ARGV)) {
656 sub gen_usergroup_list
() {
657 parse_package_metadata
($ARGV[0]) or exit 1;
658 for my $name (keys %usernames) {
659 print "user $name $usernames{$name}{id} $usernames{$name}{makefile}\n";
661 for my $name (keys %groupnames) {
662 print "group $name $groupnames{$name}{id} $groupnames{$name}{makefile}\n";
666 sub parse_command
() {
667 GetOptions
("ignore=s", \
@ignore);
668 my $cmd = shift @ARGV;
670 /^mk$/ and return gen_package_mk
();
671 /^config$/ and return gen_package_config
();
672 /^kconfig/ and return gen_kconfig_overrides
();
673 /^source$/ and return gen_package_source
();
674 /^subdirs$/ and return gen_package_subdirs
();
675 /^license$/ and return gen_package_license
(0);
676 /^licensefull$/ and return gen_package_license
(1);
677 /^usergroup$/ and return gen_usergroup_list
();
678 /^version_filter$/ and return gen_version_filtered_list
();
682 $0 mk [file] Package metadata in makefile format
683 $0 config [file] Package metadata in Kconfig format
684 $0 kconfig [file] [config] [patchver] Kernel config overrides
685 $0 source [file] Package source file information
686 $0 subdirs [file] Package subdir information in makefile format
687 $0 license [file] Package license information
688 $0 licensefull [file] Package license information (full list)
689 $0 usergroup [file] Package usergroup allocation list
690 $0 version_filter [patchver] [list...] Filter list of version tagged strings
693 --ignore <name> Ignore the source package <name>