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 /fpu/ and $ret .= "\tselect HAS_FPU\n";
167 /ramdisk/ and $ret .= "\tselect USES_INITRAMFS\n";
174 my $parent = $target->{parent};
176 return $target->{parent}->{name}." - ".$target->{name};
178 return $target->{name};
185 $v =~ /(\d+_\d+_\d+)(_\d+)?/ and $v = $1;
189 sub print_target($) {
191 my $features = target_config_features(@{$target->{features}});
192 my $help = $target->{desc};
193 my $kernel = $target->{kernel};
199 if ($help =~ /\w+/) {
200 $help =~ s/^\s*/\t /mg;
201 $help = "\thelp\n$help";
206 my $v = kver($target->{version});
207 if (@{$target->{subtargets}} == 0) {
209 config TARGET_$target->{conf}
210 bool "$target->{name}"
217 config TARGET_$target->{conf}
218 bool "$target->{name}"
221 if ($target->{subtarget}) {
222 $confstr .= "\tdepends TARGET_$target->{boardconf}\n";
224 if (@{$target->{subtargets}} > 0) {
225 $confstr .= "\tselect HAS_SUBTARGETS\n";
227 $confstr .= "\tselect $target->{arch}\n";
228 foreach my $dep (@{$target->{depends}}) {
229 my $mode = "depends";
233 $dep =~ /^([@\+\-]+)(.+)$/;
237 next if $name =~ /:/;
238 $flags =~ /-/ and $mode = "deselect";
239 $flags =~ /\+/ and $mode = "select";
240 $flags =~ /@/ and $confstr .= "\t$mode $name\n";
242 $confstr .= $features;
245 $confstr .= "$help\n\n";
249 sub gen_target_config() {
250 my @target = parse_target_metadata();
253 my @target_sort = sort {
254 target_name($a) cmp target_name($b);
260 prompt "Target System"
261 default TARGET_brcm_2_4
266 foreach my $target (@target_sort) {
267 next if $target->{subtarget};
268 print_target($target);
275 prompt "Subtarget" if HAS_SUBTARGETS
278 foreach my $target (@target) {
279 next unless $target->{subtarget};
280 print_target($target);
287 prompt "Target Profile"
291 foreach my $target (@target) {
292 my $profiles = $target->{profiles};
294 foreach my $profile (@$profiles) {
296 config TARGET_$target->{conf}_$profile->{id}
297 bool "$profile->{name}"
298 depends TARGET_$target->{conf}
301 $profile->{kconfig} and print "\tselect PROFILE_KCONFIG\n";
302 my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
303 foreach my $pkg (@pkglist) {
304 print "\tselect DEFAULT_$pkg\n";
307 my $help = $profile->{desc};
308 if ($help =~ /\w+/) {
309 $help =~ s/^\s*/\t /mg;
310 $help = "\thelp\n$help";
321 config HAS_SUBTARGETS
328 foreach my $target (@target) {
329 $target->{subtarget} or print "\t\tdefault \"".$target->{board}."\" if TARGET_".$target->{conf}."\n";
333 config DEFAULT_TARGET_OPTIMIZATION
336 foreach my $target (@target) {
337 next if @{$target->{subtargets}} > 0;
338 print "\tdefault \"".$target->{cflags}."\" if TARGET_".$target->{conf}."\n";
340 print "\tdefault \"-Os -pipe -funit-at-a-time\"\n";
343 foreach my $target (@target) {
344 my $v = kver($target->{version});
354 foreach my $def (sort keys %defaults) {
355 print "\tconfig DEFAULT_".$def."\n";
356 print "\t\tbool\n\n";
361 sub __find_package_dep($$) {
364 my $deps = ($pkg->{vdepends} or $pkg->{depends});
366 return 0 unless defined $deps;
367 foreach my $dep (@{$deps}) {
368 next if $dep_check{$dep};
369 $dep_check{$dep} = 1;
370 return 1 if $dep eq $name;
371 return 1 if ($package{$dep} and (__find_package_dep($package{$dep},$name) == 1));
376 # wrapper to avoid infinite recursion
377 sub find_package_dep($$) {
382 return __find_package_dep($pkg, $name);
385 sub package_depends($$) {
390 return 0 if ($a->{submenu} ne $b->{submenu});
391 if (find_package_dep($a, $b->{name}) == 1) {
393 } elsif (find_package_dep($b, $a->{name}) == 1) {
404 my $only_dep = shift;
408 my $parent_condition = shift;
413 my @depends = @$depends;
414 foreach my $depend (@depends) {
416 $depend =~ s/^([@\+]+)//;
419 my $condition = $parent_condition;
421 if ($depend =~ /^(.+):(.+)$/) {
422 if ($1 ne "PACKAGE_$pkgname") {
424 $condition = "$condition && $1";
431 next if $seen->{$depend};
432 next if $package{$depend} and $package{$depend}->{buildonly};
433 $seen->{$depend} = 1;
434 if ($vdep = $package{$depend}->{vdepends}) {
435 $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
437 $flags =~ /\+/ and do {
438 # Menuconfig will not treat 'select FOO' as a real dependency
439 # thus if FOO depends on other config options, these dependencies
440 # will not be checked. To fix this, we simply emit all of FOO's
441 # depends here as well.
442 $package{$depend} and mconf_depends($pkgname, $package{$depend}->{depends}, 1, $dep, $seen, $condition);
447 $flags =~ /@/ or $depend = "PACKAGE_$depend";
449 if ($m =~ /select/) {
450 $depend = "$depend if $condition";
452 $depend = "!($condition) || $depend";
456 $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
458 foreach my $depend (keys %$dep) {
459 my $m = $dep->{$depend};
460 $res .= "\t\t$m $depend\n";
465 sub print_package_config_category($) {
470 return unless $category{$cat};
472 print "menu \"$cat\"\n\n";
473 my %spkg = %{$category{$cat}};
475 foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
476 foreach my $pkg (@{$spkg{$spkg}}) {
477 next if $pkg->{buildonly};
478 my $menu = $pkg->{submenu};
480 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
484 $menus{$menu} or $menus{$menu} = [];
485 push @{$menus{$menu}}, $pkg;
489 ($a eq 'undef' ? 1 : 0) or
490 ($b eq 'undef' ? -1 : 0) or
494 foreach my $menu (@menus) {
496 package_depends($a, $b) or
497 ($a->{name} cmp $b->{name})
499 if ($menu ne 'undef') {
500 $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
501 print "menu \"$menu\"\n";
503 foreach my $pkg (@pkgs) {
504 my $title = $pkg->{name};
505 my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
507 $title .= ("." x $c). " ". $pkg->{title};
510 $pkg->{menu} and print "menu";
511 print "config PACKAGE_".$pkg->{name}."\n";
512 print "\t\t".($pkg->{tristate} ? 'tristate' : 'bool')." \"$title\"\n";
513 print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
514 foreach my $default (split /\s*,\s*/, $pkg->{default}) {
515 print "\t\tdefault $default\n";
517 print mconf_depends($pkg->{name}, $pkg->{depends}, 0);
519 print $pkg->{description};
522 $pkg->{config} and print $pkg->{config}."\n";
524 if ($menu ne 'undef') {
526 $menu_dep{$menu} and print "endif\n";
531 undef $category{$cat};
534 sub gen_package_config() {
535 parse_package_metadata($ARGV[0]) or exit 1;
536 print "menuconfig UCI_PRECONFIG\n\tbool \"Image configuration\"\n" if %preconfig;
537 foreach my $preconfig (keys %preconfig) {
538 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
539 my $conf = $preconfig{$preconfig}->{$cfg}->{id};
542 config UCI_PRECONFIG_$conf
543 string "$preconfig{$preconfig}->{$cfg}->{label}" if UCI_PRECONFIG
544 depends PACKAGE_$preconfig
545 default "$preconfig{$preconfig}->{$cfg}->{default}"
550 print_package_config_category 'Base system';
551 foreach my $cat (keys %category) {
552 print_package_config_category $cat;
556 sub get_conditional_dep($$) {
557 my $condition = shift;
560 if ($condition =~ /^!(.+)/) {
561 return "\$(if \$(CONFIG_$1),,$depstr)";
563 return "\$(if \$(CONFIG_$condition),$depstr)";
570 sub gen_package_mk() {
576 parse_package_metadata($ARGV[0]) or exit 1;
577 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
579 my $pkg = $package{$name};
582 next if defined $pkg->{vdepends};
585 $conf{$pkg->{src}} or do {
587 $conf{$pkg->{src}} = 1;
590 $config = "\$(CONFIG_PACKAGE_$name)"
593 $pkg->{buildonly} and $config = "";
594 print "package-$config += $pkg->{subdir}$pkg->{src}\n";
595 $pkg->{prereq} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
598 next if $done{$pkg->{src}};
599 $done{$pkg->{src}} = 1;
601 if (@{$pkg->{buildtypes}} > 0) {
602 print "buildtypes-$pkg->{subdir}$pkg->{src} = ".join(' ', @{$pkg->{buildtypes}})."\n";
605 foreach my $spkg (@{$srcpackage{$pkg->{src}}}) {
606 foreach my $dep (@{$spkg->{depends}}, @{$spkg->{builddepends}}) {
613 foreach my $type (@{$pkg->{buildtypes}}) {
617 next unless $pkg->{"builddepends/$type"};
618 foreach my $dep (@{$pkg->{"builddepends/$type"}}) {
622 if ($dep =~ /^(.+):(.+)/) {
626 if ($dep =~ /^(.+)(\/.+)/) {
630 my $pkg_dep = $package{$dep};
631 next unless $pkg_dep;
634 if (defined $pkg_dep->{src}) {
635 $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
636 } elsif (defined($srcpackage{$dep})) {
637 $idx = $subdir{$dep}.$dep;
639 my $depstr = "\$(curdir)/$idx$suffix/compile";
640 my $depline = get_conditional_dep($condition, $depstr);
642 $deplines{$dep} = $depline;
645 my $depline = join(" ", values %deplines);
647 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/$type/compile += $depline\n";
653 foreach my $deps (@srcdeps) {
659 if ($deps =~ /^(.+):(.+)/) {
663 if ($deps =~ /^(.+)(\/.+)/) {
668 my $pkg_dep = $package{$deps};
671 if ($pkg_dep->{vdepends}) {
672 @deps = @{$pkg_dep->{vdepends}};
677 foreach my $dep (@deps) {
678 $pkg_dep = $package{$deps};
679 if (defined $pkg_dep->{src}) {
680 ($pkg->{src} ne $pkg_dep->{src}.$suffix) and $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
681 } elsif (defined($srcpackage{$dep})) {
682 $idx = $subdir{$dep}.$dep;
685 undef $idx if $idx =~ /^(kernel)|(base-files)$/;
688 next if $pkg->{src} eq $pkg_dep->{src}.$suffix;
689 next if $dep{$pkg->{src}."->".$idx};
690 next if $dep{$pkg->{src}."->($dep)".$idx} and $pkg_dep->{vdepends};
693 if ($pkg_dep->{vdepends}) {
694 $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
695 $dep{$pkg->{src}."->($dep)".$idx} = 1;
697 $depstr = "\$(curdir)/$idx/compile";
698 $dep{$pkg->{src}."->".$idx} = 1;
700 $depline = get_conditional_dep($condition, $depstr);
702 $deplines{$idx.$dep} = $depline;
707 my $depline = join(" ", values %deplines);
709 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/compile += $depline\n";
716 foreach my $preconfig (keys %preconfig) {
718 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
719 my $conf = $preconfig{$preconfig}->{$cfg}->{id};
721 $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
726 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
731 ifneq (\$(UCI_PRECONFIG)\$(CONFIG_UCI_PRECONFIG),)
732 package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
738 sub gen_package_source() {
739 parse_package_metadata($ARGV[0]) or exit 1;
740 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
741 my $pkg = $package{$name};
742 if ($pkg->{name} && $pkg->{source}) {
743 print "$pkg->{name}: ";
744 print "$pkg->{source}\n";
749 sub parse_command() {
750 my $cmd = shift @ARGV;
752 /^target_config$/ and return gen_target_config();
753 /^package_mk$/ and return gen_package_mk();
754 /^package_config$/ and return gen_package_config();
755 /^kconfig/ and return gen_kconfig_overrides();
756 /^package_source$/ and return gen_package_source();
760 $0 target_config [file] Target metadata in Kconfig format
761 $0 package_mk [file] Package metadata in makefile format
762 $0 package_config [file] Package metadata in Kconfig format
763 $0 kconfig [file] [config] Kernel config overrides
764 $0 package_source [file] Package source file information