15 $str .= (($_ and $prefix) ? $prefix . $_ : $_);
21 sub parse_target_metadata() {
22 my ($target, @target, $profile);
25 /^Target:\s*(.+)\s*$/ and do {
33 push @target, $target;
35 /^Target-Kernel:\s*(\d+\.\d+)\s*$/ and $target->{kernel} = $1;
36 /^Target-Name:\s*(.+)\s*$/ and $target->{name} = $1;
37 /^Target-Path:\s*(.+)\s*$/ and $target->{path} = $1;
38 /^Target-Arch:\s*(.+)\s*$/ and $target->{arch} = $1;
39 /^Target-Features:\s*(.+)\s*$/ and $target->{features} = [ split(/\s+/, $1) ];
40 /^Target-Description:/ and $target->{desc} = get_multiline();
41 /^Linux-Version:\s*(.+)\s*$/ and $target->{version} = $1;
42 /^Linux-Release:\s*(.+)\s*$/ and $target->{release} = $1;
43 /^Linux-Kernel-Arch:\s*(.+)\s*$/ and $target->{karch} = $1;
44 /^Default-Packages:\s*(.+)\s*$/ and $target->{packages} = [ split(/\s+/, $1) ];
45 /^Target-Profile:\s*(.+)\s*$/ and do {
51 push @{$target->{profiles}}, $profile;
53 /^Target-Profile-Name:\s*(.+)\s*$/ and $profile->{name} = $1;
54 /^Target-Profile-Packages:\s*(.*)\s*$/ and $profile->{packages} = [ split(/\s+/, $1) ];
55 /^Target-Profile-Description:\s*(.*)\s*/ and $profile->{desc} = get_multiline();
56 /^Target-Profile-Config:/ and $profile->{config} = get_multiline("\t");
57 /^Target-Profile-Kconfig:/ and $profile->{kconfig} = 1;
59 foreach my $target (@target) {
60 @{$target->{profiles}} > 0 or $target->{profiles} = [
71 sub parse_package_metadata() {
79 /^Source-Makefile: \s*((.+\/)([^\/]+)\/Makefile)\s*$/ and do {
83 $subdir =~ s/^package\///;
84 $subdir{$src} = $subdir;
85 $srcpackage{$src} = [];
88 /^Package:\s*(.+?)\s*$/ and do {
91 $pkg->{makefile} = $makefile;
93 $pkg->{default} = "m if ALL";
95 $pkg->{builddepends} = [];
96 $pkg->{subdir} = $subdir;
98 push @{$srcpackage{$src}}, $pkg;
100 /^Version: \s*(.+)\s*$/ and $pkg->{version} = $1;
101 /^Title: \s*(.+)\s*$/ and $pkg->{title} = $1;
102 /^Menu: \s*(.+)\s*$/ and $pkg->{menu} = $1;
103 /^Submenu: \s*(.+)\s*$/ and $pkg->{submenu} = $1;
104 /^Submenu-Depends: \s*(.+)\s*$/ and $pkg->{submenudep} = $1;
105 /^Default: \s*(.+)\s*$/ and $pkg->{default} = $1;
106 /^Provides: \s*(.+)\s*$/ and do {
107 my @vpkg = split /\s+/, $1;
108 foreach my $vpkg (@vpkg) {
109 $package{$vpkg} or $package{$vpkg} = { vdepends => [] };
110 push @{$package{$vpkg}->{vdepends}}, $pkg->{name};
113 /^Depends: \s*(.+)\s*$/ and $pkg->{depends} = [ split /\s+/, $1 ];
114 /^Build-Depends: \s*(.+)\s*$/ and $pkg->{builddepends} = [ split /\s+/, $1 ];
115 /^Category: \s*(.+)\s*$/ and do {
116 $pkg->{category} = $1;
117 defined $category{$1} or $category{$1} = {};
118 defined $category{$1}->{$src} or $category{$1}->{$src} = [];
119 push @{$category{$1}->{$src}}, $pkg;
121 /^Description: \s*(.*)\s*$/ and $pkg->{description} = "\t\t $1\n". get_multiline("\t\t ");
122 /^Config: \s*(.*)\s*$/ and $pkg->{config} = "$1\n".get_multiline();
123 /^Prereq-Check:/ and $pkg->{prereq} = 1;
124 /^Preconfig:\s*(.+)\s*$/ and do {
125 my $pkgname = $pkg->{name};
126 $preconfig{$pkgname} or $preconfig{$pkgname} = [];
130 push @{$preconfig{$pkgname}}, $preconfig;
132 /^Preconfig-Type:\s*(.*?)\s*$/ and $preconfig->{type} = $1;
133 /^Preconfig-Label:\s*(.*?)\s*$/ and $preconfig->{label} = $1;
134 /^Preconfig-Default:\s*(.*?)\s*$/ and $preconfig->{default} = $1;
139 sub gen_kconfig_overrides() {
143 my $pkginfo = shift @ARGV;
144 my $cfgfile = shift @ARGV;
146 # parameter 2: build system config
147 open FILE, "<$cfgfile" or return;
149 /^(CONFIG_.+?)=(.+)$/ and $config{$1} = 1;
153 # parameter 1: package metadata
154 open FILE, "<$pkginfo" or return;
156 /^Package:\s*(.+?)\s*$/ and $package = $1;
157 /^Kernel-Config:\s*(.+?)\s*$/ and do {
158 my @config = split /\s+/, $1;
159 foreach my $config (@config) {
162 if ($config =~ /^(.+?)=(.+)$/) {
167 if ($config{"CONFIG_PACKAGE_$package"} and ($config ne 'n')) {
168 $kconfig{$config} = $val;
169 } elsif (!$override) {
170 $kconfig{$config} or $kconfig{$config} = 'n';
177 foreach my $kconfig (sort keys %kconfig) {
178 if ($kconfig{$kconfig} eq 'n') {
179 print "# $kconfig is not set\n";
181 print "$kconfig=$kconfig{$kconfig}\n";
186 sub merge_package_lists($$) {
192 foreach my $pkg (@$list1, @$list2) {
195 foreach my $pkg (keys %pkgs) {
196 push @l, $pkg unless ($pkg =~ /^-/ or $pkgs{"-$pkg"});
201 sub gen_target_mk() {
202 my @target = parse_target_metadata();
205 $a->{board} cmp $b->{board}
208 foreach my $target (@target) {
209 my ($profiles_def, $profiles_eval);
211 foreach my $profile (@{$target->{profiles}}) {
213 define Profile/$target->{conf}\_$profile->{id}
215 NAME:=$profile->{name}
216 PACKAGES:=".join(" ", merge_package_lists($target->{packages}, $profile->{packages}))."\n";
217 $profile->{kconfig} and $profiles_def .= " KCONFIG:=1\n";
218 $profiles_def .= " endef";
220 \$(eval \$(call AddProfile,$target->{conf}\_$profile->{id}))"
223 ifeq (\$(CONFIG_TARGET_$target->{conf}),y)
225 KERNEL:=$target->{kernel}
226 BOARD:=$target->{board}
227 BOARDNAME:=$target->{name}
228 LINUX_VERSION:=$target->{version}
229 LINUX_RELEASE:=$target->{release}
230 LINUX_KARCH:=$target->{karch}
231 DEFAULT_PACKAGES:=".join(" ", @{$target->{packages}})."
237 print "\$(eval \$(call Target))\n";
240 sub target_config_features(@) {
243 while ($_ = shift @_) {
244 /broken/ and $ret .= "\tdepends BROKEN\n";
245 /pci/ and $ret .= "\tselect PCI_SUPPORT\n";
246 /usb/ and $ret .= "\tselect USB_SUPPORT\n";
247 /pcmcia/ and $ret .= "\tselect PCMCIA_SUPPORT\n";
248 /squashfs/ and $ret .= "\tselect USES_SQUASHFS\n";
249 /jffs2/ and $ret .= "\tselect USES_JFFS2\n";
250 /ext2/ and $ret .= "\tselect USES_EXT2\n";
251 /tgz/ and $ret .= "\tselect USES_TGZ\n";
257 sub gen_target_config() {
258 my @target = parse_target_metadata();
261 $a->{name} cmp $b->{name}
267 prompt "Target System"
268 default TARGET_BRCM_2_4
273 foreach my $target (@target) {
274 my $features = target_config_features(@{$target->{features}});
275 my $help = $target->{desc};
276 my $kernel = $target->{kernel};
281 if ($help =~ /\w+/) {
282 $help =~ s/^\s*/\t /mg;
283 $help = "\thelp\n$help";
289 config TARGET_$target->{conf}
290 bool "$target->{name}"
291 select $target->{arch}
302 prompt "Target Profile"
306 foreach my $target (@target) {
307 my $profiles = $target->{profiles};
309 foreach my $profile (@$profiles) {
311 config TARGET_$target->{conf}_$profile->{id}
312 bool "$profile->{name}"
313 depends TARGET_$target->{conf}
316 $profile->{kconfig} and print "\tselect PROFILE_KCONFIG\n";
317 my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
318 foreach my $pkg (@pkglist) {
319 print "\tselect DEFAULT_$pkg\n";
329 sub find_package_dep($$) {
332 my $deps = ($pkg->{vdepends} or $pkg->{depends});
334 return 0 unless defined $deps;
335 foreach my $dep (@{$deps}) {
336 return 1 if $dep eq $name;
337 return 1 if ($package{$dep} and (find_package_dep($package{$dep},$name) == 1));
342 sub package_depends($$) {
347 return 0 if ($a->{submenu} ne $b->{submenu});
348 if (find_package_dep($a, $b->{name}) == 1) {
350 } elsif (find_package_dep($b, $a->{name}) == 1) {
358 sub mconf_depends($$) {
360 my $only_dep = shift;
364 my @depends = @$depends;
365 foreach my $depend (@depends) {
367 $depend =~ s/^([@\+]+)//;
371 if ($vdep = $package{$depend}->{vdepends}) {
372 $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
374 $flags =~ /\+/ and do {
378 # Menuconfig will not treat 'select FOO' as a real dependency
379 # thus if FOO depends on other config options, these dependencies
380 # will not be checked. To fix this, we simply emit all of FOO's
381 # depends here as well.
382 $package{$depend} and $res .= mconf_depends($package{$depend}->{depends}, 1);
384 $flags =~ /@/ or $depend = "PACKAGE_$depend";
386 $res .= "\t\t$m $depend\n";
391 sub print_package_config_category($) {
396 return unless $category{$cat};
398 print "menu \"$cat\"\n\n";
399 my %spkg = %{$category{$cat}};
401 foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
402 foreach my $pkg (@{$spkg{$spkg}}) {
403 my $menu = $pkg->{submenu};
405 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
409 $menus{$menu} or $menus{$menu} = [];
410 push @{$menus{$menu}}, $pkg;
411 print "\tconfig DEFAULT_".$pkg->{name}."\n";
412 print "\t\tbool\n\n";
416 ($a eq 'undef' ? 1 : 0) or
417 ($b eq 'undef' ? -1 : 0) or
421 foreach my $menu (@menus) {
423 package_depends($a, $b) or
424 ($a->{name} cmp $b->{name})
426 if ($menu ne 'undef') {
427 $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
428 print "menu \"$menu\"\n";
430 foreach my $pkg (@pkgs) {
431 my $title = $pkg->{name};
432 my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
434 $title .= ("." x $c). " ". $pkg->{title};
437 $pkg->{menu} and print "menu";
438 print "config PACKAGE_".$pkg->{name}."\n";
439 print "\t\ttristate \"$title\"\n";
440 print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
441 foreach my $default (split /\s*,\s*/, $pkg->{default}) {
442 print "\t\tdefault $default\n";
444 print mconf_depends($pkg->{depends}, 0);
446 print $pkg->{description};
449 $pkg->{config} and print $pkg->{config}."\n";
451 if ($menu ne 'undef') {
453 $menu_dep{$menu} and print "endif\n";
458 undef $category{$cat};
461 sub gen_package_config() {
462 parse_package_metadata();
463 print "menuconfig UCI_PRECONFIG\n\tbool \"Image configuration\"\n";
464 foreach my $preconfig (keys %preconfig) {
465 foreach my $cfg (@{$preconfig{$preconfig}}) {
466 my $conf = $cfg->{id};
469 config UCI_PRECONFIG_$conf
470 string "$cfg->{label}" if UCI_PRECONFIG
471 depends PACKAGE_$preconfig
472 default "$cfg->{default}"
477 print_package_config_category 'Base system';
478 foreach my $cat (keys %category) {
479 print_package_config_category $cat;
483 sub gen_package_mk() {
488 parse_package_metadata();
489 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
491 my $pkg = $package{$name};
493 next if defined $pkg->{vdepends};
495 $conf{$pkg->{src}} or do {
497 $conf{$pkg->{src}} = 1;
500 $config = "\$(CONFIG_PACKAGE_$name)"
503 print "package-$config += $pkg->{subdir}$pkg->{src}\n";
504 $pkg->{prereq} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
509 foreach my $dep (@{$pkg->{depends}}, @{$pkg->{builddepends}}) {
513 my $pkg_dep = $package{$dep};
514 next if defined $pkg_dep->{vdepends};
516 if (defined $pkg_dep->{src}) {
517 ($pkg->{src} ne $pkg_dep->{src}) and $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
518 } elsif (defined($srcpackage{$dep})) {
519 $idx = $subdir{$dep}.$dep;
521 undef $idx if $idx =~ /^(kernel)|(base-files)$/;
523 next if $dep{$pkg->{src}."->".$idx};
524 $depline .= " \$(curdir)/$idx/compile";
525 $dep{$pkg->{src}."->".$idx} = 1;
529 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/compile += $depline\n";
536 foreach my $preconfig (keys %preconfig) {
538 foreach my $cfg (@{$preconfig{$preconfig}}) {
539 my $conf = $cfg->{id};
541 $cmds .= "\techo \"uci set '$cfg->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
546 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
551 ifneq (\$(UCI_PRECONFIG)\$(CONFIG_UCI_PRECONFIG),)
552 package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
559 sub parse_command() {
560 my $cmd = shift @ARGV;
562 /^target_mk$/ and return gen_target_mk();
563 /^target_config$/ and return gen_target_config();
564 /^package_mk$/ and return gen_package_mk();
565 /^package_config$/ and return gen_package_config();
566 /^kconfig/ and return gen_kconfig_overrides();
570 $0 target_mk [file] Target metadata in makefile format
571 $0 target_config [file] Target metadata in Kconfig format
572 $0 package_mk [file] Package metadata in makefile format
573 $0 package_config [file] Package metadata in Kconfig format
574 $0 kconfig [file] [config] Kernel config overrides