3 use lib "$FindBin::Bin";
9 sub version_to_num($) {
13 if (defined($str) && $str =~ /^\d+(?:\.\d+)+$/)
15 my @n = (split(/\./, $str), 0, 0, 0, 0);
16 $num = ($n[0] << 24) | ($n[1] << 16) | ($n[2] << 8) | $n[3];
22 sub version_filter_list(@) {
23 my $cmpver = version_to_num(shift @_);
28 if ($item =~ s/@(lt|le|gt|ge|eq|ne)(\d+(?:\.\d+)+)\b//)
31 my $symver = version_to_num($2);
33 if ($symver > 0 && $cmpver > 0)
35 next unless (($op eq 'lt' && $cmpver < $symver) ||
36 ($op eq 'le' && $cmpver <= $symver) ||
37 ($op eq 'gt' && $cmpver > $symver) ||
38 ($op eq 'ge' && $cmpver >= $symver) ||
39 ($op eq 'eq' && $cmpver == $symver) ||
40 ($op eq 'ne' && $cmpver != $symver));
50 sub gen_kconfig_overrides() {
54 my $pkginfo = shift @ARGV;
55 my $cfgfile = shift @ARGV;
56 my $patchver = shift @ARGV;
58 # parameter 2: build system config
59 open FILE, "<$cfgfile" or return;
61 /^(CONFIG_.+?)=(.+)$/ and $config{$1} = 1;
65 # parameter 1: package metadata
66 open FILE, "<$pkginfo" or return;
68 /^Package:\s*(.+?)\s*$/ and $package = $1;
69 /^Kernel-Config:\s*(.+?)\s*$/ and do {
70 my @config = split /\s+/, $1;
71 foreach my $config (version_filter_list($patchver, @config)) {
74 if ($config =~ /^(.+?)=(.+)$/) {
79 if ($config{"CONFIG_PACKAGE_$package"} and ($config ne 'n')) {
80 next if $kconfig{$config} eq 'y';
81 $kconfig{$config} = $val;
82 } elsif (!$override) {
83 $kconfig{$config} or $kconfig{$config} = 'n';
90 foreach my $kconfig (sort keys %kconfig) {
91 if ($kconfig{$kconfig} eq 'n') {
92 print "# $kconfig is not set\n";
94 print "$kconfig=$kconfig{$kconfig}\n";
99 sub merge_package_lists($$) {
105 foreach my $pkg (@$list1, @$list2) {
108 foreach my $pkg (keys %pkgs) {
109 push @l, $pkg unless ($pkg =~ /^-/ or $pkgs{"-$pkg"});
114 sub target_config_features(@) {
117 while ($_ = shift @_) {
118 /arm_v(\w+)/ and $ret .= "\tselect arm_v$1\n";
119 /broken/ and $ret .= "\tdepends on BROKEN\n";
120 /audio/ and $ret .= "\tselect AUDIO_SUPPORT\n";
121 /display/ and $ret .= "\tselect DISPLAY_SUPPORT\n";
122 /dt/ and $ret .= "\tselect USES_DEVICETREE\n";
123 /gpio/ and $ret .= "\tselect GPIO_SUPPORT\n";
124 /pci/ and $ret .= "\tselect PCI_SUPPORT\n";
125 /pcie/ and $ret .= "\tselect PCIE_SUPPORT\n";
126 /usb/ and $ret .= "\tselect USB_SUPPORT\n";
127 /usbgadget/ and $ret .= "\tselect USB_GADGET_SUPPORT\n";
128 /pcmcia/ and $ret .= "\tselect PCMCIA_SUPPORT\n";
129 /rtc/ and $ret .= "\tselect RTC_SUPPORT\n";
130 /squashfs/ and $ret .= "\tselect USES_SQUASHFS\n";
131 /jffs2$/ and $ret .= "\tselect USES_JFFS2\n";
132 /jffs2_nand/ and $ret .= "\tselect USES_JFFS2_NAND\n";
133 /ext4/ and $ret .= "\tselect USES_EXT4\n";
134 /targz/ and $ret .= "\tselect USES_TARGZ\n";
135 /cpiogz/ and $ret .= "\tselect USES_CPIOGZ\n";
136 /ubifs/ and $ret .= "\tselect USES_UBIFS\n";
137 /fpu/ and $ret .= "\tselect HAS_FPU\n";
138 /spe_fpu/ and $ret .= "\tselect HAS_SPE_FPU\n";
139 /ramdisk/ and $ret .= "\tselect USES_INITRAMFS\n";
140 /powerpc64/ and $ret .= "\tselect powerpc64\n";
141 /nommu/ and $ret .= "\tselect NOMMU\n";
142 /mips16/ and $ret .= "\tselect HAS_MIPS16\n";
143 /rfkill/ and $ret .= "\tselect RFKILL_SUPPORT\n";
144 /low_mem/ and $ret .= "\tselect LOW_MEMORY_FOOTPRINT\n";
145 /nand/ and $ret .= "\tselect NAND_SUPPORT\n";
152 my $parent = $target->{parent};
154 return $target->{parent}->{name}." - ".$target->{name};
156 return $target->{name};
163 if (substr($v,0,2) eq "2_") {
164 $v =~ /(\d+_\d+_\d+)(_\d+)?/ and $v = $1;
166 $v =~ /(\d+_\d+)(_\d+)?/ and $v = $1;
171 sub print_target($) {
173 my $features = target_config_features(@{$target->{features}});
174 my $help = $target->{desc};
179 if ($help =~ /\w+/) {
180 $help =~ s/^\s*/\t /mg;
181 $help = "\thelp\n$help";
186 my $v = kver($target->{version});
187 if (@{$target->{subtargets}} == 0) {
189 config TARGET_$target->{conf}
190 bool "$target->{name}"
196 config TARGET_$target->{conf}
197 bool "$target->{name}"
200 if ($target->{subtarget}) {
201 $confstr .= "\tdepends on TARGET_$target->{boardconf}\n";
203 if (@{$target->{subtargets}} > 0) {
204 $confstr .= "\tselect HAS_SUBTARGETS\n";
205 grep { /broken/ } @{$target->{features}} and $confstr .= "\tdepends on BROKEN\n";
207 $confstr .= $features;
208 if ($target->{arch} =~ /\w/) {
209 $confstr .= "\tselect $target->{arch}\n";
213 foreach my $dep (@{$target->{depends}}) {
214 my $mode = "depends on";
218 $dep =~ /^([@\+\-]+)(.+)$/;
222 next if $name =~ /:/;
223 $flags =~ /-/ and $mode = "deselect";
224 $flags =~ /\+/ and $mode = "select";
225 $flags =~ /@/ and $confstr .= "\t$mode $name\n";
227 $confstr .= "$help\n\n";
231 sub gen_target_config() {
232 my $file = shift @ARGV;
233 my @target = parse_target_metadata($file);
236 my @target_sort = sort {
237 target_name($a) cmp target_name($b);
243 prompt "Target System"
244 default TARGET_ar71xx
249 foreach my $target (@target_sort) {
250 next if $target->{subtarget};
251 print_target($target);
258 prompt "Subtarget" if HAS_SUBTARGETS
260 foreach my $target (@target) {
261 next unless $target->{def_subtarget};
263 default TARGET_$target->{conf}_$target->{def_subtarget} if TARGET_$target->{conf}
269 foreach my $target (@target) {
270 next unless $target->{subtarget};
271 print_target($target);
278 prompt "Target Profile"
282 foreach my $target (@target) {
283 my $profiles = $target->{profiles};
285 foreach my $profile (@$profiles) {
287 config TARGET_$target->{conf}_$profile->{id}
288 bool "$profile->{name}"
289 depends on TARGET_$target->{conf}
292 $profile->{kconfig} and print "\tselect PROFILE_KCONFIG\n";
293 my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
294 foreach my $pkg (@pkglist) {
295 print "\tselect DEFAULT_$pkg\n";
298 my $help = $profile->{desc};
299 if ($help =~ /\w+/) {
300 $help =~ s/^\s*/\t /mg;
301 $help = "\thelp\n$help";
312 config HAS_SUBTARGETS
319 foreach my $target (@target) {
320 $target->{subtarget} or print "\t\tdefault \"".$target->{board}."\" if TARGET_".$target->{conf}."\n";
323 config TARGET_ARCH_PACKAGES
327 foreach my $target (@target) {
328 next if @{$target->{subtargets}} > 0;
329 print "\t\tdefault \"".($target->{arch_packages} || $target->{board})."\" if TARGET_".$target->{conf}."\n";
333 config DEFAULT_TARGET_OPTIMIZATION
336 foreach my $target (@target) {
337 next if @{$target->{subtargets}} > 0;
338 print "\tdefault \"".$target->{cflags}."\" if TARGET_".$target->{conf}."\n";
340 print "\tdefault \"-Os -pipe -funit-at-a-time\"\n";
346 foreach my $target (@target) {
347 next if @{$target->{subtargets}} > 0;
348 print "\tdefault \"".$target->{cputype}."\" if TARGET_".$target->{conf}."\n";
350 print "\tdefault \"\"\n";
353 foreach my $target (@target) {
354 my $v = kver($target->{version});
364 foreach my $def (sort keys %defaults) {
365 print "\tconfig DEFAULT_".$def."\n";
366 print "\t\tbool\n\n";
371 sub __find_package_dep($$) {
374 my $deps = ($pkg->{vdepends} or $pkg->{depends});
376 return 0 unless defined $deps;
377 foreach my $dep (@{$deps}) {
378 next if $dep_check{$dep};
379 $dep_check{$dep} = 1;
380 return 1 if $dep eq $name;
381 return 1 if ($package{$dep} and (__find_package_dep($package{$dep},$name) == 1));
386 # wrapper to avoid infinite recursion
387 sub find_package_dep($$) {
392 return __find_package_dep($pkg, $name);
395 sub package_depends($$) {
400 return 0 if ($a->{submenu} ne $b->{submenu});
401 if (find_package_dep($a, $b->{name}) == 1) {
403 } elsif (find_package_dep($b, $a->{name}) == 1) {
414 my $only_dep = shift;
418 my $parent_condition = shift;
424 my @depends = @$depends;
425 foreach my $depend (@depends) {
426 my $m = "depends on";
428 $depend =~ s/^([@\+]+)// and $flags = $1;
430 my $condition = $parent_condition;
432 next if $condition eq $depend;
433 next if $seen->{"$parent_condition:$depend"};
434 next if $seen->{":$depend"};
435 $seen->{"$parent_condition:$depend"} = 1;
436 if ($depend =~ /^(.+):(.+)$/) {
437 if ($1 ne "PACKAGE_$pkgname") {
439 $condition = "$condition && $1";
446 next if $package{$depend} and $package{$depend}->{buildonly};
447 if ($vdep = $package{$depend}->{vdepends}) {
448 $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
450 $flags =~ /\+/ and do {
451 # Menuconfig will not treat 'select FOO' as a real dependency
452 # thus if FOO depends on other config options, these dependencies
453 # will not be checked. To fix this, we simply emit all of FOO's
454 # depends here as well.
455 $package{$depend} and push @t_depends, [ $package{$depend}->{depends}, $condition ];
460 $flags =~ /@/ or $depend = "PACKAGE_$depend";
462 if ($m =~ /select/) {
463 next if $depend eq $condition;
464 $depend = "$depend if $condition";
466 $depend = "!($condition) || $depend" unless $dep->{$condition} eq 'select';
470 $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
473 foreach my $tdep (@t_depends) {
474 mconf_depends($pkgname, $tdep->[0], 1, $dep, $seen, $tdep->[1]);
477 foreach my $depend (keys %$dep) {
478 my $m = $dep->{$depend};
479 $res .= "\t\t$m $depend\n";
484 sub mconf_conflicts {
489 foreach my $depend (@$depends) {
490 next unless $package{$depend};
491 $res .= "\t\tdepends on m || (PACKAGE_$depend != y)\n";
496 sub print_package_config_category($) {
501 return unless $category{$cat};
503 print "menu \"$cat\"\n\n";
504 my %spkg = %{$category{$cat}};
506 foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
507 foreach my $pkg (@{$spkg{$spkg}}) {
508 next if $pkg->{buildonly};
509 my $menu = $pkg->{submenu};
511 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
515 $menus{$menu} or $menus{$menu} = [];
516 push @{$menus{$menu}}, $pkg;
520 ($a eq 'undef' ? 1 : 0) or
521 ($b eq 'undef' ? -1 : 0) or
525 foreach my $menu (@menus) {
527 package_depends($a, $b) or
528 ($a->{name} cmp $b->{name})
530 if ($menu ne 'undef') {
531 $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
532 print "menu \"$menu\"\n";
534 foreach my $pkg (@pkgs) {
535 my $title = $pkg->{name};
536 my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
538 $title .= ("." x $c). " ". $pkg->{title};
540 $title = "\"$title\"";
542 $pkg->{menu} and print "menu";
543 print "config PACKAGE_".$pkg->{name}."\n";
544 $pkg->{hidden} and $title = "";
545 print "\t\t".($pkg->{tristate} ? 'tristate' : 'bool')." $title\n";
546 print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
547 unless ($pkg->{hidden}) {
548 if ($pkg->{name} =~ /^kmod-/) {
549 $pkg->{default} ||= "m if ALL_KMODS";
551 $pkg->{default} ||= "m if ALL";
554 if ($pkg->{default}) {
555 foreach my $default (split /\s*,\s*/, $pkg->{default}) {
556 print "\t\tdefault $default\n";
559 print mconf_depends($pkg->{name}, $pkg->{depends}, 0);
560 print mconf_depends($pkg->{name}, $pkg->{mdepends}, 0);
561 print mconf_conflicts($pkg->{name}, $pkg->{conflicts});
563 print $pkg->{description};
566 $pkg->{config} and print $pkg->{config}."\n";
568 if ($menu ne 'undef') {
570 $menu_dep{$menu} and print "endif\n";
575 undef $category{$cat};
578 sub print_package_features() {
579 keys %features > 0 or return;
580 print "menu \"Package features\"\n";
581 foreach my $n (keys %features) {
582 my @features = sort { $b->{priority} <=> $a->{priority} or $a->{title} cmp $b->{title} } @{$features{$n}};
585 prompt "$features[0]->{target_title}"
586 default FEATURE_$features[0]->{name}
589 foreach my $feature (@features) {
591 config FEATURE_$feature->{name}
592 bool "$feature->{title}"
594 $feature->{description} =~ /\w/ and do {
595 print "\t\thelp\n".$feature->{description}."\n";
603 sub print_package_overrides() {
604 keys %overrides > 0 or return;
605 print "\tconfig OVERRIDE_PKGS\n";
606 print "\t\tstring\n";
607 print "\t\tdefault \"".join(" ", keys %overrides)."\"\n\n";
610 sub gen_package_config() {
611 parse_package_metadata($ARGV[0]) or exit 1;
612 print "menuconfig IMAGEOPT\n\tbool \"Image configuration\"\n\tdefault n\n";
613 foreach my $preconfig (keys %preconfig) {
614 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
615 my $conf = $preconfig{$preconfig}->{$cfg}->{id};
618 config UCI_PRECONFIG_$conf
619 string "$preconfig{$preconfig}->{$cfg}->{label}" if IMAGEOPT
620 depends on PACKAGE_$preconfig
621 default "$preconfig{$preconfig}->{$cfg}->{default}"
626 print "source \"package/*/image-config.in\"\n";
627 if (scalar glob "package/feeds/*/*/image-config.in") {
628 print "source \"package/feeds/*/*/image-config.in\"\n";
630 print_package_features();
631 print_package_config_category 'Base system';
632 foreach my $cat (sort {uc($a) cmp uc($b)} keys %category) {
633 print_package_config_category $cat;
635 print_package_overrides();
638 sub get_conditional_dep($$) {
639 my $condition = shift;
642 if ($condition =~ /^!(.+)/) {
643 return "\$(if \$(CONFIG_$1),,$depstr)";
645 return "\$(if \$(CONFIG_$condition),$depstr)";
652 sub gen_package_mk() {
658 parse_package_metadata($ARGV[0]) or exit 1;
659 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
661 my $pkg = $package{$name};
664 next if defined $pkg->{vdepends};
666 $config = "\$(CONFIG_PACKAGE_$name)";
668 $pkg->{buildonly} and $config = "";
669 print "package-$config += $pkg->{subdir}$pkg->{src}\n";
670 if ($pkg->{variant}) {
671 if (!defined($done{$pkg->{src}}) or $pkg->{variant_default}) {
672 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/default-variant := $pkg->{variant}\n";
674 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/variants += \$(if $config,$pkg->{variant})\n"
676 $pkg->{prereq} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
679 next if $done{$pkg->{src}};
680 $done{$pkg->{src}} = 1;
682 if (@{$pkg->{buildtypes}} > 0) {
683 print "buildtypes-$pkg->{subdir}$pkg->{src} = ".join(' ', @{$pkg->{buildtypes}})."\n";
686 foreach my $spkg (@{$srcpackage{$pkg->{src}}}) {
687 foreach my $dep (@{$spkg->{depends}}, @{$spkg->{builddepends}}) {
694 foreach my $type (@{$pkg->{buildtypes}}) {
698 next unless $pkg->{"builddepends/$type"};
699 foreach my $dep (@{$pkg->{"builddepends/$type"}}) {
703 if ($dep =~ /^(.+):(.+)/) {
707 if ($dep =~ /^(.+)(\/.+)/) {
713 my $pkg_dep = $package{$dep};
714 if (defined($pkg_dep) && defined($pkg_dep->{src})) {
715 $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
716 } elsif (defined($srcpackage{$dep})) {
717 $idx = $subdir{$dep}.$dep;
721 my $depstr = "\$(curdir)/$idx$suffix/compile";
722 my $depline = get_conditional_dep($condition, $depstr);
724 $deplines{$depline}++;
727 my $depline = join(" ", sort keys %deplines);
729 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/$type/compile += $depline\n";
735 foreach my $deps (@srcdeps) {
741 if ($deps =~ /^(.+):(.+)/) {
745 if ($deps =~ /^(.+)(\/.+)/) {
750 my $pkg_dep = $package{$deps};
753 if ($pkg_dep->{vdepends}) {
754 @deps = @{$pkg_dep->{vdepends}};
759 foreach my $dep (@deps) {
760 $pkg_dep = $package{$deps};
761 if (defined $pkg_dep->{src}) {
762 ($pkg->{src} ne $pkg_dep->{src}.$suffix) and $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
763 } elsif (defined($srcpackage{$dep})) {
764 $idx = $subdir{$dep}.$dep;
766 undef $idx if $idx eq 'base-files';
771 next if $pkg->{src} eq $pkg_dep->{src}.$suffix;
772 next if $dep{$condition.":".$pkg->{src}."->".$idx};
773 next if $dep{$pkg->{src}."->($dep)".$idx} and $pkg_dep->{vdepends};
776 if ($pkg_dep->{vdepends}) {
777 $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
778 $dep{$pkg->{src}."->($dep)".$idx} = 1;
780 $depstr = "\$(curdir)/$idx/compile";
781 $dep{$pkg->{src}."->".$idx} = 1;
783 $depline = get_conditional_dep($condition, $depstr);
785 $deplines{$depline}++;
790 my $depline = join(" ", sort keys %deplines);
792 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/compile += $depline\n";
799 foreach my $preconfig (keys %preconfig) {
801 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
802 my $conf = $preconfig{$preconfig}->{$cfg}->{id};
804 $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
809 ifndef DUMP_TARGET_DB
810 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
815 ifneq (\$(IMAGEOPT)\$(CONFIG_IMAGEOPT),)
816 package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
824 sub gen_package_source() {
825 parse_package_metadata($ARGV[0]) or exit 1;
826 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
827 my $pkg = $package{$name};
828 if ($pkg->{name} && $pkg->{source}) {
829 print "$pkg->{name}: ";
830 print "$pkg->{source}\n";
835 sub gen_package_feeds() {
836 parse_package_metadata($ARGV[0]) or exit 1;
837 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
838 my $pkg = $package{$name};
839 if ($pkg->{name} && $pkg->{feed}) {
840 print "Package/$name/feed = $pkg->{feed}\n";
845 sub gen_package_license($) {
847 parse_package_metadata($ARGV[0]) or exit 1;
848 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
849 my $pkg = $package{$name};
851 if ($pkg->{license}) {
852 print "$pkg->{name}: ";
853 print "$pkg->{license}\n";
854 if ($pkg->{licensefiles} && $level == 0) {
855 print "\tFiles: $pkg->{licensefiles}\n";
859 print "$pkg->{name}: Missing license! ";
860 print "Please fix $pkg->{makefile}\n";
867 sub gen_version_filtered_list() {
868 foreach my $item (version_filter_list(@ARGV)) {
873 sub parse_command() {
874 my $cmd = shift @ARGV;
876 /^target_config$/ and return gen_target_config();
877 /^package_mk$/ and return gen_package_mk();
878 /^package_config$/ and return gen_package_config();
879 /^kconfig/ and return gen_kconfig_overrides();
880 /^package_source$/ and return gen_package_source();
881 /^package_feeds$/ and return gen_package_feeds();
882 /^package_license$/ and return gen_package_license(0);
883 /^package_licensefull$/ and return gen_package_license(1);
884 /^version_filter$/ and return gen_version_filtered_list();
888 $0 target_config [file] Target metadata in Kconfig format
889 $0 package_mk [file] Package metadata in makefile format
890 $0 package_config [file] Package metadata in Kconfig format
891 $0 kconfig [file] [config] [patchver] Kernel config overrides
892 $0 package_source [file] Package source file information
893 $0 package_feeds [file] Package feed information in makefile format
894 $0 package_license [file] Package license information
895 $0 package_licensefull [file] Package license information (full list)
896 $0 version_filter [patchver] [list...] Filter list of version tagged strings