preinit: fix #6596
[openwrt.git] / scripts / metadata.pl
1 #!/usr/bin/env perl
2 use FindBin;
3 use lib "$FindBin::Bin";
4 use strict;
5 use metadata;
6
7 my %board;
8
9 sub confstr($) {
10         my $conf = shift;
11         $conf =~ tr#/\.\-/#___#;
12         return $conf;
13 }
14
15 sub parse_target_metadata() {
16         my $file = shift @ARGV;
17         my ($target, @target, $profile);
18         my %target;
19
20         open FILE, "<$file" or do {
21                 warn "Can't open file '$file': $!\n";
22                 return;
23         };
24         while (<FILE>) {
25                 chomp;
26                 /^Target:\s*(.+)\s*$/ and do {
27                         my $name = $1;
28                         $target = {
29                                 id => $name,
30                                 board => $name,
31                                 boardconf => confstr($name),
32                                 conf => confstr($name),
33                                 profiles => [],
34                                 features => [],
35                                 depends => [],
36                                 subtargets => []
37                         };
38                         push @target, $target;
39                         $target{$name} = $target;
40                         if ($name =~ /([^\/]+)\/([^\/]+)/) {
41                                 push @{$target{$1}->{subtargets}}, $2;
42                                 $target->{board} = $1;
43                                 $target->{boardconf} = confstr($1);
44                                 $target->{subtarget} = 1;
45                                 $target->{parent} = $target{$1};
46                         }
47                 };
48                 /^Target-Kernel:\s*(\d+\.\d+)\s*$/ and $target->{kernel} = $1;
49                 /^Target-Name:\s*(.+)\s*$/ and $target->{name} = $1;
50                 /^Target-Path:\s*(.+)\s*$/ and $target->{path} = $1;
51                 /^Target-Arch:\s*(.+)\s*$/ and $target->{arch} = $1;
52                 /^Target-Features:\s*(.+)\s*$/ and $target->{features} = [ split(/\s+/, $1) ];
53                 /^Target-Depends:\s*(.+)\s*$/ and $target->{depends} = [ split(/\s+/, $1) ];
54                 /^Target-Description:/ and $target->{desc} = get_multiline(*FILE);
55                 /^Target-Optimization:\s*(.+)\s*$/ and $target->{cflags} = $1;
56                 /^Linux-Version:\s*(.+)\s*$/ and $target->{version} = $1;
57                 /^Linux-Release:\s*(.+)\s*$/ and $target->{release} = $1;
58                 /^Linux-Kernel-Arch:\s*(.+)\s*$/ and $target->{karch} = $1;
59                 /^Default-Packages:\s*(.+)\s*$/ and $target->{packages} = [ split(/\s+/, $1) ];
60                 /^Target-Profile:\s*(.+)\s*$/ and do {
61                         $profile = {
62                                 id => $1,
63                                 name => $1,
64                                 packages => []
65                         };
66                         push @{$target->{profiles}}, $profile;
67                 };
68                 /^Target-Profile-Name:\s*(.+)\s*$/ and $profile->{name} = $1;
69                 /^Target-Profile-Packages:\s*(.*)\s*$/ and $profile->{packages} = [ split(/\s+/, $1) ];
70                 /^Target-Profile-Description:\s*(.*)\s*/ and $profile->{desc} = get_multiline(*FILE);
71                 /^Target-Profile-Config:/ and $profile->{config} = get_multiline(*FILE, "\t");
72                 /^Target-Profile-Kconfig:/ and $profile->{kconfig} = 1;
73         }
74         close FILE;
75         foreach my $target (@target) {
76                 next if @{$target->{subtargets}} > 0;
77                 @{$target->{profiles}} > 0 or $target->{profiles} = [
78                         {
79                                 id => 'Default',
80                                 name => 'Default',
81                                 packages => []
82                         }
83                 ];
84         }
85         return @target;
86 }
87
88 sub gen_kconfig_overrides() {
89         my %config;
90         my %kconfig;
91         my $package;
92         my $pkginfo = shift @ARGV;
93         my $cfgfile = shift @ARGV;
94
95         # parameter 2: build system config
96         open FILE, "<$cfgfile" or return;
97         while (<FILE>) {
98                 /^(CONFIG_.+?)=(.+)$/ and $config{$1} = 1;
99         }
100         close FILE;
101
102         # parameter 1: package metadata
103         open FILE, "<$pkginfo" or return;
104         while (<FILE>) {
105                 /^Package:\s*(.+?)\s*$/ and $package = $1;
106                 /^Kernel-Config:\s*(.+?)\s*$/ and do {
107                         my @config = split /\s+/, $1;
108                         foreach my $config (@config) {
109                                 my $val = 'm';
110                                 my $override;
111                                 if ($config =~ /^(.+?)=(.+)$/) {
112                                         $config = $1;
113                                         $override = 1;
114                                         $val = $2;
115                                 }
116                                 if ($config{"CONFIG_PACKAGE_$package"} and ($config ne 'n')) {
117                                         $kconfig{$config} = $val;
118                                 } elsif (!$override) {
119                                         $kconfig{$config} or $kconfig{$config} = 'n';
120                                 }
121                         }
122                 };
123         };
124         close FILE;
125
126         foreach my $kconfig (sort keys %kconfig) {
127                 if ($kconfig{$kconfig} eq 'n') {
128                         print "# $kconfig is not set\n";
129                 } else {
130                         print "$kconfig=$kconfig{$kconfig}\n";
131                 }
132         }
133 }
134
135 sub merge_package_lists($$) {
136         my $list1 = shift;
137         my $list2 = shift;
138         my @l = ();
139         my %pkgs;
140
141         foreach my $pkg (@$list1, @$list2) {
142                 $pkgs{$pkg} = 1;
143         }
144         foreach my $pkg (keys %pkgs) {
145                 push @l, $pkg unless ($pkg =~ /^-/ or $pkgs{"-$pkg"});
146         }
147         return sort(@l);
148 }
149
150 sub target_config_features(@) {
151         my $ret;
152
153         while ($_ = shift @_) {
154                 /broken/ and $ret .= "\tdepends BROKEN\n";
155                 /display/ and $ret .= "\tselect DISPLAY_SUPPORT\n";
156                 /gpio/ and $ret .= "\tselect GPIO_SUPPORT\n";
157                 /pci/ and $ret .= "\tselect PCI_SUPPORT\n";
158                 /pcie/ and $ret .= "\tselect PCIE_SUPPORT\n";
159                 /usb/ and $ret .= "\tselect USB_SUPPORT\n";
160                 /pcmcia/ and $ret .= "\tselect PCMCIA_SUPPORT\n";
161                 /squashfs/ and $ret .= "\tselect USES_SQUASHFS\n";
162                 /jffs2/ and $ret .= "\tselect USES_JFFS2\n";
163                 /ext2/ and $ret .= "\tselect USES_EXT2\n";
164                 /tgz/ and $ret .= "\tselect USES_TGZ\n";
165                 /cpiogz/ and $ret .= "\tselect USES_CPIOGZ\n";
166                 /ubifs/ and $ret .= "\tselect USES_UBIFS\n";
167                 /fpu/ and $ret .= "\tselect HAS_FPU\n";
168                 /ramdisk/ and $ret .= "\tselect USES_INITRAMFS\n";
169                 /powerpc64/ and $ret .= "\tselect powerpc64\n";
170                 /nommu/ and $ret .= "\tselect NOMMU\n";
171         }
172         return $ret;
173 }
174
175 sub target_name($) {
176         my $target = shift;
177         my $parent = $target->{parent};
178         if ($parent) {
179                 return $target->{parent}->{name}." - ".$target->{name};
180         } else {
181                 return $target->{name};
182         }
183 }
184
185 sub kver($) {
186         my $v = shift;
187         $v =~ tr/\./_/;
188         $v =~ /(\d+_\d+_\d+)(_\d+)?/ and $v = $1;
189         return $v;
190 }
191
192 sub print_target($) {
193         my $target = shift;
194         my $features = target_config_features(@{$target->{features}});
195         my $help = $target->{desc};
196         my $kernel = $target->{kernel};
197         my $confstr;
198         $kernel =~ tr/./_/;
199
200         chomp $features;
201         $features .= "\n";
202         if ($help =~ /\w+/) {
203                 $help =~ s/^\s*/\t  /mg;
204                 $help = "\thelp\n$help";
205         } else {
206                 undef $help;
207         }
208
209         my $v = kver($target->{version});
210         if (@{$target->{subtargets}} == 0) {
211         $confstr = <<EOF;
212 config TARGET_$target->{conf}
213         bool "$target->{name}"
214         select LINUX_$kernel
215         select LINUX_$v
216 EOF
217         }
218         else {
219                 $confstr = <<EOF;
220 config TARGET_$target->{conf}
221         bool "$target->{name}"
222 EOF
223         }
224         if ($target->{subtarget}) {
225                 $confstr .= "\tdepends TARGET_$target->{boardconf}\n";
226         }
227         if (@{$target->{subtargets}} > 0) {
228                 $confstr .= "\tselect HAS_SUBTARGETS\n";
229         }
230
231         if ($target->{arch} =~ /\w/) {
232                 $confstr .= "\tselect $target->{arch}\n";
233         }
234         foreach my $dep (@{$target->{depends}}) {
235                 my $mode = "depends";
236                 my $flags;
237                 my $name;
238
239                 $dep =~ /^([@\+\-]+)(.+)$/;
240                 $flags = $1;
241                 $name = $2;
242
243                 next if $name =~ /:/;
244                 $flags =~ /-/ and $mode = "deselect";
245                 $flags =~ /\+/ and $mode = "select";
246                 $flags =~ /@/ and $confstr .= "\t$mode $name\n";
247         }
248         $confstr .= $features;
249         $confstr .= "$help\n\n";
250         print $confstr;
251 }
252
253 sub gen_target_config() {
254         my @target = parse_target_metadata();
255         my %defaults;
256
257         my @target_sort = sort {
258                 target_name($a) cmp target_name($b);
259         } @target;
260
261
262         print <<EOF;
263 choice
264         prompt "Target System"
265         default TARGET_brcm_2_4
266         reset if !DEVEL
267         
268 EOF
269
270         foreach my $target (@target_sort) {
271                 next if $target->{subtarget};
272                 print_target($target);
273         }
274
275         print <<EOF;
276 endchoice
277
278 choice
279         prompt "Subtarget" if HAS_SUBTARGETS
280
281 EOF
282         foreach my $target (@target) {
283                 next unless $target->{subtarget};
284                 print_target($target);
285         }
286
287 print <<EOF;
288 endchoice
289
290 choice
291         prompt "Target Profile"
292
293 EOF
294
295         foreach my $target (@target) {
296                 my $profiles = $target->{profiles};
297
298                 foreach my $profile (@$profiles) {
299                         print <<EOF;
300 config TARGET_$target->{conf}_$profile->{id}
301         bool "$profile->{name}"
302         depends TARGET_$target->{conf}
303 $profile->{config}
304 EOF
305                         $profile->{kconfig} and print "\tselect PROFILE_KCONFIG\n";
306                         my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
307                         foreach my $pkg (@pkglist) {
308                                 print "\tselect DEFAULT_$pkg\n";
309                                 $defaults{$pkg} = 1;
310                         }
311                         my $help = $profile->{desc};
312                         if ($help =~ /\w+/) {
313                                 $help =~ s/^\s*/\t  /mg;
314                                 $help = "\thelp\n$help";
315                         } else {
316                                 undef $help;
317                         }
318                         print "$help\n";
319                 }
320         }
321
322         print <<EOF;
323 endchoice
324
325 config HAS_SUBTARGETS
326         bool
327
328 config TARGET_BOARD
329         string
330
331 EOF
332         foreach my $target (@target) {
333                 $target->{subtarget} or print "\t\tdefault \"".$target->{board}."\" if TARGET_".$target->{conf}."\n";
334         }
335         print <<EOF;
336
337 config DEFAULT_TARGET_OPTIMIZATION
338         string
339 EOF
340         foreach my $target (@target) {
341                 next if @{$target->{subtargets}} > 0;
342                 print "\tdefault \"".$target->{cflags}."\" if TARGET_".$target->{conf}."\n";
343         }
344         print "\tdefault \"-Os -pipe -funit-at-a-time\"\n";
345
346         my %kver;
347         foreach my $target (@target) {
348                 my $v = kver($target->{version});
349                 next if $kver{$v};
350                 $kver{$v} = 1;
351                 print <<EOF;
352
353 config LINUX_$v
354         bool
355
356 EOF
357         }
358         foreach my $def (sort keys %defaults) {
359                 print "\tconfig DEFAULT_".$def."\n";
360                 print "\t\tbool\n\n";
361         }
362 }
363
364 my %dep_check;
365 sub __find_package_dep($$) {
366         my $pkg = shift;
367         my $name = shift;
368         my $deps = ($pkg->{vdepends} or $pkg->{depends});
369
370         return 0 unless defined $deps;
371         foreach my $dep (@{$deps}) {
372                 next if $dep_check{$dep};
373                 $dep_check{$dep} = 1;
374                 return 1 if $dep eq $name;
375                 return 1 if ($package{$dep} and (__find_package_dep($package{$dep},$name) == 1));
376         }
377         return 0;
378 }
379
380 # wrapper to avoid infinite recursion
381 sub find_package_dep($$) {
382         my $pkg = shift;
383         my $name = shift;
384
385         %dep_check = ();
386         return __find_package_dep($pkg, $name);
387 }
388
389 sub package_depends($$) {
390         my $a = shift;
391         my $b = shift;
392         my $ret;
393
394         return 0 if ($a->{submenu} ne $b->{submenu});
395         if (find_package_dep($a, $b->{name}) == 1) {
396                 $ret = 1;
397         } elsif (find_package_dep($b, $a->{name}) == 1) {
398                 $ret = -1;
399         } else {
400                 return 0;
401         }
402         return $ret;
403 }
404
405 sub mconf_depends {
406         my $pkgname = shift;
407         my $depends = shift;
408         my $only_dep = shift;
409         my $res;
410         my $dep = shift;
411         my $seen = shift;
412         my $parent_condition = shift;
413         $dep or $dep = {};
414         $seen or $seen = {};
415
416         $depends or return;
417         my @depends = @$depends;
418         foreach my $depend (@depends) {
419                 my $m = "depends";
420                 my $flags = "";
421                 $depend =~ s/^([@\+]+)// and $flags = $1;
422                 my $vdep;
423                 my $condition = $parent_condition;
424
425                 if ($depend =~ /^(.+):(.+)$/) {
426                         if ($1 ne "PACKAGE_$pkgname") {
427                                 if ($condition) {
428                                         $condition = "$condition && $1";
429                                 } else {
430                                         $condition = $1;
431                                 }
432                         }
433                         $depend = $2;
434                 }
435                 next if $seen->{$depend};
436                 next if $package{$depend} and $package{$depend}->{buildonly};
437                 $seen->{$depend} = 1;
438                 if ($vdep = $package{$depend}->{vdepends}) {
439                         $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
440                 } else {
441                         $flags =~ /\+/ and do {
442                                 # Menuconfig will not treat 'select FOO' as a real dependency
443                                 # thus if FOO depends on other config options, these dependencies
444                                 # will not be checked. To fix this, we simply emit all of FOO's
445                                 # depends here as well.
446                                 $package{$depend} and mconf_depends($pkgname, $package{$depend}->{depends}, 1, $dep, $seen, $condition);
447
448                                 $m = "select";
449                                 next if $only_dep;
450                         };
451                         $flags =~ /@/ or $depend = "PACKAGE_$depend";
452                         if ($condition) {
453                                 if ($m =~ /select/) {
454                                         $depend = "$depend if $condition";
455                                 } else {
456                                         $depend = "!($condition) || $depend";
457                                 }
458                         }
459                 }
460                 $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
461         }
462         foreach my $depend (keys %$dep) {
463                 my $m = $dep->{$depend};
464                 $res .= "\t\t$m $depend\n";
465         }
466         return $res;
467 }
468
469 sub print_package_config_category($) {
470         my $cat = shift;
471         my %menus;
472         my %menu_dep;
473
474         return unless $category{$cat};
475
476         print "menu \"$cat\"\n\n";
477         my %spkg = %{$category{$cat}};
478
479         foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
480                 foreach my $pkg (@{$spkg{$spkg}}) {
481                         next if $pkg->{buildonly};
482                         my $menu = $pkg->{submenu};
483                         if ($menu) {
484                                 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
485                         } else {
486                                 $menu = 'undef';
487                         }
488                         $menus{$menu} or $menus{$menu} = [];
489                         push @{$menus{$menu}}, $pkg;
490                 }
491         }
492         my @menus = sort {
493                 ($a eq 'undef' ?  1 : 0) or
494                 ($b eq 'undef' ? -1 : 0) or
495                 ($a cmp $b)
496         } keys %menus;
497
498         foreach my $menu (@menus) {
499                 my @pkgs = sort {
500                         package_depends($a, $b) or
501                         ($a->{name} cmp $b->{name})
502                 } @{$menus{$menu}};
503                 if ($menu ne 'undef') {
504                         $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
505                         print "menu \"$menu\"\n";
506                 }
507                 foreach my $pkg (@pkgs) {
508                         my $title = $pkg->{name};
509                         my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
510                         if ($c > 0) {
511                                 $title .= ("." x $c). " ". $pkg->{title};
512                         }
513                         print "\t";
514                         $pkg->{menu} and print "menu";
515                         print "config PACKAGE_".$pkg->{name}."\n";
516                         print "\t\t".($pkg->{tristate} ? 'tristate' : 'bool')." \"$title\"\n";
517                         print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
518                         foreach my $default (split /\s*,\s*/, $pkg->{default}) {
519                                 print "\t\tdefault $default\n";
520                         }
521                         print mconf_depends($pkg->{name}, $pkg->{depends}, 0);
522                         print "\t\thelp\n";
523                         print $pkg->{description};
524                         print "\n";
525
526                         $pkg->{config} and print $pkg->{config}."\n";
527                 }
528                 if ($menu ne 'undef') {
529                         print "endmenu\n";
530                         $menu_dep{$menu} and print "endif\n";
531                 }
532         }
533         print "endmenu\n\n";
534
535         undef $category{$cat};
536 }
537
538 sub print_package_features() {
539         keys %features > 0 or return;
540         print "menu \"Package features\"\n";
541         foreach my $n (keys %features) {
542                 my @features = sort { $b->{priority} <=> $a->{priority} or $a->{title} cmp $b->{title} } @{$features{$n}};
543                 print <<EOF;
544 choice
545         prompt "$features[0]->{target_title}"
546         default FEATURE_$features[0]->{name}
547 EOF
548
549                 foreach my $feature (@features) {
550                         print <<EOF;
551         config FEATURE_$feature->{name}
552                 bool "$feature->{title}"
553 EOF
554                         $feature->{description} =~ /\w/ and do {
555                                 print "\t\thelp\n".$feature->{description}."\n";
556                         };
557                 }
558                 print "endchoice\n"
559         }
560         print "endmenu\n\n";
561 }
562
563 sub gen_package_config() {
564         parse_package_metadata($ARGV[0]) or exit 1;
565         print "menuconfig IMAGEOPT\n\tbool \"Image configuration\"\n\tdefault n\n";
566         foreach my $preconfig (keys %preconfig) {
567                 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
568                         my $conf = $preconfig{$preconfig}->{$cfg}->{id};
569                         $conf =~ tr/\.-/__/;
570                         print <<EOF
571         config UCI_PRECONFIG_$conf
572                 string "$preconfig{$preconfig}->{$cfg}->{label}" if IMAGEOPT
573                 depends PACKAGE_$preconfig
574                 default "$preconfig{$preconfig}->{$cfg}->{default}"
575
576 EOF
577                 }
578         }
579         print "source \"package/*/image-config.in\"\n";
580         if (scalar glob "package/feeds/*/*/image-config.in") {
581             print "source \"package/feeds/*/*/image-config.in\"\n";
582         }
583         print_package_features();
584         print_package_config_category 'Base system';
585         foreach my $cat (keys %category) {
586                 print_package_config_category $cat;
587         }
588 }
589
590 sub get_conditional_dep($$) {
591         my $condition = shift;
592         my $depstr = shift;
593         if ($condition) {
594                 if ($condition =~ /^!(.+)/) {
595                         return "\$(if \$(CONFIG_$1),,$depstr)";
596                 } else {
597                         return "\$(if \$(CONFIG_$condition),$depstr)";
598                 }
599         } else {
600                 return $depstr;
601         }
602 }
603
604 sub gen_package_mk() {
605         my %conf;
606         my %dep;
607         my %done;
608         my $line;
609
610         parse_package_metadata($ARGV[0]) or exit 1;
611         foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
612                 my $config;
613                 my $pkg = $package{$name};
614                 my @srcdeps;
615
616                 next if defined $pkg->{vdepends};
617
618                 if ($ENV{SDK}) {
619                         $conf{$pkg->{src}} or do {
620                                 $config = 'm';
621                                 $conf{$pkg->{src}} = 1;
622                         };
623                 } else {
624                         $config = "\$(CONFIG_PACKAGE_$name)"
625                 }
626                 if ($config) {
627                         $pkg->{buildonly} and $config = "";
628                         print "package-$config += $pkg->{subdir}$pkg->{src}\n";
629                         if ($pkg->{variant}) {
630                                 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/variants += \$(if $config,$pkg->{variant})\n"
631                         }
632                         $pkg->{prereq} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
633                 }
634
635                 next if $done{$pkg->{src}};
636                 $done{$pkg->{src}} = 1;
637
638                 if (@{$pkg->{buildtypes}} > 0) {
639                         print "buildtypes-$pkg->{subdir}$pkg->{src} = ".join(' ', @{$pkg->{buildtypes}})."\n";
640                 }
641
642                 foreach my $spkg (@{$srcpackage{$pkg->{src}}}) {
643                         foreach my $dep (@{$spkg->{depends}}, @{$spkg->{builddepends}}) {
644                                 $dep =~ /@/ or do {
645                                         $dep =~ s/\+//g;
646                                         push @srcdeps, $dep;
647                                 };
648                         }
649                 }
650                 foreach my $type (@{$pkg->{buildtypes}}) {
651                         my @extra_deps;
652                         my %deplines;
653
654                         next unless $pkg->{"builddepends/$type"};
655                         foreach my $dep (@{$pkg->{"builddepends/$type"}}) {
656                                 my $suffix = "";
657                                 my $condition;
658
659                                 if ($dep =~ /^(.+):(.+)/) {
660                                         $condition = $1;
661                                         $dep = $2;
662                                 }
663                                 if ($dep =~ /^(.+)(\/.+)/) {
664                                         $dep = $1;
665                                         $suffix = $2;
666                                 }
667                                 my $pkg_dep = $package{$dep};
668                                 next unless $pkg_dep;
669
670                                 my $idx = "";
671                                 if (defined $pkg_dep->{src}) {
672                                         $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
673                                 } elsif (defined($srcpackage{$dep})) {
674                                         $idx = $subdir{$dep}.$dep;
675                                 }
676                                 my $depstr = "\$(curdir)/$idx$suffix/compile";
677                                 my $depline = get_conditional_dep($condition, $depstr);
678                                 if ($depline) {
679                                         $deplines{$dep} = $depline;
680                                 }
681                         }
682                         my $depline = join(" ", values %deplines);
683                         if ($depline) {
684                                 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/$type/compile += $depline\n";
685                         }
686                 }
687
688                 my $hasdeps = 0;
689                 my %deplines;
690                 foreach my $deps (@srcdeps) {
691                         my $idx;
692                         my $condition;
693                         my $prefix = "";
694                         my $suffix = "";
695
696                         if ($deps =~ /^(.+):(.+)/) {
697                                 $condition = $1;
698                                 $deps = $2;
699                         }
700                         if ($deps =~ /^(.+)(\/.+)/) {
701                                 $deps = $1;
702                                 $suffix = $2;
703                         }
704
705                         my $pkg_dep = $package{$deps};
706                         my @deps;
707
708                         if ($pkg_dep->{vdepends}) {
709                                 @deps = @{$pkg_dep->{vdepends}};
710                         } else {
711                                 @deps = ($deps);
712                         }
713
714                         foreach my $dep (@deps) {
715                                 $pkg_dep = $package{$deps};
716                                 if (defined $pkg_dep->{src}) {
717                                         ($pkg->{src} ne $pkg_dep->{src}.$suffix) and $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
718                                 } elsif (defined($srcpackage{$dep})) {
719                                         $idx = $subdir{$dep}.$dep;
720                                 }
721                                 $idx .= $suffix;
722                                 undef $idx if $idx =~ /^(kernel)|(base-files)$/;
723                                 if ($idx) {
724                                         my $depline;
725                                         next if $pkg->{src} eq $pkg_dep->{src}.$suffix;
726                                         next if $dep{$pkg->{src}."->".$idx};
727                                         next if $dep{$pkg->{src}."->($dep)".$idx} and $pkg_dep->{vdepends};
728                                         my $depstr;
729
730                                         if ($pkg_dep->{vdepends}) {
731                                                 $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
732                                                 $dep{$pkg->{src}."->($dep)".$idx} = 1;
733                                         } else {
734                                                 $depstr = "\$(curdir)/$idx/compile";
735                                                 $dep{$pkg->{src}."->".$idx} = 1;
736                                         }
737                                         $depline = get_conditional_dep($condition, $depstr);
738                                         if ($depline) {
739                                                 $deplines{$idx.$dep} = $depline;
740                                         }
741                                 }
742                         }
743                 }
744                 my $depline = join(" ", values %deplines);
745                 if ($depline) {
746                         $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/compile += $depline\n";
747                 }
748         }
749
750         if ($line ne "") {
751                 print "\n$line";
752         }
753         foreach my $preconfig (keys %preconfig) {
754                 my $cmds;
755                 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
756                         my $conf = $preconfig{$preconfig}->{$cfg}->{id};
757                         $conf =~ tr/\.-/__/;
758                         $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
759                 }
760                 next unless $cmds;
761                 print <<EOF
762
763 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
764         ( \\
765 $cmds \\
766         ) > \$@
767         
768 ifneq (\$(IMAGEOPT)\$(CONFIG_IMAGEOPT),)
769   package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
770 endif
771 EOF
772         }
773 }
774
775 sub gen_package_source() {
776         parse_package_metadata($ARGV[0]) or exit 1;
777         foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
778                 my $pkg = $package{$name};
779                 if ($pkg->{name} && $pkg->{source}) {
780                         print "$pkg->{name}: ";
781                         print "$pkg->{source}\n";
782                 }
783         }
784 }
785
786 sub parse_command() {
787         my $cmd = shift @ARGV;
788         for ($cmd) {
789                 /^target_config$/ and return gen_target_config();
790                 /^package_mk$/ and return gen_package_mk();
791                 /^package_config$/ and return gen_package_config();
792                 /^kconfig/ and return gen_kconfig_overrides();
793                 /^package_source$/ and return gen_package_source();
794         }
795         print <<EOF
796 Available Commands:
797         $0 target_config [file]         Target metadata in Kconfig format
798         $0 package_mk [file]            Package metadata in makefile format
799         $0 package_config [file]        Package metadata in Kconfig format
800         $0 kconfig [file] [config]      Kernel config overrides
801         $0 package_source [file]        Package source file information
802
803 EOF
804 }
805
806 parse_command();