fix image preconfiguration
[openwrt/openwrt.git] / scripts / metadata.pl
index fe135418ca1becae3ca613353f061a51bca7f3b3..03041f97c4c3ccacfc7aea48bf5afddb9c1969c3 100755 (executable)
@@ -5,6 +5,7 @@ my %package;
 my %srcpackage;
 my %category;
 my %subdir;
+my %board;
 
 sub get_multiline {
        my $prefix = shift;
@@ -18,20 +19,28 @@ sub get_multiline {
        return $str;
 }
 
+sub confstr($) {
+       my $conf = shift;
+       $conf =~ tr#/\.\-/#___#;
+       return $conf;
+}
+
 sub parse_target_metadata() {
        my ($target, @target, $profile);        
        while (<>) {
                chomp;
                /^Target:\s*(.+)\s*$/ and do {
-                       my $conf = uc $1;
-                       $conf =~ tr/\.-/__/;
                        $target = {
-                               conf => $conf,
-                               board => $1,
+                               id => $1,
+                               conf => confstr($1),
                                profiles => []
                        };
                        push @target, $target;
                };
+               /^Target-Board:\s*(.+)\s*$/ and do {
+                       $target->{board} = $1;
+                       $target->{boardconf} = confstr($1);
+               };
                /^Target-Kernel:\s*(\d+\.\d+)\s*$/ and $target->{kernel} = $1;
                /^Target-Name:\s*(.+)\s*$/ and $target->{name} = $1;
                /^Target-Path:\s*(.+)\s*$/ and $target->{path} = $1;
@@ -123,11 +132,15 @@ sub parse_package_metadata() {
                /^Prereq-Check:/ and $pkg->{prereq} = 1;
                /^Preconfig:\s*(.+)\s*$/ and do {
                        my $pkgname = $pkg->{name};
-                       $preconfig{$pkgname} or $preconfig{$pkgname} = [];
-                       $preconfig = {
-                               id => $1
-                       };
-                       push @{$preconfig{$pkgname}}, $preconfig;
+                       $preconfig{$pkgname} or $preconfig{$pkgname} = {};
+                       if (exists $preconfig{$pkgname}->{$1}) {
+                               $preconfig = $preconfig{$pkgname}->{$1};
+                       } else {
+                               $preconfig = {
+                                       id => $1
+                               };
+                               $preconfig{$pkgname}->{$1} = $preconfig;
+                       }
                };
                /^Preconfig-Type:\s*(.*?)\s*$/ and $preconfig->{type} = $1;
                /^Preconfig-Label:\s*(.*?)\s*$/ and $preconfig->{label} = $1;
@@ -198,45 +211,6 @@ sub merge_package_lists($$) {
        return sort(@l);
 }
 
-sub gen_target_mk() {
-       my @target = parse_target_metadata();
-       
-       @target = sort {
-               $a->{board} cmp $b->{board}
-       } @target;
-       
-       foreach my $target (@target) {
-               my ($profiles_def, $profiles_eval);
-
-               foreach my $profile (@{$target->{profiles}}) {
-                       $profiles_def .= "
-  define Profile/$target->{conf}\_$profile->{id}
-    ID:=$profile->{id}
-    NAME:=$profile->{name}
-    PACKAGES:=".join(" ", merge_package_lists($target->{packages}, $profile->{packages}))."\n";
-                       $profile->{kconfig} and $profiles_def .= "    KCONFIG:=1\n";
-                       $profiles_def .= "  endef";
-                       $profiles_eval .= "
-\$(eval \$(call AddProfile,$target->{conf}\_$profile->{id}))"
-               }
-               print "
-ifeq (\$(CONFIG_TARGET_$target->{conf}),y)
-  define Target
-    KERNEL:=$target->{kernel}
-    BOARD:=$target->{board}
-    BOARDNAME:=$target->{name}
-    LINUX_VERSION:=$target->{version}
-    LINUX_RELEASE:=$target->{release}
-    LINUX_KARCH:=$target->{karch}
-    DEFAULT_PACKAGES:=".join(" ", @{$target->{packages}})."
-  endef$profiles_def
-endif$profiles_eval
-
-"
-       }
-       print "\$(eval \$(call Target))\n";
-}
-
 sub target_config_features(@) {
        my $ret;
 
@@ -265,7 +239,7 @@ sub gen_target_config() {
        print <<EOF;
 choice
        prompt "Target System"
-       default TARGET_BRCM_2_4
+       default TARGET_brcm_2_4
        reset if !DEVEL
        
 EOF
@@ -285,19 +259,38 @@ EOF
                        undef $help;
                }
        
-               print <<EOF
+               print <<EOF;
 config TARGET_$target->{conf}
        bool "$target->{name}"
        select $target->{arch}
        select LINUX_$kernel
-$features$help
-
 EOF
+               if ($target->{id} ne $target->{board}) {
+                       print "\tselect TARGET_".$target->{boardconf}."\n";
+               }
+               print "$features$help\n\n"
        }
 
        print <<EOF;
 endchoice
 
+config TARGET_BOARD
+       string
+EOF
+       foreach my $target (@target) {
+               print "\t\tdefault \"".$target->{board}."\" if TARGET_".$target->{conf}."\n";
+       }
+
+       # add hidden target config options 
+       foreach my $target (@target) {
+               next if $board{$target->{board}};
+               if ($target->{id} ne $target->{board}) {
+                       print "\nconfig TARGET_".$target->{boardconf}."\n\tbool\n";
+                       $board{$target->{board}} = 1;
+               }
+       }
+       print <<EOF;
+
 choice
        prompt "Target Profile"
 
@@ -462,14 +455,14 @@ sub gen_package_config() {
        parse_package_metadata();
        print "menuconfig UCI_PRECONFIG\n\tbool \"Image configuration\"\n";
        foreach my $preconfig (keys %preconfig) {
-               foreach my $cfg (@{$preconfig{$preconfig}}) {
-                       my $conf = $cfg->{id};
+               foreach my $cfg (keys %{$preconfig{$preconfig}}) {
+                       my $conf = $preconfig{$preconfig}->{$cfg}->{id};
                        $conf =~ tr/\.-/__/;
                        print <<EOF
        config UCI_PRECONFIG_$conf
-               string "$cfg->{label}" if UCI_PRECONFIG
+               string "$preconfig{$preconfig}->{$cfg}->{label}" if UCI_PRECONFIG
                depends PACKAGE_$preconfig
-               default "$cfg->{default}"
+               default "$preconfig{$preconfig}->{$cfg}->{default}"
 
 EOF
                }
@@ -535,10 +528,10 @@ sub gen_package_mk() {
        }
        foreach my $preconfig (keys %preconfig) {
                my $cmds;
-               foreach my $cfg (@{$preconfig{$preconfig}}) {
-                       my $conf = $cfg->{id};
+               foreach my $cfg (keys %{$preconfig{$preconfig}}) {
+                       my $conf = $preconfig{$preconfig}->{$cfg}->{id};
                        $conf =~ tr/\.-/__/;
-                       $cmds .= "\techo \"uci set '$cfg->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
+                       $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
                }
                next unless $cmds;
                print <<EOF
@@ -559,7 +552,6 @@ EOF
 sub parse_command() {
        my $cmd = shift @ARGV;
        for ($cmd) {
-               /^target_mk$/ and return gen_target_mk();
                /^target_config$/ and return gen_target_config();
                /^package_mk$/ and return gen_package_mk();
                /^package_config$/ and return gen_package_config();
@@ -567,7 +559,6 @@ sub parse_command() {
        }
        print <<EOF
 Available Commands:
-       $0 target_mk [file]             Target metadata in makefile format
        $0 target_config [file]         Target metadata in Kconfig format
        $0 package_mk [file]            Package metadata in makefile format
        $0 package_config [file]        Package metadata in Kconfig format