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;
210 if ($target->{arch} =~ /\w/) {
211 $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 $pkg->{default} ||= "m if ALL";
550 if ($pkg->{default}) {
551 foreach my $default (split /\s*,\s*/, $pkg->{default}) {
552 print "\t\tdefault $default\n";
555 print mconf_depends($pkg->{name}, $pkg->{depends}, 0);
556 print mconf_depends($pkg->{name}, $pkg->{mdepends}, 0);
557 print mconf_conflicts($pkg->{name}, $pkg->{conflicts});
559 print $pkg->{description};
562 $pkg->{config} and print $pkg->{config}."\n";
564 if ($menu ne 'undef') {
566 $menu_dep{$menu} and print "endif\n";
571 undef $category{$cat};
574 sub print_package_features() {
575 keys %features > 0 or return;
576 print "menu \"Package features\"\n";
577 foreach my $n (keys %features) {
578 my @features = sort { $b->{priority} <=> $a->{priority} or $a->{title} cmp $b->{title} } @{$features{$n}};
581 prompt "$features[0]->{target_title}"
582 default FEATURE_$features[0]->{name}
585 foreach my $feature (@features) {
587 config FEATURE_$feature->{name}
588 bool "$feature->{title}"
590 $feature->{description} =~ /\w/ and do {
591 print "\t\thelp\n".$feature->{description}."\n";
599 sub print_package_overrides() {
600 keys %overrides > 0 or return;
601 print "\tconfig OVERRIDE_PKGS\n";
602 print "\t\tstring\n";
603 print "\t\tdefault \"".join(" ", keys %overrides)."\"\n\n";
606 sub gen_package_config() {
607 parse_package_metadata($ARGV[0]) or exit 1;
608 print "menuconfig IMAGEOPT\n\tbool \"Image configuration\"\n\tdefault n\n";
609 foreach my $preconfig (keys %preconfig) {
610 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
611 my $conf = $preconfig{$preconfig}->{$cfg}->{id};
614 config UCI_PRECONFIG_$conf
615 string "$preconfig{$preconfig}->{$cfg}->{label}" if IMAGEOPT
616 depends on PACKAGE_$preconfig
617 default "$preconfig{$preconfig}->{$cfg}->{default}"
622 print "source \"package/*/image-config.in\"\n";
623 if (scalar glob "package/feeds/*/*/image-config.in") {
624 print "source \"package/feeds/*/*/image-config.in\"\n";
626 print_package_features();
627 print_package_config_category 'Base system';
628 foreach my $cat (sort {uc($a) cmp uc($b)} keys %category) {
629 print_package_config_category $cat;
631 print_package_overrides();
634 sub get_conditional_dep($$) {
635 my $condition = shift;
638 if ($condition =~ /^!(.+)/) {
639 return "\$(if \$(CONFIG_$1),,$depstr)";
641 return "\$(if \$(CONFIG_$condition),$depstr)";
648 sub gen_package_mk() {
654 parse_package_metadata($ARGV[0]) or exit 1;
655 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
657 my $pkg = $package{$name};
660 next if defined $pkg->{vdepends};
662 $config = "\$(CONFIG_PACKAGE_$name)";
664 $pkg->{buildonly} and $config = "";
665 print "package-$config += $pkg->{subdir}$pkg->{src}\n";
666 if ($pkg->{variant}) {
667 if (!defined($done{$pkg->{src}}) or $pkg->{variant_default}) {
668 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/default-variant := $pkg->{variant}\n";
670 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/variants += \$(if $config,$pkg->{variant})\n"
672 $pkg->{prereq} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
675 next if $done{$pkg->{src}};
676 $done{$pkg->{src}} = 1;
678 if (@{$pkg->{buildtypes}} > 0) {
679 print "buildtypes-$pkg->{subdir}$pkg->{src} = ".join(' ', @{$pkg->{buildtypes}})."\n";
682 foreach my $spkg (@{$srcpackage{$pkg->{src}}}) {
683 foreach my $dep (@{$spkg->{depends}}, @{$spkg->{builddepends}}) {
690 foreach my $type (@{$pkg->{buildtypes}}) {
694 next unless $pkg->{"builddepends/$type"};
695 foreach my $dep (@{$pkg->{"builddepends/$type"}}) {
699 if ($dep =~ /^(.+):(.+)/) {
703 if ($dep =~ /^(.+)(\/.+)/) {
709 my $pkg_dep = $package{$dep};
710 if (defined($pkg_dep) && defined($pkg_dep->{src})) {
711 $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
712 } elsif (defined($srcpackage{$dep})) {
713 $idx = $subdir{$dep}.$dep;
717 my $depstr = "\$(curdir)/$idx$suffix/compile";
718 my $depline = get_conditional_dep($condition, $depstr);
720 $deplines{$depline}++;
723 my $depline = join(" ", sort keys %deplines);
725 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/$type/compile += $depline\n";
731 foreach my $deps (@srcdeps) {
737 if ($deps =~ /^(.+):(.+)/) {
741 if ($deps =~ /^(.+)(\/.+)/) {
746 my $pkg_dep = $package{$deps};
749 if ($pkg_dep->{vdepends}) {
750 @deps = @{$pkg_dep->{vdepends}};
755 foreach my $dep (@deps) {
756 $pkg_dep = $package{$deps};
757 if (defined $pkg_dep->{src}) {
758 ($pkg->{src} ne $pkg_dep->{src}.$suffix) and $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
759 } elsif (defined($srcpackage{$dep})) {
760 $idx = $subdir{$dep}.$dep;
762 undef $idx if $idx eq 'base-files';
767 next if $pkg->{src} eq $pkg_dep->{src}.$suffix;
768 next if $dep{$condition.":".$pkg->{src}."->".$idx};
769 next if $dep{$pkg->{src}."->($dep)".$idx} and $pkg_dep->{vdepends};
772 if ($pkg_dep->{vdepends}) {
773 $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
774 $dep{$pkg->{src}."->($dep)".$idx} = 1;
776 $depstr = "\$(curdir)/$idx/compile";
777 $dep{$pkg->{src}."->".$idx} = 1;
779 $depline = get_conditional_dep($condition, $depstr);
781 $deplines{$depline}++;
786 my $depline = join(" ", sort keys %deplines);
788 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/compile += $depline\n";
795 foreach my $preconfig (keys %preconfig) {
797 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
798 my $conf = $preconfig{$preconfig}->{$cfg}->{id};
800 $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
805 ifndef DUMP_TARGET_DB
806 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
811 ifneq (\$(IMAGEOPT)\$(CONFIG_IMAGEOPT),)
812 package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
820 sub gen_package_source() {
821 parse_package_metadata($ARGV[0]) or exit 1;
822 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
823 my $pkg = $package{$name};
824 if ($pkg->{name} && $pkg->{source}) {
825 print "$pkg->{name}: ";
826 print "$pkg->{source}\n";
831 sub gen_package_feeds() {
832 parse_package_metadata($ARGV[0]) or exit 1;
833 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
834 my $pkg = $package{$name};
835 if ($pkg->{name} && $pkg->{feed}) {
836 print "Package/$name/feed = $pkg->{feed}\n";
841 sub gen_package_license($) {
843 parse_package_metadata($ARGV[0]) or exit 1;
844 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
845 my $pkg = $package{$name};
847 if ($pkg->{license}) {
848 print "$pkg->{name}: ";
849 print "$pkg->{license}\n";
850 if ($pkg->{licensefiles} && $level == 0) {
851 print "\tFiles: $pkg->{licensefiles}\n";
855 print "$pkg->{name}: Missing license! ";
856 print "Please fix $pkg->{makefile}\n";
863 sub gen_version_filtered_list() {
864 foreach my $item (version_filter_list(@ARGV)) {
869 sub parse_command() {
870 my $cmd = shift @ARGV;
872 /^target_config$/ and return gen_target_config();
873 /^package_mk$/ and return gen_package_mk();
874 /^package_config$/ and return gen_package_config();
875 /^kconfig/ and return gen_kconfig_overrides();
876 /^package_source$/ and return gen_package_source();
877 /^package_feeds$/ and return gen_package_feeds();
878 /^package_license$/ and return gen_package_license(0);
879 /^package_licensefull$/ and return gen_package_license(1);
880 /^version_filter$/ and return gen_version_filtered_list();
884 $0 target_config [file] Target metadata in Kconfig format
885 $0 package_mk [file] Package metadata in makefile format
886 $0 package_config [file] Package metadata in Kconfig format
887 $0 kconfig [file] [config] [patchver] Kernel config overrides
888 $0 package_source [file] Package source file information
889 $0 package_feeds [file] Package feed information in makefile format
890 $0 package_license [file] Package license information
891 $0 package_licensefull [file] Package license information (full list)
892 $0 version_filter [patchver] [list...] Filter list of version tagged strings