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-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 /pcmcia/ and $ret .= "\tselect PCMCIA_SUPPORT\n";
162 /squashfs/ and $ret .= "\tselect USES_SQUASHFS\n";
163 /jffs2/ and $ret .= "\tselect USES_JFFS2\n";
164 /ext2/ and $ret .= "\tselect USES_EXT2\n";
165 /tgz/ and $ret .= "\tselect USES_TGZ\n";
166 /cpiogz/ and $ret .= "\tselect USES_CPIOGZ\n";
167 /ubifs/ and $ret .= "\tselect USES_UBIFS\n";
168 /fpu/ and $ret .= "\tselect HAS_FPU\n";
169 /ramdisk/ and $ret .= "\tselect USES_INITRAMFS\n";
170 /powerpc64/ and $ret .= "\tselect powerpc64\n";
171 /nommu/ and $ret .= "\tselect NOMMU\n";
178 my $parent = $target->{parent};
180 return $target->{parent}->{name}." - ".$target->{name};
182 return $target->{name};
189 $v =~ /(\d+_\d+_\d+)(_\d+)?/ and $v = $1;
193 sub print_target($) {
195 my $features = target_config_features(@{$target->{features}});
196 my $help = $target->{desc};
197 my $kernel = $target->{kernel};
203 if ($help =~ /\w+/) {
204 $help =~ s/^\s*/\t /mg;
205 $help = "\thelp\n$help";
210 my $v = kver($target->{version});
211 if (@{$target->{subtargets}} == 0) {
213 config TARGET_$target->{conf}
214 bool "$target->{name}"
221 config TARGET_$target->{conf}
222 bool "$target->{name}"
225 if ($target->{subtarget}) {
226 $confstr .= "\tdepends TARGET_$target->{boardconf}\n";
228 if (@{$target->{subtargets}} > 0) {
229 $confstr .= "\tselect HAS_SUBTARGETS\n";
232 if ($target->{arch} =~ /\w/) {
233 $confstr .= "\tselect $target->{arch}\n";
235 foreach my $dep (@{$target->{depends}}) {
236 my $mode = "depends";
240 $dep =~ /^([@\+\-]+)(.+)$/;
244 next if $name =~ /:/;
245 $flags =~ /-/ and $mode = "deselect";
246 $flags =~ /\+/ and $mode = "select";
247 $flags =~ /@/ and $confstr .= "\t$mode $name\n";
249 $confstr .= $features;
250 $confstr .= "$help\n\n";
254 sub gen_target_config() {
255 my @target = parse_target_metadata();
258 my @target_sort = sort {
259 target_name($a) cmp target_name($b);
265 prompt "Target System"
266 default TARGET_brcm_2_4
271 foreach my $target (@target_sort) {
272 next if $target->{subtarget};
273 print_target($target);
280 prompt "Subtarget" if HAS_SUBTARGETS
283 foreach my $target (@target) {
284 next unless $target->{subtarget};
285 print_target($target);
292 prompt "Target Profile"
296 foreach my $target (@target) {
297 my $profiles = $target->{profiles};
299 foreach my $profile (@$profiles) {
301 config TARGET_$target->{conf}_$profile->{id}
302 bool "$profile->{name}"
303 depends TARGET_$target->{conf}
306 $profile->{kconfig} and print "\tselect PROFILE_KCONFIG\n";
307 my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
308 foreach my $pkg (@pkglist) {
309 print "\tselect DEFAULT_$pkg\n";
312 my $help = $profile->{desc};
313 if ($help =~ /\w+/) {
314 $help =~ s/^\s*/\t /mg;
315 $help = "\thelp\n$help";
326 config HAS_SUBTARGETS
333 foreach my $target (@target) {
334 $target->{subtarget} or print "\t\tdefault \"".$target->{board}."\" if TARGET_".$target->{conf}."\n";
338 config DEFAULT_TARGET_OPTIMIZATION
341 foreach my $target (@target) {
342 next if @{$target->{subtargets}} > 0;
343 print "\tdefault \"".$target->{cflags}."\" if TARGET_".$target->{conf}."\n";
345 print "\tdefault \"-Os -pipe -funit-at-a-time\"\n";
348 foreach my $target (@target) {
349 my $v = kver($target->{version});
359 foreach my $def (sort keys %defaults) {
360 print "\tconfig DEFAULT_".$def."\n";
361 print "\t\tbool\n\n";
366 sub __find_package_dep($$) {
369 my $deps = ($pkg->{vdepends} or $pkg->{depends});
371 return 0 unless defined $deps;
372 foreach my $dep (@{$deps}) {
373 next if $dep_check{$dep};
374 $dep_check{$dep} = 1;
375 return 1 if $dep eq $name;
376 return 1 if ($package{$dep} and (__find_package_dep($package{$dep},$name) == 1));
381 # wrapper to avoid infinite recursion
382 sub find_package_dep($$) {
387 return __find_package_dep($pkg, $name);
390 sub package_depends($$) {
395 return 0 if ($a->{submenu} ne $b->{submenu});
396 if (find_package_dep($a, $b->{name}) == 1) {
398 } elsif (find_package_dep($b, $a->{name}) == 1) {
409 my $only_dep = shift;
413 my $parent_condition = shift;
418 my @depends = @$depends;
419 foreach my $depend (@depends) {
422 $depend =~ s/^([@\+]+)// and $flags = $1;
424 my $condition = $parent_condition;
426 if ($depend =~ /^(.+):(.+)$/) {
427 if ($1 ne "PACKAGE_$pkgname") {
429 $condition = "$condition && $1";
436 next if $seen->{$depend};
437 next if $package{$depend} and $package{$depend}->{buildonly};
438 $seen->{$depend} = 1;
439 if ($vdep = $package{$depend}->{vdepends}) {
440 $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
442 $flags =~ /\+/ and do {
443 # Menuconfig will not treat 'select FOO' as a real dependency
444 # thus if FOO depends on other config options, these dependencies
445 # will not be checked. To fix this, we simply emit all of FOO's
446 # depends here as well.
447 $package{$depend} and mconf_depends($pkgname, $package{$depend}->{depends}, 1, $dep, $seen, $condition);
452 $flags =~ /@/ or $depend = "PACKAGE_$depend";
454 if ($m =~ /select/) {
455 $depend = "$depend if $condition";
457 $depend = "!($condition) || $depend";
461 $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
463 foreach my $depend (keys %$dep) {
464 my $m = $dep->{$depend};
465 $res .= "\t\t$m $depend\n";
470 sub print_package_config_category($) {
475 return unless $category{$cat};
477 print "menu \"$cat\"\n\n";
478 my %spkg = %{$category{$cat}};
480 foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
481 foreach my $pkg (@{$spkg{$spkg}}) {
482 next if $pkg->{buildonly};
483 my $menu = $pkg->{submenu};
485 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
489 $menus{$menu} or $menus{$menu} = [];
490 push @{$menus{$menu}}, $pkg;
494 ($a eq 'undef' ? 1 : 0) or
495 ($b eq 'undef' ? -1 : 0) or
499 foreach my $menu (@menus) {
501 package_depends($a, $b) or
502 ($a->{name} cmp $b->{name})
504 if ($menu ne 'undef') {
505 $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
506 print "menu \"$menu\"\n";
508 foreach my $pkg (@pkgs) {
509 my $title = $pkg->{name};
510 my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
512 $title .= ("." x $c). " ". $pkg->{title};
515 $pkg->{menu} and print "menu";
516 print "config PACKAGE_".$pkg->{name}."\n";
517 print "\t\t".($pkg->{tristate} ? 'tristate' : 'bool')." \"$title\"\n";
518 print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
519 foreach my $default (split /\s*,\s*/, $pkg->{default}) {
520 print "\t\tdefault $default\n";
522 print mconf_depends($pkg->{name}, $pkg->{depends}, 0);
524 print $pkg->{description};
527 $pkg->{config} and print $pkg->{config}."\n";
529 if ($menu ne 'undef') {
531 $menu_dep{$menu} and print "endif\n";
536 undef $category{$cat};
539 sub print_package_features() {
540 keys %features > 0 or return;
541 print "menu \"Package features\"\n";
542 foreach my $n (keys %features) {
543 my @features = sort { $b->{priority} <=> $a->{priority} or $a->{title} cmp $b->{title} } @{$features{$n}};
546 prompt "$features[0]->{target_title}"
547 default FEATURE_$features[0]->{name}
550 foreach my $feature (@features) {
552 config FEATURE_$feature->{name}
553 bool "$feature->{title}"
555 $feature->{description} =~ /\w/ and do {
556 print "\t\thelp\n".$feature->{description}."\n";
564 sub gen_package_config() {
565 parse_package_metadata($ARGV[0]) or exit 1;
566 print "menuconfig IMAGEOPT\n\tbool \"Image configuration\"\n\tdefault n\n";
567 foreach my $preconfig (keys %preconfig) {
568 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
569 my $conf = $preconfig{$preconfig}->{$cfg}->{id};
572 config UCI_PRECONFIG_$conf
573 string "$preconfig{$preconfig}->{$cfg}->{label}" if IMAGEOPT
574 depends PACKAGE_$preconfig
575 default "$preconfig{$preconfig}->{$cfg}->{default}"
580 print "source \"package/*/image-config.in\"\n";
581 if (scalar glob "package/feeds/*/*/image-config.in") {
582 print "source \"package/feeds/*/*/image-config.in\"\n";
584 print_package_features();
585 print_package_config_category 'Base system';
586 foreach my $cat (keys %category) {
587 print_package_config_category $cat;
591 sub get_conditional_dep($$) {
592 my $condition = shift;
595 if ($condition =~ /^!(.+)/) {
596 return "\$(if \$(CONFIG_$1),,$depstr)";
598 return "\$(if \$(CONFIG_$condition),$depstr)";
605 sub gen_package_mk() {
611 parse_package_metadata($ARGV[0]) or exit 1;
612 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
614 my $pkg = $package{$name};
617 next if defined $pkg->{vdepends};
620 $conf{$pkg->{src}} or do {
622 $conf{$pkg->{src}} = 1;
625 $config = "\$(CONFIG_PACKAGE_$name)"
628 $pkg->{buildonly} and $config = "";
629 print "package-$config += $pkg->{subdir}$pkg->{src}\n";
630 if ($pkg->{variant}) {
631 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/variants += \$(if $config,$pkg->{variant})\n"
633 $pkg->{prereq} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
636 next if $done{$pkg->{src}};
637 $done{$pkg->{src}} = 1;
639 if (@{$pkg->{buildtypes}} > 0) {
640 print "buildtypes-$pkg->{subdir}$pkg->{src} = ".join(' ', @{$pkg->{buildtypes}})."\n";
643 foreach my $spkg (@{$srcpackage{$pkg->{src}}}) {
644 foreach my $dep (@{$spkg->{depends}}, @{$spkg->{builddepends}}) {
651 foreach my $type (@{$pkg->{buildtypes}}) {
655 next unless $pkg->{"builddepends/$type"};
656 foreach my $dep (@{$pkg->{"builddepends/$type"}}) {
660 if ($dep =~ /^(.+):(.+)/) {
664 if ($dep =~ /^(.+)(\/.+)/) {
668 my $pkg_dep = $package{$dep};
669 next unless $pkg_dep;
672 if (defined $pkg_dep->{src}) {
673 $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
674 } elsif (defined($srcpackage{$dep})) {
675 $idx = $subdir{$dep}.$dep;
677 my $depstr = "\$(curdir)/$idx$suffix/compile";
678 my $depline = get_conditional_dep($condition, $depstr);
680 $deplines{$dep} = $depline;
683 my $depline = join(" ", values %deplines);
685 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/$type/compile += $depline\n";
691 foreach my $deps (@srcdeps) {
697 if ($deps =~ /^(.+):(.+)/) {
701 if ($deps =~ /^(.+)(\/.+)/) {
706 my $pkg_dep = $package{$deps};
709 if ($pkg_dep->{vdepends}) {
710 @deps = @{$pkg_dep->{vdepends}};
715 foreach my $dep (@deps) {
716 $pkg_dep = $package{$deps};
717 if (defined $pkg_dep->{src}) {
718 ($pkg->{src} ne $pkg_dep->{src}.$suffix) and $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
719 } elsif (defined($srcpackage{$dep})) {
720 $idx = $subdir{$dep}.$dep;
723 undef $idx if $idx =~ /^(kernel)|(base-files)$/;
726 next if $pkg->{src} eq $pkg_dep->{src}.$suffix;
727 next if $dep{$pkg->{src}."->".$idx};
728 next if $dep{$pkg->{src}."->($dep)".$idx} and $pkg_dep->{vdepends};
731 if ($pkg_dep->{vdepends}) {
732 $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
733 $dep{$pkg->{src}."->($dep)".$idx} = 1;
735 $depstr = "\$(curdir)/$idx/compile";
736 $dep{$pkg->{src}."->".$idx} = 1;
738 $depline = get_conditional_dep($condition, $depstr);
740 $deplines{$idx.$dep} = $depline;
745 my $depline = join(" ", values %deplines);
747 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/compile += $depline\n";
754 foreach my $preconfig (keys %preconfig) {
756 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
757 my $conf = $preconfig{$preconfig}->{$cfg}->{id};
759 $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
764 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
769 ifneq (\$(IMAGEOPT)\$(CONFIG_IMAGEOPT),)
770 package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
776 sub gen_package_source() {
777 parse_package_metadata($ARGV[0]) or exit 1;
778 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
779 my $pkg = $package{$name};
780 if ($pkg->{name} && $pkg->{source}) {
781 print "$pkg->{name}: ";
782 print "$pkg->{source}\n";
787 sub parse_command() {
788 my $cmd = shift @ARGV;
790 /^target_config$/ and return gen_target_config();
791 /^package_mk$/ and return gen_package_mk();
792 /^package_config$/ and return gen_package_config();
793 /^kconfig/ and return gen_kconfig_overrides();
794 /^package_source$/ and return gen_package_source();
798 $0 target_config [file] Target metadata in Kconfig format
799 $0 package_mk [file] Package metadata in makefile format
800 $0 package_config [file] Package metadata in Kconfig format
801 $0 kconfig [file] [config] Kernel config overrides
802 $0 package_source [file] Package source file information