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
}) {
180 $depend = shift @vdeps;
182 $condition = '!('.join("||", map { "PACKAGE_".$_ } @vdeps).')';
183 } elsif (@vdeps > 0) {
184 $condition = '!PACKAGE_'.$vdeps[0];
188 # Menuconfig will not treat 'select FOO' as a real dependency
189 # thus if FOO depends on other config options, these dependencies
190 # will not be checked. To fix this, we simply emit all of FOO's
191 # depends here as well.
192 $package{$depend} and push @t_depends, [ $package{$depend}->{depends
}, $condition ];
197 if ($vdep = $package{$depend}->{vdepends
}) {
198 $depend = join("||", map { "PACKAGE_".$_ } @
$vdep);
201 $flags =~ /@/ or $depend = "PACKAGE_$depend";
203 if ($m =~ /select/) {
204 next if $depend eq $condition;
205 $depend = "$depend if $condition";
207 $depend = "!($condition) || $depend" unless $dep->{$condition} eq 'select';
210 $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
213 foreach my $tdep (@t_depends) {
214 mconf_depends
($pkgname, $tdep->[0], 1, $dep, $seen, $tdep->[1]);
217 foreach my $depend (keys %$dep) {
218 my $m = $dep->{$depend};
219 $res .= "\t\t$m $depend\n";
224 sub mconf_conflicts
{
229 foreach my $depend (@
$depends) {
230 next unless $package{$depend};
231 $res .= "\t\tdepends on m || (PACKAGE_$depend != y)\n";
236 sub print_package_config_category
($) {
241 return unless $category{$cat};
243 print "menu \"$cat\"\n\n";
244 my %spkg = %{$category{$cat}};
246 foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
247 foreach my $pkg (@
{$spkg{$spkg}}) {
248 next if $pkg->{buildonly
};
249 my $menu = $pkg->{submenu
};
251 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep
};
255 $menus{$menu} or $menus{$menu} = [];
256 push @
{$menus{$menu}}, $pkg;
260 ($a eq 'undef' ?
1 : 0) or
261 ($b eq 'undef' ?
-1 : 0) or
265 foreach my $menu (@menus) {
267 package_depends
($a, $b) or
268 ($a->{name
} cmp $b->{name
})
270 if ($menu ne 'undef') {
271 $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
272 print "menu \"$menu\"\n";
274 foreach my $pkg (@pkgs) {
275 next if $pkg->{ignore
};
276 my $title = $pkg->{name
};
277 my $c = (72 - length($pkg->{name
}) - length($pkg->{title
}));
279 $title .= ("." x
$c). " ". $pkg->{title
};
281 $title = "\"$title\"";
283 $pkg->{menu
} and print "menu";
284 print "config PACKAGE_".$pkg->{name
}."\n";
285 $pkg->{hidden
} and $title = "";
286 print "\t\t".($pkg->{tristate
} ?
'tristate' : 'bool')." $title\n";
287 print "\t\tdefault y if DEFAULT_".$pkg->{name
}."\n";
288 unless ($pkg->{hidden
}) {
290 if (!exists($pkg->{repository
})) {
291 push @def, "ALL_NONSHARED";
293 if ($pkg->{name
} =~ /^kmod-/) {
294 push @def, "ALL_KMODS";
296 $pkg->{default} ||= "m if " . join("||", @def);
298 if ($pkg->{default}) {
299 foreach my $default (split /\s*,\s*/, $pkg->{default}) {
300 print "\t\tdefault $default\n";
303 print mconf_depends
($pkg->{name
}, $pkg->{depends
}, 0);
304 print mconf_depends
($pkg->{name
}, $pkg->{mdepends
}, 0);
305 print mconf_conflicts
($pkg->{name
}, $pkg->{conflicts
});
307 print $pkg->{description
};
310 $pkg->{config
} and print $pkg->{config
}."\n";
312 if ($menu ne 'undef') {
314 $menu_dep{$menu} and print "endif\n";
319 undef $category{$cat};
322 sub print_package_features
() {
323 keys %features > 0 or return;
324 print "menu \"Package features\"\n";
325 foreach my $n (keys %features) {
326 my @features = sort { $b->{priority
} <=> $a->{priority
} or $a->{title
} cmp $b->{title
} } @
{$features{$n}};
329 prompt "$features[0]->{target_title}"
330 default FEATURE_$features[0]->{name}
333 foreach my $feature (@features) {
335 config FEATURE_$feature->{name}
336 bool "$feature->{title}"
338 $feature->{description
} =~ /\w/ and do {
339 print "\t\thelp\n".$feature->{description
}."\n";
347 sub print_package_overrides
() {
348 keys %overrides > 0 or return;
349 print "\tconfig OVERRIDE_PKGS\n";
350 print "\t\tstring\n";
351 print "\t\tdefault \"".join(" ", keys %overrides)."\"\n\n";
354 sub gen_package_config
() {
355 parse_package_metadata
($ARGV[0]) or exit 1;
356 print "menuconfig IMAGEOPT\n\tbool \"Image configuration\"\n\tdefault n\n";
357 foreach my $preconfig (keys %preconfig) {
358 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
359 my $conf = $preconfig{$preconfig}->{$cfg}->{id
};
362 config UCI_PRECONFIG_$conf
363 string "$preconfig{$preconfig}->{$cfg}->{label}" if IMAGEOPT
364 depends on PACKAGE_$preconfig
365 default "$preconfig{$preconfig}->{$cfg}->{default}"
370 print "source \"package/*/image-config.in\"\n";
371 if (scalar glob "package/feeds/*/*/image-config.in") {
372 print "source \"package/feeds/*/*/image-config.in\"\n";
374 print_package_features
();
375 print_package_config_category
'Base system';
376 foreach my $cat (sort {uc($a) cmp uc($b)} keys %category) {
377 print_package_config_category
$cat;
379 print_package_overrides
();
382 sub get_conditional_dep
($$) {
383 my $condition = shift;
386 if ($condition =~ /^!(.+)/) {
387 return "\$(if \$(CONFIG_$1),,$depstr)";
389 return "\$(if \$(CONFIG_$condition),$depstr)";
396 sub gen_package_mk
() {
402 parse_package_metadata
($ARGV[0]) or exit 1;
403 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
405 my $pkg = $package{$name};
408 next if defined $pkg->{vdepends
};
410 $config = "\$(CONFIG_PACKAGE_$name)";
412 $pkg->{buildonly
} and $config = "";
413 print "package-$config += $pkg->{subdir}$pkg->{src}\n";
414 if ($pkg->{variant
}) {
415 if (!defined($done{$pkg->{src
}}) or $pkg->{variant_default
}) {
416 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/default-variant := $pkg->{variant}\n";
418 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/variants += \$(if $config,$pkg->{variant})\n"
420 $pkg->{prereq
} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
423 next if $done{$pkg->{src
}};
424 $done{$pkg->{src
}} = 1;
426 if (@
{$pkg->{buildtypes
}} > 0) {
427 print "buildtypes-$pkg->{subdir}$pkg->{src} = ".join(' ', @
{$pkg->{buildtypes
}})."\n";
430 foreach my $spkg (@
{$srcpackage{$pkg->{src
}}}) {
431 foreach my $dep (@
{$spkg->{depends
}}, @
{$spkg->{builddepends
}}) {
438 foreach my $type (@
{$pkg->{buildtypes
}}) {
442 next unless $pkg->{"builddepends/$type"};
443 foreach my $dep (@
{$pkg->{"builddepends/$type"}}) {
447 if ($dep =~ /^(.+):(.+)/) {
451 if ($dep =~ /^(.+)(\/.+)/) {
457 my $pkg_dep = $package{$dep};
458 if (defined($pkg_dep) && defined($pkg_dep->{src
})) {
459 $idx = $pkg_dep->{subdir
}.$pkg_dep->{src
};
460 } elsif (defined($srcpackage{$dep})) {
461 $idx = $subdir{$dep}.$dep;
465 my $depstr = "\$(curdir)/$idx$suffix/compile";
466 my $depline = get_conditional_dep
($condition, $depstr);
468 $deplines{$depline}++;
471 my $depline = join(" ", sort keys %deplines);
473 $line .= "\$(curdir)/".$pkg->{subdir
}."$pkg->{src}/$type/compile += $depline\n";
479 foreach my $deps (@srcdeps) {
485 if ($deps =~ /^(.+):(.+)/) {
489 if ($deps =~ /^(.+)(\/.+)/) {
494 my $pkg_dep = $package{$deps};
497 if ($pkg_dep->{vdepends
}) {
498 @deps = @
{$pkg_dep->{vdepends
}};
503 foreach my $dep (@deps) {
504 $pkg_dep = $package{$deps};
505 if (defined $pkg_dep->{src
}) {
506 ($pkg->{src
} ne $pkg_dep->{src
}.$suffix) and $idx = $pkg_dep->{subdir
}.$pkg_dep->{src
};
507 } elsif (defined($srcpackage{$dep})) {
508 $idx = $subdir{$dep}.$dep;
510 undef $idx if $idx eq 'base-files';
515 next if $pkg->{src
} eq $pkg_dep->{src
}.$suffix;
516 next if $dep{$condition.":".$pkg->{src
}."->".$idx};
517 next if $dep{$pkg->{src
}."->($dep)".$idx} and $pkg_dep->{vdepends
};
520 if ($pkg_dep->{vdepends
}) {
521 $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
522 $dep{$pkg->{src
}."->($dep)".$idx} = 1;
524 $depstr = "\$(curdir)/$idx/compile";
525 $dep{$pkg->{src
}."->".$idx} = 1;
527 $depline = get_conditional_dep
($condition, $depstr);
529 $deplines{$depline}++;
534 my $depline = join(" ", sort keys %deplines);
536 $line .= "\$(curdir)/".$pkg->{subdir
}."$pkg->{src}/compile += $depline\n";
543 foreach my $preconfig (keys %preconfig) {
545 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
546 my $conf = $preconfig{$preconfig}->{$cfg}->{id
};
548 $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
553 ifndef DUMP_TARGET_DB
554 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
559 ifneq (\$(IMAGEOPT)\$(CONFIG_IMAGEOPT),)
560 package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
568 sub gen_package_source
() {
569 parse_package_metadata
($ARGV[0]) or exit 1;
570 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
571 my $pkg = $package{$name};
572 if ($pkg->{name
} && $pkg->{source
}) {
573 print "$pkg->{name}: ";
574 print "$pkg->{source}\n";
579 sub gen_package_subdirs
() {
580 parse_package_metadata
($ARGV[0]) or exit 1;
581 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
582 my $pkg = $package{$name};
583 if ($pkg->{name
} && $pkg->{repository
}) {
584 print "Package/$name/subdir = $pkg->{repository}\n";
589 sub gen_package_license
($) {
591 parse_package_metadata
($ARGV[0]) or exit 1;
592 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
593 my $pkg = $package{$name};
595 if ($pkg->{license
}) {
596 print "$pkg->{name}: ";
597 print "$pkg->{license}\n";
598 if ($pkg->{licensefiles
} && $level == 0) {
599 print "\tFiles: $pkg->{licensefiles}\n";
603 print "$pkg->{name}: Missing license! ";
604 print "Please fix $pkg->{makefile}\n";
611 sub gen_version_filtered_list
() {
612 foreach my $item (version_filter_list
(@ARGV)) {
617 sub parse_command
() {
618 GetOptions
("ignore=s", \
@ignore);
619 my $cmd = shift @ARGV;
621 /^mk$/ and return gen_package_mk
();
622 /^config$/ and return gen_package_config
();
623 /^kconfig/ and return gen_kconfig_overrides
();
624 /^source$/ and return gen_package_source
();
625 /^subdirs$/ and return gen_package_subdirs
();
626 /^license$/ and return gen_package_license
(0);
627 /^licensefull$/ and return gen_package_license
(1);
628 /^version_filter$/ and return gen_version_filtered_list
();
632 $0 mk [file] Package metadata in makefile format
633 $0 config [file] Package metadata in Kconfig format
634 $0 kconfig [file] [config] [patchver] Kernel config overrides
635 $0 source [file] Package source file information
636 $0 subdirs [file] Package subdir information in makefile format
637 $0 license [file] Package license information
638 $0 licensefull [file] Package license information (full list)
639 $0 version_filter [patchver] [list...] Filter list of version tagged strings
642 --ignore <name> Ignore the source package <name>