fix infinite recursion in metadata.pl
[openwrt/openwrt.git] / scripts / metadata.pl
index d920f59fcaf60a5beccbe6b88c1c691b04088736..9c10f9554ca01c7e8c4d9ca44c6bad04ecb64488 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
 use FindBin;
 use lib "$FindBin::Bin";
 use strict;
@@ -223,6 +223,7 @@ EOF
                        $flags = $1;
                        $name = $2;
 
+                       next if $name =~ /:/;
                        $flags =~ /-/ and $mode = "deselect";
                        $flags =~ /\+/ and $mode = "select";
                        $flags =~ /@/ and $confstr .= "\t$mode $name\n";
@@ -366,7 +367,9 @@ sub mconf_depends($$) {
        my $only_dep = shift;
        my $res;
        my $dep = shift;
+       my $seen = shift;
        $dep or $dep = {};
+       $seen or $seen = {};
 
        $depends or return;
        my @depends = @$depends;
@@ -374,8 +377,15 @@ sub mconf_depends($$) {
                my $m = "depends";
                $depend =~ s/^([@\+]+)//;
                my $flags = $1;
+               my $condition;
                my $vdep;
 
+               if ($depend =~ /^(.+):(.+)$/) {
+                       $condition = $1;
+                       $depend = $2;
+               }
+               next if $seen->{$depend};
+               $seen->{$depend} = 1;
                if ($vdep = $package{$depend}->{vdepends}) {
                        $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
                } else {
@@ -384,12 +394,13 @@ sub mconf_depends($$) {
                                # thus if FOO depends on other config options, these dependencies
                                # will not be checked. To fix this, we simply emit all of FOO's
                                # depends here as well.
-                               $package{$depend} and mconf_depends($package{$depend}->{depends}, 1, $dep);
+                               $package{$depend} and mconf_depends($package{$depend}->{depends}, 1, $dep, $seen);
 
                                $m = "select";
                                next if $only_dep;
                        };
                        $flags =~ /@/ or $depend = "PACKAGE_$depend";
+                       $condition and $depend = "$depend if $condition";
                }
                $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
        }
@@ -472,7 +483,7 @@ sub print_package_config_category($) {
 
 sub gen_package_config() {
        parse_package_metadata($ARGV[0]) or exit 1;
-       print "menuconfig UCI_PRECONFIG\n\tbool \"Image configuration\"\n";
+       print "menuconfig UCI_PRECONFIG\n\tbool \"Image configuration\"\n" if %preconfig;
        foreach my $preconfig (keys %preconfig) {
                foreach my $cfg (keys %{$preconfig{$preconfig}}) {
                        my $conf = $preconfig{$preconfig}->{$cfg}->{id};
@@ -535,6 +546,13 @@ sub gen_package_mk() {
                my $depline = "";
                foreach my $deps (@srcdeps) {
                        my $idx;
+                       my $condition;
+
+                       if ($deps =~ /^(.+):(.+)/) {
+                               $condition = $1;
+                               $deps = $2;
+                       }
+
                        my $pkg_dep = $package{$deps};
                        my @deps;
 
@@ -556,13 +574,20 @@ sub gen_package_mk() {
                                        next if $pkg->{src} eq $pkg_dep->{src};
                                        next if $dep{$pkg->{src}."->".$idx};
                                        next if $dep{$pkg->{src}."->($dep)".$idx};
+                                       my $depstr;
+
                                        if ($pkg_dep->{vdepends}) {
-                                               $depline .= " \$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
+                                               $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
                                                $dep{$pkg->{src}."->($dep)".$idx} = 1;
                                        } else {
-                                               $depline .= " \$(curdir)/$idx/compile";
+                                               $depstr = "\$(curdir)/$idx/compile";
                                                $dep{$pkg->{src}."->".$idx} = 1;
                                        }
+                                       if ($condition) {
+                                               $depline .= " \$(if \$(CONFIG_$condition),$depstr)";
+                                       } else {
+                                               $depline .= " $depstr";
+                                       }
                                }
                        }
                }