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 /^Linux-Version:\s*(.+)\s*$/ and $target->{version} = $1;
57 /^Linux-Release:\s*(.+)\s*$/ and $target->{release} = $1;
58 /^Linux-Kernel-Arch:\s*(.+)\s*$/ and $target->{karch} = $1;
59 /^Default-Packages:\s*(.+)\s*$/ and $target->{packages} = [ split(/\s+/, $1) ];
60 /^Target-Profile:\s*(.+)\s*$/ and do {
66 push @{$target->{profiles}}, $profile;
68 /^Target-Profile-Name:\s*(.+)\s*$/ and $profile->{name} = $1;
69 /^Target-Profile-Packages:\s*(.*)\s*$/ and $profile->{packages} = [ split(/\s+/, $1) ];
70 /^Target-Profile-Description:\s*(.*)\s*/ and $profile->{desc} = get_multiline(*FILE);
71 /^Target-Profile-Config:/ and $profile->{config} = get_multiline(*FILE, "\t");
72 /^Target-Profile-Kconfig:/ and $profile->{kconfig} = 1;
75 foreach my $target (@target) {
76 next if @{$target->{subtargets}} > 0;
77 @{$target->{profiles}} > 0 or $target->{profiles} = [
88 sub gen_kconfig_overrides() {
92 my $pkginfo = shift @ARGV;
93 my $cfgfile = shift @ARGV;
95 # parameter 2: build system config
96 open FILE, "<$cfgfile" or return;
98 /^(CONFIG_.+?)=(.+)$/ and $config{$1} = 1;
102 # parameter 1: package metadata
103 open FILE, "<$pkginfo" or return;
105 /^Package:\s*(.+?)\s*$/ and $package = $1;
106 /^Kernel-Config:\s*(.+?)\s*$/ and do {
107 my @config = split /\s+/, $1;
108 foreach my $config (@config) {
111 if ($config =~ /^(.+?)=(.+)$/) {
116 if ($config{"CONFIG_PACKAGE_$package"} and ($config ne 'n')) {
117 $kconfig{$config} = $val;
118 } elsif (!$override) {
119 $kconfig{$config} or $kconfig{$config} = 'n';
126 foreach my $kconfig (sort keys %kconfig) {
127 if ($kconfig{$kconfig} eq 'n') {
128 print "# $kconfig is not set\n";
130 print "$kconfig=$kconfig{$kconfig}\n";
135 sub merge_package_lists($$) {
141 foreach my $pkg (@$list1, @$list2) {
144 foreach my $pkg (keys %pkgs) {
145 push @l, $pkg unless ($pkg =~ /^-/ or $pkgs{"-$pkg"});
150 sub target_config_features(@) {
153 while ($_ = shift @_) {
154 /broken/ and $ret .= "\tdepends BROKEN\n";
155 /audio/ and $ret .= "\tselect AUDIO_SUPPORT\n";
156 /display/ and $ret .= "\tselect DISPLAY_SUPPORT\n";
157 /gpio/ and $ret .= "\tselect GPIO_SUPPORT\n";
158 /pci/ and $ret .= "\tselect PCI_SUPPORT\n";
159 /pcie/ and $ret .= "\tselect PCIE_SUPPORT\n";
160 /usb/ and $ret .= "\tselect USB_SUPPORT\n";
161 /usbgadget/ and $ret .= "\tselect USB_GADGET_SUPPORT\n";
162 /pcmcia/ and $ret .= "\tselect PCMCIA_SUPPORT\n";
163 /squashfs/ and $ret .= "\tselect USES_SQUASHFS\n";
164 /jffs2/ and $ret .= "\tselect USES_JFFS2\n";
165 /ext4/ and $ret .= "\tselect USES_EXT4\n";
166 /targz/ and $ret .= "\tselect USES_TARGZ\n";
167 /cpiogz/ and $ret .= "\tselect USES_CPIOGZ\n";
168 /ubifs/ and $ret .= "\tselect USES_UBIFS\n";
169 /fpu/ and $ret .= "\tselect HAS_FPU\n";
170 /spe_fpu/ and $ret .= "\tselect HAS_SPE_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 if (substr($v,0,2) eq "2_") {
192 $v =~ /(\d+_\d+_\d+)(_\d+)?/ and $v = $1;
194 $v =~ /(\d+_\d+)(_\d+)?/ and $v = $1;
199 sub print_target($) {
201 my $features = target_config_features(@{$target->{features}});
202 my $help = $target->{desc};
207 if ($help =~ /\w+/) {
208 $help =~ s/^\s*/\t /mg;
209 $help = "\thelp\n$help";
214 my $v = kver($target->{version});
215 if (@{$target->{subtargets}} == 0) {
217 config TARGET_$target->{conf}
218 bool "$target->{name}"
224 config TARGET_$target->{conf}
225 bool "$target->{name}"
228 if ($target->{subtarget}) {
229 $confstr .= "\tdepends TARGET_$target->{boardconf}\n";
231 if (@{$target->{subtargets}} > 0) {
232 $confstr .= "\tselect HAS_SUBTARGETS\n";
234 $confstr .= $features;
237 if ($target->{arch} =~ /\w/) {
238 $confstr .= "\tselect $target->{arch}\n";
240 foreach my $dep (@{$target->{depends}}) {
241 my $mode = "depends";
245 $dep =~ /^([@\+\-]+)(.+)$/;
249 next if $name =~ /:/;
250 $flags =~ /-/ and $mode = "deselect";
251 $flags =~ /\+/ and $mode = "select";
252 $flags =~ /@/ and $confstr .= "\t$mode $name\n";
254 $confstr .= "$help\n\n";
258 sub gen_target_config() {
259 my @target = parse_target_metadata();
262 my @target_sort = sort {
263 target_name($a) cmp target_name($b);
269 prompt "Target System"
270 default TARGET_brcm47xx
275 foreach my $target (@target_sort) {
276 next if $target->{subtarget};
277 print_target($target);
284 prompt "Subtarget" if HAS_SUBTARGETS
287 foreach my $target (@target) {
288 next unless $target->{subtarget};
289 print_target($target);
296 prompt "Target Profile"
300 foreach my $target (@target) {
301 my $profiles = $target->{profiles};
303 foreach my $profile (@$profiles) {
305 config TARGET_$target->{conf}_$profile->{id}
306 bool "$profile->{name}"
307 depends TARGET_$target->{conf}
310 $profile->{kconfig} and print "\tselect PROFILE_KCONFIG\n";
311 my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
312 foreach my $pkg (@pkglist) {
313 print "\tselect DEFAULT_$pkg\n";
316 my $help = $profile->{desc};
317 if ($help =~ /\w+/) {
318 $help =~ s/^\s*/\t /mg;
319 $help = "\thelp\n$help";
330 config HAS_SUBTARGETS
337 foreach my $target (@target) {
338 $target->{subtarget} or print "\t\tdefault \"".$target->{board}."\" if TARGET_".$target->{conf}."\n";
341 config TARGET_ARCH_PACKAGES
345 foreach my $target (@target) {
346 next if @{$target->{subtargets}} > 0;
347 print "\t\tdefault \"".($target->{arch_packages} || $target->{board})."\" if TARGET_".$target->{conf}."\n";
351 config DEFAULT_TARGET_OPTIMIZATION
354 foreach my $target (@target) {
355 next if @{$target->{subtargets}} > 0;
356 print "\tdefault \"".$target->{cflags}."\" if TARGET_".$target->{conf}."\n";
358 print "\tdefault \"-Os -pipe -funit-at-a-time\"\n";
361 foreach my $target (@target) {
362 my $v = kver($target->{version});
372 foreach my $def (sort keys %defaults) {
373 print "\tconfig DEFAULT_".$def."\n";
374 print "\t\tbool\n\n";
379 sub __find_package_dep($$) {
382 my $deps = ($pkg->{vdepends} or $pkg->{depends});
384 return 0 unless defined $deps;
385 foreach my $dep (@{$deps}) {
386 next if $dep_check{$dep};
387 $dep_check{$dep} = 1;
388 return 1 if $dep eq $name;
389 return 1 if ($package{$dep} and (__find_package_dep($package{$dep},$name) == 1));
394 # wrapper to avoid infinite recursion
395 sub find_package_dep($$) {
400 return __find_package_dep($pkg, $name);
403 sub package_depends($$) {
408 return 0 if ($a->{submenu} ne $b->{submenu});
409 if (find_package_dep($a, $b->{name}) == 1) {
411 } elsif (find_package_dep($b, $a->{name}) == 1) {
422 my $only_dep = shift;
426 my $parent_condition = shift;
431 my @depends = @$depends;
432 foreach my $depend (@depends) {
435 $depend =~ s/^([@\+]+)// and $flags = $1;
437 my $condition = $parent_condition;
439 next if $condition eq $depend;
440 next if $seen->{"$parent_condition:$depend"};
441 $seen->{"$parent_condition:$depend"} = 1;
442 if ($depend =~ /^(.+):(.+)$/) {
443 if ($1 ne "PACKAGE_$pkgname") {
445 $condition = "$condition && $1";
452 next if $package{$depend} and $package{$depend}->{buildonly};
453 if ($vdep = $package{$depend}->{vdepends}) {
454 $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
456 $flags =~ /\+/ and do {
457 # Menuconfig will not treat 'select FOO' as a real dependency
458 # thus if FOO depends on other config options, these dependencies
459 # will not be checked. To fix this, we simply emit all of FOO's
460 # depends here as well.
461 $package{$depend} and mconf_depends($pkgname, $package{$depend}->{depends}, 1, $dep, $seen, $condition);
466 $flags =~ /@/ or $depend = "PACKAGE_$depend";
468 if ($m =~ /select/) {
469 $depend = "$depend if $condition";
471 $depend = "!($condition) || $depend";
475 $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
477 foreach my $depend (keys %$dep) {
478 my $m = $dep->{$depend};
479 $res .= "\t\t$m $depend\n";
484 sub print_package_config_category($) {
489 return unless $category{$cat};
491 print "menu \"$cat\"\n\n";
492 my %spkg = %{$category{$cat}};
494 foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
495 foreach my $pkg (@{$spkg{$spkg}}) {
496 next if $pkg->{buildonly};
497 my $menu = $pkg->{submenu};
499 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
503 $menus{$menu} or $menus{$menu} = [];
504 push @{$menus{$menu}}, $pkg;
508 ($a eq 'undef' ? 1 : 0) or
509 ($b eq 'undef' ? -1 : 0) or
513 foreach my $menu (@menus) {
515 package_depends($a, $b) or
516 ($a->{name} cmp $b->{name})
518 if ($menu ne 'undef') {
519 $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
520 print "menu \"$menu\"\n";
522 foreach my $pkg (@pkgs) {
523 my $title = $pkg->{name};
524 my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
526 $title .= ("." x $c). " ". $pkg->{title};
529 $pkg->{menu} and print "menu";
530 print "config PACKAGE_".$pkg->{name}."\n";
531 print "\t\t".($pkg->{tristate} ? 'tristate' : 'bool')." \"$title\"\n";
532 print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
533 foreach my $default (split /\s*,\s*/, $pkg->{default}) {
534 print "\t\tdefault $default\n";
536 print mconf_depends($pkg->{name}, $pkg->{depends}, 0);
537 print mconf_depends($pkg->{name}, $pkg->{mdepends}, 0);
539 print $pkg->{description};
542 $pkg->{config} and print $pkg->{config}."\n";
544 if ($menu ne 'undef') {
546 $menu_dep{$menu} and print "endif\n";
551 undef $category{$cat};
554 sub print_package_features() {
555 keys %features > 0 or return;
556 print "menu \"Package features\"\n";
557 foreach my $n (keys %features) {
558 my @features = sort { $b->{priority} <=> $a->{priority} or $a->{title} cmp $b->{title} } @{$features{$n}};
561 prompt "$features[0]->{target_title}"
562 default FEATURE_$features[0]->{name}
565 foreach my $feature (@features) {
567 config FEATURE_$feature->{name}
568 bool "$feature->{title}"
570 $feature->{description} =~ /\w/ and do {
571 print "\t\thelp\n".$feature->{description}."\n";
579 sub gen_package_config() {
580 parse_package_metadata($ARGV[0]) or exit 1;
581 print "menuconfig IMAGEOPT\n\tbool \"Image configuration\"\n\tdefault n\n";
582 foreach my $preconfig (keys %preconfig) {
583 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
584 my $conf = $preconfig{$preconfig}->{$cfg}->{id};
587 config UCI_PRECONFIG_$conf
588 string "$preconfig{$preconfig}->{$cfg}->{label}" if IMAGEOPT
589 depends PACKAGE_$preconfig
590 default "$preconfig{$preconfig}->{$cfg}->{default}"
595 print "source \"package/*/image-config.in\"\n";
596 if (scalar glob "package/feeds/*/*/image-config.in") {
597 print "source \"package/feeds/*/*/image-config.in\"\n";
599 print_package_features();
600 print_package_config_category 'Base system';
601 foreach my $cat (keys %category) {
602 print_package_config_category $cat;
606 sub get_conditional_dep($$) {
607 my $condition = shift;
610 if ($condition =~ /^!(.+)/) {
611 return "\$(if \$(CONFIG_$1),,$depstr)";
613 return "\$(if \$(CONFIG_$condition),$depstr)";
620 sub gen_package_mk() {
626 parse_package_metadata($ARGV[0]) or exit 1;
627 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
629 my $pkg = $package{$name};
632 next if defined $pkg->{vdepends};
635 $conf{$pkg->{src}} or do {
637 $conf{$pkg->{src}} = 1;
640 $config = "\$(CONFIG_PACKAGE_$name)"
643 $pkg->{buildonly} and $config = "";
644 print "package-$config += $pkg->{subdir}$pkg->{src}\n";
645 if ($pkg->{variant}) {
646 if (!defined($done{$pkg->{src}})) {
647 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/default-variant := $pkg->{variant}\n";
649 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/variants += \$(if $config,$pkg->{variant})\n"
651 $pkg->{prereq} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
654 next if $done{$pkg->{src}};
655 $done{$pkg->{src}} = 1;
657 if (@{$pkg->{buildtypes}} > 0) {
658 print "buildtypes-$pkg->{subdir}$pkg->{src} = ".join(' ', @{$pkg->{buildtypes}})."\n";
661 foreach my $spkg (@{$srcpackage{$pkg->{src}}}) {
662 foreach my $dep (@{$spkg->{depends}}, @{$spkg->{builddepends}}) {
669 foreach my $type (@{$pkg->{buildtypes}}) {
673 next unless $pkg->{"builddepends/$type"};
674 foreach my $dep (@{$pkg->{"builddepends/$type"}}) {
678 if ($dep =~ /^(.+):(.+)/) {
682 if ($dep =~ /^(.+)(\/.+)/) {
688 my $pkg_dep = $package{$dep};
689 if (defined($pkg_dep) && defined($pkg_dep->{src})) {
690 $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
691 } elsif (defined($srcpackage{$dep})) {
692 $idx = $subdir{$dep}.$dep;
696 my $depstr = "\$(curdir)/$idx$suffix/compile";
697 my $depline = get_conditional_dep($condition, $depstr);
699 $deplines{$dep} = $depline;
702 my $depline = join(" ", values %deplines);
704 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/$type/compile += $depline\n";
710 foreach my $deps (@srcdeps) {
716 if ($deps =~ /^(.+):(.+)/) {
720 if ($deps =~ /^(.+)(\/.+)/) {
725 my $pkg_dep = $package{$deps};
728 if ($pkg_dep->{vdepends}) {
729 @deps = @{$pkg_dep->{vdepends}};
734 foreach my $dep (@deps) {
735 $pkg_dep = $package{$deps};
736 if (defined $pkg_dep->{src}) {
737 ($pkg->{src} ne $pkg_dep->{src}.$suffix) and $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
738 } elsif (defined($srcpackage{$dep})) {
739 $idx = $subdir{$dep}.$dep;
742 undef $idx if $idx =~ /^(kernel)|(base-files)$/;
745 next if $pkg->{src} eq $pkg_dep->{src}.$suffix;
746 next if $dep{$condition.":".$pkg->{src}."->".$idx};
747 next if $dep{$pkg->{src}."->($dep)".$idx} and $pkg_dep->{vdepends};
750 if ($pkg_dep->{vdepends}) {
751 $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
752 $dep{$pkg->{src}."->($dep)".$idx} = 1;
754 $depstr = "\$(curdir)/$idx/compile";
755 $dep{$pkg->{src}."->".$idx} = 1;
757 $depline = get_conditional_dep($condition, $depstr);
759 $deplines{$idx.$dep} = $depline;
764 my $depline = join(" ", values %deplines);
766 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/compile += $depline\n";
773 foreach my $preconfig (keys %preconfig) {
775 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
776 my $conf = $preconfig{$preconfig}->{$cfg}->{id};
778 $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
783 ifndef DUMP_TARGET_DB
784 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
789 ifneq (\$(IMAGEOPT)\$(CONFIG_IMAGEOPT),)
790 package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
798 sub gen_package_source() {
799 parse_package_metadata($ARGV[0]) or exit 1;
800 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
801 my $pkg = $package{$name};
802 if ($pkg->{name} && $pkg->{source}) {
803 print "$pkg->{name}: ";
804 print "$pkg->{source}\n";
809 sub parse_command() {
810 my $cmd = shift @ARGV;
812 /^target_config$/ and return gen_target_config();
813 /^package_mk$/ and return gen_package_mk();
814 /^package_config$/ and return gen_package_config();
815 /^kconfig/ and return gen_kconfig_overrides();
816 /^package_source$/ and return gen_package_source();
820 $0 target_config [file] Target metadata in Kconfig format
821 $0 package_mk [file] Package metadata in makefile format
822 $0 package_config [file] Package metadata in Kconfig format
823 $0 kconfig [file] [config] Kernel config overrides
824 $0 package_source [file] Package source file information