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 /^Linux-Version:\s*(.+)\s*$/ and $target->{version
} = $1;
56 /^Linux-Release:\s*(.+)\s*$/ and $target->{release
} = $1;
57 /^Linux-Kernel-Arch:\s*(.+)\s*$/ and $target->{karch
} = $1;
58 /^Default-Packages:\s*(.+)\s*$/ and $target->{packages
} = [ split(/\s+/, $1) ];
59 /^Target-Profile:\s*(.+)\s*$/ and do {
65 push @
{$target->{profiles
}}, $profile;
67 /^Target-Profile-Name:\s*(.+)\s*$/ and $profile->{name
} = $1;
68 /^Target-Profile-Packages:\s*(.*)\s*$/ and $profile->{packages
} = [ split(/\s+/, $1) ];
69 /^Target-Profile-Description:\s*(.*)\s*/ and $profile->{desc
} = get_multiline
(*FILE
);
70 /^Target-Profile-Config:/ and $profile->{config
} = get_multiline
(*FILE
, "\t");
71 /^Target-Profile-Kconfig:/ and $profile->{kconfig
} = 1;
74 foreach my $target (@target) {
75 next if @
{$target->{subtargets
}} > 0;
76 @
{$target->{profiles
}} > 0 or $target->{profiles
} = [
87 sub gen_kconfig_overrides
() {
91 my $pkginfo = shift @ARGV;
92 my $cfgfile = shift @ARGV;
94 # parameter 2: build system config
95 open FILE
, "<$cfgfile" or return;
97 /^(CONFIG_.+?)=(.+)$/ and $config{$1} = 1;
101 # parameter 1: package metadata
102 open FILE
, "<$pkginfo" or return;
104 /^Package:\s*(.+?)\s*$/ and $package = $1;
105 /^Kernel-Config:\s*(.+?)\s*$/ and do {
106 my @config = split /\s+/, $1;
107 foreach my $config (@config) {
110 if ($config =~ /^(.+?)=(.+)$/) {
115 if ($config{"CONFIG_PACKAGE_$package"} and ($config ne 'n')) {
116 $kconfig{$config} = $val;
117 } elsif (!$override) {
118 $kconfig{$config} or $kconfig{$config} = 'n';
125 foreach my $kconfig (sort keys %kconfig) {
126 if ($kconfig{$kconfig} eq 'n') {
127 print "# $kconfig is not set\n";
129 print "$kconfig=$kconfig{$kconfig}\n";
134 sub merge_package_lists
($$) {
140 foreach my $pkg (@
$list1, @
$list2) {
143 foreach my $pkg (keys %pkgs) {
144 push @l, $pkg unless ($pkg =~ /^-/ or $pkgs{"-$pkg"});
149 sub target_config_features
(@
) {
152 while ($_ = shift @_) {
153 /broken/ and $ret .= "\tdepends BROKEN\n";
154 /display/ and $ret .= "\tselect DISPLAY_SUPPORT\n";
155 /gpio/ and $ret .= "\tselect GPIO_SUPPORT\n";
156 /pci/ and $ret .= "\tselect PCI_SUPPORT\n";
157 /usb/ and $ret .= "\tselect USB_SUPPORT\n";
158 /pcmcia/ and $ret .= "\tselect PCMCIA_SUPPORT\n";
159 /squashfs/ and $ret .= "\tselect USES_SQUASHFS\n";
160 /jffs2/ and $ret .= "\tselect USES_JFFS2\n";
161 /ext2/ and $ret .= "\tselect USES_EXT2\n";
162 /tgz/ and $ret .= "\tselect USES_TGZ\n";
163 /cpiogz/ and $ret .= "\tselect USES_CPIOGZ\n";
164 /fpu/ and $ret .= "\tselect HAS_FPU\n";
171 my $parent = $target->{parent
};
173 return $target->{parent
}->{name
}." - ".$target->{name
};
175 return $target->{name
};
182 $v =~ /(\d+_\d+_\d+)(_\d+)?/ and $v = $1;
186 sub print_target
($) {
188 my $features = target_config_features
(@
{$target->{features
}});
189 my $help = $target->{desc
};
190 my $kernel = $target->{kernel
};
196 if ($help =~ /\w+/) {
197 $help =~ s/^\s*/\t /mg;
198 $help = "\thelp\n$help";
203 my $v = kver
($target->{version
});
205 config TARGET_$target->{conf}
206 bool "$target->{name}"
210 if ($target->{subtarget
}) {
211 $confstr .= "\tdepends TARGET_$target->{boardconf}\n";
213 if (@
{$target->{subtargets
}} > 0) {
214 $confstr .= "\tselect HAS_SUBTARGETS\n";
216 $confstr .= "\tselect $target->{arch}\n";
217 foreach my $dep (@
{$target->{depends
}}) {
218 my $mode = "depends";
222 $dep =~ /^([@\+\-]+)(.+)$/;
226 next if $name =~ /:/;
227 $flags =~ /-/ and $mode = "deselect";
228 $flags =~ /\+/ and $mode = "select";
229 $flags =~ /@/ and $confstr .= "\t$mode $name\n";
231 $confstr .= $features;
234 $confstr .= "$help\n\n";
238 sub gen_target_config
() {
239 my @target = parse_target_metadata
();
242 my @target_sort = sort {
243 target_name
($a) cmp target_name
($b);
249 prompt "Target System"
250 default TARGET_brcm_2_4
255 foreach my $target (@target_sort) {
256 next if $target->{subtarget
};
257 print_target
($target);
264 prompt "Subtarget" if HAS_SUBTARGETS
267 foreach my $target (@target) {
268 next unless $target->{subtarget
};
269 print_target
($target);
276 prompt "Target Profile"
280 foreach my $target (@target) {
281 my $profiles = $target->{profiles
};
283 foreach my $profile (@
$profiles) {
285 config TARGET_$target->{conf}_$profile->{id}
286 bool "$profile->{name}"
287 depends TARGET_$target->{conf}
290 $profile->{kconfig
} and print "\tselect PROFILE_KCONFIG\n";
291 my @pkglist = merge_package_lists
($target->{packages
}, $profile->{packages
});
292 foreach my $pkg (@pkglist) {
293 print "\tselect DEFAULT_$pkg\n";
303 config HAS_SUBTARGETS
310 foreach my $target (@target) {
311 $target->{subtarget
} or print "\t\tdefault \"".$target->{board
}."\" if TARGET_".$target->{conf
}."\n";
315 foreach my $target (@target) {
316 my $v = kver
($target->{version
});
324 foreach my $def (sort keys %defaults) {
325 print "\tconfig DEFAULT_".$def."\n";
326 print "\t\tbool\n\n";
331 sub __find_package_dep
($$) {
334 my $deps = ($pkg->{vdepends
} or $pkg->{depends
});
336 return 0 unless defined $deps;
337 foreach my $dep (@
{$deps}) {
338 next if $dep_check{$dep};
339 $dep_check{$dep} = 1;
340 return 1 if $dep eq $name;
341 return 1 if ($package{$dep} and (__find_package_dep
($package{$dep},$name) == 1));
346 # wrapper to avoid infinite recursion
347 sub find_package_dep
($$) {
352 return __find_package_dep
($pkg, $name);
355 sub package_depends
($$) {
360 return 0 if ($a->{submenu
} ne $b->{submenu
});
361 if (find_package_dep
($a, $b->{name
}) == 1) {
363 } elsif (find_package_dep
($b, $a->{name
}) == 1) {
374 my $only_dep = shift;
378 my $condition = shift;
383 my @depends = @
$depends;
384 foreach my $depend (@depends) {
386 $depend =~ s/^([@\+]+)//;
390 if ($depend =~ /^(.+):(.+)$/) {
391 if ($1 ne "PACKAGE_$pkgname") {
393 $condition = "$condition && $1";
400 next if $seen->{$depend};
401 $seen->{$depend} = 1;
402 if ($vdep = $package{$depend}->{vdepends
}) {
403 $depend = join("||", map { "PACKAGE_".$_ } @
$vdep);
405 $flags =~ /\+/ and do {
406 # Menuconfig will not treat 'select FOO' as a real dependency
407 # thus if FOO depends on other config options, these dependencies
408 # will not be checked. To fix this, we simply emit all of FOO's
409 # depends here as well.
410 $package{$depend} and mconf_depends
($pkgname, $package{$depend}->{depends
}, 1, $dep, $seen, $condition);
415 $flags =~ /@/ or $depend = "PACKAGE_$depend";
417 if ($m =~ /select/) {
418 $depend = "$depend if $condition";
420 $depend = "!($condition) || $depend";
424 $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
426 foreach my $depend (keys %$dep) {
427 my $m = $dep->{$depend};
428 $res .= "\t\t$m $depend\n";
433 sub print_package_config_category
($) {
438 return unless $category{$cat};
440 print "menu \"$cat\"\n\n";
441 my %spkg = %{$category{$cat}};
443 foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
444 foreach my $pkg (@
{$spkg{$spkg}}) {
445 my $menu = $pkg->{submenu
};
447 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep
};
451 $menus{$menu} or $menus{$menu} = [];
452 push @
{$menus{$menu}}, $pkg;
456 ($a eq 'undef' ?
1 : 0) or
457 ($b eq 'undef' ?
-1 : 0) or
461 foreach my $menu (@menus) {
463 package_depends
($a, $b) or
464 ($a->{name
} cmp $b->{name
})
466 if ($menu ne 'undef') {
467 $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
468 print "menu \"$menu\"\n";
470 foreach my $pkg (@pkgs) {
471 my $title = $pkg->{name
};
472 my $c = (72 - length($pkg->{name
}) - length($pkg->{title
}));
474 $title .= ("." x
$c). " ". $pkg->{title
};
477 $pkg->{menu
} and print "menu";
478 print "config PACKAGE_".$pkg->{name
}."\n";
479 print "\t\t".($pkg->{tristate
} ?
'tristate' : 'bool')." \"$title\"\n";
480 print "\t\tdefault y if DEFAULT_".$pkg->{name
}."\n";
481 foreach my $default (split /\s*,\s*/, $pkg->{default}) {
482 print "\t\tdefault $default\n";
484 print mconf_depends
($pkg->{name
}, $pkg->{depends
}, 0);
486 print $pkg->{description
};
489 $pkg->{config
} and print $pkg->{config
}."\n";
491 if ($menu ne 'undef') {
493 $menu_dep{$menu} and print "endif\n";
498 undef $category{$cat};
501 sub gen_package_config
() {
502 parse_package_metadata
($ARGV[0]) or exit 1;
503 print "menuconfig UCI_PRECONFIG\n\tbool \"Image configuration\"\n" if %preconfig;
504 foreach my $preconfig (keys %preconfig) {
505 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
506 my $conf = $preconfig{$preconfig}->{$cfg}->{id
};
509 config UCI_PRECONFIG_$conf
510 string "$preconfig{$preconfig}->{$cfg}->{label}" if UCI_PRECONFIG
511 depends PACKAGE_$preconfig
512 default "$preconfig{$preconfig}->{$cfg}->{default}"
517 print_package_config_category
'Base system';
518 foreach my $cat (keys %category) {
519 print_package_config_category
$cat;
523 sub gen_package_mk
() {
529 parse_package_metadata
($ARGV[0]) or exit 1;
530 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
532 my $pkg = $package{$name};
535 next if defined $pkg->{vdepends
};
538 $conf{$pkg->{src
}} or do {
540 $conf{$pkg->{src
}} = 1;
543 $config = "\$(CONFIG_PACKAGE_$name)"
546 print "package-$config += $pkg->{subdir}$pkg->{src}\n";
547 $pkg->{prereq
} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
550 next if $done{$pkg->{src
}};
551 $done{$pkg->{src
}} = 1;
553 foreach my $spkg (@
{$srcpackage{$pkg->{src
}}}) {
554 foreach my $dep (@
{$spkg->{depends
}}, @
{$spkg->{builddepends
}}) {
564 foreach my $deps (@srcdeps) {
568 if ($deps =~ /^(.+):(.+)/) {
573 my $pkg_dep = $package{$deps};
576 if ($pkg_dep->{vdepends
}) {
577 @deps = @
{$pkg_dep->{vdepends
}};
582 foreach my $dep (@deps) {
583 $pkg_dep = $package{$deps};
584 if (defined $pkg_dep->{src
}) {
585 ($pkg->{src
} ne $pkg_dep->{src
}) and $idx = $pkg_dep->{subdir
}.$pkg_dep->{src
};
586 } elsif (defined($srcpackage{$dep})) {
587 $idx = $subdir{$dep}.$dep;
589 undef $idx if $idx =~ /^(kernel)|(base-files)$/;
592 next if $pkg->{src
} eq $pkg_dep->{src
};
593 next if $dep{$pkg->{src
}."->".$idx};
594 next if $dep{$pkg->{src
}."->($dep)".$idx} and $pkg_dep->{vdepends
};
597 if ($pkg_dep->{vdepends
}) {
598 $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
599 $dep{$pkg->{src
}."->($dep)".$idx} = 1;
601 $depstr = "\$(curdir)/$idx/compile";
602 $dep{$pkg->{src
}."->".$idx} = 1;
605 if ($condition =~ /^!(.+)/) {
606 $depline = "\$(if \$(CONFIG_$1),,$depstr)";
608 $depline = "\$(if \$(CONFIG_$condition),$depstr)";
614 $deplines{$idx.$dep} = $depline;
619 my $depline = join(" ", values %deplines);
621 $line .= "\$(curdir)/".$pkg->{subdir
}."$pkg->{src}/compile += $depline\n";
628 foreach my $preconfig (keys %preconfig) {
630 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
631 my $conf = $preconfig{$preconfig}->{$cfg}->{id
};
633 $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
638 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
643 ifneq (\$(UCI_PRECONFIG)\$(CONFIG_UCI_PRECONFIG),)
644 package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
651 sub parse_command
() {
652 my $cmd = shift @ARGV;
654 /^target_config$/ and return gen_target_config
();
655 /^package_mk$/ and return gen_package_mk
();
656 /^package_config$/ and return gen_package_config
();
657 /^kconfig/ and return gen_kconfig_overrides
();
661 $0 target_config [file] Target metadata in Kconfig format
662 $0 package_mk [file] Package metadata in makefile format
663 $0 package_config [file] Package metadata in Kconfig format
664 $0 kconfig [file] [config] Kernel config overrides