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";
192 my $parent = $target->{parent};
194 return $target->{parent}->{name}." - ".$target->{name};
196 return $target->{name};
203 if (substr($v,0,2) eq "2_") {
204 $v =~ /(\d+_\d+_\d+)(_\d+)?/ and $v = $1;
206 $v =~ /(\d+_\d+)(_\d+)?/ and $v = $1;
211 sub print_target($) {
213 my $features = target_config_features(@{$target->{features}});
214 my $help = $target->{desc};
219 if ($help =~ /\w+/) {
220 $help =~ s/^\s*/\t /mg;
221 $help = "\thelp\n$help";
226 my $v = kver($target->{version});
227 if (@{$target->{subtargets}} == 0) {
229 config TARGET_$target->{conf}
230 bool "$target->{name}"
236 config TARGET_$target->{conf}
237 bool "$target->{name}"
240 if ($target->{subtarget}) {
241 $confstr .= "\tdepends on TARGET_$target->{boardconf}\n";
243 if (@{$target->{subtargets}} > 0) {
244 $confstr .= "\tselect HAS_SUBTARGETS\n";
245 grep { /broken/ } @{$target->{features}} and $confstr .= "\tdepends on BROKEN\n";
247 $confstr .= $features;
250 if ($target->{arch} =~ /\w/) {
251 $confstr .= "\tselect $target->{arch}\n";
253 foreach my $dep (@{$target->{depends}}) {
254 my $mode = "depends on";
258 $dep =~ /^([@\+\-]+)(.+)$/;
262 next if $name =~ /:/;
263 $flags =~ /-/ and $mode = "deselect";
264 $flags =~ /\+/ and $mode = "select";
265 $flags =~ /@/ and $confstr .= "\t$mode $name\n";
267 $confstr .= "$help\n\n";
271 sub gen_target_config() {
272 my @target = parse_target_metadata();
275 my @target_sort = sort {
276 target_name($a) cmp target_name($b);
282 prompt "Target System"
283 default TARGET_ar71xx
288 foreach my $target (@target_sort) {
289 next if $target->{subtarget};
290 print_target($target);
297 prompt "Subtarget" if HAS_SUBTARGETS
299 foreach my $target (@target) {
300 next unless $target->{def_subtarget};
302 default TARGET_$target->{conf}_$target->{def_subtarget} if TARGET_$target->{conf}
308 foreach my $target (@target) {
309 next unless $target->{subtarget};
310 print_target($target);
317 prompt "Target Profile"
321 foreach my $target (@target) {
322 my $profiles = $target->{profiles};
324 foreach my $profile (@$profiles) {
326 config TARGET_$target->{conf}_$profile->{id}
327 bool "$profile->{name}"
328 depends on TARGET_$target->{conf}
331 $profile->{kconfig} and print "\tselect PROFILE_KCONFIG\n";
332 my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
333 foreach my $pkg (@pkglist) {
334 print "\tselect DEFAULT_$pkg\n";
337 my $help = $profile->{desc};
338 if ($help =~ /\w+/) {
339 $help =~ s/^\s*/\t /mg;
340 $help = "\thelp\n$help";
351 config HAS_SUBTARGETS
358 foreach my $target (@target) {
359 $target->{subtarget} or print "\t\tdefault \"".$target->{board}."\" if TARGET_".$target->{conf}."\n";
362 config TARGET_ARCH_PACKAGES
366 foreach my $target (@target) {
367 next if @{$target->{subtargets}} > 0;
368 print "\t\tdefault \"".($target->{arch_packages} || $target->{board})."\" if TARGET_".$target->{conf}."\n";
372 config DEFAULT_TARGET_OPTIMIZATION
375 foreach my $target (@target) {
376 next if @{$target->{subtargets}} > 0;
377 print "\tdefault \"".$target->{cflags}."\" if TARGET_".$target->{conf}."\n";
379 print "\tdefault \"-Os -pipe -funit-at-a-time\"\n";
385 foreach my $target (@target) {
386 next if @{$target->{subtargets}} > 0;
387 print "\tdefault \"".$target->{cputype}."\" if TARGET_".$target->{conf}."\n";
389 print "\tdefault \"\"\n";
392 foreach my $target (@target) {
393 my $v = kver($target->{version});
403 foreach my $def (sort keys %defaults) {
404 print "\tconfig DEFAULT_".$def."\n";
405 print "\t\tbool\n\n";
410 sub __find_package_dep($$) {
413 my $deps = ($pkg->{vdepends} or $pkg->{depends});
415 return 0 unless defined $deps;
416 foreach my $dep (@{$deps}) {
417 next if $dep_check{$dep};
418 $dep_check{$dep} = 1;
419 return 1 if $dep eq $name;
420 return 1 if ($package{$dep} and (__find_package_dep($package{$dep},$name) == 1));
425 # wrapper to avoid infinite recursion
426 sub find_package_dep($$) {
431 return __find_package_dep($pkg, $name);
434 sub package_depends($$) {
439 return 0 if ($a->{submenu} ne $b->{submenu});
440 if (find_package_dep($a, $b->{name}) == 1) {
442 } elsif (find_package_dep($b, $a->{name}) == 1) {
453 my $only_dep = shift;
457 my $parent_condition = shift;
463 my @depends = @$depends;
464 foreach my $depend (@depends) {
465 my $m = "depends on";
467 $depend =~ s/^([@\+]+)// and $flags = $1;
469 my $condition = $parent_condition;
471 next if $condition eq $depend;
472 next if $seen->{"$parent_condition:$depend"};
473 next if $seen->{":$depend"};
474 $seen->{"$parent_condition:$depend"} = 1;
475 if ($depend =~ /^(.+):(.+)$/) {
476 if ($1 ne "PACKAGE_$pkgname") {
478 $condition = "$condition && $1";
485 next if $package{$depend} and $package{$depend}->{buildonly};
486 if ($vdep = $package{$depend}->{vdepends}) {
487 $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
489 $flags =~ /\+/ and do {
490 # Menuconfig will not treat 'select FOO' as a real dependency
491 # thus if FOO depends on other config options, these dependencies
492 # will not be checked. To fix this, we simply emit all of FOO's
493 # depends here as well.
494 $package{$depend} and push @t_depends, [ $package{$depend}->{depends}, $condition ];
499 $flags =~ /@/ or $depend = "PACKAGE_$depend";
501 if ($m =~ /select/) {
502 next if $depend eq $condition;
503 $depend = "$depend if $condition";
505 $depend = "!($condition) || $depend";
509 $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
512 foreach my $tdep (@t_depends) {
513 mconf_depends($pkgname, $tdep->[0], 1, $dep, $seen, $tdep->[1]);
516 foreach my $depend (keys %$dep) {
517 my $m = $dep->{$depend};
518 $res .= "\t\t$m $depend\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}) {
575 $pkg->{default} ||= "m if ALL";
577 if ($pkg->{default}) {
578 foreach my $default (split /\s*,\s*/, $pkg->{default}) {
579 print "\t\tdefault $default\n";
582 print mconf_depends($pkg->{name}, $pkg->{depends}, 0);
583 print mconf_depends($pkg->{name}, $pkg->{mdepends}, 0);
585 print $pkg->{description};
588 $pkg->{config} and print $pkg->{config}."\n";
590 if ($menu ne 'undef') {
592 $menu_dep{$menu} and print "endif\n";
597 undef $category{$cat};
600 sub print_package_features() {
601 keys %features > 0 or return;
602 print "menu \"Package features\"\n";
603 foreach my $n (keys %features) {
604 my @features = sort { $b->{priority} <=> $a->{priority} or $a->{title} cmp $b->{title} } @{$features{$n}};
607 prompt "$features[0]->{target_title}"
608 default FEATURE_$features[0]->{name}
611 foreach my $feature (@features) {
613 config FEATURE_$feature->{name}
614 bool "$feature->{title}"
616 $feature->{description} =~ /\w/ and do {
617 print "\t\thelp\n".$feature->{description}."\n";
625 sub gen_package_config() {
626 parse_package_metadata($ARGV[0]) or exit 1;
627 print "menuconfig IMAGEOPT\n\tbool \"Image configuration\"\n\tdefault n\n";
628 foreach my $preconfig (keys %preconfig) {
629 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
630 my $conf = $preconfig{$preconfig}->{$cfg}->{id};
633 config UCI_PRECONFIG_$conf
634 string "$preconfig{$preconfig}->{$cfg}->{label}" if IMAGEOPT
635 depends on PACKAGE_$preconfig
636 default "$preconfig{$preconfig}->{$cfg}->{default}"
641 print "source \"package/*/image-config.in\"\n";
642 if (scalar glob "package/feeds/*/*/image-config.in") {
643 print "source \"package/feeds/*/*/image-config.in\"\n";
645 print_package_features();
646 print_package_config_category 'Base system';
647 foreach my $cat (sort {uc($a) cmp uc($b)} keys %category) {
648 print_package_config_category $cat;
652 sub get_conditional_dep($$) {
653 my $condition = shift;
656 if ($condition =~ /^!(.+)/) {
657 return "\$(if \$(CONFIG_$1),,$depstr)";
659 return "\$(if \$(CONFIG_$condition),$depstr)";
666 sub gen_package_mk() {
672 parse_package_metadata($ARGV[0]) or exit 1;
673 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
675 my $pkg = $package{$name};
678 next if defined $pkg->{vdepends};
680 $config = "\$(CONFIG_PACKAGE_$name)";
682 $pkg->{buildonly} and $config = "";
683 print "package-$config += $pkg->{subdir}$pkg->{src}\n";
684 if ($pkg->{variant}) {
685 if (!defined($done{$pkg->{src}})) {
686 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/default-variant := $pkg->{variant}\n";
688 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/variants += \$(if $config,$pkg->{variant})\n"
690 $pkg->{prereq} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
693 next if $done{$pkg->{src}};
694 $done{$pkg->{src}} = 1;
696 if (@{$pkg->{buildtypes}} > 0) {
697 print "buildtypes-$pkg->{subdir}$pkg->{src} = ".join(' ', @{$pkg->{buildtypes}})."\n";
700 foreach my $spkg (@{$srcpackage{$pkg->{src}}}) {
701 foreach my $dep (@{$spkg->{depends}}, @{$spkg->{builddepends}}) {
708 foreach my $type (@{$pkg->{buildtypes}}) {
712 next unless $pkg->{"builddepends/$type"};
713 foreach my $dep (@{$pkg->{"builddepends/$type"}}) {
717 if ($dep =~ /^(.+):(.+)/) {
721 if ($dep =~ /^(.+)(\/.+)/) {
727 my $pkg_dep = $package{$dep};
728 if (defined($pkg_dep) && defined($pkg_dep->{src})) {
729 $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
730 } elsif (defined($srcpackage{$dep})) {
731 $idx = $subdir{$dep}.$dep;
735 my $depstr = "\$(curdir)/$idx$suffix/compile";
736 my $depline = get_conditional_dep($condition, $depstr);
738 $deplines{$depline}++;
741 my $depline = join(" ", sort keys %deplines);
743 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/$type/compile += $depline\n";
749 foreach my $deps (@srcdeps) {
755 if ($deps =~ /^(.+):(.+)/) {
759 if ($deps =~ /^(.+)(\/.+)/) {
764 my $pkg_dep = $package{$deps};
767 if ($pkg_dep->{vdepends}) {
768 @deps = @{$pkg_dep->{vdepends}};
773 foreach my $dep (@deps) {
774 $pkg_dep = $package{$deps};
775 if (defined $pkg_dep->{src}) {
776 ($pkg->{src} ne $pkg_dep->{src}.$suffix) and $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
777 } elsif (defined($srcpackage{$dep})) {
778 $idx = $subdir{$dep}.$dep;
780 undef $idx if $idx eq 'base-files';
785 next if $pkg->{src} eq $pkg_dep->{src}.$suffix;
786 next if $dep{$condition.":".$pkg->{src}."->".$idx};
787 next if $dep{$pkg->{src}."->($dep)".$idx} and $pkg_dep->{vdepends};
790 if ($pkg_dep->{vdepends}) {
791 $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
792 $dep{$pkg->{src}."->($dep)".$idx} = 1;
794 $depstr = "\$(curdir)/$idx/compile";
795 $dep{$pkg->{src}."->".$idx} = 1;
797 $depline = get_conditional_dep($condition, $depstr);
799 $deplines{$depline}++;
804 my $depline = join(" ", sort keys %deplines);
806 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/compile += $depline\n";
813 foreach my $preconfig (keys %preconfig) {
815 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
816 my $conf = $preconfig{$preconfig}->{$cfg}->{id};
818 $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
823 ifndef DUMP_TARGET_DB
824 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
829 ifneq (\$(IMAGEOPT)\$(CONFIG_IMAGEOPT),)
830 package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
838 sub gen_package_source() {
839 parse_package_metadata($ARGV[0]) or exit 1;
840 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
841 my $pkg = $package{$name};
842 if ($pkg->{name} && $pkg->{source}) {
843 print "$pkg->{name}: ";
844 print "$pkg->{source}\n";
849 sub parse_command() {
850 my $cmd = shift @ARGV;
852 /^target_config$/ and return gen_target_config();
853 /^package_mk$/ and return gen_package_mk();
854 /^package_config$/ and return gen_package_config();
855 /^kconfig/ and return gen_kconfig_overrides();
856 /^package_source$/ and return gen_package_source();
860 $0 target_config [file] Target metadata in Kconfig format
861 $0 package_mk [file] Package metadata in makefile format
862 $0 package_config [file] Package metadata in Kconfig format
863 $0 kconfig [file] [config] Kernel config overrides
864 $0 package_source [file] Package source file information