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";
100 sub merge_package_lists($$) {
106 foreach my $pkg (@$list1, @$list2) {
109 foreach my $pkg (keys %pkgs) {
110 push @l, $pkg unless ($pkg =~ /^-/ or $pkgs{"-$pkg"});
115 sub target_config_features(@) {
118 while ($_ = shift @_) {
119 /arm_v(\w+)/ and $ret .= "\tselect arm_v$1\n";
120 /broken/ and $ret .= "\tdepends on BROKEN\n";
121 /audio/ and $ret .= "\tselect AUDIO_SUPPORT\n";
122 /display/ and $ret .= "\tselect DISPLAY_SUPPORT\n";
123 /dt/ and $ret .= "\tselect USES_DEVICETREE\n";
124 /gpio/ and $ret .= "\tselect GPIO_SUPPORT\n";
125 /pci/ and $ret .= "\tselect PCI_SUPPORT\n";
126 /pcie/ and $ret .= "\tselect PCIE_SUPPORT\n";
127 /usb/ and $ret .= "\tselect USB_SUPPORT\n";
128 /usbgadget/ and $ret .= "\tselect USB_GADGET_SUPPORT\n";
129 /pcmcia/ and $ret .= "\tselect PCMCIA_SUPPORT\n";
130 /rtc/ and $ret .= "\tselect RTC_SUPPORT\n";
131 /squashfs/ and $ret .= "\tselect USES_SQUASHFS\n";
132 /jffs2$/ and $ret .= "\tselect USES_JFFS2\n";
133 /jffs2_nand/ and $ret .= "\tselect USES_JFFS2_NAND\n";
134 /ext4/ and $ret .= "\tselect USES_EXT4\n";
135 /targz/ and $ret .= "\tselect USES_TARGZ\n";
136 /cpiogz/ and $ret .= "\tselect USES_CPIOGZ\n";
137 /ubifs/ and $ret .= "\tselect USES_UBIFS\n";
138 /fpu/ and $ret .= "\tselect HAS_FPU\n";
139 /spe_fpu/ and $ret .= "\tselect HAS_SPE_FPU\n";
140 /ramdisk/ and $ret .= "\tselect USES_INITRAMFS\n";
141 /powerpc64/ and $ret .= "\tselect powerpc64\n";
142 /nommu/ and $ret .= "\tselect NOMMU\n";
143 /mips16/ and $ret .= "\tselect HAS_MIPS16\n";
144 /rfkill/ and $ret .= "\tselect RFKILL_SUPPORT\n";
145 /low_mem/ and $ret .= "\tselect LOW_MEMORY_FOOTPRINT\n";
146 /nand/ and $ret .= "\tselect NAND_SUPPORT\n";
153 my $parent = $target->{parent};
155 return $target->{parent}->{name}." - ".$target->{name};
157 return $target->{name};
164 if (substr($v,0,2) eq "2_") {
165 $v =~ /(\d+_\d+_\d+)(_\d+)?/ and $v = $1;
167 $v =~ /(\d+_\d+)(_\d+)?/ and $v = $1;
172 sub print_target($) {
174 my $features = target_config_features(@{$target->{features}});
175 my $help = $target->{desc};
180 if ($help =~ /\w+/) {
181 $help =~ s/^\s*/\t /mg;
182 $help = "\thelp\n$help";
187 my $v = kver($target->{version});
188 if (@{$target->{subtargets}} == 0) {
190 config TARGET_$target->{conf}
191 bool "$target->{name}"
197 config TARGET_$target->{conf}
198 bool "$target->{name}"
201 if ($target->{subtarget}) {
202 $confstr .= "\tdepends on TARGET_$target->{boardconf}\n";
204 if (@{$target->{subtargets}} > 0) {
205 $confstr .= "\tselect HAS_SUBTARGETS\n";
206 grep { /broken/ } @{$target->{features}} and $confstr .= "\tdepends on BROKEN\n";
208 $confstr .= $features;
209 if ($target->{arch} =~ /\w/) {
210 $confstr .= "\tselect $target->{arch}\n";
214 foreach my $dep (@{$target->{depends}}) {
215 my $mode = "depends on";
219 $dep =~ /^([@\+\-]+)(.+)$/;
223 next if $name =~ /:/;
224 $flags =~ /-/ and $mode = "deselect";
225 $flags =~ /\+/ and $mode = "select";
226 $flags =~ /@/ and $confstr .= "\t$mode $name\n";
228 $confstr .= "$help\n\n";
232 sub gen_target_config() {
233 my $file = shift @ARGV;
234 my @target = parse_target_metadata($file);
237 my @target_sort = sort {
238 target_name($a) cmp target_name($b);
244 prompt "Target System"
245 default TARGET_ar71xx
250 foreach my $target (@target_sort) {
251 next if $target->{subtarget};
252 print_target($target);
259 prompt "Subtarget" if HAS_SUBTARGETS
261 foreach my $target (@target) {
262 next unless $target->{def_subtarget};
264 default TARGET_$target->{conf}_$target->{def_subtarget} if TARGET_$target->{conf}
270 foreach my $target (@target) {
271 next unless $target->{subtarget};
272 print_target($target);
279 prompt "Target Profile"
283 foreach my $target (@target) {
284 my $profiles = $target->{profiles};
285 $target->{sort} and @$profiles = sort {
286 $a->{priority} <=> $b->{priority} or
287 $a->{name} cmp $b->{name};
290 foreach my $profile (@$profiles) {
292 config TARGET_$target->{conf}_$profile->{id}
293 bool "$profile->{name}"
294 depends on TARGET_$target->{conf}
297 $profile->{kconfig} and print "\tselect PROFILE_KCONFIG\n";
298 my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
299 foreach my $pkg (@pkglist) {
300 print "\tselect DEFAULT_$pkg\n";
303 my $help = $profile->{desc};
304 if ($help =~ /\w+/) {
305 $help =~ s/^\s*/\t /mg;
306 $help = "\thelp\n$help";
317 config HAS_SUBTARGETS
324 foreach my $target (@target) {
325 $target->{subtarget} or print "\t\tdefault \"".$target->{board}."\" if TARGET_".$target->{conf}."\n";
328 config TARGET_SUBTARGET
330 default "generic" if !HAS_SUBTARGETS
334 foreach my $target (@target) {
335 foreach my $subtarget (@{$target->{subtargets}}) {
336 print "\t\tdefault \"$subtarget\" if TARGET_".$target->{conf}."_$subtarget\n";
340 config TARGET_ARCH_PACKAGES
344 foreach my $target (@target) {
345 next if @{$target->{subtargets}} > 0;
346 print "\t\tdefault \"".($target->{arch_packages} || $target->{board})."\" if TARGET_".$target->{conf}."\n";
350 config DEFAULT_TARGET_OPTIMIZATION
353 foreach my $target (@target) {
354 next if @{$target->{subtargets}} > 0;
355 print "\tdefault \"".$target->{cflags}."\" if TARGET_".$target->{conf}."\n";
357 print "\tdefault \"-Os -pipe -funit-at-a-time\"\n";
363 foreach my $target (@target) {
364 next if @{$target->{subtargets}} > 0;
365 print "\tdefault \"".$target->{cputype}."\" if TARGET_".$target->{conf}."\n";
367 print "\tdefault \"\"\n";
370 foreach my $target (@target) {
371 my $v = kver($target->{version});
381 foreach my $def (sort keys %defaults) {
382 print "\tconfig DEFAULT_".$def."\n";
383 print "\t\tbool\n\n";
388 sub __find_package_dep($$) {
391 my $deps = ($pkg->{vdepends} or $pkg->{depends});
393 return 0 unless defined $deps;
394 foreach my $dep (@{$deps}) {
395 next if $dep_check{$dep};
396 $dep_check{$dep} = 1;
397 return 1 if $dep eq $name;
398 return 1 if ($package{$dep} and (__find_package_dep($package{$dep},$name) == 1));
403 # wrapper to avoid infinite recursion
404 sub find_package_dep($$) {
409 return __find_package_dep($pkg, $name);
412 sub package_depends($$) {
417 return 0 if ($a->{submenu} ne $b->{submenu});
418 if (find_package_dep($a, $b->{name}) == 1) {
420 } elsif (find_package_dep($b, $a->{name}) == 1) {
431 my $only_dep = shift;
435 my $parent_condition = shift;
441 my @depends = @$depends;
442 foreach my $depend (@depends) {
443 my $m = "depends on";
445 $depend =~ s/^([@\+]+)// and $flags = $1;
447 my $condition = $parent_condition;
449 next if $condition eq $depend;
450 next if $seen->{"$parent_condition:$depend"};
451 next if $seen->{":$depend"};
452 $seen->{"$parent_condition:$depend"} = 1;
453 if ($depend =~ /^(.+):(.+)$/) {
454 if ($1 ne "PACKAGE_$pkgname") {
456 $condition = "$condition && $1";
463 next if $package{$depend} and $package{$depend}->{buildonly};
464 if ($flags =~ /\+/) {
465 if ($vdep = $package{$depend}->{vdepends}) {
467 $depend = shift @vdeps;
469 $condition = '!('.join("||", map { "PACKAGE_".$_ } @vdeps).')';
470 } elsif (@vdeps > 0) {
471 $condition = '!PACKAGE_'.$vdeps[0];
475 # Menuconfig will not treat 'select FOO' as a real dependency
476 # thus if FOO depends on other config options, these dependencies
477 # will not be checked. To fix this, we simply emit all of FOO's
478 # depends here as well.
479 $package{$depend} and push @t_depends, [ $package{$depend}->{depends}, $condition ];
484 if ($vdep = $package{$depend}->{vdepends}) {
485 $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
488 $flags =~ /@/ or $depend = "PACKAGE_$depend";
490 if ($m =~ /select/) {
491 next if $depend eq $condition;
492 $depend = "$depend if $condition";
494 $depend = "!($condition) || $depend" unless $dep->{$condition} eq 'select';
497 $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
500 foreach my $tdep (@t_depends) {
501 mconf_depends($pkgname, $tdep->[0], 1, $dep, $seen, $tdep->[1]);
504 foreach my $depend (keys %$dep) {
505 my $m = $dep->{$depend};
506 $res .= "\t\t$m $depend\n";
511 sub mconf_conflicts {
516 foreach my $depend (@$depends) {
517 next unless $package{$depend};
518 $res .= "\t\tdepends on m || (PACKAGE_$depend != y)\n";
523 sub print_package_config_category($) {
528 return unless $category{$cat};
530 print "menu \"$cat\"\n\n";
531 my %spkg = %{$category{$cat}};
533 foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
534 foreach my $pkg (@{$spkg{$spkg}}) {
535 next if $pkg->{buildonly};
536 my $menu = $pkg->{submenu};
538 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
542 $menus{$menu} or $menus{$menu} = [];
543 push @{$menus{$menu}}, $pkg;
547 ($a eq 'undef' ? 1 : 0) or
548 ($b eq 'undef' ? -1 : 0) or
552 foreach my $menu (@menus) {
554 package_depends($a, $b) or
555 ($a->{name} cmp $b->{name})
557 if ($menu ne 'undef') {
558 $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
559 print "menu \"$menu\"\n";
561 foreach my $pkg (@pkgs) {
562 my $title = $pkg->{name};
563 my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
565 $title .= ("." x $c). " ". $pkg->{title};
567 $title = "\"$title\"";
569 $pkg->{menu} and print "menu";
570 print "config PACKAGE_".$pkg->{name}."\n";
571 $pkg->{hidden} and $title = "";
572 print "\t\t".($pkg->{tristate} ? 'tristate' : 'bool')." $title\n";
573 print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
574 unless ($pkg->{hidden}) {
576 if (!exists($pkg->{repository})) {
577 push @def, "ALL_NONSHARED";
579 if ($pkg->{name} =~ /^kmod-/) {
580 push @def, "ALL_KMODS";
582 $pkg->{default} ||= "m if " . join("||", @def);
584 if ($pkg->{default}) {
585 foreach my $default (split /\s*,\s*/, $pkg->{default}) {
586 print "\t\tdefault $default\n";
589 print mconf_depends($pkg->{name}, $pkg->{depends}, 0);
590 print mconf_depends($pkg->{name}, $pkg->{mdepends}, 0);
591 print mconf_conflicts($pkg->{name}, $pkg->{conflicts});
593 print $pkg->{description};
596 $pkg->{config} and print $pkg->{config}."\n";
598 if ($menu ne 'undef') {
600 $menu_dep{$menu} and print "endif\n";
605 undef $category{$cat};
608 sub print_package_features() {
609 keys %features > 0 or return;
610 print "menu \"Package features\"\n";
611 foreach my $n (keys %features) {
612 my @features = sort { $b->{priority} <=> $a->{priority} or $a->{title} cmp $b->{title} } @{$features{$n}};
615 prompt "$features[0]->{target_title}"
616 default FEATURE_$features[0]->{name}
619 foreach my $feature (@features) {
621 config FEATURE_$feature->{name}
622 bool "$feature->{title}"
624 $feature->{description} =~ /\w/ and do {
625 print "\t\thelp\n".$feature->{description}."\n";
633 sub print_package_overrides() {
634 keys %overrides > 0 or return;
635 print "\tconfig OVERRIDE_PKGS\n";
636 print "\t\tstring\n";
637 print "\t\tdefault \"".join(" ", keys %overrides)."\"\n\n";
640 sub gen_package_config() {
641 parse_package_metadata($ARGV[0]) or exit 1;
642 print "menuconfig IMAGEOPT\n\tbool \"Image configuration\"\n\tdefault n\n";
643 foreach my $preconfig (keys %preconfig) {
644 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
645 my $conf = $preconfig{$preconfig}->{$cfg}->{id};
648 config UCI_PRECONFIG_$conf
649 string "$preconfig{$preconfig}->{$cfg}->{label}" if IMAGEOPT
650 depends on PACKAGE_$preconfig
651 default "$preconfig{$preconfig}->{$cfg}->{default}"
656 print "source \"package/*/image-config.in\"\n";
657 if (scalar glob "package/feeds/*/*/image-config.in") {
658 print "source \"package/feeds/*/*/image-config.in\"\n";
660 print_package_features();
661 print_package_config_category 'Base system';
662 foreach my $cat (sort {uc($a) cmp uc($b)} keys %category) {
663 print_package_config_category $cat;
665 print_package_overrides();
668 sub get_conditional_dep($$) {
669 my $condition = shift;
672 if ($condition =~ /^!(.+)/) {
673 return "\$(if \$(CONFIG_$1),,$depstr)";
675 return "\$(if \$(CONFIG_$condition),$depstr)";
682 sub gen_package_mk() {
688 parse_package_metadata($ARGV[0]) or exit 1;
689 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
691 my $pkg = $package{$name};
694 next if defined $pkg->{vdepends};
696 $config = "\$(CONFIG_PACKAGE_$name)";
698 $pkg->{buildonly} and $config = "";
699 print "package-$config += $pkg->{subdir}$pkg->{src}\n";
700 if ($pkg->{variant}) {
701 if (!defined($done{$pkg->{src}}) or $pkg->{variant_default}) {
702 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/default-variant := $pkg->{variant}\n";
704 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/variants += \$(if $config,$pkg->{variant})\n"
706 $pkg->{prereq} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
709 next if $done{$pkg->{src}};
710 $done{$pkg->{src}} = 1;
712 if (@{$pkg->{buildtypes}} > 0) {
713 print "buildtypes-$pkg->{subdir}$pkg->{src} = ".join(' ', @{$pkg->{buildtypes}})."\n";
716 foreach my $spkg (@{$srcpackage{$pkg->{src}}}) {
717 foreach my $dep (@{$spkg->{depends}}, @{$spkg->{builddepends}}) {
724 foreach my $type (@{$pkg->{buildtypes}}) {
728 next unless $pkg->{"builddepends/$type"};
729 foreach my $dep (@{$pkg->{"builddepends/$type"}}) {
733 if ($dep =~ /^(.+):(.+)/) {
737 if ($dep =~ /^(.+)(\/.+)/) {
743 my $pkg_dep = $package{$dep};
744 if (defined($pkg_dep) && defined($pkg_dep->{src})) {
745 $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
746 } elsif (defined($srcpackage{$dep})) {
747 $idx = $subdir{$dep}.$dep;
751 my $depstr = "\$(curdir)/$idx$suffix/compile";
752 my $depline = get_conditional_dep($condition, $depstr);
754 $deplines{$depline}++;
757 my $depline = join(" ", sort keys %deplines);
759 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/$type/compile += $depline\n";
765 foreach my $deps (@srcdeps) {
771 if ($deps =~ /^(.+):(.+)/) {
775 if ($deps =~ /^(.+)(\/.+)/) {
780 my $pkg_dep = $package{$deps};
783 if ($pkg_dep->{vdepends}) {
784 @deps = @{$pkg_dep->{vdepends}};
789 foreach my $dep (@deps) {
790 $pkg_dep = $package{$deps};
791 if (defined $pkg_dep->{src}) {
792 ($pkg->{src} ne $pkg_dep->{src}.$suffix) and $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
793 } elsif (defined($srcpackage{$dep})) {
794 $idx = $subdir{$dep}.$dep;
796 undef $idx if $idx eq 'base-files';
801 next if $pkg->{src} eq $pkg_dep->{src}.$suffix;
802 next if $dep{$condition.":".$pkg->{src}."->".$idx};
803 next if $dep{$pkg->{src}."->($dep)".$idx} and $pkg_dep->{vdepends};
806 if ($pkg_dep->{vdepends}) {
807 $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
808 $dep{$pkg->{src}."->($dep)".$idx} = 1;
810 $depstr = "\$(curdir)/$idx/compile";
811 $dep{$pkg->{src}."->".$idx} = 1;
813 $depline = get_conditional_dep($condition, $depstr);
815 $deplines{$depline}++;
820 my $depline = join(" ", sort keys %deplines);
822 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/compile += $depline\n";
829 foreach my $preconfig (keys %preconfig) {
831 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
832 my $conf = $preconfig{$preconfig}->{$cfg}->{id};
834 $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
839 ifndef DUMP_TARGET_DB
840 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
845 ifneq (\$(IMAGEOPT)\$(CONFIG_IMAGEOPT),)
846 package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
854 sub gen_package_source() {
855 parse_package_metadata($ARGV[0]) or exit 1;
856 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
857 my $pkg = $package{$name};
858 if ($pkg->{name} && $pkg->{source}) {
859 print "$pkg->{name}: ";
860 print "$pkg->{source}\n";
865 sub gen_package_subdirs() {
866 parse_package_metadata($ARGV[0]) or exit 1;
867 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
868 my $pkg = $package{$name};
869 if ($pkg->{name} && $pkg->{repository}) {
870 print "Package/$name/subdir = $pkg->{repository}\n";
875 sub gen_package_license($) {
877 parse_package_metadata($ARGV[0]) or exit 1;
878 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
879 my $pkg = $package{$name};
881 if ($pkg->{license}) {
882 print "$pkg->{name}: ";
883 print "$pkg->{license}\n";
884 if ($pkg->{licensefiles} && $level == 0) {
885 print "\tFiles: $pkg->{licensefiles}\n";
889 print "$pkg->{name}: Missing license! ";
890 print "Please fix $pkg->{makefile}\n";
897 sub gen_version_filtered_list() {
898 foreach my $item (version_filter_list(@ARGV)) {
903 sub parse_command() {
904 GetOptions("ignore=s", \@ignore);
905 my $cmd = shift @ARGV;
907 /^target_config$/ and return gen_target_config();
908 /^package_mk$/ and return gen_package_mk();
909 /^package_config$/ and return gen_package_config();
910 /^kconfig/ and return gen_kconfig_overrides();
911 /^package_source$/ and return gen_package_source();
912 /^package_subdirs$/ and return gen_package_subdirs();
913 /^package_license$/ and return gen_package_license(0);
914 /^package_licensefull$/ and return gen_package_license(1);
915 /^version_filter$/ and return gen_version_filtered_list();
919 $0 target_config [file] Target metadata in Kconfig format
920 $0 package_mk [file] Package metadata in makefile format
921 $0 package_config [file] Package metadata in Kconfig format
922 $0 kconfig [file] [config] [patchver] Kernel config overrides
923 $0 package_source [file] Package source file information
924 $0 package_subdirs [file] Package subdir information in makefile format
925 $0 package_license [file] Package license information
926 $0 package_licensefull [file] Package license information (full list)
927 $0 version_filter [patchver] [list...] Filter list of version tagged strings
930 --ignore <name> Ignore the source package <name>