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-Subtarget:\s*(.+)\s*$/ and $target->{def_subtarget} = $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 next if $kconfig{$config} eq 'y';
119 $kconfig{$config} = $val;
120 } elsif (!$override) {
121 $kconfig{$config} or $kconfig{$config} = 'n';
128 foreach my $kconfig (sort keys %kconfig) {
129 if ($kconfig{$kconfig} eq 'n') {
130 print "# $kconfig is not set\n";
132 print "$kconfig=$kconfig{$kconfig}\n";
137 sub merge_package_lists($$) {
143 foreach my $pkg (@$list1, @$list2) {
146 foreach my $pkg (keys %pkgs) {
147 push @l, $pkg unless ($pkg =~ /^-/ or $pkgs{"-$pkg"});
152 sub target_config_features(@) {
155 while ($_ = shift @_) {
156 /arm_v(\w+)/ and $ret .= "\tselect arm_v$1\n";
157 /broken/ and $ret .= "\tdepends on BROKEN\n";
158 /audio/ and $ret .= "\tselect AUDIO_SUPPORT\n";
159 /display/ and $ret .= "\tselect DISPLAY_SUPPORT\n";
160 /gpio/ and $ret .= "\tselect GPIO_SUPPORT\n";
161 /pci/ and $ret .= "\tselect PCI_SUPPORT\n";
162 /pcie/ and $ret .= "\tselect PCIE_SUPPORT\n";
163 /usb/ and $ret .= "\tselect USB_SUPPORT\n";
164 /usbgadget/ and $ret .= "\tselect USB_GADGET_SUPPORT\n";
165 /pcmcia/ and $ret .= "\tselect PCMCIA_SUPPORT\n";
166 /rtc/ and $ret .= "\tselect RTC_SUPPORT\n";
167 /squashfs/ and $ret .= "\tselect USES_SQUASHFS\n";
168 /jffs2$/ and $ret .= "\tselect USES_JFFS2\n";
169 /jffs2_nand/ and $ret .= "\tselect USES_JFFS2_NAND\n";
170 /ext4/ and $ret .= "\tselect USES_EXT4\n";
171 /targz/ and $ret .= "\tselect USES_TARGZ\n";
172 /cpiogz/ and $ret .= "\tselect USES_CPIOGZ\n";
173 /ubifs/ and $ret .= "\tselect USES_UBIFS\n";
174 /fpu/ and $ret .= "\tselect HAS_FPU\n";
175 /spe_fpu/ and $ret .= "\tselect HAS_SPE_FPU\n";
176 /ramdisk/ and $ret .= "\tselect USES_INITRAMFS\n";
177 /powerpc64/ and $ret .= "\tselect powerpc64\n";
178 /nommu/ and $ret .= "\tselect NOMMU\n";
179 /mips16/ and $ret .= "\tselect HAS_MIPS16\n";
180 /rfkill/ and $ret .= "\tselect RFKILL_SUPPORT\n";
187 my $parent = $target->{parent};
189 return $target->{parent}->{name}." - ".$target->{name};
191 return $target->{name};
198 if (substr($v,0,2) eq "2_") {
199 $v =~ /(\d+_\d+_\d+)(_\d+)?/ and $v = $1;
201 $v =~ /(\d+_\d+)(_\d+)?/ and $v = $1;
206 sub print_target($) {
208 my $features = target_config_features(@{$target->{features}});
209 my $help = $target->{desc};
214 if ($help =~ /\w+/) {
215 $help =~ s/^\s*/\t /mg;
216 $help = "\thelp\n$help";
221 my $v = kver($target->{version});
222 if (@{$target->{subtargets}} == 0) {
224 config TARGET_$target->{conf}
225 bool "$target->{name}"
231 config TARGET_$target->{conf}
232 bool "$target->{name}"
235 if ($target->{subtarget}) {
236 $confstr .= "\tdepends on TARGET_$target->{boardconf}\n";
238 if (@{$target->{subtargets}} > 0) {
239 $confstr .= "\tselect HAS_SUBTARGETS\n";
240 grep { /broken/ } @{$target->{features}} and $confstr .= "\tdepends on BROKEN\n";
242 $confstr .= $features;
245 if ($target->{arch} =~ /\w/) {
246 $confstr .= "\tselect $target->{arch}\n";
248 foreach my $dep (@{$target->{depends}}) {
249 my $mode = "depends on";
253 $dep =~ /^([@\+\-]+)(.+)$/;
257 next if $name =~ /:/;
258 $flags =~ /-/ and $mode = "deselect";
259 $flags =~ /\+/ and $mode = "select";
260 $flags =~ /@/ and $confstr .= "\t$mode $name\n";
262 $confstr .= "$help\n\n";
266 sub gen_target_config() {
267 my @target = parse_target_metadata();
270 my @target_sort = sort {
271 target_name($a) cmp target_name($b);
277 prompt "Target System"
278 default TARGET_ar71xx
283 foreach my $target (@target_sort) {
284 next if $target->{subtarget};
285 print_target($target);
292 prompt "Subtarget" if HAS_SUBTARGETS
294 foreach my $target (@target) {
295 next unless $target->{def_subtarget};
297 default TARGET_$target->{conf}_$target->{def_subtarget} if TARGET_$target->{conf}
303 foreach my $target (@target) {
304 next unless $target->{subtarget};
305 print_target($target);
312 prompt "Target Profile"
316 foreach my $target (@target) {
317 my $profiles = $target->{profiles};
319 foreach my $profile (@$profiles) {
321 config TARGET_$target->{conf}_$profile->{id}
322 bool "$profile->{name}"
323 depends on TARGET_$target->{conf}
326 $profile->{kconfig} and print "\tselect PROFILE_KCONFIG\n";
327 my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
328 foreach my $pkg (@pkglist) {
329 print "\tselect DEFAULT_$pkg\n";
332 my $help = $profile->{desc};
333 if ($help =~ /\w+/) {
334 $help =~ s/^\s*/\t /mg;
335 $help = "\thelp\n$help";
346 config HAS_SUBTARGETS
353 foreach my $target (@target) {
354 $target->{subtarget} or print "\t\tdefault \"".$target->{board}."\" if TARGET_".$target->{conf}."\n";
357 config TARGET_ARCH_PACKAGES
361 foreach my $target (@target) {
362 next if @{$target->{subtargets}} > 0;
363 print "\t\tdefault \"".($target->{arch_packages} || $target->{board})."\" if TARGET_".$target->{conf}."\n";
367 config DEFAULT_TARGET_OPTIMIZATION
370 foreach my $target (@target) {
371 next if @{$target->{subtargets}} > 0;
372 print "\tdefault \"".$target->{cflags}."\" if TARGET_".$target->{conf}."\n";
374 print "\tdefault \"-Os -pipe -funit-at-a-time\"\n";
377 foreach my $target (@target) {
378 my $v = kver($target->{version});
388 foreach my $def (sort keys %defaults) {
389 print "\tconfig DEFAULT_".$def."\n";
390 print "\t\tbool\n\n";
395 sub __find_package_dep($$) {
398 my $deps = ($pkg->{vdepends} or $pkg->{depends});
400 return 0 unless defined $deps;
401 foreach my $dep (@{$deps}) {
402 next if $dep_check{$dep};
403 $dep_check{$dep} = 1;
404 return 1 if $dep eq $name;
405 return 1 if ($package{$dep} and (__find_package_dep($package{$dep},$name) == 1));
410 # wrapper to avoid infinite recursion
411 sub find_package_dep($$) {
416 return __find_package_dep($pkg, $name);
419 sub package_depends($$) {
424 return 0 if ($a->{submenu} ne $b->{submenu});
425 if (find_package_dep($a, $b->{name}) == 1) {
427 } elsif (find_package_dep($b, $a->{name}) == 1) {
438 my $only_dep = shift;
442 my $parent_condition = shift;
447 my @depends = @$depends;
448 foreach my $depend (@depends) {
449 my $m = "depends on";
451 $depend =~ s/^([@\+]+)// and $flags = $1;
453 my $condition = $parent_condition;
455 next if $condition eq $depend;
456 next if $seen->{"$parent_condition:$depend"};
457 next if $seen->{":$depend"};
458 $seen->{"$parent_condition:$depend"} = 1;
459 if ($depend =~ /^(.+):(.+)$/) {
460 if ($1 ne "PACKAGE_$pkgname") {
462 $condition = "$condition && $1";
469 next if $package{$depend} and $package{$depend}->{buildonly};
470 if ($vdep = $package{$depend}->{vdepends}) {
471 $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
473 $flags =~ /\+/ and do {
474 # Menuconfig will not treat 'select FOO' as a real dependency
475 # thus if FOO depends on other config options, these dependencies
476 # will not be checked. To fix this, we simply emit all of FOO's
477 # depends here as well.
478 $package{$depend} and mconf_depends($pkgname, $package{$depend}->{depends}, 1, $dep, $seen, $condition);
483 $flags =~ /@/ or $depend = "PACKAGE_$depend";
485 if ($m =~ /select/) {
486 next if $depend eq $condition;
487 $depend = "$depend if $condition";
489 $depend = "!($condition) || $depend";
493 $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
495 foreach my $depend (keys %$dep) {
496 my $m = $dep->{$depend};
497 $res .= "\t\t$m $depend\n";
502 sub print_package_config_category($) {
507 return unless $category{$cat};
509 print "menu \"$cat\"\n\n";
510 my %spkg = %{$category{$cat}};
512 foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
513 foreach my $pkg (@{$spkg{$spkg}}) {
514 next if $pkg->{buildonly};
515 my $menu = $pkg->{submenu};
517 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
521 $menus{$menu} or $menus{$menu} = [];
522 push @{$menus{$menu}}, $pkg;
526 ($a eq 'undef' ? 1 : 0) or
527 ($b eq 'undef' ? -1 : 0) or
531 foreach my $menu (@menus) {
533 package_depends($a, $b) or
534 ($a->{name} cmp $b->{name})
536 if ($menu ne 'undef') {
537 $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
538 print "menu \"$menu\"\n";
540 foreach my $pkg (@pkgs) {
541 my $title = $pkg->{name};
542 my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
544 $title .= ("." x $c). " ". $pkg->{title};
546 $title = "\"$title\"";
548 $pkg->{menu} and print "menu";
549 print "config PACKAGE_".$pkg->{name}."\n";
550 $pkg->{hidden} and $title = "";
551 print "\t\t".($pkg->{tristate} ? 'tristate' : 'bool')." $title\n";
552 print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
553 unless ($pkg->{hidden}) {
554 $pkg->{default} ||= "m if ALL";
556 if ($pkg->{default}) {
557 foreach my $default (split /\s*,\s*/, $pkg->{default}) {
558 print "\t\tdefault $default\n";
561 print mconf_depends($pkg->{name}, $pkg->{depends}, 0);
562 print mconf_depends($pkg->{name}, $pkg->{mdepends}, 0);
564 print $pkg->{description};
567 $pkg->{config} and print $pkg->{config}."\n";
569 if ($menu ne 'undef') {
571 $menu_dep{$menu} and print "endif\n";
576 undef $category{$cat};
579 sub print_package_features() {
580 keys %features > 0 or return;
581 print "menu \"Package features\"\n";
582 foreach my $n (keys %features) {
583 my @features = sort { $b->{priority} <=> $a->{priority} or $a->{title} cmp $b->{title} } @{$features{$n}};
586 prompt "$features[0]->{target_title}"
587 default FEATURE_$features[0]->{name}
590 foreach my $feature (@features) {
592 config FEATURE_$feature->{name}
593 bool "$feature->{title}"
595 $feature->{description} =~ /\w/ and do {
596 print "\t\thelp\n".$feature->{description}."\n";
604 sub gen_package_config() {
605 parse_package_metadata($ARGV[0]) or exit 1;
606 print "menuconfig IMAGEOPT\n\tbool \"Image configuration\"\n\tdefault n\n";
607 foreach my $preconfig (keys %preconfig) {
608 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
609 my $conf = $preconfig{$preconfig}->{$cfg}->{id};
612 config UCI_PRECONFIG_$conf
613 string "$preconfig{$preconfig}->{$cfg}->{label}" if IMAGEOPT
614 depends on PACKAGE_$preconfig
615 default "$preconfig{$preconfig}->{$cfg}->{default}"
620 print "source \"package/*/image-config.in\"\n";
621 if (scalar glob "package/feeds/*/*/image-config.in") {
622 print "source \"package/feeds/*/*/image-config.in\"\n";
624 print_package_features();
625 print_package_config_category 'Base system';
626 foreach my $cat (sort {uc($a) cmp uc($b)} keys %category) {
627 print_package_config_category $cat;
631 sub get_conditional_dep($$) {
632 my $condition = shift;
635 if ($condition =~ /^!(.+)/) {
636 return "\$(if \$(CONFIG_$1),,$depstr)";
638 return "\$(if \$(CONFIG_$condition),$depstr)";
645 sub gen_package_mk() {
651 parse_package_metadata($ARGV[0]) or exit 1;
652 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
654 my $pkg = $package{$name};
657 next if defined $pkg->{vdepends};
660 $conf{$pkg->{src}} or do {
662 $conf{$pkg->{src}} = 1;
665 $config = "\$(CONFIG_PACKAGE_$name)"
668 $pkg->{buildonly} and $config = "";
669 print "package-$config += $pkg->{subdir}$pkg->{src}\n";
670 if ($pkg->{variant}) {
671 if (!defined($done{$pkg->{src}})) {
672 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/default-variant := $pkg->{variant}\n";
674 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/variants += \$(if $config,$pkg->{variant})\n"
676 $pkg->{prereq} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
679 next if $done{$pkg->{src}};
680 $done{$pkg->{src}} = 1;
682 if (@{$pkg->{buildtypes}} > 0) {
683 print "buildtypes-$pkg->{subdir}$pkg->{src} = ".join(' ', @{$pkg->{buildtypes}})."\n";
686 foreach my $spkg (@{$srcpackage{$pkg->{src}}}) {
687 foreach my $dep (@{$spkg->{depends}}, @{$spkg->{builddepends}}) {
694 foreach my $type (@{$pkg->{buildtypes}}) {
698 next unless $pkg->{"builddepends/$type"};
699 foreach my $dep (@{$pkg->{"builddepends/$type"}}) {
703 if ($dep =~ /^(.+):(.+)/) {
707 if ($dep =~ /^(.+)(\/.+)/) {
713 my $pkg_dep = $package{$dep};
714 if (defined($pkg_dep) && defined($pkg_dep->{src})) {
715 $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
716 } elsif (defined($srcpackage{$dep})) {
717 $idx = $subdir{$dep}.$dep;
721 my $depstr = "\$(curdir)/$idx$suffix/compile";
722 my $depline = get_conditional_dep($condition, $depstr);
724 $deplines{$depline}++;
727 my $depline = join(" ", sort keys %deplines);
729 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/$type/compile += $depline\n";
735 foreach my $deps (@srcdeps) {
741 if ($deps =~ /^(.+):(.+)/) {
745 if ($deps =~ /^(.+)(\/.+)/) {
750 my $pkg_dep = $package{$deps};
753 if ($pkg_dep->{vdepends}) {
754 @deps = @{$pkg_dep->{vdepends}};
759 foreach my $dep (@deps) {
760 $pkg_dep = $package{$deps};
761 if (defined $pkg_dep->{src}) {
762 ($pkg->{src} ne $pkg_dep->{src}.$suffix) and $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
763 } elsif (defined($srcpackage{$dep})) {
764 $idx = $subdir{$dep}.$dep;
767 undef $idx if $idx eq 'base-files';
770 next if $pkg->{src} eq $pkg_dep->{src}.$suffix;
771 next if $dep{$condition.":".$pkg->{src}."->".$idx};
772 next if $dep{$pkg->{src}."->($dep)".$idx} and $pkg_dep->{vdepends};
775 if ($pkg_dep->{vdepends}) {
776 $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
777 $dep{$pkg->{src}."->($dep)".$idx} = 1;
779 $depstr = "\$(curdir)/$idx/compile";
780 $dep{$pkg->{src}."->".$idx} = 1;
782 $depline = get_conditional_dep($condition, $depstr);
784 $deplines{$depline}++;
789 my $depline = join(" ", sort keys %deplines);
791 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/compile += $depline\n";
798 foreach my $preconfig (keys %preconfig) {
800 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
801 my $conf = $preconfig{$preconfig}->{$cfg}->{id};
803 $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
808 ifndef DUMP_TARGET_DB
809 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
814 ifneq (\$(IMAGEOPT)\$(CONFIG_IMAGEOPT),)
815 package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
823 sub gen_package_source() {
824 parse_package_metadata($ARGV[0]) or exit 1;
825 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
826 my $pkg = $package{$name};
827 if ($pkg->{name} && $pkg->{source}) {
828 print "$pkg->{name}: ";
829 print "$pkg->{source}\n";
834 sub parse_command() {
835 my $cmd = shift @ARGV;
837 /^target_config$/ and return gen_target_config();
838 /^package_mk$/ and return gen_package_mk();
839 /^package_config$/ and return gen_package_config();
840 /^kconfig/ and return gen_kconfig_overrides();
841 /^package_source$/ and return gen_package_source();
845 $0 target_config [file] Target metadata in Kconfig format
846 $0 package_mk [file] Package metadata in makefile format
847 $0 package_config [file] Package metadata in Kconfig format
848 $0 kconfig [file] [config] Kernel config overrides
849 $0 package_source [file] Package source file information