scripts/package-metadata.pl: sort dependency keys
[openwrt/staging/stintel.git] / scripts / package-metadata.pl
index 53bb45a62c83d628e62dc3857eb8d9d48d3b9ba3..3e9e4e986e9f7ad62e64c301bd3d7bee0a14238f 100755 (executable)
@@ -161,9 +161,6 @@ sub mconf_depends {
                my $condition = $parent_condition;
 
                next if $condition eq $depend;
-               next if $seen->{"$parent_condition:$depend"};
-               next if $seen->{":$depend"};
-               $seen->{"$parent_condition:$depend"} = 1;
                if ($depend =~ /^(.+):(.+)$/) {
                        if ($1 ne "PACKAGE_$pkgname") {
                                if ($condition) {
@@ -174,6 +171,9 @@ sub mconf_depends {
                        }
                        $depend = $2;
                }
+               next if $seen->{"$parent_condition:$depend"};
+               next if $seen->{":$depend"};
+               $seen->{"$parent_condition:$depend"} = 1;
                if ($flags =~ /\+/) {
                        my $vdep = $vpackage{$depend};
                        if ($vdep) {
@@ -191,9 +191,9 @@ sub mconf_depends {
                                $depend = shift @vdeps;
 
                                if (@vdeps > 1) {
-                                       $condition = ($condition ? "$condition && " : '') . '!('.join("||", map { "PACKAGE_".$_ } @vdeps).')';
+                                       $condition = ($condition ? "$condition && " : '') . join("&&", map { "PACKAGE_$_<PACKAGE_$pkgname" } @vdeps);
                                } elsif (@vdeps > 0) {
-                                       $condition = ($condition ? "$condition && " : '') . '!PACKAGE_'.$vdeps[0];
+                                       $condition = ($condition ? "$condition && " : '') . "PACKAGE_${vdeps[0]}<PACKAGE_$pkgname";
                                }
                        }
 
@@ -232,7 +232,7 @@ sub mconf_depends {
                mconf_depends($pkgname, $tdep->[0], 1, $dep, $seen, $tdep->[1]);
        }
 
-       foreach my $depend (keys %$dep) {
+       foreach my $depend (sort keys %$dep) {
                my $m = $dep->{$depend};
                $res .= "\t\t$m $depend\n";
        }
@@ -358,14 +358,30 @@ sub gen_package_config() {
        print_package_overrides();
 }
 
+sub and_condition($) {
+       my $condition = shift;
+       my @spl_and = split('\&\&', $condition);
+       if (@spl_and == 1) {
+               return "\$(CONFIG_$spl_and[0])";
+       }
+       return "\$(and " . join (',', map("\$(CONFIG_$_)", @spl_and)) . ")";
+}
+
+sub gen_condition ($) {
+       my $condition = shift;
+       # remove '!()', just as include/package-ipkg.mk does
+       $condition =~ s/[()!]//g;
+       return join("", map(and_condition($_), split('\|\|', $condition)));
+}
+
 sub get_conditional_dep($$) {
        my $condition = shift;
        my $depstr = shift;
        if ($condition) {
                if ($condition =~ /^!(.+)/) {
-                       return "\$(if \$(CONFIG_$1),,$depstr)";
+                       return "\$(if " . gen_condition($1) . ",,$depstr)";
                } else {
-                       return "\$(if \$(CONFIG_$condition),$depstr)";
+                       return "\$(if " . gen_condition($condition) . ",$depstr)";
                }
        } else {
                return $depstr;
@@ -509,13 +525,25 @@ sub gen_package_source() {
        }
 }
 
-sub gen_package_subdirs() {
+sub gen_package_auxiliary() {
        parse_package_metadata($ARGV[0]) or exit 1;
        foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
                my $pkg = $package{$name};
                if ($pkg->{name} && $pkg->{repository}) {
                        print "Package/$name/subdir = $pkg->{repository}\n";
                }
+               my %depends;
+               foreach my $dep (@{$pkg->{depends} || []}) {
+                       if ($dep =~ m!^\+?(?:[^:]+:)?([^@]+)$!) {
+                               $depends{$1}++;
+                       }
+               }
+               my @depends = sort keys %depends;
+               if (@depends > 0) {
+                       foreach my $n (@{$pkg->{provides}}) {
+                               print "Package/$n/depends = @depends\n";
+                       }
+               }
        }
 }
 
@@ -557,6 +585,40 @@ sub gen_usergroup_list() {
        }
 }
 
+sub gen_package_manifest_json() {
+       my $json;
+       parse_package_metadata($ARGV[0]) or exit 1;
+       foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
+               my %depends;
+               my $pkg = $package{$name};
+               foreach my $dep (@{$pkg->{depends} || []}) {
+                       if ($dep =~ m!^\+?(?:[^:]+:)?([^@]+)$!) {
+                               $depends{$1}++;
+                       }
+               }
+               my @depends = sort keys %depends;
+               my $pkg_deps = join ' ', map { qq/"$_",/ } @depends;
+               $pkg_deps =~ s/\,$//;
+
+               my $pkg_maintainer = join ' ', map { qq/"$_",/ } @{$pkg->{maintainer} || []};
+               $pkg_maintainer =~ s/\,$//;
+
+               $json = <<"END_JSON";
+${json}{
+"name":"$name",
+"version":"$pkg->{version}",
+"category":"$pkg->{category}",
+"license":"$pkg->{license}",
+"maintainer": [$pkg_maintainer],
+"depends":[$pkg_deps]},
+END_JSON
+       }
+
+       $json =~ s/[\n\r]//g;
+       $json =~ s/\,$//;
+       print "[$json]";
+}
+
 sub parse_command() {
        GetOptions("ignore=s", \@ignore);
        my $cmd = shift @ARGV;
@@ -565,7 +627,8 @@ sub parse_command() {
                /^config$/ and return gen_package_config();
                /^kconfig/ and return gen_kconfig_overrides();
                /^source$/ and return gen_package_source();
-               /^subdirs$/ and return gen_package_subdirs();
+               /^pkgaux$/ and return gen_package_auxiliary();
+               /^pkgmanifestjson$/ and return gen_package_manifest_json();
                /^license$/ and return gen_package_license(0);
                /^licensefull$/ and return gen_package_license(1);
                /^usergroup$/ and return gen_usergroup_list();
@@ -577,7 +640,8 @@ Available Commands:
        $0 config [file]                        Package metadata in Kconfig format
        $0 kconfig [file] [config] [patchver]   Kernel config overrides
        $0 source [file]                        Package source file information
-       $0 subdirs [file]                       Package subdir information in makefile format
+       $0 pkgaux [file]                        Package auxiliary variables in makefile format
+       $0 pkgmanifestjson [file]               Package manifests in JSON format
        $0 license [file]                       Package license information
        $0 licensefull [file]                   Package license information (full list)
        $0 usergroup [file]                     Package usergroup allocation list