+sub get_conditional_dep($$) {
+ my $condition = shift;
+ my $depstr = shift;
+ if ($condition) {
+ if ($condition =~ /^!(.+)/) {
+ return "\$(if \$(CONFIG_$1),,$depstr)";
+ } else {
+ return "\$(if \$(CONFIG_$condition),$depstr)";
+ }
+ } else {
+ return $depstr;
+ }
+}
+
+sub gen_package_mk() {
+ my %conf;
+ my %dep;
+ my %done;
+ my $line;
+
+ parse_package_metadata($ARGV[0]) or exit 1;
+ foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
+ my $config;
+ my $pkg = $package{$name};
+ my @srcdeps;
+
+ next if defined $pkg->{vdepends};
+
+ if ($ENV{SDK}) {
+ $conf{$pkg->{src}} or do {
+ $config = 'm';
+ $conf{$pkg->{src}} = 1;
+ };
+ } else {
+ $config = "\$(CONFIG_PACKAGE_$name)"
+ }
+ if ($config) {
+ $pkg->{buildonly} and $config = "";
+ print "package-$config += $pkg->{subdir}$pkg->{src}\n";
+ if ($pkg->{variant}) {
+ if (!defined($done{$pkg->{src}})) {
+ print "\$(curdir)/$pkg->{subdir}$pkg->{src}/default-variant := $pkg->{variant}\n";
+ }
+ print "\$(curdir)/$pkg->{subdir}$pkg->{src}/variants += \$(if $config,$pkg->{variant})\n"
+ }
+ $pkg->{prereq} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
+ }
+
+ next if $done{$pkg->{src}};
+ $done{$pkg->{src}} = 1;
+
+ if (@{$pkg->{buildtypes}} > 0) {
+ print "buildtypes-$pkg->{subdir}$pkg->{src} = ".join(' ', @{$pkg->{buildtypes}})."\n";
+ }
+
+ foreach my $spkg (@{$srcpackage{$pkg->{src}}}) {
+ foreach my $dep (@{$spkg->{depends}}, @{$spkg->{builddepends}}) {
+ $dep =~ /@/ or do {
+ $dep =~ s/\+//g;
+ push @srcdeps, $dep;
+ };
+ }
+ }
+ foreach my $type (@{$pkg->{buildtypes}}) {
+ my @extra_deps;
+ my %deplines;
+
+ next unless $pkg->{"builddepends/$type"};
+ foreach my $dep (@{$pkg->{"builddepends/$type"}}) {
+ my $suffix = "";
+ my $condition;
+
+ if ($dep =~ /^(.+):(.+)/) {
+ $condition = $1;
+ $dep = $2;
+ }
+ if ($dep =~ /^(.+)(\/.+)/) {
+ $dep = $1;
+ $suffix = $2;
+ }
+
+ my $idx = "";
+ my $pkg_dep = $package{$dep};
+ if (defined($pkg_dep) && defined($pkg_dep->{src})) {
+ $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
+ } elsif (defined($srcpackage{$dep})) {
+ $idx = $subdir{$dep}.$dep;
+ } else {
+ next;
+ }
+ my $depstr = "\$(curdir)/$idx$suffix/compile";
+ my $depline = get_conditional_dep($condition, $depstr);
+ if ($depline) {
+ $deplines{$dep} = $depline;
+ }
+ }
+ my $depline = join(" ", values %deplines);
+ if ($depline) {
+ $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/$type/compile += $depline\n";
+ }
+ }
+
+ my $hasdeps = 0;
+ my %deplines;
+ foreach my $deps (@srcdeps) {
+ my $idx;
+ my $condition;
+ my $prefix = "";
+ my $suffix = "";
+
+ if ($deps =~ /^(.+):(.+)/) {
+ $condition = $1;
+ $deps = $2;
+ }
+ if ($deps =~ /^(.+)(\/.+)/) {
+ $deps = $1;
+ $suffix = $2;
+ }
+
+ my $pkg_dep = $package{$deps};
+ my @deps;
+
+ if ($pkg_dep->{vdepends}) {
+ @deps = @{$pkg_dep->{vdepends}};
+ } else {
+ @deps = ($deps);
+ }
+
+ foreach my $dep (@deps) {
+ $pkg_dep = $package{$deps};
+ if (defined $pkg_dep->{src}) {
+ ($pkg->{src} ne $pkg_dep->{src}.$suffix) and $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
+ } elsif (defined($srcpackage{$dep})) {
+ $idx = $subdir{$dep}.$dep;
+ }
+ $idx .= $suffix;
+ undef $idx if $idx =~ /^(kernel)|(base-files)$/;
+ if ($idx) {
+ my $depline;
+ next if $pkg->{src} eq $pkg_dep->{src}.$suffix;
+ next if $dep{$condition.":".$pkg->{src}."->".$idx};
+ next if $dep{$pkg->{src}."->($dep)".$idx} and $pkg_dep->{vdepends};
+ my $depstr;
+
+ if ($pkg_dep->{vdepends}) {
+ $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
+ $dep{$pkg->{src}."->($dep)".$idx} = 1;
+ } else {
+ $depstr = "\$(curdir)/$idx/compile";
+ $dep{$pkg->{src}."->".$idx} = 1;
+ }
+ $depline = get_conditional_dep($condition, $depstr);
+ if ($depline) {
+ $deplines{$idx.$dep} = $depline;
+ }
+ }
+ }
+ }
+ my $depline = join(" ", values %deplines);
+ if ($depline) {
+ $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/compile += $depline\n";
+ }
+ }
+
+ if ($line ne "") {
+ print "\n$line";
+ }
+ foreach my $preconfig (keys %preconfig) {
+ my $cmds;
+ foreach my $cfg (keys %{$preconfig{$preconfig}}) {
+ my $conf = $preconfig{$preconfig}->{$cfg}->{id};
+ $conf =~ tr/\.-/__/;
+ $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
+ }
+ next unless $cmds;
+ print <<EOF
+
+ifndef DUMP_TARGET_DB
+\$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
+ ( \\
+$cmds \\
+ ) > \$@
+
+ifneq (\$(IMAGEOPT)\$(CONFIG_IMAGEOPT),)
+ package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
+endif
+endif
+
+EOF
+ }
+}
+
+sub gen_package_source() {
+ 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->{source}) {
+ print "$pkg->{name}: ";
+ print "$pkg->{source}\n";
+ }
+ }
+}
+