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 /usb/ and $ret .= "\tselect USB_SUPPORT\n";
159 /pcmcia/ and $ret .= "\tselect PCMCIA_SUPPORT\n";
160 /squashfs/ and $ret .= "\tselect USES_SQUASHFS\n";
161 /jffs2/ and $ret .= "\tselect USES_JFFS2\n";
162 /ext2/ and $ret .= "\tselect USES_EXT2\n";
163 /tgz/ and $ret .= "\tselect USES_TGZ\n";
164 /cpiogz/ and $ret .= "\tselect USES_CPIOGZ\n";
165 /fpu/ and $ret .= "\tselect HAS_FPU\n";
166 /ramdisk/ and $ret .= "\tselect USES_INITRAMFS\n";
173 my $parent = $target->{parent
};
175 return $target->{parent
}->{name
}." - ".$target->{name
};
177 return $target->{name
};
184 $v =~ /(\d+_\d+_\d+)(_\d+)?/ and $v = $1;
188 sub print_target
($) {
190 my $features = target_config_features
(@
{$target->{features
}});
191 my $help = $target->{desc
};
192 my $kernel = $target->{kernel
};
198 if ($help =~ /\w+/) {
199 $help =~ s/^\s*/\t /mg;
200 $help = "\thelp\n$help";
205 my $v = kver
($target->{version
});
206 if (@
{$target->{subtargets
}} == 0) {
208 config TARGET_$target->{conf}
209 bool "$target->{name}"
216 config TARGET_$target->{conf}
217 bool "$target->{name}"
220 if ($target->{subtarget
}) {
221 $confstr .= "\tdepends TARGET_$target->{boardconf}\n";
223 if (@
{$target->{subtargets
}} > 0) {
224 $confstr .= "\tselect HAS_SUBTARGETS\n";
226 $confstr .= "\tselect $target->{arch}\n";
227 foreach my $dep (@
{$target->{depends
}}) {
228 my $mode = "depends";
232 $dep =~ /^([@\+\-]+)(.+)$/;
236 next if $name =~ /:/;
237 $flags =~ /-/ and $mode = "deselect";
238 $flags =~ /\+/ and $mode = "select";
239 $flags =~ /@/ and $confstr .= "\t$mode $name\n";
241 $confstr .= $features;
244 $confstr .= "$help\n\n";
248 sub gen_target_config
() {
249 my @target = parse_target_metadata
();
252 my @target_sort = sort {
253 target_name
($a) cmp target_name
($b);
259 prompt "Target System"
260 default TARGET_brcm_2_4
265 foreach my $target (@target_sort) {
266 next if $target->{subtarget
};
267 print_target
($target);
274 prompt "Subtarget" if HAS_SUBTARGETS
277 foreach my $target (@target) {
278 next unless $target->{subtarget
};
279 print_target
($target);
286 prompt "Target Profile"
290 foreach my $target (@target) {
291 my $profiles = $target->{profiles
};
293 foreach my $profile (@
$profiles) {
295 config TARGET_$target->{conf}_$profile->{id}
296 bool "$profile->{name}"
297 depends TARGET_$target->{conf}
300 $profile->{kconfig
} and print "\tselect PROFILE_KCONFIG\n";
301 my @pkglist = merge_package_lists
($target->{packages
}, $profile->{packages
});
302 foreach my $pkg (@pkglist) {
303 print "\tselect DEFAULT_$pkg\n";
306 my $help = $profile->{desc
};
307 if ($help =~ /\w+/) {
308 $help =~ s/^\s*/\t /mg;
309 $help = "\thelp\n$help";
320 config HAS_SUBTARGETS
327 foreach my $target (@target) {
328 $target->{subtarget
} or print "\t\tdefault \"".$target->{board
}."\" if TARGET_".$target->{conf
}."\n";
332 config DEFAULT_TARGET_OPTIMIZATION
335 foreach my $target (@target) {
336 next if @
{$target->{subtargets
}} > 0;
337 print "\tdefault \"".$target->{cflags
}."\" if TARGET_".$target->{conf
}."\n";
339 print "\tdefault \"-Os -pipe -funit-at-a-time\"\n";
342 foreach my $target (@target) {
343 my $v = kver
($target->{version
});
353 foreach my $def (sort keys %defaults) {
354 print "\tconfig DEFAULT_".$def."\n";
355 print "\t\tbool\n\n";
360 sub __find_package_dep
($$) {
363 my $deps = ($pkg->{vdepends
} or $pkg->{depends
});
365 return 0 unless defined $deps;
366 foreach my $dep (@
{$deps}) {
367 next if $dep_check{$dep};
368 $dep_check{$dep} = 1;
369 return 1 if $dep eq $name;
370 return 1 if ($package{$dep} and (__find_package_dep
($package{$dep},$name) == 1));
375 # wrapper to avoid infinite recursion
376 sub find_package_dep
($$) {
381 return __find_package_dep
($pkg, $name);
384 sub package_depends
($$) {
389 return 0 if ($a->{submenu
} ne $b->{submenu
});
390 if (find_package_dep
($a, $b->{name
}) == 1) {
392 } elsif (find_package_dep
($b, $a->{name
}) == 1) {
403 my $only_dep = shift;
407 my $parent_condition = shift;
412 my @depends = @
$depends;
413 foreach my $depend (@depends) {
415 $depend =~ s/^([@\+]+)//;
418 my $condition = $parent_condition;
420 if ($depend =~ /^(.+):(.+)$/) {
421 if ($1 ne "PACKAGE_$pkgname") {
423 $condition = "$condition && $1";
430 next if $seen->{$depend};
431 next if $package{$depend} and $package{$depend}->{buildonly
};
432 $seen->{$depend} = 1;
433 if ($vdep = $package{$depend}->{vdepends
}) {
434 $depend = join("||", map { "PACKAGE_".$_ } @
$vdep);
436 $flags =~ /\+/ and do {
437 # Menuconfig will not treat 'select FOO' as a real dependency
438 # thus if FOO depends on other config options, these dependencies
439 # will not be checked. To fix this, we simply emit all of FOO's
440 # depends here as well.
441 $package{$depend} and mconf_depends
($pkgname, $package{$depend}->{depends
}, 1, $dep, $seen, $condition);
446 $flags =~ /@/ or $depend = "PACKAGE_$depend";
448 if ($m =~ /select/) {
449 $depend = "$depend if $condition";
451 $depend = "!($condition) || $depend";
455 $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
457 foreach my $depend (keys %$dep) {
458 my $m = $dep->{$depend};
459 $res .= "\t\t$m $depend\n";
464 sub print_package_config_category
($) {
469 return unless $category{$cat};
471 print "menu \"$cat\"\n\n";
472 my %spkg = %{$category{$cat}};
474 foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
475 foreach my $pkg (@
{$spkg{$spkg}}) {
476 next if $pkg->{buildonly
};
477 my $menu = $pkg->{submenu
};
479 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep
};
483 $menus{$menu} or $menus{$menu} = [];
484 push @
{$menus{$menu}}, $pkg;
488 ($a eq 'undef' ?
1 : 0) or
489 ($b eq 'undef' ?
-1 : 0) or
493 foreach my $menu (@menus) {
495 package_depends
($a, $b) or
496 ($a->{name
} cmp $b->{name
})
498 if ($menu ne 'undef') {
499 $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
500 print "menu \"$menu\"\n";
502 foreach my $pkg (@pkgs) {
503 my $title = $pkg->{name
};
504 my $c = (72 - length($pkg->{name
}) - length($pkg->{title
}));
506 $title .= ("." x
$c). " ". $pkg->{title
};
509 $pkg->{menu
} and print "menu";
510 print "config PACKAGE_".$pkg->{name
}."\n";
511 print "\t\t".($pkg->{tristate
} ?
'tristate' : 'bool')." \"$title\"\n";
512 print "\t\tdefault y if DEFAULT_".$pkg->{name
}."\n";
513 foreach my $default (split /\s*,\s*/, $pkg->{default}) {
514 print "\t\tdefault $default\n";
516 print mconf_depends
($pkg->{name
}, $pkg->{depends
}, 0);
518 print $pkg->{description
};
521 $pkg->{config
} and print $pkg->{config
}."\n";
523 if ($menu ne 'undef') {
525 $menu_dep{$menu} and print "endif\n";
530 undef $category{$cat};
533 sub gen_package_config
() {
534 parse_package_metadata
($ARGV[0]) or exit 1;
535 print "menuconfig UCI_PRECONFIG\n\tbool \"Image configuration\"\n" if %preconfig;
536 foreach my $preconfig (keys %preconfig) {
537 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
538 my $conf = $preconfig{$preconfig}->{$cfg}->{id
};
541 config UCI_PRECONFIG_$conf
542 string "$preconfig{$preconfig}->{$cfg}->{label}" if UCI_PRECONFIG
543 depends PACKAGE_$preconfig
544 default "$preconfig{$preconfig}->{$cfg}->{default}"
549 print_package_config_category
'Base system';
550 foreach my $cat (keys %category) {
551 print_package_config_category
$cat;
555 sub get_conditional_dep
($$) {
556 my $condition = shift;
559 if ($condition =~ /^!(.+)/) {
560 return "\$(if \$(CONFIG_$1),,$depstr)";
562 return "\$(if \$(CONFIG_$condition),$depstr)";
569 sub gen_package_mk
() {
575 parse_package_metadata
($ARGV[0]) or exit 1;
576 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
578 my $pkg = $package{$name};
581 next if defined $pkg->{vdepends
};
584 $conf{$pkg->{src
}} or do {
586 $conf{$pkg->{src
}} = 1;
589 $config = "\$(CONFIG_PACKAGE_$name)"
592 $pkg->{buildonly
} and $config = "";
593 print "package-$config += $pkg->{subdir}$pkg->{src}\n";
594 $pkg->{prereq
} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
597 next if $done{$pkg->{src
}};
598 $done{$pkg->{src
}} = 1;
600 if (@
{$pkg->{buildtypes
}} > 0) {
601 print "buildtypes-$pkg->{subdir}$pkg->{src} = ".join(' ', @
{$pkg->{buildtypes
}})."\n";
604 foreach my $spkg (@
{$srcpackage{$pkg->{src
}}}) {
605 foreach my $dep (@
{$spkg->{depends
}}, @
{$spkg->{builddepends
}}) {
612 foreach my $type (@
{$pkg->{buildtypes
}}) {
616 next unless $pkg->{"builddepends/$type"};
617 foreach my $dep (@
{$pkg->{"builddepends/$type"}}) {
621 if ($dep =~ /^(.+):(.+)/) {
625 if ($dep =~ /^(.+)(\/.+)/) {
629 my $pkg_dep = $package{$dep};
630 next unless $pkg_dep;
633 if (defined $pkg_dep->{src
}) {
634 $idx = $pkg_dep->{subdir
}.$pkg_dep->{src
};
635 } elsif (defined($srcpackage{$dep})) {
636 $idx = $subdir{$dep}.$dep;
638 my $depstr = "\$(curdir)/$idx$suffix/compile";
639 my $depline = get_conditional_dep
($condition, $depstr);
641 $deplines{$dep} = $depline;
644 my $depline = join(" ", values %deplines);
646 $line .= "\$(curdir)/".$pkg->{subdir
}."$pkg->{src}/$type/compile += $depline\n";
652 foreach my $deps (@srcdeps) {
658 if ($deps =~ /^(.+):(.+)/) {
662 if ($deps =~ /^(.+)(\/.+)/) {
667 my $pkg_dep = $package{$deps};
670 if ($pkg_dep->{vdepends
}) {
671 @deps = @
{$pkg_dep->{vdepends
}};
676 foreach my $dep (@deps) {
677 $pkg_dep = $package{$deps};
678 if (defined $pkg_dep->{src
}) {
679 ($pkg->{src
} ne $pkg_dep->{src
}.$suffix) and $idx = $pkg_dep->{subdir
}.$pkg_dep->{src
};
680 } elsif (defined($srcpackage{$dep})) {
681 $idx = $subdir{$dep}.$dep;
684 undef $idx if $idx =~ /^(kernel)|(base-files)$/;
687 next if $pkg->{src
} eq $pkg_dep->{src
}.$suffix;
688 next if $dep{$pkg->{src
}."->".$idx};
689 next if $dep{$pkg->{src
}."->($dep)".$idx} and $pkg_dep->{vdepends
};
692 if ($pkg_dep->{vdepends
}) {
693 $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
694 $dep{$pkg->{src
}."->($dep)".$idx} = 1;
696 $depstr = "\$(curdir)/$idx/compile";
697 $dep{$pkg->{src
}."->".$idx} = 1;
699 $depline = get_conditional_dep
($condition, $depstr);
701 $deplines{$idx.$dep} = $depline;
706 my $depline = join(" ", values %deplines);
708 $line .= "\$(curdir)/".$pkg->{subdir
}."$pkg->{src}/compile += $depline\n";
715 foreach my $preconfig (keys %preconfig) {
717 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
718 my $conf = $preconfig{$preconfig}->{$cfg}->{id
};
720 $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
725 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
730 ifneq (\$(UCI_PRECONFIG)\$(CONFIG_UCI_PRECONFIG),)
731 package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
737 sub gen_package_source
() {
738 parse_package_metadata
($ARGV[0]) or exit 1;
739 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
740 my $pkg = $package{$name};
741 if ($pkg->{name
} && $pkg->{source
}) {
742 print "$pkg->{name}: ";
743 print "$pkg->{source}\n";
748 sub parse_command
() {
749 my $cmd = shift @ARGV;
751 /^target_config$/ and return gen_target_config
();
752 /^package_mk$/ and return gen_package_mk
();
753 /^package_config$/ and return gen_package_config
();
754 /^kconfig/ and return gen_kconfig_overrides
();
755 /^package_source$/ and return gen_package_source
();
759 $0 target_config [file] Target metadata in Kconfig format
760 $0 package_mk [file] Package metadata in makefile format
761 $0 package_config [file] Package metadata in Kconfig format
762 $0 kconfig [file] [config] Kernel config overrides
763 $0 package_source [file] Package source file information