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 /display/ and $ret .= "\tselect DISPLAY_SUPPORT\n";
156 /gpio/ and $ret .= "\tselect GPIO_SUPPORT\n";
157 /pci/ and $ret .= "\tselect PCI_SUPPORT\n";
158 /pcie/ and $ret .= "\tselect PCIE_SUPPORT\n";
159 /usb/ and $ret .= "\tselect USB_SUPPORT\n";
160 /pcmcia/ and $ret .= "\tselect PCMCIA_SUPPORT\n";
161 /squashfs/ and $ret .= "\tselect USES_SQUASHFS\n";
162 /jffs2/ and $ret .= "\tselect USES_JFFS2\n";
163 /ext2/ and $ret .= "\tselect USES_EXT2\n";
164 /tgz/ and $ret .= "\tselect USES_TGZ\n";
165 /cpiogz/ and $ret .= "\tselect USES_CPIOGZ\n";
166 /ubifs/ and $ret .= "\tselect USES_UBIFS\n";
167 /fpu/ and $ret .= "\tselect HAS_FPU\n";
168 /ramdisk/ and $ret .= "\tselect USES_INITRAMFS\n";
169 /powerpc64/ and $ret .= "\tselect powerpc64\n";
170 /nommu/ and $ret .= "\tselect NOMMU\n";
177 my $parent = $target->{parent};
179 return $target->{parent}->{name}." - ".$target->{name};
181 return $target->{name};
188 $v =~ /(\d+_\d+_\d+)(_\d+)?/ and $v = $1;
192 sub print_target($) {
194 my $features = target_config_features(@{$target->{features}});
195 my $help = $target->{desc};
196 my $kernel = $target->{kernel};
202 if ($help =~ /\w+/) {
203 $help =~ s/^\s*/\t /mg;
204 $help = "\thelp\n$help";
209 my $v = kver($target->{version});
210 if (@{$target->{subtargets}} == 0) {
212 config TARGET_$target->{conf}
213 bool "$target->{name}"
220 config TARGET_$target->{conf}
221 bool "$target->{name}"
224 if ($target->{subtarget}) {
225 $confstr .= "\tdepends TARGET_$target->{boardconf}\n";
227 if (@{$target->{subtargets}} > 0) {
228 $confstr .= "\tselect HAS_SUBTARGETS\n";
231 if ($target->{arch} =~ /\w/) {
232 $confstr .= "\tselect $target->{arch}\n";
234 foreach my $dep (@{$target->{depends}}) {
235 my $mode = "depends";
239 $dep =~ /^([@\+\-]+)(.+)$/;
243 next if $name =~ /:/;
244 $flags =~ /-/ and $mode = "deselect";
245 $flags =~ /\+/ and $mode = "select";
246 $flags =~ /@/ and $confstr .= "\t$mode $name\n";
248 $confstr .= $features;
249 $confstr .= "$help\n\n";
253 sub gen_target_config() {
254 my @target = parse_target_metadata();
257 my @target_sort = sort {
258 target_name($a) cmp target_name($b);
264 prompt "Target System"
265 default TARGET_brcm_2_4
270 foreach my $target (@target_sort) {
271 next if $target->{subtarget};
272 print_target($target);
279 prompt "Subtarget" if HAS_SUBTARGETS
282 foreach my $target (@target) {
283 next unless $target->{subtarget};
284 print_target($target);
291 prompt "Target Profile"
295 foreach my $target (@target) {
296 my $profiles = $target->{profiles};
298 foreach my $profile (@$profiles) {
300 config TARGET_$target->{conf}_$profile->{id}
301 bool "$profile->{name}"
302 depends TARGET_$target->{conf}
305 $profile->{kconfig} and print "\tselect PROFILE_KCONFIG\n";
306 my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
307 foreach my $pkg (@pkglist) {
308 print "\tselect DEFAULT_$pkg\n";
311 my $help = $profile->{desc};
312 if ($help =~ /\w+/) {
313 $help =~ s/^\s*/\t /mg;
314 $help = "\thelp\n$help";
325 config HAS_SUBTARGETS
332 foreach my $target (@target) {
333 $target->{subtarget} or print "\t\tdefault \"".$target->{board}."\" if TARGET_".$target->{conf}."\n";
337 config DEFAULT_TARGET_OPTIMIZATION
340 foreach my $target (@target) {
341 next if @{$target->{subtargets}} > 0;
342 print "\tdefault \"".$target->{cflags}."\" if TARGET_".$target->{conf}."\n";
344 print "\tdefault \"-Os -pipe -funit-at-a-time\"\n";
347 foreach my $target (@target) {
348 my $v = kver($target->{version});
358 foreach my $def (sort keys %defaults) {
359 print "\tconfig DEFAULT_".$def."\n";
360 print "\t\tbool\n\n";
365 sub __find_package_dep($$) {
368 my $deps = ($pkg->{vdepends} or $pkg->{depends});
370 return 0 unless defined $deps;
371 foreach my $dep (@{$deps}) {
372 next if $dep_check{$dep};
373 $dep_check{$dep} = 1;
374 return 1 if $dep eq $name;
375 return 1 if ($package{$dep} and (__find_package_dep($package{$dep},$name) == 1));
380 # wrapper to avoid infinite recursion
381 sub find_package_dep($$) {
386 return __find_package_dep($pkg, $name);
389 sub package_depends($$) {
394 return 0 if ($a->{submenu} ne $b->{submenu});
395 if (find_package_dep($a, $b->{name}) == 1) {
397 } elsif (find_package_dep($b, $a->{name}) == 1) {
408 my $only_dep = shift;
412 my $parent_condition = shift;
417 my @depends = @$depends;
418 foreach my $depend (@depends) {
421 $depend =~ s/^([@\+]+)// and $flags = $1;
423 my $condition = $parent_condition;
425 if ($depend =~ /^(.+):(.+)$/) {
426 if ($1 ne "PACKAGE_$pkgname") {
428 $condition = "$condition && $1";
435 next if $seen->{$depend};
436 next if $package{$depend} and $package{$depend}->{buildonly};
437 $seen->{$depend} = 1;
438 if ($vdep = $package{$depend}->{vdepends}) {
439 $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
441 $flags =~ /\+/ and do {
442 # Menuconfig will not treat 'select FOO' as a real dependency
443 # thus if FOO depends on other config options, these dependencies
444 # will not be checked. To fix this, we simply emit all of FOO's
445 # depends here as well.
446 $package{$depend} and mconf_depends($pkgname, $package{$depend}->{depends}, 1, $dep, $seen, $condition);
451 $flags =~ /@/ or $depend = "PACKAGE_$depend";
453 if ($m =~ /select/) {
454 $depend = "$depend if $condition";
456 $depend = "!($condition) || $depend";
460 $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
462 foreach my $depend (keys %$dep) {
463 my $m = $dep->{$depend};
464 $res .= "\t\t$m $depend\n";
469 sub print_package_config_category($) {
474 return unless $category{$cat};
476 print "menu \"$cat\"\n\n";
477 my %spkg = %{$category{$cat}};
479 foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
480 foreach my $pkg (@{$spkg{$spkg}}) {
481 next if $pkg->{buildonly};
482 my $menu = $pkg->{submenu};
484 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
488 $menus{$menu} or $menus{$menu} = [];
489 push @{$menus{$menu}}, $pkg;
493 ($a eq 'undef' ? 1 : 0) or
494 ($b eq 'undef' ? -1 : 0) or
498 foreach my $menu (@menus) {
500 package_depends($a, $b) or
501 ($a->{name} cmp $b->{name})
503 if ($menu ne 'undef') {
504 $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
505 print "menu \"$menu\"\n";
507 foreach my $pkg (@pkgs) {
508 my $title = $pkg->{name};
509 my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
511 $title .= ("." x $c). " ". $pkg->{title};
514 $pkg->{menu} and print "menu";
515 print "config PACKAGE_".$pkg->{name}."\n";
516 print "\t\t".($pkg->{tristate} ? 'tristate' : 'bool')." \"$title\"\n";
517 print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
518 foreach my $default (split /\s*,\s*/, $pkg->{default}) {
519 print "\t\tdefault $default\n";
521 print mconf_depends($pkg->{name}, $pkg->{depends}, 0);
523 print $pkg->{description};
526 $pkg->{config} and print $pkg->{config}."\n";
528 if ($menu ne 'undef') {
530 $menu_dep{$menu} and print "endif\n";
535 undef $category{$cat};
538 sub print_package_features() {
539 keys %features > 0 or return;
540 print "menu \"Package features\"\n";
541 foreach my $n (keys %features) {
542 my @features = sort { $b->{priority} <=> $a->{priority} or $a->{title} cmp $b->{title} } @{$features{$n}};
545 prompt "$features[0]->{target_title}"
546 default FEATURE_$features[0]->{name}
549 foreach my $feature (@features) {
551 config FEATURE_$feature->{name}
552 bool "$feature->{title}"
554 $feature->{description} =~ /\w/ and do {
555 print "\t\thelp\n".$feature->{description}."\n";
563 sub gen_package_config() {
564 parse_package_metadata($ARGV[0]) or exit 1;
565 print "menuconfig UCI_PRECONFIG\n\tbool \"Image configuration\"\n" if %preconfig;
566 foreach my $preconfig (keys %preconfig) {
567 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
568 my $conf = $preconfig{$preconfig}->{$cfg}->{id};
571 config UCI_PRECONFIG_$conf
572 string "$preconfig{$preconfig}->{$cfg}->{label}" if UCI_PRECONFIG
573 depends PACKAGE_$preconfig
574 default "$preconfig{$preconfig}->{$cfg}->{default}"
579 print_package_features();
580 print_package_config_category 'Base system';
581 foreach my $cat (keys %category) {
582 print_package_config_category $cat;
586 sub get_conditional_dep($$) {
587 my $condition = shift;
590 if ($condition =~ /^!(.+)/) {
591 return "\$(if \$(CONFIG_$1),,$depstr)";
593 return "\$(if \$(CONFIG_$condition),$depstr)";
600 sub gen_package_mk() {
606 parse_package_metadata($ARGV[0]) or exit 1;
607 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
609 my $pkg = $package{$name};
612 next if defined $pkg->{vdepends};
615 $conf{$pkg->{src}} or do {
617 $conf{$pkg->{src}} = 1;
620 $config = "\$(CONFIG_PACKAGE_$name)"
623 $pkg->{buildonly} and $config = "";
624 print "package-$config += $pkg->{subdir}$pkg->{src}\n";
625 if ($pkg->{variant}) {
626 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/variants += \$(if $config,$pkg->{variant})\n"
628 $pkg->{prereq} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
631 next if $done{$pkg->{src}};
632 $done{$pkg->{src}} = 1;
634 if (@{$pkg->{buildtypes}} > 0) {
635 print "buildtypes-$pkg->{subdir}$pkg->{src} = ".join(' ', @{$pkg->{buildtypes}})."\n";
638 foreach my $spkg (@{$srcpackage{$pkg->{src}}}) {
639 foreach my $dep (@{$spkg->{depends}}, @{$spkg->{builddepends}}) {
646 foreach my $type (@{$pkg->{buildtypes}}) {
650 next unless $pkg->{"builddepends/$type"};
651 foreach my $dep (@{$pkg->{"builddepends/$type"}}) {
655 if ($dep =~ /^(.+):(.+)/) {
659 if ($dep =~ /^(.+)(\/.+)/) {
663 my $pkg_dep = $package{$dep};
664 next unless $pkg_dep;
667 if (defined $pkg_dep->{src}) {
668 $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
669 } elsif (defined($srcpackage{$dep})) {
670 $idx = $subdir{$dep}.$dep;
672 my $depstr = "\$(curdir)/$idx$suffix/compile";
673 my $depline = get_conditional_dep($condition, $depstr);
675 $deplines{$dep} = $depline;
678 my $depline = join(" ", values %deplines);
680 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/$type/compile += $depline\n";
686 foreach my $deps (@srcdeps) {
692 if ($deps =~ /^(.+):(.+)/) {
696 if ($deps =~ /^(.+)(\/.+)/) {
701 my $pkg_dep = $package{$deps};
704 if ($pkg_dep->{vdepends}) {
705 @deps = @{$pkg_dep->{vdepends}};
710 foreach my $dep (@deps) {
711 $pkg_dep = $package{$deps};
712 if (defined $pkg_dep->{src}) {
713 ($pkg->{src} ne $pkg_dep->{src}.$suffix) and $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
714 } elsif (defined($srcpackage{$dep})) {
715 $idx = $subdir{$dep}.$dep;
718 undef $idx if $idx =~ /^(kernel)|(base-files)$/;
721 next if $pkg->{src} eq $pkg_dep->{src}.$suffix;
722 next if $dep{$pkg->{src}."->".$idx};
723 next if $dep{$pkg->{src}."->($dep)".$idx} and $pkg_dep->{vdepends};
726 if ($pkg_dep->{vdepends}) {
727 $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
728 $dep{$pkg->{src}."->($dep)".$idx} = 1;
730 $depstr = "\$(curdir)/$idx/compile";
731 $dep{$pkg->{src}."->".$idx} = 1;
733 $depline = get_conditional_dep($condition, $depstr);
735 $deplines{$idx.$dep} = $depline;
740 my $depline = join(" ", values %deplines);
742 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/compile += $depline\n";
749 foreach my $preconfig (keys %preconfig) {
751 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
752 my $conf = $preconfig{$preconfig}->{$cfg}->{id};
754 $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
759 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
764 ifneq (\$(UCI_PRECONFIG)\$(CONFIG_UCI_PRECONFIG),)
765 package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
771 sub gen_package_source() {
772 parse_package_metadata($ARGV[0]) or exit 1;
773 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
774 my $pkg = $package{$name};
775 if ($pkg->{name} && $pkg->{source}) {
776 print "$pkg->{name}: ";
777 print "$pkg->{source}\n";
782 sub parse_command() {
783 my $cmd = shift @ARGV;
785 /^target_config$/ and return gen_target_config();
786 /^package_mk$/ and return gen_package_mk();
787 /^package_config$/ and return gen_package_config();
788 /^kconfig/ and return gen_kconfig_overrides();
789 /^package_source$/ and return gen_package_source();
793 $0 target_config [file] Target metadata in Kconfig format
794 $0 package_mk [file] Package metadata in makefile format
795 $0 package_config [file] Package metadata in Kconfig format
796 $0 kconfig [file] [config] Kernel config overrides
797 $0 package_source [file] Package source file information