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-Kernel:\s*(\d+\.\d+)\s*$/ and $target->{kernel} = $1;
49 /^Target-Name:\s*(.+)\s*$/ and $target->{name} = $1;
50 /^Target-Path:\s*(.+)\s*$/ and $target->{path} = $1;
51 /^Target-Arch:\s*(.+)\s*$/ and $target->{arch} = $1;
52 /^Target-Arch-Packages:\s*(.+)\s*$/ and $target->{arch_packages} = $1;
53 /^Target-Features:\s*(.+)\s*$/ and $target->{features} = [ split(/\s+/, $1) ];
54 /^Target-Depends:\s*(.+)\s*$/ and $target->{depends} = [ split(/\s+/, $1) ];
55 /^Target-Description:/ and $target->{desc} = get_multiline(*FILE);
56 /^Target-Optimization:\s*(.+)\s*$/ and $target->{cflags} = $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-Packages:\s*(.+)\s*$/ and $target->{packages} = [ split(/\s+/, $1) ];
61 /^Target-Profile:\s*(.+)\s*$/ and do {
67 push @{$target->{profiles}}, $profile;
69 /^Target-Profile-Name:\s*(.+)\s*$/ and $profile->{name} = $1;
70 /^Target-Profile-Packages:\s*(.*)\s*$/ and $profile->{packages} = [ split(/\s+/, $1) ];
71 /^Target-Profile-Description:\s*(.*)\s*/ and $profile->{desc} = get_multiline(*FILE);
72 /^Target-Profile-Config:/ and $profile->{config} = get_multiline(*FILE, "\t");
73 /^Target-Profile-Kconfig:/ and $profile->{kconfig} = 1;
76 foreach my $target (@target) {
77 next if @{$target->{subtargets}} > 0;
78 @{$target->{profiles}} > 0 or $target->{profiles} = [
89 sub gen_kconfig_overrides() {
93 my $pkginfo = shift @ARGV;
94 my $cfgfile = shift @ARGV;
96 # parameter 2: build system config
97 open FILE, "<$cfgfile" or return;
99 /^(CONFIG_.+?)=(.+)$/ and $config{$1} = 1;
103 # parameter 1: package metadata
104 open FILE, "<$pkginfo" or return;
106 /^Package:\s*(.+?)\s*$/ and $package = $1;
107 /^Kernel-Config:\s*(.+?)\s*$/ and do {
108 my @config = split /\s+/, $1;
109 foreach my $config (@config) {
112 if ($config =~ /^(.+?)=(.+)$/) {
117 if ($config{"CONFIG_PACKAGE_$package"} and ($config ne 'n')) {
118 $kconfig{$config} = $val;
119 } elsif (!$override) {
120 $kconfig{$config} or $kconfig{$config} = 'n';
127 foreach my $kconfig (sort keys %kconfig) {
128 if ($kconfig{$kconfig} eq 'n') {
129 print "# $kconfig is not set\n";
131 print "$kconfig=$kconfig{$kconfig}\n";
136 sub merge_package_lists($$) {
142 foreach my $pkg (@$list1, @$list2) {
145 foreach my $pkg (keys %pkgs) {
146 push @l, $pkg unless ($pkg =~ /^-/ or $pkgs{"-$pkg"});
151 sub target_config_features(@) {
154 while ($_ = shift @_) {
155 /broken/ and $ret .= "\tdepends BROKEN\n";
156 /audio/ and $ret .= "\tselect AUDIO_SUPPORT\n";
157 /display/ and $ret .= "\tselect DISPLAY_SUPPORT\n";
158 /gpio/ and $ret .= "\tselect GPIO_SUPPORT\n";
159 /pci/ and $ret .= "\tselect PCI_SUPPORT\n";
160 /pcie/ and $ret .= "\tselect PCIE_SUPPORT\n";
161 /usb/ and $ret .= "\tselect USB_SUPPORT\n";
162 /usbgadget/ and $ret .= "\tselect USB_GADGET_SUPPORT\n";
163 /pcmcia/ and $ret .= "\tselect PCMCIA_SUPPORT\n";
164 /squashfs/ and $ret .= "\tselect USES_SQUASHFS\n";
165 /jffs2/ and $ret .= "\tselect USES_JFFS2\n";
166 /ext2/ and $ret .= "\tselect USES_EXT2\n";
167 /targz/ and $ret .= "\tselect USES_TARGZ\n";
168 /cpiogz/ and $ret .= "\tselect USES_CPIOGZ\n";
169 /ubifs/ and $ret .= "\tselect USES_UBIFS\n";
170 /fpu/ and $ret .= "\tselect HAS_FPU\n";
171 /ramdisk/ and $ret .= "\tselect USES_INITRAMFS\n";
172 /powerpc64/ and $ret .= "\tselect powerpc64\n";
173 /nommu/ and $ret .= "\tselect NOMMU\n";
180 my $parent = $target->{parent};
182 return $target->{parent}->{name}." - ".$target->{name};
184 return $target->{name};
191 $v =~ /(\d+_\d+_\d+)(_\d+)?/ and $v = $1;
195 sub print_target($) {
197 my $features = target_config_features(@{$target->{features}});
198 my $help = $target->{desc};
199 my $kernel = $target->{kernel};
205 if ($help =~ /\w+/) {
206 $help =~ s/^\s*/\t /mg;
207 $help = "\thelp\n$help";
212 my $v = kver($target->{version});
213 if (@{$target->{subtargets}} == 0) {
215 config TARGET_$target->{conf}
216 bool "$target->{name}"
223 config TARGET_$target->{conf}
224 bool "$target->{name}"
227 if ($target->{subtarget}) {
228 $confstr .= "\tdepends TARGET_$target->{boardconf}\n";
230 if (@{$target->{subtargets}} > 0) {
231 $confstr .= "\tselect HAS_SUBTARGETS\n";
234 if ($target->{arch} =~ /\w/) {
235 $confstr .= "\tselect $target->{arch}\n";
237 foreach my $dep (@{$target->{depends}}) {
238 my $mode = "depends";
242 $dep =~ /^([@\+\-]+)(.+)$/;
246 next if $name =~ /:/;
247 $flags =~ /-/ and $mode = "deselect";
248 $flags =~ /\+/ and $mode = "select";
249 $flags =~ /@/ and $confstr .= "\t$mode $name\n";
251 $confstr .= $features;
252 $confstr .= "$help\n\n";
256 sub gen_target_config() {
257 my @target = parse_target_metadata();
260 my @target_sort = sort {
261 target_name($a) cmp target_name($b);
267 prompt "Target System"
268 default TARGET_brcm47xx
273 foreach my $target (@target_sort) {
274 next if $target->{subtarget};
275 print_target($target);
282 prompt "Subtarget" if HAS_SUBTARGETS
285 foreach my $target (@target) {
286 next unless $target->{subtarget};
287 print_target($target);
294 prompt "Target Profile"
298 foreach my $target (@target) {
299 my $profiles = $target->{profiles};
301 foreach my $profile (@$profiles) {
303 config TARGET_$target->{conf}_$profile->{id}
304 bool "$profile->{name}"
305 depends TARGET_$target->{conf}
308 $profile->{kconfig} and print "\tselect PROFILE_KCONFIG\n";
309 my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
310 foreach my $pkg (@pkglist) {
311 print "\tselect DEFAULT_$pkg\n";
314 my $help = $profile->{desc};
315 if ($help =~ /\w+/) {
316 $help =~ s/^\s*/\t /mg;
317 $help = "\thelp\n$help";
328 config HAS_SUBTARGETS
335 foreach my $target (@target) {
336 $target->{subtarget} or print "\t\tdefault \"".$target->{board}."\" if TARGET_".$target->{conf}."\n";
339 config TARGET_ARCH_PACKAGES
343 foreach my $target (@target) {
344 next if @{$target->{subtargets}} > 0;
345 print "\t\tdefault \"".($target->{arch_packages} || $target->{board})."\" if TARGET_".$target->{conf}."\n";
349 config DEFAULT_TARGET_OPTIMIZATION
352 foreach my $target (@target) {
353 next if @{$target->{subtargets}} > 0;
354 print "\tdefault \"".$target->{cflags}."\" if TARGET_".$target->{conf}."\n";
356 print "\tdefault \"-Os -pipe -funit-at-a-time\"\n";
359 foreach my $target (@target) {
360 my $v = kver($target->{version});
370 foreach my $def (sort keys %defaults) {
371 print "\tconfig DEFAULT_".$def."\n";
372 print "\t\tbool\n\n";
377 sub __find_package_dep($$) {
380 my $deps = ($pkg->{vdepends} or $pkg->{depends});
382 return 0 unless defined $deps;
383 foreach my $dep (@{$deps}) {
384 next if $dep_check{$dep};
385 $dep_check{$dep} = 1;
386 return 1 if $dep eq $name;
387 return 1 if ($package{$dep} and (__find_package_dep($package{$dep},$name) == 1));
392 # wrapper to avoid infinite recursion
393 sub find_package_dep($$) {
398 return __find_package_dep($pkg, $name);
401 sub package_depends($$) {
406 return 0 if ($a->{submenu} ne $b->{submenu});
407 if (find_package_dep($a, $b->{name}) == 1) {
409 } elsif (find_package_dep($b, $a->{name}) == 1) {
420 my $only_dep = shift;
424 my $parent_condition = shift;
429 my @depends = @$depends;
430 foreach my $depend (@depends) {
433 $depend =~ s/^([@\+]+)// and $flags = $1;
435 my $condition = $parent_condition;
437 next if $condition eq $depend;
438 next if $seen->{"$parent_condition:$depend"};
439 $seen->{"$parent_condition:$depend"} = 1;
440 if ($depend =~ /^(.+):(.+)$/) {
441 if ($1 ne "PACKAGE_$pkgname") {
443 $condition = "$condition && $1";
450 next if $package{$depend} and $package{$depend}->{buildonly};
451 if ($vdep = $package{$depend}->{vdepends}) {
452 $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
454 $flags =~ /\+/ and do {
455 # Menuconfig will not treat 'select FOO' as a real dependency
456 # thus if FOO depends on other config options, these dependencies
457 # will not be checked. To fix this, we simply emit all of FOO's
458 # depends here as well.
459 $package{$depend} and mconf_depends($pkgname, $package{$depend}->{depends}, 1, $dep, $seen, $condition);
464 $flags =~ /@/ or $depend = "PACKAGE_$depend";
466 if ($m =~ /select/) {
467 $depend = "$depend if $condition";
469 $depend = "!($condition) || $depend";
473 $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
475 foreach my $depend (keys %$dep) {
476 my $m = $dep->{$depend};
477 $res .= "\t\t$m $depend\n";
482 sub print_package_config_category($) {
487 return unless $category{$cat};
489 print "menu \"$cat\"\n\n";
490 my %spkg = %{$category{$cat}};
492 foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
493 foreach my $pkg (@{$spkg{$spkg}}) {
494 next if $pkg->{buildonly};
495 my $menu = $pkg->{submenu};
497 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
501 $menus{$menu} or $menus{$menu} = [];
502 push @{$menus{$menu}}, $pkg;
506 ($a eq 'undef' ? 1 : 0) or
507 ($b eq 'undef' ? -1 : 0) or
511 foreach my $menu (@menus) {
513 package_depends($a, $b) or
514 ($a->{name} cmp $b->{name})
516 if ($menu ne 'undef') {
517 $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
518 print "menu \"$menu\"\n";
520 foreach my $pkg (@pkgs) {
521 my $title = $pkg->{name};
522 my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
524 $title .= ("." x $c). " ". $pkg->{title};
527 $pkg->{menu} and print "menu";
528 print "config PACKAGE_".$pkg->{name}."\n";
529 print "\t\t".($pkg->{tristate} ? 'tristate' : 'bool')." \"$title\"\n";
530 print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
531 foreach my $default (split /\s*,\s*/, $pkg->{default}) {
532 print "\t\tdefault $default\n";
534 print mconf_depends($pkg->{name}, $pkg->{depends}, 0);
536 print $pkg->{description};
539 $pkg->{config} and print $pkg->{config}."\n";
541 if ($menu ne 'undef') {
543 $menu_dep{$menu} and print "endif\n";
548 undef $category{$cat};
551 sub print_package_features() {
552 keys %features > 0 or return;
553 print "menu \"Package features\"\n";
554 foreach my $n (keys %features) {
555 my @features = sort { $b->{priority} <=> $a->{priority} or $a->{title} cmp $b->{title} } @{$features{$n}};
558 prompt "$features[0]->{target_title}"
559 default FEATURE_$features[0]->{name}
562 foreach my $feature (@features) {
564 config FEATURE_$feature->{name}
565 bool "$feature->{title}"
567 $feature->{description} =~ /\w/ and do {
568 print "\t\thelp\n".$feature->{description}."\n";
576 sub gen_package_config() {
577 parse_package_metadata($ARGV[0]) or exit 1;
578 print "menuconfig IMAGEOPT\n\tbool \"Image configuration\"\n\tdefault n\n";
579 foreach my $preconfig (keys %preconfig) {
580 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
581 my $conf = $preconfig{$preconfig}->{$cfg}->{id};
584 config UCI_PRECONFIG_$conf
585 string "$preconfig{$preconfig}->{$cfg}->{label}" if IMAGEOPT
586 depends PACKAGE_$preconfig
587 default "$preconfig{$preconfig}->{$cfg}->{default}"
592 print "source \"package/*/image-config.in\"\n";
593 if (scalar glob "package/feeds/*/*/image-config.in") {
594 print "source \"package/feeds/*/*/image-config.in\"\n";
596 print_package_features();
597 print_package_config_category 'Base system';
598 foreach my $cat (keys %category) {
599 print_package_config_category $cat;
603 sub get_conditional_dep($$) {
604 my $condition = shift;
607 if ($condition =~ /^!(.+)/) {
608 return "\$(if \$(CONFIG_$1),,$depstr)";
610 return "\$(if \$(CONFIG_$condition),$depstr)";
617 sub gen_package_mk() {
623 parse_package_metadata($ARGV[0]) or exit 1;
624 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
626 my $pkg = $package{$name};
629 next if defined $pkg->{vdepends};
632 $conf{$pkg->{src}} or do {
634 $conf{$pkg->{src}} = 1;
637 $config = "\$(CONFIG_PACKAGE_$name)"
640 $pkg->{buildonly} and $config = "";
641 print "package-$config += $pkg->{subdir}$pkg->{src}\n";
642 if ($pkg->{variant}) {
643 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/variants += \$(if $config,$pkg->{variant})\n"
645 $pkg->{prereq} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
648 next if $done{$pkg->{src}};
649 $done{$pkg->{src}} = 1;
651 if (@{$pkg->{buildtypes}} > 0) {
652 print "buildtypes-$pkg->{subdir}$pkg->{src} = ".join(' ', @{$pkg->{buildtypes}})."\n";
655 foreach my $spkg (@{$srcpackage{$pkg->{src}}}) {
656 foreach my $dep (@{$spkg->{depends}}, @{$spkg->{builddepends}}) {
663 foreach my $type (@{$pkg->{buildtypes}}) {
667 next unless $pkg->{"builddepends/$type"};
668 foreach my $dep (@{$pkg->{"builddepends/$type"}}) {
672 if ($dep =~ /^(.+):(.+)/) {
676 if ($dep =~ /^(.+)(\/.+)/) {
682 my $pkg_dep = $package{$dep};
683 if (defined($pkg_dep) && defined($pkg_dep->{src})) {
684 $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
685 } elsif (defined($srcpackage{$dep})) {
686 $idx = $subdir{$dep}.$dep;
690 my $depstr = "\$(curdir)/$idx$suffix/compile";
691 my $depline = get_conditional_dep($condition, $depstr);
693 $deplines{$dep} = $depline;
696 my $depline = join(" ", values %deplines);
698 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/$type/compile += $depline\n";
704 foreach my $deps (@srcdeps) {
710 if ($deps =~ /^(.+):(.+)/) {
714 if ($deps =~ /^(.+)(\/.+)/) {
719 my $pkg_dep = $package{$deps};
722 if ($pkg_dep->{vdepends}) {
723 @deps = @{$pkg_dep->{vdepends}};
728 foreach my $dep (@deps) {
729 $pkg_dep = $package{$deps};
730 if (defined $pkg_dep->{src}) {
731 ($pkg->{src} ne $pkg_dep->{src}.$suffix) and $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
732 } elsif (defined($srcpackage{$dep})) {
733 $idx = $subdir{$dep}.$dep;
736 undef $idx if $idx =~ /^(kernel)|(base-files)$/;
739 next if $pkg->{src} eq $pkg_dep->{src}.$suffix;
740 next if $dep{$pkg->{src}."->".$idx};
741 next if $dep{$pkg->{src}."->($dep)".$idx} and $pkg_dep->{vdepends};
744 if ($pkg_dep->{vdepends}) {
745 $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
746 $dep{$pkg->{src}."->($dep)".$idx} = 1;
748 $depstr = "\$(curdir)/$idx/compile";
749 $dep{$pkg->{src}."->".$idx} = 1;
751 $depline = get_conditional_dep($condition, $depstr);
753 $deplines{$idx.$dep} = $depline;
758 my $depline = join(" ", values %deplines);
760 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/compile += $depline\n";
767 foreach my $preconfig (keys %preconfig) {
769 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
770 my $conf = $preconfig{$preconfig}->{$cfg}->{id};
772 $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
777 ifndef DUMP_TARGET_DB
778 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
783 ifneq (\$(IMAGEOPT)\$(CONFIG_IMAGEOPT),)
784 package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
792 sub gen_package_source() {
793 parse_package_metadata($ARGV[0]) or exit 1;
794 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
795 my $pkg = $package{$name};
796 if ($pkg->{name} && $pkg->{source}) {
797 print "$pkg->{name}: ";
798 print "$pkg->{source}\n";
803 sub parse_command() {
804 my $cmd = shift @ARGV;
806 /^target_config$/ and return gen_target_config();
807 /^package_mk$/ and return gen_package_mk();
808 /^package_config$/ and return gen_package_config();
809 /^kconfig/ and return gen_kconfig_overrides();
810 /^package_source$/ and return gen_package_source();
814 $0 target_config [file] Target metadata in Kconfig format
815 $0 package_mk [file] Package metadata in makefile format
816 $0 package_config [file] Package metadata in Kconfig format
817 $0 kconfig [file] [config] Kernel config overrides
818 $0 package_source [file] Package source file information