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";
193 my $parent = $target->{parent};
195 return $target->{parent}->{name}." - ".$target->{name};
197 return $target->{name};
204 if (substr($v,0,2) eq "2_") {
205 $v =~ /(\d+_\d+_\d+)(_\d+)?/ and $v = $1;
207 $v =~ /(\d+_\d+)(_\d+)?/ and $v = $1;
212 sub print_target($) {
214 my $features = target_config_features(@{$target->{features}});
215 my $help = $target->{desc};
220 if ($help =~ /\w+/) {
221 $help =~ s/^\s*/\t /mg;
222 $help = "\thelp\n$help";
227 my $v = kver($target->{version});
228 if (@{$target->{subtargets}} == 0) {
230 config TARGET_$target->{conf}
231 bool "$target->{name}"
237 config TARGET_$target->{conf}
238 bool "$target->{name}"
241 if ($target->{subtarget}) {
242 $confstr .= "\tdepends on TARGET_$target->{boardconf}\n";
244 if (@{$target->{subtargets}} > 0) {
245 $confstr .= "\tselect HAS_SUBTARGETS\n";
246 grep { /broken/ } @{$target->{features}} and $confstr .= "\tdepends on BROKEN\n";
248 $confstr .= $features;
251 if ($target->{arch} =~ /\w/) {
252 $confstr .= "\tselect $target->{arch}\n";
254 foreach my $dep (@{$target->{depends}}) {
255 my $mode = "depends on";
259 $dep =~ /^([@\+\-]+)(.+)$/;
263 next if $name =~ /:/;
264 $flags =~ /-/ and $mode = "deselect";
265 $flags =~ /\+/ and $mode = "select";
266 $flags =~ /@/ and $confstr .= "\t$mode $name\n";
268 $confstr .= "$help\n\n";
272 sub gen_target_config() {
273 my @target = parse_target_metadata();
276 my @target_sort = sort {
277 target_name($a) cmp target_name($b);
283 prompt "Target System"
284 default TARGET_ar71xx
289 foreach my $target (@target_sort) {
290 next if $target->{subtarget};
291 print_target($target);
298 prompt "Subtarget" if HAS_SUBTARGETS
300 foreach my $target (@target) {
301 next unless $target->{def_subtarget};
303 default TARGET_$target->{conf}_$target->{def_subtarget} if TARGET_$target->{conf}
309 foreach my $target (@target) {
310 next unless $target->{subtarget};
311 print_target($target);
318 prompt "Target Profile"
322 foreach my $target (@target) {
323 my $profiles = $target->{profiles};
325 foreach my $profile (@$profiles) {
327 config TARGET_$target->{conf}_$profile->{id}
328 bool "$profile->{name}"
329 depends on TARGET_$target->{conf}
332 $profile->{kconfig} and print "\tselect PROFILE_KCONFIG\n";
333 my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
334 foreach my $pkg (@pkglist) {
335 print "\tselect DEFAULT_$pkg\n";
338 my $help = $profile->{desc};
339 if ($help =~ /\w+/) {
340 $help =~ s/^\s*/\t /mg;
341 $help = "\thelp\n$help";
352 config HAS_SUBTARGETS
359 foreach my $target (@target) {
360 $target->{subtarget} or print "\t\tdefault \"".$target->{board}."\" if TARGET_".$target->{conf}."\n";
363 config TARGET_ARCH_PACKAGES
367 foreach my $target (@target) {
368 next if @{$target->{subtargets}} > 0;
369 print "\t\tdefault \"".($target->{arch_packages} || $target->{board})."\" if TARGET_".$target->{conf}."\n";
373 config DEFAULT_TARGET_OPTIMIZATION
376 foreach my $target (@target) {
377 next if @{$target->{subtargets}} > 0;
378 print "\tdefault \"".$target->{cflags}."\" if TARGET_".$target->{conf}."\n";
380 print "\tdefault \"-Os -pipe -funit-at-a-time\"\n";
386 foreach my $target (@target) {
387 next if @{$target->{subtargets}} > 0;
388 print "\tdefault \"".$target->{cputype}."\" if TARGET_".$target->{conf}."\n";
390 print "\tdefault \"\"\n";
393 foreach my $target (@target) {
394 my $v = kver($target->{version});
404 foreach my $def (sort keys %defaults) {
405 print "\tconfig DEFAULT_".$def."\n";
406 print "\t\tbool\n\n";
411 sub __find_package_dep($$) {
414 my $deps = ($pkg->{vdepends} or $pkg->{depends});
416 return 0 unless defined $deps;
417 foreach my $dep (@{$deps}) {
418 next if $dep_check{$dep};
419 $dep_check{$dep} = 1;
420 return 1 if $dep eq $name;
421 return 1 if ($package{$dep} and (__find_package_dep($package{$dep},$name) == 1));
426 # wrapper to avoid infinite recursion
427 sub find_package_dep($$) {
432 return __find_package_dep($pkg, $name);
435 sub package_depends($$) {
440 return 0 if ($a->{submenu} ne $b->{submenu});
441 if (find_package_dep($a, $b->{name}) == 1) {
443 } elsif (find_package_dep($b, $a->{name}) == 1) {
454 my $only_dep = shift;
458 my $parent_condition = shift;
464 my @depends = @$depends;
465 foreach my $depend (@depends) {
466 my $m = "depends on";
468 $depend =~ s/^([@\+]+)// and $flags = $1;
470 my $condition = $parent_condition;
472 next if $condition eq $depend;
473 next if $seen->{"$parent_condition:$depend"};
474 next if $seen->{":$depend"};
475 $seen->{"$parent_condition:$depend"} = 1;
476 if ($depend =~ /^(.+):(.+)$/) {
477 if ($1 ne "PACKAGE_$pkgname") {
479 $condition = "$condition && $1";
486 next if $package{$depend} and $package{$depend}->{buildonly};
487 if ($vdep = $package{$depend}->{vdepends}) {
488 $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
490 $flags =~ /\+/ and do {
491 # Menuconfig will not treat 'select FOO' as a real dependency
492 # thus if FOO depends on other config options, these dependencies
493 # will not be checked. To fix this, we simply emit all of FOO's
494 # depends here as well.
495 $package{$depend} and push @t_depends, [ $package{$depend}->{depends}, $condition ];
500 $flags =~ /@/ or $depend = "PACKAGE_$depend";
502 if ($m =~ /select/) {
503 next if $depend eq $condition;
504 $depend = "$depend if $condition";
506 $depend = "!($condition) || $depend";
510 $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
513 foreach my $tdep (@t_depends) {
514 mconf_depends($pkgname, $tdep->[0], 1, $dep, $seen, $tdep->[1]);
517 foreach my $depend (keys %$dep) {
518 my $m = $dep->{$depend};
519 $res .= "\t\t$m $depend\n";
524 sub print_package_config_category($) {
529 return unless $category{$cat};
531 print "menu \"$cat\"\n\n";
532 my %spkg = %{$category{$cat}};
534 foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
535 foreach my $pkg (@{$spkg{$spkg}}) {
536 next if $pkg->{buildonly};
537 my $menu = $pkg->{submenu};
539 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
543 $menus{$menu} or $menus{$menu} = [];
544 push @{$menus{$menu}}, $pkg;
548 ($a eq 'undef' ? 1 : 0) or
549 ($b eq 'undef' ? -1 : 0) or
553 foreach my $menu (@menus) {
555 package_depends($a, $b) or
556 ($a->{name} cmp $b->{name})
558 if ($menu ne 'undef') {
559 $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
560 print "menu \"$menu\"\n";
562 foreach my $pkg (@pkgs) {
563 my $title = $pkg->{name};
564 my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
566 $title .= ("." x $c). " ". $pkg->{title};
568 $title = "\"$title\"";
570 $pkg->{menu} and print "menu";
571 print "config PACKAGE_".$pkg->{name}."\n";
572 $pkg->{hidden} and $title = "";
573 print "\t\t".($pkg->{tristate} ? 'tristate' : 'bool')." $title\n";
574 print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
575 unless ($pkg->{hidden}) {
576 $pkg->{default} ||= "m if ALL";
578 if ($pkg->{default}) {
579 foreach my $default (split /\s*,\s*/, $pkg->{default}) {
580 print "\t\tdefault $default\n";
583 print mconf_depends($pkg->{name}, $pkg->{depends}, 0);
584 print mconf_depends($pkg->{name}, $pkg->{mdepends}, 0);
586 print $pkg->{description};
589 $pkg->{config} and print $pkg->{config}."\n";
591 if ($menu ne 'undef') {
593 $menu_dep{$menu} and print "endif\n";
598 undef $category{$cat};
601 sub print_package_features() {
602 keys %features > 0 or return;
603 print "menu \"Package features\"\n";
604 foreach my $n (keys %features) {
605 my @features = sort { $b->{priority} <=> $a->{priority} or $a->{title} cmp $b->{title} } @{$features{$n}};
608 prompt "$features[0]->{target_title}"
609 default FEATURE_$features[0]->{name}
612 foreach my $feature (@features) {
614 config FEATURE_$feature->{name}
615 bool "$feature->{title}"
617 $feature->{description} =~ /\w/ and do {
618 print "\t\thelp\n".$feature->{description}."\n";
626 sub gen_package_config() {
627 parse_package_metadata($ARGV[0]) or exit 1;
628 print "menuconfig IMAGEOPT\n\tbool \"Image configuration\"\n\tdefault n\n";
629 foreach my $preconfig (keys %preconfig) {
630 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
631 my $conf = $preconfig{$preconfig}->{$cfg}->{id};
634 config UCI_PRECONFIG_$conf
635 string "$preconfig{$preconfig}->{$cfg}->{label}" if IMAGEOPT
636 depends on PACKAGE_$preconfig
637 default "$preconfig{$preconfig}->{$cfg}->{default}"
642 print "source \"package/*/image-config.in\"\n";
643 if (scalar glob "package/feeds/*/*/image-config.in") {
644 print "source \"package/feeds/*/*/image-config.in\"\n";
646 print_package_features();
647 print_package_config_category 'Base system';
648 foreach my $cat (sort {uc($a) cmp uc($b)} keys %category) {
649 print_package_config_category $cat;
653 sub get_conditional_dep($$) {
654 my $condition = shift;
657 if ($condition =~ /^!(.+)/) {
658 return "\$(if \$(CONFIG_$1),,$depstr)";
660 return "\$(if \$(CONFIG_$condition),$depstr)";
667 sub gen_package_mk() {
673 parse_package_metadata($ARGV[0]) or exit 1;
674 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
676 my $pkg = $package{$name};
679 next if defined $pkg->{vdepends};
681 $config = "\$(CONFIG_PACKAGE_$name)";
683 $pkg->{buildonly} and $config = "";
684 print "package-$config += $pkg->{subdir}$pkg->{src}\n";
685 if ($pkg->{variant}) {
686 if (!defined($done{$pkg->{src}})) {
687 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/default-variant := $pkg->{variant}\n";
689 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/variants += \$(if $config,$pkg->{variant})\n"
691 $pkg->{prereq} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
694 next if $done{$pkg->{src}};
695 $done{$pkg->{src}} = 1;
697 if (@{$pkg->{buildtypes}} > 0) {
698 print "buildtypes-$pkg->{subdir}$pkg->{src} = ".join(' ', @{$pkg->{buildtypes}})."\n";
701 foreach my $spkg (@{$srcpackage{$pkg->{src}}}) {
702 foreach my $dep (@{$spkg->{depends}}, @{$spkg->{builddepends}}) {
709 foreach my $type (@{$pkg->{buildtypes}}) {
713 next unless $pkg->{"builddepends/$type"};
714 foreach my $dep (@{$pkg->{"builddepends/$type"}}) {
718 if ($dep =~ /^(.+):(.+)/) {
722 if ($dep =~ /^(.+)(\/.+)/) {
728 my $pkg_dep = $package{$dep};
729 if (defined($pkg_dep) && defined($pkg_dep->{src})) {
730 $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
731 } elsif (defined($srcpackage{$dep})) {
732 $idx = $subdir{$dep}.$dep;
736 my $depstr = "\$(curdir)/$idx$suffix/compile";
737 my $depline = get_conditional_dep($condition, $depstr);
739 $deplines{$depline}++;
742 my $depline = join(" ", sort keys %deplines);
744 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/$type/compile += $depline\n";
750 foreach my $deps (@srcdeps) {
756 if ($deps =~ /^(.+):(.+)/) {
760 if ($deps =~ /^(.+)(\/.+)/) {
765 my $pkg_dep = $package{$deps};
768 if ($pkg_dep->{vdepends}) {
769 @deps = @{$pkg_dep->{vdepends}};
774 foreach my $dep (@deps) {
775 $pkg_dep = $package{$deps};
776 if (defined $pkg_dep->{src}) {
777 ($pkg->{src} ne $pkg_dep->{src}.$suffix) and $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
778 } elsif (defined($srcpackage{$dep})) {
779 $idx = $subdir{$dep}.$dep;
781 undef $idx if $idx eq 'base-files';
786 next if $pkg->{src} eq $pkg_dep->{src}.$suffix;
787 next if $dep{$condition.":".$pkg->{src}."->".$idx};
788 next if $dep{$pkg->{src}."->($dep)".$idx} and $pkg_dep->{vdepends};
791 if ($pkg_dep->{vdepends}) {
792 $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
793 $dep{$pkg->{src}."->($dep)".$idx} = 1;
795 $depstr = "\$(curdir)/$idx/compile";
796 $dep{$pkg->{src}."->".$idx} = 1;
798 $depline = get_conditional_dep($condition, $depstr);
800 $deplines{$depline}++;
805 my $depline = join(" ", sort keys %deplines);
807 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/compile += $depline\n";
814 foreach my $preconfig (keys %preconfig) {
816 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
817 my $conf = $preconfig{$preconfig}->{$cfg}->{id};
819 $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
824 ifndef DUMP_TARGET_DB
825 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
830 ifneq (\$(IMAGEOPT)\$(CONFIG_IMAGEOPT),)
831 package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
839 sub gen_package_source() {
840 parse_package_metadata($ARGV[0]) or exit 1;
841 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
842 my $pkg = $package{$name};
843 if ($pkg->{name} && $pkg->{source}) {
844 print "$pkg->{name}: ";
845 print "$pkg->{source}\n";
850 sub parse_command() {
851 my $cmd = shift @ARGV;
853 /^target_config$/ and return gen_target_config();
854 /^package_mk$/ and return gen_package_mk();
855 /^package_config$/ and return gen_package_config();
856 /^kconfig/ and return gen_kconfig_overrides();
857 /^package_source$/ and return gen_package_source();
861 $0 target_config [file] Target metadata in Kconfig format
862 $0 package_mk [file] Package metadata in makefile format
863 $0 package_config [file] Package metadata in Kconfig format
864 $0 kconfig [file] [config] Kernel config overrides
865 $0 package_source [file] Package source file information