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 next if @{$target->{subtargets}} > 0;
79 @{$target->{profiles}} > 0 or $target->{profiles} = [
90 sub gen_kconfig_overrides() {
94 my $pkginfo = shift @ARGV;
95 my $cfgfile = shift @ARGV;
97 # parameter 2: build system config
98 open FILE, "<$cfgfile" or return;
100 /^(CONFIG_.+?)=(.+)$/ and $config{$1} = 1;
104 # parameter 1: package metadata
105 open FILE, "<$pkginfo" or return;
107 /^Package:\s*(.+?)\s*$/ and $package = $1;
108 /^Kernel-Config:\s*(.+?)\s*$/ and do {
109 my @config = split /\s+/, $1;
110 foreach my $config (@config) {
113 if ($config =~ /^(.+?)=(.+)$/) {
118 if ($config{"CONFIG_PACKAGE_$package"} and ($config ne 'n')) {
119 next if $kconfig{$config} eq 'y';
120 $kconfig{$config} = $val;
121 } elsif (!$override) {
122 $kconfig{$config} or $kconfig{$config} = 'n';
129 foreach my $kconfig (sort keys %kconfig) {
130 if ($kconfig{$kconfig} eq 'n') {
131 print "# $kconfig is not set\n";
133 print "$kconfig=$kconfig{$kconfig}\n";
138 sub merge_package_lists($$) {
144 foreach my $pkg (@$list1, @$list2) {
147 foreach my $pkg (keys %pkgs) {
148 push @l, $pkg unless ($pkg =~ /^-/ or $pkgs{"-$pkg"});
153 sub target_config_features(@) {
156 while ($_ = shift @_) {
157 /arm_v(\w+)/ and $ret .= "\tselect arm_v$1\n";
158 /broken/ and $ret .= "\tdepends on BROKEN\n";
159 /audio/ and $ret .= "\tselect AUDIO_SUPPORT\n";
160 /display/ and $ret .= "\tselect DISPLAY_SUPPORT\n";
161 /gpio/ and $ret .= "\tselect GPIO_SUPPORT\n";
162 /pci/ and $ret .= "\tselect PCI_SUPPORT\n";
163 /pcie/ and $ret .= "\tselect PCIE_SUPPORT\n";
164 /usb/ and $ret .= "\tselect USB_SUPPORT\n";
165 /usbgadget/ and $ret .= "\tselect USB_GADGET_SUPPORT\n";
166 /pcmcia/ and $ret .= "\tselect PCMCIA_SUPPORT\n";
167 /rtc/ and $ret .= "\tselect RTC_SUPPORT\n";
168 /squashfs/ and $ret .= "\tselect USES_SQUASHFS\n";
169 /jffs2$/ and $ret .= "\tselect USES_JFFS2\n";
170 /jffs2_nand/ and $ret .= "\tselect USES_JFFS2_NAND\n";
171 /ext4/ and $ret .= "\tselect USES_EXT4\n";
172 /targz/ and $ret .= "\tselect USES_TARGZ\n";
173 /cpiogz/ and $ret .= "\tselect USES_CPIOGZ\n";
174 /ubifs/ and $ret .= "\tselect USES_UBIFS\n";
175 /fpu/ and $ret .= "\tselect HAS_FPU\n";
176 /spe_fpu/ and $ret .= "\tselect HAS_SPE_FPU\n";
177 /ramdisk/ and $ret .= "\tselect USES_INITRAMFS\n";
178 /powerpc64/ and $ret .= "\tselect powerpc64\n";
179 /nommu/ and $ret .= "\tselect NOMMU\n";
180 /mips16/ and $ret .= "\tselect HAS_MIPS16\n";
181 /rfkill/ and $ret .= "\tselect RFKILL_SUPPORT\n";
188 my $parent = $target->{parent};
190 return $target->{parent}->{name}." - ".$target->{name};
192 return $target->{name};
199 if (substr($v,0,2) eq "2_") {
200 $v =~ /(\d+_\d+_\d+)(_\d+)?/ and $v = $1;
202 $v =~ /(\d+_\d+)(_\d+)?/ and $v = $1;
207 sub print_target($) {
209 my $features = target_config_features(@{$target->{features}});
210 my $help = $target->{desc};
215 if ($help =~ /\w+/) {
216 $help =~ s/^\s*/\t /mg;
217 $help = "\thelp\n$help";
222 my $v = kver($target->{version});
223 if (@{$target->{subtargets}} == 0) {
225 config TARGET_$target->{conf}
226 bool "$target->{name}"
232 config TARGET_$target->{conf}
233 bool "$target->{name}"
236 if ($target->{subtarget}) {
237 $confstr .= "\tdepends on TARGET_$target->{boardconf}\n";
239 if (@{$target->{subtargets}} > 0) {
240 $confstr .= "\tselect HAS_SUBTARGETS\n";
241 grep { /broken/ } @{$target->{features}} and $confstr .= "\tdepends on BROKEN\n";
243 $confstr .= $features;
246 if ($target->{arch} =~ /\w/) {
247 $confstr .= "\tselect $target->{arch}\n";
249 foreach my $dep (@{$target->{depends}}) {
250 my $mode = "depends on";
254 $dep =~ /^([@\+\-]+)(.+)$/;
258 next if $name =~ /:/;
259 $flags =~ /-/ and $mode = "deselect";
260 $flags =~ /\+/ and $mode = "select";
261 $flags =~ /@/ and $confstr .= "\t$mode $name\n";
263 $confstr .= "$help\n\n";
267 sub gen_target_config() {
268 my @target = parse_target_metadata();
271 my @target_sort = sort {
272 target_name($a) cmp target_name($b);
278 prompt "Target System"
279 default TARGET_ar71xx
284 foreach my $target (@target_sort) {
285 next if $target->{subtarget};
286 print_target($target);
293 prompt "Subtarget" if HAS_SUBTARGETS
295 foreach my $target (@target) {
296 next unless $target->{def_subtarget};
298 default TARGET_$target->{conf}_$target->{def_subtarget} if TARGET_$target->{conf}
304 foreach my $target (@target) {
305 next unless $target->{subtarget};
306 print_target($target);
313 prompt "Target Profile"
317 foreach my $target (@target) {
318 my $profiles = $target->{profiles};
320 foreach my $profile (@$profiles) {
322 config TARGET_$target->{conf}_$profile->{id}
323 bool "$profile->{name}"
324 depends on TARGET_$target->{conf}
327 $profile->{kconfig} and print "\tselect PROFILE_KCONFIG\n";
328 my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
329 foreach my $pkg (@pkglist) {
330 print "\tselect DEFAULT_$pkg\n";
333 my $help = $profile->{desc};
334 if ($help =~ /\w+/) {
335 $help =~ s/^\s*/\t /mg;
336 $help = "\thelp\n$help";
347 config HAS_SUBTARGETS
354 foreach my $target (@target) {
355 $target->{subtarget} or print "\t\tdefault \"".$target->{board}."\" if TARGET_".$target->{conf}."\n";
358 config TARGET_ARCH_PACKAGES
362 foreach my $target (@target) {
363 next if @{$target->{subtargets}} > 0;
364 print "\t\tdefault \"".($target->{arch_packages} || $target->{board})."\" if TARGET_".$target->{conf}."\n";
368 config DEFAULT_TARGET_OPTIMIZATION
371 foreach my $target (@target) {
372 next if @{$target->{subtargets}} > 0;
373 print "\tdefault \"".$target->{cflags}."\" if TARGET_".$target->{conf}."\n";
375 print "\tdefault \"-Os -pipe -funit-at-a-time\"\n";
381 foreach my $target (@target) {
382 next if @{$target->{subtargets}} > 0;
383 print "\tdefault \"".$target->{cputype}."\" if TARGET_".$target->{conf}."\n";
385 print "\tdefault \"\"\n";
388 foreach my $target (@target) {
389 my $v = kver($target->{version});
399 foreach my $def (sort keys %defaults) {
400 print "\tconfig DEFAULT_".$def."\n";
401 print "\t\tbool\n\n";
406 sub __find_package_dep($$) {
409 my $deps = ($pkg->{vdepends} or $pkg->{depends});
411 return 0 unless defined $deps;
412 foreach my $dep (@{$deps}) {
413 next if $dep_check{$dep};
414 $dep_check{$dep} = 1;
415 return 1 if $dep eq $name;
416 return 1 if ($package{$dep} and (__find_package_dep($package{$dep},$name) == 1));
421 # wrapper to avoid infinite recursion
422 sub find_package_dep($$) {
427 return __find_package_dep($pkg, $name);
430 sub package_depends($$) {
435 return 0 if ($a->{submenu} ne $b->{submenu});
436 if (find_package_dep($a, $b->{name}) == 1) {
438 } elsif (find_package_dep($b, $a->{name}) == 1) {
449 my $only_dep = shift;
453 my $parent_condition = shift;
459 my @depends = @$depends;
460 foreach my $depend (@depends) {
461 my $m = "depends on";
463 $depend =~ s/^([@\+]+)// and $flags = $1;
465 my $condition = $parent_condition;
467 next if $condition eq $depend;
468 next if $seen->{"$parent_condition:$depend"};
469 next if $seen->{":$depend"};
470 $seen->{"$parent_condition:$depend"} = 1;
471 if ($depend =~ /^(.+):(.+)$/) {
472 if ($1 ne "PACKAGE_$pkgname") {
474 $condition = "$condition && $1";
481 next if $package{$depend} and $package{$depend}->{buildonly};
482 if ($vdep = $package{$depend}->{vdepends}) {
483 $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
485 $flags =~ /\+/ and do {
486 # Menuconfig will not treat 'select FOO' as a real dependency
487 # thus if FOO depends on other config options, these dependencies
488 # will not be checked. To fix this, we simply emit all of FOO's
489 # depends here as well.
490 $package{$depend} and push @t_depends, [ $package{$depend}->{depends}, $condition ];
495 $flags =~ /@/ or $depend = "PACKAGE_$depend";
497 if ($m =~ /select/) {
498 next if $depend eq $condition;
499 $depend = "$depend if $condition";
501 $depend = "!($condition) || $depend";
505 $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
508 foreach my $tdep (@t_depends) {
509 mconf_depends($pkgname, $tdep->[0], 1, $dep, $seen, $tdep->[1]);
512 foreach my $depend (keys %$dep) {
513 my $m = $dep->{$depend};
514 $res .= "\t\t$m $depend\n";
519 sub print_package_config_category($) {
524 return unless $category{$cat};
526 print "menu \"$cat\"\n\n";
527 my %spkg = %{$category{$cat}};
529 foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
530 foreach my $pkg (@{$spkg{$spkg}}) {
531 next if $pkg->{buildonly};
532 my $menu = $pkg->{submenu};
534 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
538 $menus{$menu} or $menus{$menu} = [];
539 push @{$menus{$menu}}, $pkg;
543 ($a eq 'undef' ? 1 : 0) or
544 ($b eq 'undef' ? -1 : 0) or
548 foreach my $menu (@menus) {
550 package_depends($a, $b) or
551 ($a->{name} cmp $b->{name})
553 if ($menu ne 'undef') {
554 $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
555 print "menu \"$menu\"\n";
557 foreach my $pkg (@pkgs) {
558 my $title = $pkg->{name};
559 my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
561 $title .= ("." x $c). " ". $pkg->{title};
563 $title = "\"$title\"";
565 $pkg->{menu} and print "menu";
566 print "config PACKAGE_".$pkg->{name}."\n";
567 $pkg->{hidden} and $title = "";
568 print "\t\t".($pkg->{tristate} ? 'tristate' : 'bool')." $title\n";
569 print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
570 unless ($pkg->{hidden}) {
571 $pkg->{default} ||= "m if ALL";
573 if ($pkg->{default}) {
574 foreach my $default (split /\s*,\s*/, $pkg->{default}) {
575 print "\t\tdefault $default\n";
578 print mconf_depends($pkg->{name}, $pkg->{depends}, 0);
579 print mconf_depends($pkg->{name}, $pkg->{mdepends}, 0);
581 print $pkg->{description};
584 $pkg->{config} and print $pkg->{config}."\n";
586 if ($menu ne 'undef') {
588 $menu_dep{$menu} and print "endif\n";
593 undef $category{$cat};
596 sub print_package_features() {
597 keys %features > 0 or return;
598 print "menu \"Package features\"\n";
599 foreach my $n (keys %features) {
600 my @features = sort { $b->{priority} <=> $a->{priority} or $a->{title} cmp $b->{title} } @{$features{$n}};
603 prompt "$features[0]->{target_title}"
604 default FEATURE_$features[0]->{name}
607 foreach my $feature (@features) {
609 config FEATURE_$feature->{name}
610 bool "$feature->{title}"
612 $feature->{description} =~ /\w/ and do {
613 print "\t\thelp\n".$feature->{description}."\n";
621 sub gen_package_config() {
622 parse_package_metadata($ARGV[0]) or exit 1;
623 print "menuconfig IMAGEOPT\n\tbool \"Image configuration\"\n\tdefault n\n";
624 foreach my $preconfig (keys %preconfig) {
625 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
626 my $conf = $preconfig{$preconfig}->{$cfg}->{id};
629 config UCI_PRECONFIG_$conf
630 string "$preconfig{$preconfig}->{$cfg}->{label}" if IMAGEOPT
631 depends on PACKAGE_$preconfig
632 default "$preconfig{$preconfig}->{$cfg}->{default}"
637 print "source \"package/*/image-config.in\"\n";
638 if (scalar glob "package/feeds/*/*/image-config.in") {
639 print "source \"package/feeds/*/*/image-config.in\"\n";
641 print_package_features();
642 print_package_config_category 'Base system';
643 foreach my $cat (sort {uc($a) cmp uc($b)} keys %category) {
644 print_package_config_category $cat;
648 sub get_conditional_dep($$) {
649 my $condition = shift;
652 if ($condition =~ /^!(.+)/) {
653 return "\$(if \$(CONFIG_$1),,$depstr)";
655 return "\$(if \$(CONFIG_$condition),$depstr)";
662 sub gen_package_mk() {
668 parse_package_metadata($ARGV[0]) or exit 1;
669 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
671 my $pkg = $package{$name};
674 next if defined $pkg->{vdepends};
677 $conf{$pkg->{src}} or do {
679 $conf{$pkg->{src}} = 1;
682 $config = "\$(CONFIG_PACKAGE_$name)"
685 $pkg->{buildonly} and $config = "";
686 print "package-$config += $pkg->{subdir}$pkg->{src}\n";
687 if ($pkg->{variant}) {
688 if (!defined($done{$pkg->{src}})) {
689 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/default-variant := $pkg->{variant}\n";
691 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/variants += \$(if $config,$pkg->{variant})\n"
693 $pkg->{prereq} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
696 next if $done{$pkg->{src}};
697 $done{$pkg->{src}} = 1;
699 if (@{$pkg->{buildtypes}} > 0) {
700 print "buildtypes-$pkg->{subdir}$pkg->{src} = ".join(' ', @{$pkg->{buildtypes}})."\n";
703 foreach my $spkg (@{$srcpackage{$pkg->{src}}}) {
704 foreach my $dep (@{$spkg->{depends}}, @{$spkg->{builddepends}}) {
711 foreach my $type (@{$pkg->{buildtypes}}) {
715 next unless $pkg->{"builddepends/$type"};
716 foreach my $dep (@{$pkg->{"builddepends/$type"}}) {
720 if ($dep =~ /^(.+):(.+)/) {
724 if ($dep =~ /^(.+)(\/.+)/) {
730 my $pkg_dep = $package{$dep};
731 if (defined($pkg_dep) && defined($pkg_dep->{src})) {
732 $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
733 } elsif (defined($srcpackage{$dep})) {
734 $idx = $subdir{$dep}.$dep;
738 my $depstr = "\$(curdir)/$idx$suffix/compile";
739 my $depline = get_conditional_dep($condition, $depstr);
741 $deplines{$depline}++;
744 my $depline = join(" ", sort keys %deplines);
746 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/$type/compile += $depline\n";
752 foreach my $deps (@srcdeps) {
758 if ($deps =~ /^(.+):(.+)/) {
762 if ($deps =~ /^(.+)(\/.+)/) {
767 my $pkg_dep = $package{$deps};
770 if ($pkg_dep->{vdepends}) {
771 @deps = @{$pkg_dep->{vdepends}};
776 foreach my $dep (@deps) {
777 $pkg_dep = $package{$deps};
778 if (defined $pkg_dep->{src}) {
779 ($pkg->{src} ne $pkg_dep->{src}.$suffix) and $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
780 } elsif (defined($srcpackage{$dep})) {
781 $idx = $subdir{$dep}.$dep;
784 undef $idx if $idx eq 'base-files';
787 next if $pkg->{src} eq $pkg_dep->{src}.$suffix;
788 next if $dep{$condition.":".$pkg->{src}."->".$idx};
789 next if $dep{$pkg->{src}."->($dep)".$idx} and $pkg_dep->{vdepends};
792 if ($pkg_dep->{vdepends}) {
793 $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
794 $dep{$pkg->{src}."->($dep)".$idx} = 1;
796 $depstr = "\$(curdir)/$idx/compile";
797 $dep{$pkg->{src}."->".$idx} = 1;
799 $depline = get_conditional_dep($condition, $depstr);
801 $deplines{$depline}++;
806 my $depline = join(" ", sort keys %deplines);
808 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/compile += $depline\n";
815 foreach my $preconfig (keys %preconfig) {
817 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
818 my $conf = $preconfig{$preconfig}->{$cfg}->{id};
820 $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
825 ifndef DUMP_TARGET_DB
826 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
831 ifneq (\$(IMAGEOPT)\$(CONFIG_IMAGEOPT),)
832 package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
840 sub gen_package_source() {
841 parse_package_metadata($ARGV[0]) or exit 1;
842 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
843 my $pkg = $package{$name};
844 if ($pkg->{name} && $pkg->{source}) {
845 print "$pkg->{name}: ";
846 print "$pkg->{source}\n";
851 sub parse_command() {
852 my $cmd = shift @ARGV;
854 /^target_config$/ and return gen_target_config();
855 /^package_mk$/ and return gen_package_mk();
856 /^package_config$/ and return gen_package_config();
857 /^kconfig/ and return gen_kconfig_overrides();
858 /^package_source$/ and return gen_package_source();
862 $0 target_config [file] Target metadata in Kconfig format
863 $0 package_mk [file] Package metadata in makefile format
864 $0 package_config [file] Package metadata in Kconfig format
865 $0 kconfig [file] [config] Kernel config overrides
866 $0 package_source [file] Package source file information