+sub gen_package_mk() {
+ my %conf;
+ my %dep;
+ my $line;
+
+ parse_package_metadata();
+ foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
+ my $config;
+ my $pkg = $package{$name};
+
+ 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->{src}\n";
+ $pkg->{prereq} and print "prereq-$config += $pkg->{src}\n";
+ }
+
+ my $hasdeps = 0;
+ my $depline = "";
+ foreach my $dep (@{$pkg->{depends}}, @{$pkg->{builddepends}}) {
+ next if $dep =~ /@/;
+ $dep =~ s/\+//;
+ my $idx;
+ my $pkg_dep = $package{$dep};
+ $pkg_dep or $pkg_dep = $srcpackage{$dep}->[0];
+ next unless defined $pkg_dep;
+ next if defined $pkg_dep->{vdepends};
+
+ if (defined $pkg_dep->{src}) {
+ ($pkg->{src} ne $pkg_dep->{src}) and $idx = $pkg_dep->{src};
+ } elsif (defined($pkg_dep) && !defined($ENV{SDK})) {
+ $idx = $dep;
+ }
+ undef $idx if $idx =~ /^(kernel)|(base-files)$/;
+ if ($idx) {
+ next if $dep{$pkg->{src}."->".$idx};
+ $depline .= " $idx\-compile";
+ $dep{$pkg->{src}."->".$idx} = 1;
+ }
+ }
+ if ($depline) {
+ $line .= "$pkg->{src}-compile: $depline\n";
+ }
+ }
+
+ if ($line ne "") {
+ print "\n$line";
+ }
+ foreach my $preconfig (keys %preconfig) {
+ my $cmds;
+ foreach my $cfg (@{$preconfig{$preconfig}}) {
+ my $conf = $cfg->{id};
+ $conf =~ tr/\.-/__/;
+ $cmds .= "\techo \"uci set '$cfg->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
+ }
+ next unless $cmds;
+ print <<EOF
+
+\$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
+ ( \\
+$cmds \\
+ ) > \$@
+
+preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
+EOF
+ }
+}
+
+