3 use lib "$FindBin::Bin";
11 $conf =~ tr#/\.\-/#___#;
15 sub parse_target_metadata() {
16 my $file = shift @ARGV;
17 my ($target, @target, $profile);
20 open FILE, "<$file" or do {
21 warn "Can't open file '$file': $!\n";
26 /^Target:\s*(.+)\s*$/ and do {
31 boardconf => confstr($name),
32 conf => confstr($name),
38 push @target, $target;
39 $target{$name} = $target;
40 if ($name =~ /([^\/]+)\/([^\/]+)/) {
41 push @{$target{$1}->{subtargets}}, $2;
42 $target->{board} = $1;
43 $target->{boardconf} = confstr($1);
44 $target->{subtarget} = 1;
45 $target->{parent} = $target{$1};
48 /^Target-Name:\s*(.+)\s*$/ and $target->{name} = $1;
49 /^Target-Path:\s*(.+)\s*$/ and $target->{path} = $1;
50 /^Target-Arch:\s*(.+)\s*$/ and $target->{arch} = $1;
51 /^Target-Arch-Packages:\s*(.+)\s*$/ and $target->{arch_packages} = $1;
52 /^Target-Features:\s*(.+)\s*$/ and $target->{features} = [ split(/\s+/, $1) ];
53 /^Target-Depends:\s*(.+)\s*$/ and $target->{depends} = [ split(/\s+/, $1) ];
54 /^Target-Description:/ and $target->{desc} = get_multiline(*FILE);
55 /^Target-Optimization:\s*(.+)\s*$/ and $target->{cflags} = $1;
56 /^CPU-Type:\s*(.+)\s*$/ and $target->{cputype} = $1;
57 /^Linux-Version:\s*(.+)\s*$/ and $target->{version} = $1;
58 /^Linux-Release:\s*(.+)\s*$/ and $target->{release} = $1;
59 /^Linux-Kernel-Arch:\s*(.+)\s*$/ and $target->{karch} = $1;
60 /^Default-Subtarget:\s*(.+)\s*$/ and $target->{def_subtarget} = $1;
61 /^Default-Packages:\s*(.+)\s*$/ and $target->{packages} = [ split(/\s+/, $1) ];
62 /^Target-Profile:\s*(.+)\s*$/ and do {
68 push @{$target->{profiles}}, $profile;
70 /^Target-Profile-Name:\s*(.+)\s*$/ and $profile->{name} = $1;
71 /^Target-Profile-Packages:\s*(.*)\s*$/ and $profile->{packages} = [ split(/\s+/, $1) ];
72 /^Target-Profile-Description:\s*(.*)\s*/ and $profile->{desc} = get_multiline(*FILE);
73 /^Target-Profile-Config:/ and $profile->{config} = get_multiline(*FILE, "\t");
74 /^Target-Profile-Kconfig:/ and $profile->{kconfig} = 1;
77 foreach my $target (@target) {
78 if (@{$target->{subtargets}} > 0) {
79 $target->{profiles} = [];
82 @{$target->{profiles}} > 0 or $target->{profiles} = [
93 sub gen_kconfig_overrides() {
97 my $pkginfo = shift @ARGV;
98 my $cfgfile = shift @ARGV;
100 # parameter 2: build system config
101 open FILE, "<$cfgfile" or return;
103 /^(CONFIG_.+?)=(.+)$/ and $config{$1} = 1;
107 # parameter 1: package metadata
108 open FILE, "<$pkginfo" or return;
110 /^Package:\s*(.+?)\s*$/ and $package = $1;
111 /^Kernel-Config:\s*(.+?)\s*$/ and do {
112 my @config = split /\s+/, $1;
113 foreach my $config (@config) {
116 if ($config =~ /^(.+?)=(.+)$/) {
121 if ($config{"CONFIG_PACKAGE_$package"} and ($config ne 'n')) {
122 next if $kconfig{$config} eq 'y';
123 $kconfig{$config} = $val;
124 } elsif (!$override) {
125 $kconfig{$config} or $kconfig{$config} = 'n';
132 foreach my $kconfig (sort keys %kconfig) {
133 if ($kconfig{$kconfig} eq 'n') {
134 print "# $kconfig is not set\n";
136 print "$kconfig=$kconfig{$kconfig}\n";
141 sub merge_package_lists($$) {
147 foreach my $pkg (@$list1, @$list2) {
150 foreach my $pkg (keys %pkgs) {
151 push @l, $pkg unless ($pkg =~ /^-/ or $pkgs{"-$pkg"});
156 sub target_config_features(@) {
159 while ($_ = shift @_) {
160 /arm_v(\w+)/ and $ret .= "\tselect arm_v$1\n";
161 /broken/ and $ret .= "\tdepends on BROKEN\n";
162 /audio/ and $ret .= "\tselect AUDIO_SUPPORT\n";
163 /display/ and $ret .= "\tselect DISPLAY_SUPPORT\n";
164 /dt/ and $ret .= "\tselect USES_DEVICETREE\n";
165 /gpio/ and $ret .= "\tselect GPIO_SUPPORT\n";
166 /pci/ and $ret .= "\tselect PCI_SUPPORT\n";
167 /pcie/ and $ret .= "\tselect PCIE_SUPPORT\n";
168 /usb/ and $ret .= "\tselect USB_SUPPORT\n";
169 /usbgadget/ and $ret .= "\tselect USB_GADGET_SUPPORT\n";
170 /pcmcia/ and $ret .= "\tselect PCMCIA_SUPPORT\n";
171 /rtc/ and $ret .= "\tselect RTC_SUPPORT\n";
172 /squashfs/ and $ret .= "\tselect USES_SQUASHFS\n";
173 /jffs2$/ and $ret .= "\tselect USES_JFFS2\n";
174 /jffs2_nand/ and $ret .= "\tselect USES_JFFS2_NAND\n";
175 /ext4/ and $ret .= "\tselect USES_EXT4\n";
176 /targz/ and $ret .= "\tselect USES_TARGZ\n";
177 /cpiogz/ and $ret .= "\tselect USES_CPIOGZ\n";
178 /ubifs/ and $ret .= "\tselect USES_UBIFS\n";
179 /fpu/ and $ret .= "\tselect HAS_FPU\n";
180 /spe_fpu/ and $ret .= "\tselect HAS_SPE_FPU\n";
181 /ramdisk/ and $ret .= "\tselect USES_INITRAMFS\n";
182 /powerpc64/ and $ret .= "\tselect powerpc64\n";
183 /nommu/ and $ret .= "\tselect NOMMU\n";
184 /mips16/ and $ret .= "\tselect HAS_MIPS16\n";
185 /rfkill/ and $ret .= "\tselect RFKILL_SUPPORT\n";
186 /low_mem/ and $ret .= "\tselect LOW_MEMORY_FOOTPRINT\n";
187 /nand/ and $ret .= "\tselect NAND_SUPPORT\n";
194 my $parent = $target->{parent};
196 return $target->{parent}->{name}." - ".$target->{name};
198 return $target->{name};
205 if (substr($v,0,2) eq "2_") {
206 $v =~ /(\d+_\d+_\d+)(_\d+)?/ and $v = $1;
208 $v =~ /(\d+_\d+)(_\d+)?/ and $v = $1;
213 sub print_target($) {
215 my $features = target_config_features(@{$target->{features}});
216 my $help = $target->{desc};
221 if ($help =~ /\w+/) {
222 $help =~ s/^\s*/\t /mg;
223 $help = "\thelp\n$help";
228 my $v = kver($target->{version});
229 if (@{$target->{subtargets}} == 0) {
231 config TARGET_$target->{conf}
232 bool "$target->{name}"
238 config TARGET_$target->{conf}
239 bool "$target->{name}"
242 if ($target->{subtarget}) {
243 $confstr .= "\tdepends on TARGET_$target->{boardconf}\n";
245 if (@{$target->{subtargets}} > 0) {
246 $confstr .= "\tselect HAS_SUBTARGETS\n";
247 grep { /broken/ } @{$target->{features}} and $confstr .= "\tdepends on BROKEN\n";
249 $confstr .= $features;
252 if ($target->{arch} =~ /\w/) {
253 $confstr .= "\tselect $target->{arch}\n";
255 foreach my $dep (@{$target->{depends}}) {
256 my $mode = "depends on";
260 $dep =~ /^([@\+\-]+)(.+)$/;
264 next if $name =~ /:/;
265 $flags =~ /-/ and $mode = "deselect";
266 $flags =~ /\+/ and $mode = "select";
267 $flags =~ /@/ and $confstr .= "\t$mode $name\n";
269 $confstr .= "$help\n\n";
273 sub gen_target_config() {
274 my @target = parse_target_metadata();
277 my @target_sort = sort {
278 target_name($a) cmp target_name($b);
284 prompt "Target System"
285 default TARGET_ar71xx
290 foreach my $target (@target_sort) {
291 next if $target->{subtarget};
292 print_target($target);
299 prompt "Subtarget" if HAS_SUBTARGETS
301 foreach my $target (@target) {
302 next unless $target->{def_subtarget};
304 default TARGET_$target->{conf}_$target->{def_subtarget} if TARGET_$target->{conf}
310 foreach my $target (@target) {
311 next unless $target->{subtarget};
312 print_target($target);
319 prompt "Target Profile"
323 foreach my $target (@target) {
324 my $profiles = $target->{profiles};
326 foreach my $profile (@$profiles) {
328 config TARGET_$target->{conf}_$profile->{id}
329 bool "$profile->{name}"
330 depends on TARGET_$target->{conf}
333 $profile->{kconfig} and print "\tselect PROFILE_KCONFIG\n";
334 my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
335 foreach my $pkg (@pkglist) {
336 print "\tselect DEFAULT_$pkg\n";
339 my $help = $profile->{desc};
340 if ($help =~ /\w+/) {
341 $help =~ s/^\s*/\t /mg;
342 $help = "\thelp\n$help";
353 config HAS_SUBTARGETS
360 foreach my $target (@target) {
361 $target->{subtarget} or print "\t\tdefault \"".$target->{board}."\" if TARGET_".$target->{conf}."\n";
364 config TARGET_ARCH_PACKAGES
368 foreach my $target (@target) {
369 next if @{$target->{subtargets}} > 0;
370 print "\t\tdefault \"".($target->{arch_packages} || $target->{board})."\" if TARGET_".$target->{conf}."\n";
374 config DEFAULT_TARGET_OPTIMIZATION
377 foreach my $target (@target) {
378 next if @{$target->{subtargets}} > 0;
379 print "\tdefault \"".$target->{cflags}."\" if TARGET_".$target->{conf}."\n";
381 print "\tdefault \"-Os -pipe -funit-at-a-time\"\n";
387 foreach my $target (@target) {
388 next if @{$target->{subtargets}} > 0;
389 print "\tdefault \"".$target->{cputype}."\" if TARGET_".$target->{conf}."\n";
391 print "\tdefault \"\"\n";
394 foreach my $target (@target) {
395 my $v = kver($target->{version});
405 foreach my $def (sort keys %defaults) {
406 print "\tconfig DEFAULT_".$def."\n";
407 print "\t\tbool\n\n";
412 sub __find_package_dep($$) {
415 my $deps = ($pkg->{vdepends} or $pkg->{depends});
417 return 0 unless defined $deps;
418 foreach my $dep (@{$deps}) {
419 next if $dep_check{$dep};
420 $dep_check{$dep} = 1;
421 return 1 if $dep eq $name;
422 return 1 if ($package{$dep} and (__find_package_dep($package{$dep},$name) == 1));
427 # wrapper to avoid infinite recursion
428 sub find_package_dep($$) {
433 return __find_package_dep($pkg, $name);
436 sub package_depends($$) {
441 return 0 if ($a->{submenu} ne $b->{submenu});
442 if (find_package_dep($a, $b->{name}) == 1) {
444 } elsif (find_package_dep($b, $a->{name}) == 1) {
455 my $only_dep = shift;
459 my $parent_condition = shift;
465 my @depends = @$depends;
466 foreach my $depend (@depends) {
467 my $m = "depends on";
469 $depend =~ s/^([@\+]+)// and $flags = $1;
471 my $condition = $parent_condition;
473 next if $condition eq $depend;
474 next if $seen->{"$parent_condition:$depend"};
475 next if $seen->{":$depend"};
476 $seen->{"$parent_condition:$depend"} = 1;
477 if ($depend =~ /^(.+):(.+)$/) {
478 if ($1 ne "PACKAGE_$pkgname") {
480 $condition = "$condition && $1";
487 next if $package{$depend} and $package{$depend}->{buildonly};
488 if ($vdep = $package{$depend}->{vdepends}) {
489 $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
491 $flags =~ /\+/ and do {
492 # Menuconfig will not treat 'select FOO' as a real dependency
493 # thus if FOO depends on other config options, these dependencies
494 # will not be checked. To fix this, we simply emit all of FOO's
495 # depends here as well.
496 $package{$depend} and push @t_depends, [ $package{$depend}->{depends}, $condition ];
501 $flags =~ /@/ or $depend = "PACKAGE_$depend";
503 if ($m =~ /select/) {
504 next if $depend eq $condition;
505 $depend = "$depend if $condition";
507 $depend = "!($condition) || $depend" unless $dep->{$condition} eq 'select';
511 $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
514 foreach my $tdep (@t_depends) {
515 mconf_depends($pkgname, $tdep->[0], 1, $dep, $seen, $tdep->[1]);
518 foreach my $depend (keys %$dep) {
519 my $m = $dep->{$depend};
520 $res .= "\t\t$m $depend\n";
525 sub mconf_conflicts {
530 foreach my $depend (@$depends) {
531 next unless $package{$depend};
532 $res .= "\t\tdepends on m || (PACKAGE_$depend != y)\n";
537 sub print_package_config_category($) {
542 return unless $category{$cat};
544 print "menu \"$cat\"\n\n";
545 my %spkg = %{$category{$cat}};
547 foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
548 foreach my $pkg (@{$spkg{$spkg}}) {
549 next if $pkg->{buildonly};
550 my $menu = $pkg->{submenu};
552 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
556 $menus{$menu} or $menus{$menu} = [];
557 push @{$menus{$menu}}, $pkg;
561 ($a eq 'undef' ? 1 : 0) or
562 ($b eq 'undef' ? -1 : 0) or
566 foreach my $menu (@menus) {
568 package_depends($a, $b) or
569 ($a->{name} cmp $b->{name})
571 if ($menu ne 'undef') {
572 $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
573 print "menu \"$menu\"\n";
575 foreach my $pkg (@pkgs) {
576 my $title = $pkg->{name};
577 my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
579 $title .= ("." x $c). " ". $pkg->{title};
581 $title = "\"$title\"";
583 $pkg->{menu} and print "menu";
584 print "config PACKAGE_".$pkg->{name}."\n";
585 $pkg->{hidden} and $title = "";
586 print "\t\t".($pkg->{tristate} ? 'tristate' : 'bool')." $title\n";
587 print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
588 unless ($pkg->{hidden}) {
589 $pkg->{default} ||= "m if ALL";
591 if ($pkg->{default}) {
592 foreach my $default (split /\s*,\s*/, $pkg->{default}) {
593 print "\t\tdefault $default\n";
596 print mconf_depends($pkg->{name}, $pkg->{depends}, 0);
597 print mconf_depends($pkg->{name}, $pkg->{mdepends}, 0);
598 print mconf_conflicts($pkg->{name}, $pkg->{conflicts});
600 print $pkg->{description};
603 $pkg->{config} and print $pkg->{config}."\n";
605 if ($menu ne 'undef') {
607 $menu_dep{$menu} and print "endif\n";
612 undef $category{$cat};
615 sub print_package_features() {
616 keys %features > 0 or return;
617 print "menu \"Package features\"\n";
618 foreach my $n (keys %features) {
619 my @features = sort { $b->{priority} <=> $a->{priority} or $a->{title} cmp $b->{title} } @{$features{$n}};
622 prompt "$features[0]->{target_title}"
623 default FEATURE_$features[0]->{name}
626 foreach my $feature (@features) {
628 config FEATURE_$feature->{name}
629 bool "$feature->{title}"
631 $feature->{description} =~ /\w/ and do {
632 print "\t\thelp\n".$feature->{description}."\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;
667 sub get_conditional_dep($$) {
668 my $condition = shift;
671 if ($condition =~ /^!(.+)/) {
672 return "\$(if \$(CONFIG_$1),,$depstr)";
674 return "\$(if \$(CONFIG_$condition),$depstr)";
681 sub gen_package_mk() {
687 parse_package_metadata($ARGV[0]) or exit 1;
688 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
690 my $pkg = $package{$name};
693 next if defined $pkg->{vdepends};
695 $config = "\$(CONFIG_PACKAGE_$name)";
697 $pkg->{buildonly} and $config = "";
698 print "package-$config += $pkg->{subdir}$pkg->{src}\n";
699 if ($pkg->{variant}) {
700 if (!defined($done{$pkg->{src}}) or $pkg->{variant_default}) {
701 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/default-variant := $pkg->{variant}\n";
703 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/variants += \$(if $config,$pkg->{variant})\n"
705 $pkg->{prereq} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
708 next if $done{$pkg->{src}};
709 $done{$pkg->{src}} = 1;
711 if (@{$pkg->{buildtypes}} > 0) {
712 print "buildtypes-$pkg->{subdir}$pkg->{src} = ".join(' ', @{$pkg->{buildtypes}})."\n";
715 foreach my $spkg (@{$srcpackage{$pkg->{src}}}) {
716 foreach my $dep (@{$spkg->{depends}}, @{$spkg->{builddepends}}) {
723 foreach my $type (@{$pkg->{buildtypes}}) {
727 next unless $pkg->{"builddepends/$type"};
728 foreach my $dep (@{$pkg->{"builddepends/$type"}}) {
732 if ($dep =~ /^(.+):(.+)/) {
736 if ($dep =~ /^(.+)(\/.+)/) {
742 my $pkg_dep = $package{$dep};
743 if (defined($pkg_dep) && defined($pkg_dep->{src})) {
744 $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
745 } elsif (defined($srcpackage{$dep})) {
746 $idx = $subdir{$dep}.$dep;
750 my $depstr = "\$(curdir)/$idx$suffix/compile";
751 my $depline = get_conditional_dep($condition, $depstr);
753 $deplines{$depline}++;
756 my $depline = join(" ", sort keys %deplines);
758 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/$type/compile += $depline\n";
764 foreach my $deps (@srcdeps) {
770 if ($deps =~ /^(.+):(.+)/) {
774 if ($deps =~ /^(.+)(\/.+)/) {
779 my $pkg_dep = $package{$deps};
782 if ($pkg_dep->{vdepends}) {
783 @deps = @{$pkg_dep->{vdepends}};
788 foreach my $dep (@deps) {
789 $pkg_dep = $package{$deps};
790 if (defined $pkg_dep->{src}) {
791 ($pkg->{src} ne $pkg_dep->{src}.$suffix) and $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
792 } elsif (defined($srcpackage{$dep})) {
793 $idx = $subdir{$dep}.$dep;
795 undef $idx if $idx eq 'base-files';
800 next if $pkg->{src} eq $pkg_dep->{src}.$suffix;
801 next if $dep{$condition.":".$pkg->{src}."->".$idx};
802 next if $dep{$pkg->{src}."->($dep)".$idx} and $pkg_dep->{vdepends};
805 if ($pkg_dep->{vdepends}) {
806 $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
807 $dep{$pkg->{src}."->($dep)".$idx} = 1;
809 $depstr = "\$(curdir)/$idx/compile";
810 $dep{$pkg->{src}."->".$idx} = 1;
812 $depline = get_conditional_dep($condition, $depstr);
814 $deplines{$depline}++;
819 my $depline = join(" ", sort keys %deplines);
821 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/compile += $depline\n";
828 foreach my $preconfig (keys %preconfig) {
830 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
831 my $conf = $preconfig{$preconfig}->{$cfg}->{id};
833 $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
838 ifndef DUMP_TARGET_DB
839 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
844 ifneq (\$(IMAGEOPT)\$(CONFIG_IMAGEOPT),)
845 package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
853 sub gen_package_source() {
854 parse_package_metadata($ARGV[0]) or exit 1;
855 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
856 my $pkg = $package{$name};
857 if ($pkg->{name} && $pkg->{source}) {
858 print "$pkg->{name}: ";
859 print "$pkg->{source}\n";
864 sub gen_package_feeds() {
865 parse_package_metadata($ARGV[0]) or exit 1;
866 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
867 my $pkg = $package{$name};
868 if ($pkg->{name} && $pkg->{feed}) {
869 print "Package/$name/feed = $pkg->{feed}\n";
874 sub gen_package_license($) {
876 parse_package_metadata($ARGV[0]) or exit 1;
877 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
878 my $pkg = $package{$name};
880 if ($pkg->{license}) {
881 print "$pkg->{name}: ";
882 print "$pkg->{license}\n";
883 if ($pkg->{licensefiles} && $level == 0) {
884 print "\tFiles: $pkg->{licensefiles}\n";
888 print "$pkg->{name}: Missing license! ";
889 print "Please fix $pkg->{makefile}\n";
896 sub parse_command() {
897 my $cmd = shift @ARGV;
899 /^target_config$/ and return gen_target_config();
900 /^package_mk$/ and return gen_package_mk();
901 /^package_config$/ and return gen_package_config();
902 /^kconfig/ and return gen_kconfig_overrides();
903 /^package_source$/ and return gen_package_source();
904 /^package_feeds$/ and return gen_package_feeds();
905 /^package_license$/ and return gen_package_license(0);
906 /^package_licensefull$/ and return gen_package_license(1);
910 $0 target_config [file] Target metadata in Kconfig format
911 $0 package_mk [file] Package metadata in makefile format
912 $0 package_config [file] Package metadata in Kconfig format
913 $0 kconfig [file] [config] Kernel config overrides
914 $0 package_source [file] Package source file information
915 $0 package_feeds [file] Package feed information in makefile format
916 $0 package_license [file] Package license information
917 $0 package_licensefull [file] Package license information (full list)