+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) {
+ print "package-$config += $pkg->{subdir}$pkg->{src}\n";
+ $pkg->{prereq} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
+ }
+
+ next if $done{$pkg->{src}};
+ $done{$pkg->{src}} = 1;
+
+ foreach my $spkg (@{$srcpackage{$pkg->{src}}}) {
+ foreach my $dep (@{$spkg->{depends}}, @{$spkg->{builddepends}}) {
+ $dep =~ /@/ or do {
+ $dep =~ s/\+//g;
+ push @srcdeps, $dep;
+ };
+ }
+ }
+
+ my $hasdeps = 0;
+ my $depline = "";
+ foreach my $deps (@srcdeps) {
+ my $idx;
+ my $condition;
+
+ if ($deps =~ /^(.+):(.+)/) {
+ $condition = $1;
+ $deps = $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}) and $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
+ } elsif (defined($srcpackage{$dep})) {
+ $idx = $subdir{$dep}.$dep;
+ }
+ undef $idx if $idx =~ /^(kernel)|(base-files)$/;
+ if ($idx) {
+ 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}) {
+ $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
+ $dep{$pkg->{src}."->($dep)".$idx} = 1;
+ } else {
+ $depstr = "\$(curdir)/$idx/compile";
+ $dep{$pkg->{src}."->".$idx} = 1;
+ }
+ if ($condition) {
+ $depline .= " \$(if \$(CONFIG_$condition),$depstr)";
+ } else {
+ $depline .= " $depstr";
+ }
+ }
+ }
+ }
+ 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
+
+\$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
+ ( \\
+$cmds \\
+ ) > \$@
+
+ifneq (\$(UCI_PRECONFIG)\$(CONFIG_UCI_PRECONFIG),)
+ package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
+endif
+EOF
+ }
+}
+
+