add support for hidden packages that get selected/built but do not show up in menuconfig
[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-Name:\s*(.+)\s*$/ and $target->{name} = $1;
49                 /^Target-Path:\s*(.+)\s*$/ and $target->{path} = $1;
50                 /^Target-Arch:\s*(.+)\s*$/ and $target->{arch} = $1;
51                 /^Target-Arch-Packages:\s*(.+)\s*$/ and $target->{arch_packages} = $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                 /audio/ and $ret .= "\tselect AUDIO_SUPPORT\n";
156                 /display/ and $ret .= "\tselect DISPLAY_SUPPORT\n";
157                 /gpio/ and $ret .= "\tselect GPIO_SUPPORT\n";
158                 /pci/ and $ret .= "\tselect PCI_SUPPORT\n";
159                 /pcie/ and $ret .= "\tselect PCIE_SUPPORT\n";
160                 /usb/ and $ret .= "\tselect USB_SUPPORT\n";
161                 /usbgadget/ and $ret .= "\tselect USB_GADGET_SUPPORT\n";
162                 /pcmcia/ and $ret .= "\tselect PCMCIA_SUPPORT\n";
163                 /squashfs/ and $ret .= "\tselect USES_SQUASHFS\n";
164                 /jffs2/ and $ret .= "\tselect USES_JFFS2\n";
165                 /ext4/ and $ret .= "\tselect USES_EXT4\n";
166                 /targz/ and $ret .= "\tselect USES_TARGZ\n";
167                 /cpiogz/ and $ret .= "\tselect USES_CPIOGZ\n";
168                 /ubifs/ and $ret .= "\tselect USES_UBIFS\n";
169                 /fpu/ and $ret .= "\tselect HAS_FPU\n";
170                 /spe_fpu/ and $ret .= "\tselect HAS_SPE_FPU\n";
171                 /ramdisk/ and $ret .= "\tselect USES_INITRAMFS\n";
172                 /powerpc64/ and $ret .= "\tselect powerpc64\n";
173                 /nommu/ and $ret .= "\tselect NOMMU\n";
174         }
175         return $ret;
176 }
177
178 sub target_name($) {
179         my $target = shift;
180         my $parent = $target->{parent};
181         if ($parent) {
182                 return $target->{parent}->{name}." - ".$target->{name};
183         } else {
184                 return $target->{name};
185         }
186 }
187
188 sub kver($) {
189         my $v = shift;
190         $v =~ tr/\./_/;
191         if (substr($v,0,2) eq "2_") {
192                 $v =~ /(\d+_\d+_\d+)(_\d+)?/ and $v = $1;
193         } else {
194                 $v =~ /(\d+_\d+)(_\d+)?/ and $v = $1;
195         }
196         return $v;
197 }
198
199 sub print_target($) {
200         my $target = shift;
201         my $features = target_config_features(@{$target->{features}});
202         my $help = $target->{desc};
203         my $confstr;
204
205         chomp $features;
206         $features .= "\n";
207         if ($help =~ /\w+/) {
208                 $help =~ s/^\s*/\t  /mg;
209                 $help = "\thelp\n$help";
210         } else {
211                 undef $help;
212         }
213
214         my $v = kver($target->{version});
215         if (@{$target->{subtargets}} == 0) {
216         $confstr = <<EOF;
217 config TARGET_$target->{conf}
218         bool "$target->{name}"
219         select LINUX_$v
220 EOF
221         }
222         else {
223                 $confstr = <<EOF;
224 config TARGET_$target->{conf}
225         bool "$target->{name}"
226 EOF
227         }
228         if ($target->{subtarget}) {
229                 $confstr .= "\tdepends TARGET_$target->{boardconf}\n";
230         }
231         if (@{$target->{subtargets}} > 0) {
232                 $confstr .= "\tselect HAS_SUBTARGETS\n";
233         } else {
234                 $confstr .= $features;
235         }
236
237         if ($target->{arch} =~ /\w/) {
238                 $confstr .= "\tselect $target->{arch}\n";
239         }
240         foreach my $dep (@{$target->{depends}}) {
241                 my $mode = "depends";
242                 my $flags;
243                 my $name;
244
245                 $dep =~ /^([@\+\-]+)(.+)$/;
246                 $flags = $1;
247                 $name = $2;
248
249                 next if $name =~ /:/;
250                 $flags =~ /-/ and $mode = "deselect";
251                 $flags =~ /\+/ and $mode = "select";
252                 $flags =~ /@/ and $confstr .= "\t$mode $name\n";
253         }
254         $confstr .= "$help\n\n";
255         print $confstr;
256 }
257
258 sub gen_target_config() {
259         my @target = parse_target_metadata();
260         my %defaults;
261
262         my @target_sort = sort {
263                 target_name($a) cmp target_name($b);
264         } @target;
265
266
267         print <<EOF;
268 choice
269         prompt "Target System"
270         default TARGET_brcm47xx
271         reset if !DEVEL
272         
273 EOF
274
275         foreach my $target (@target_sort) {
276                 next if $target->{subtarget};
277                 print_target($target);
278         }
279
280         print <<EOF;
281 endchoice
282
283 choice
284         prompt "Subtarget" if HAS_SUBTARGETS
285
286 EOF
287         foreach my $target (@target) {
288                 next unless $target->{subtarget};
289                 print_target($target);
290         }
291
292 print <<EOF;
293 endchoice
294
295 choice
296         prompt "Target Profile"
297
298 EOF
299
300         foreach my $target (@target) {
301                 my $profiles = $target->{profiles};
302
303                 foreach my $profile (@$profiles) {
304                         print <<EOF;
305 config TARGET_$target->{conf}_$profile->{id}
306         bool "$profile->{name}"
307         depends TARGET_$target->{conf}
308 $profile->{config}
309 EOF
310                         $profile->{kconfig} and print "\tselect PROFILE_KCONFIG\n";
311                         my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
312                         foreach my $pkg (@pkglist) {
313                                 print "\tselect DEFAULT_$pkg\n";
314                                 $defaults{$pkg} = 1;
315                         }
316                         my $help = $profile->{desc};
317                         if ($help =~ /\w+/) {
318                                 $help =~ s/^\s*/\t  /mg;
319                                 $help = "\thelp\n$help";
320                         } else {
321                                 undef $help;
322                         }
323                         print "$help\n";
324                 }
325         }
326
327         print <<EOF;
328 endchoice
329
330 config HAS_SUBTARGETS
331         bool
332
333 config TARGET_BOARD
334         string
335
336 EOF
337         foreach my $target (@target) {
338                 $target->{subtarget} or print "\t\tdefault \"".$target->{board}."\" if TARGET_".$target->{conf}."\n";
339         }
340         print <<EOF;
341 config TARGET_ARCH_PACKAGES
342         string
343         
344 EOF
345         foreach my $target (@target) {
346                 next if @{$target->{subtargets}} > 0;
347                 print "\t\tdefault \"".($target->{arch_packages} || $target->{board})."\" if TARGET_".$target->{conf}."\n";
348         }
349         print <<EOF;
350
351 config DEFAULT_TARGET_OPTIMIZATION
352         string
353 EOF
354         foreach my $target (@target) {
355                 next if @{$target->{subtargets}} > 0;
356                 print "\tdefault \"".$target->{cflags}."\" if TARGET_".$target->{conf}."\n";
357         }
358         print "\tdefault \"-Os -pipe -funit-at-a-time\"\n";
359
360         my %kver;
361         foreach my $target (@target) {
362                 my $v = kver($target->{version});
363                 next if $kver{$v};
364                 $kver{$v} = 1;
365                 print <<EOF;
366
367 config LINUX_$v
368         bool
369
370 EOF
371         }
372         foreach my $def (sort keys %defaults) {
373                 print "\tconfig DEFAULT_".$def."\n";
374                 print "\t\tbool\n\n";
375         }
376 }
377
378 my %dep_check;
379 sub __find_package_dep($$) {
380         my $pkg = shift;
381         my $name = shift;
382         my $deps = ($pkg->{vdepends} or $pkg->{depends});
383
384         return 0 unless defined $deps;
385         foreach my $dep (@{$deps}) {
386                 next if $dep_check{$dep};
387                 $dep_check{$dep} = 1;
388                 return 1 if $dep eq $name;
389                 return 1 if ($package{$dep} and (__find_package_dep($package{$dep},$name) == 1));
390         }
391         return 0;
392 }
393
394 # wrapper to avoid infinite recursion
395 sub find_package_dep($$) {
396         my $pkg = shift;
397         my $name = shift;
398
399         %dep_check = ();
400         return __find_package_dep($pkg, $name);
401 }
402
403 sub package_depends($$) {
404         my $a = shift;
405         my $b = shift;
406         my $ret;
407
408         return 0 if ($a->{submenu} ne $b->{submenu});
409         if (find_package_dep($a, $b->{name}) == 1) {
410                 $ret = 1;
411         } elsif (find_package_dep($b, $a->{name}) == 1) {
412                 $ret = -1;
413         } else {
414                 return 0;
415         }
416         return $ret;
417 }
418
419 sub mconf_depends {
420         my $pkgname = shift;
421         my $depends = shift;
422         my $only_dep = shift;
423         my $res;
424         my $dep = shift;
425         my $seen = shift;
426         my $parent_condition = shift;
427         $dep or $dep = {};
428         $seen or $seen = {};
429
430         $depends or return;
431         my @depends = @$depends;
432         foreach my $depend (@depends) {
433                 my $m = "depends";
434                 my $flags = "";
435                 $depend =~ s/^([@\+]+)// and $flags = $1;
436                 my $vdep;
437                 my $condition = $parent_condition;
438
439                 next if $condition eq $depend;
440                 next if $seen->{"$parent_condition:$depend"};
441                 $seen->{"$parent_condition:$depend"} = 1;
442                 if ($depend =~ /^(.+):(.+)$/) {
443                         if ($1 ne "PACKAGE_$pkgname") {
444                                 if ($condition) {
445                                         $condition = "$condition && $1";
446                                 } else {
447                                         $condition = $1;
448                                 }
449                         }
450                         $depend = $2;
451                 }
452                 next if $package{$depend} and $package{$depend}->{buildonly};
453                 if ($vdep = $package{$depend}->{vdepends}) {
454                         $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
455                 } else {
456                         $flags =~ /\+/ and do {
457                                 # Menuconfig will not treat 'select FOO' as a real dependency
458                                 # thus if FOO depends on other config options, these dependencies
459                                 # will not be checked. To fix this, we simply emit all of FOO's
460                                 # depends here as well.
461                                 $package{$depend} and mconf_depends($pkgname, $package{$depend}->{depends}, 1, $dep, $seen, $condition);
462
463                                 $m = "select";
464                                 next if $only_dep;
465                         };
466                         $flags =~ /@/ or $depend = "PACKAGE_$depend";
467                         if ($condition) {
468                                 if ($m =~ /select/) {
469                                         $depend = "$depend if $condition";
470                                 } else {
471                                         $depend = "!($condition) || $depend";
472                                 }
473                         }
474                 }
475                 $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
476         }
477         foreach my $depend (keys %$dep) {
478                 my $m = $dep->{$depend};
479                 $res .= "\t\t$m $depend\n";
480         }
481         return $res;
482 }
483
484 sub print_package_config_category($) {
485         my $cat = shift;
486         my %menus;
487         my %menu_dep;
488
489         return unless $category{$cat};
490
491         print "menu \"$cat\"\n\n";
492         my %spkg = %{$category{$cat}};
493
494         foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
495                 foreach my $pkg (@{$spkg{$spkg}}) {
496                         next if $pkg->{buildonly};
497                         my $menu = $pkg->{submenu};
498                         if ($menu) {
499                                 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
500                         } else {
501                                 $menu = 'undef';
502                         }
503                         $menus{$menu} or $menus{$menu} = [];
504                         push @{$menus{$menu}}, $pkg;
505                 }
506         }
507         my @menus = sort {
508                 ($a eq 'undef' ?  1 : 0) or
509                 ($b eq 'undef' ? -1 : 0) or
510                 ($a cmp $b)
511         } keys %menus;
512
513         foreach my $menu (@menus) {
514                 my @pkgs = sort {
515                         package_depends($a, $b) or
516                         ($a->{name} cmp $b->{name})
517                 } @{$menus{$menu}};
518                 if ($menu ne 'undef') {
519                         $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
520                         print "menu \"$menu\"\n";
521                 }
522                 foreach my $pkg (@pkgs) {
523                         my $title = $pkg->{name};
524                         my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
525                         if ($c > 0) {
526                                 $title .= ("." x $c). " ". $pkg->{title};
527                         }
528                         $title = "\"$title\"";
529                         print "\t";
530                         $pkg->{menu} and print "menu";
531                         print "config PACKAGE_".$pkg->{name}."\n";
532                         $pkg->{hidden} and $title = "";
533                         print "\t\t".($pkg->{tristate} ? 'tristate' : 'bool')." $title\n";
534                         print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
535                         foreach my $default (split /\s*,\s*/, $pkg->{default}) {
536                                 print "\t\tdefault $default\n";
537                         }
538                         print mconf_depends($pkg->{name}, $pkg->{depends}, 0);
539                         print mconf_depends($pkg->{name}, $pkg->{mdepends}, 0);
540                         print "\t\thelp\n";
541                         print $pkg->{description};
542                         print "\n";
543
544                         $pkg->{config} and print $pkg->{config}."\n";
545                 }
546                 if ($menu ne 'undef') {
547                         print "endmenu\n";
548                         $menu_dep{$menu} and print "endif\n";
549                 }
550         }
551         print "endmenu\n\n";
552
553         undef $category{$cat};
554 }
555
556 sub print_package_features() {
557         keys %features > 0 or return;
558         print "menu \"Package features\"\n";
559         foreach my $n (keys %features) {
560                 my @features = sort { $b->{priority} <=> $a->{priority} or $a->{title} cmp $b->{title} } @{$features{$n}};
561                 print <<EOF;
562 choice
563         prompt "$features[0]->{target_title}"
564         default FEATURE_$features[0]->{name}
565 EOF
566
567                 foreach my $feature (@features) {
568                         print <<EOF;
569         config FEATURE_$feature->{name}
570                 bool "$feature->{title}"
571 EOF
572                         $feature->{description} =~ /\w/ and do {
573                                 print "\t\thelp\n".$feature->{description}."\n";
574                         };
575                 }
576                 print "endchoice\n"
577         }
578         print "endmenu\n\n";
579 }
580
581 sub gen_package_config() {
582         parse_package_metadata($ARGV[0]) or exit 1;
583         print "menuconfig IMAGEOPT\n\tbool \"Image configuration\"\n\tdefault n\n";
584         foreach my $preconfig (keys %preconfig) {
585                 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
586                         my $conf = $preconfig{$preconfig}->{$cfg}->{id};
587                         $conf =~ tr/\.-/__/;
588                         print <<EOF
589         config UCI_PRECONFIG_$conf
590                 string "$preconfig{$preconfig}->{$cfg}->{label}" if IMAGEOPT
591                 depends PACKAGE_$preconfig
592                 default "$preconfig{$preconfig}->{$cfg}->{default}"
593
594 EOF
595                 }
596         }
597         print "source \"package/*/image-config.in\"\n";
598         if (scalar glob "package/feeds/*/*/image-config.in") {
599             print "source \"package/feeds/*/*/image-config.in\"\n";
600         }
601         print_package_features();
602         print_package_config_category 'Base system';
603         foreach my $cat (keys %category) {
604                 print_package_config_category $cat;
605         }
606 }
607
608 sub get_conditional_dep($$) {
609         my $condition = shift;
610         my $depstr = shift;
611         if ($condition) {
612                 if ($condition =~ /^!(.+)/) {
613                         return "\$(if \$(CONFIG_$1),,$depstr)";
614                 } else {
615                         return "\$(if \$(CONFIG_$condition),$depstr)";
616                 }
617         } else {
618                 return $depstr;
619         }
620 }
621
622 sub gen_package_mk() {
623         my %conf;
624         my %dep;
625         my %done;
626         my $line;
627
628         parse_package_metadata($ARGV[0]) or exit 1;
629         foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
630                 my $config;
631                 my $pkg = $package{$name};
632                 my @srcdeps;
633
634                 next if defined $pkg->{vdepends};
635
636                 if ($ENV{SDK}) {
637                         $conf{$pkg->{src}} or do {
638                                 $config = 'm';
639                                 $conf{$pkg->{src}} = 1;
640                         };
641                 } else {
642                         $config = "\$(CONFIG_PACKAGE_$name)"
643                 }
644                 if ($config) {
645                         $pkg->{buildonly} and $config = "";
646                         print "package-$config += $pkg->{subdir}$pkg->{src}\n";
647                         if ($pkg->{variant}) {
648                                 if (!defined($done{$pkg->{src}})) {
649                                         print "\$(curdir)/$pkg->{subdir}$pkg->{src}/default-variant := $pkg->{variant}\n";
650                                 }
651                                 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/variants += \$(if $config,$pkg->{variant})\n"
652                         }
653                         $pkg->{prereq} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
654                 }
655
656                 next if $done{$pkg->{src}};
657                 $done{$pkg->{src}} = 1;
658
659                 if (@{$pkg->{buildtypes}} > 0) {
660                         print "buildtypes-$pkg->{subdir}$pkg->{src} = ".join(' ', @{$pkg->{buildtypes}})."\n";
661                 }
662
663                 foreach my $spkg (@{$srcpackage{$pkg->{src}}}) {
664                         foreach my $dep (@{$spkg->{depends}}, @{$spkg->{builddepends}}) {
665                                 $dep =~ /@/ or do {
666                                         $dep =~ s/\+//g;
667                                         push @srcdeps, $dep;
668                                 };
669                         }
670                 }
671                 foreach my $type (@{$pkg->{buildtypes}}) {
672                         my @extra_deps;
673                         my %deplines;
674
675                         next unless $pkg->{"builddepends/$type"};
676                         foreach my $dep (@{$pkg->{"builddepends/$type"}}) {
677                                 my $suffix = "";
678                                 my $condition;
679
680                                 if ($dep =~ /^(.+):(.+)/) {
681                                         $condition = $1;
682                                         $dep = $2;
683                                 }
684                                 if ($dep =~ /^(.+)(\/.+)/) {
685                                         $dep = $1;
686                                         $suffix = $2;
687                                 }
688
689                                 my $idx = "";
690                                 my $pkg_dep = $package{$dep};
691                                 if (defined($pkg_dep) && defined($pkg_dep->{src})) {
692                                         $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
693                                 } elsif (defined($srcpackage{$dep})) {
694                                         $idx = $subdir{$dep}.$dep;
695                                 } else {
696                                         next;
697                                 }
698                                 my $depstr = "\$(curdir)/$idx$suffix/compile";
699                                 my $depline = get_conditional_dep($condition, $depstr);
700                                 if ($depline) {
701                                         $deplines{$depline}++;
702                                 }
703                         }
704                         my $depline = join(" ", sort keys %deplines);
705                         if ($depline) {
706                                 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/$type/compile += $depline\n";
707                         }
708                 }
709
710                 my $hasdeps = 0;
711                 my %deplines;
712                 foreach my $deps (@srcdeps) {
713                         my $idx;
714                         my $condition;
715                         my $prefix = "";
716                         my $suffix = "";
717
718                         if ($deps =~ /^(.+):(.+)/) {
719                                 $condition = $1;
720                                 $deps = $2;
721                         }
722                         if ($deps =~ /^(.+)(\/.+)/) {
723                                 $deps = $1;
724                                 $suffix = $2;
725                         }
726
727                         my $pkg_dep = $package{$deps};
728                         my @deps;
729
730                         if ($pkg_dep->{vdepends}) {
731                                 @deps = @{$pkg_dep->{vdepends}};
732                         } else {
733                                 @deps = ($deps);
734                         }
735
736                         foreach my $dep (@deps) {
737                                 $pkg_dep = $package{$deps};
738                                 if (defined $pkg_dep->{src}) {
739                                         ($pkg->{src} ne $pkg_dep->{src}.$suffix) and $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
740                                 } elsif (defined($srcpackage{$dep})) {
741                                         $idx = $subdir{$dep}.$dep;
742                                 }
743                                 $idx .= $suffix;
744                                 undef $idx if $idx =~ /^(kernel)|(base-files)$/;
745                                 if ($idx) {
746                                         my $depline;
747                                         next if $pkg->{src} eq $pkg_dep->{src}.$suffix;
748                                         next if $dep{$condition.":".$pkg->{src}."->".$idx};
749                                         next if $dep{$pkg->{src}."->($dep)".$idx} and $pkg_dep->{vdepends};
750                                         my $depstr;
751
752                                         if ($pkg_dep->{vdepends}) {
753                                                 $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
754                                                 $dep{$pkg->{src}."->($dep)".$idx} = 1;
755                                         } else {
756                                                 $depstr = "\$(curdir)/$idx/compile";
757                                                 $dep{$pkg->{src}."->".$idx} = 1;
758                                         }
759                                         $depline = get_conditional_dep($condition, $depstr);
760                                         if ($depline) {
761                                                 $deplines{$depline}++;
762                                         }
763                                 }
764                         }
765                 }
766                 my $depline = join(" ", sort keys %deplines);
767                 if ($depline) {
768                         $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/compile += $depline\n";
769                 }
770         }
771
772         if ($line ne "") {
773                 print "\n$line";
774         }
775         foreach my $preconfig (keys %preconfig) {
776                 my $cmds;
777                 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
778                         my $conf = $preconfig{$preconfig}->{$cfg}->{id};
779                         $conf =~ tr/\.-/__/;
780                         $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
781                 }
782                 next unless $cmds;
783                 print <<EOF
784
785 ifndef DUMP_TARGET_DB
786 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
787         ( \\
788 $cmds \\
789         ) > \$@
790         
791 ifneq (\$(IMAGEOPT)\$(CONFIG_IMAGEOPT),)
792   package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
793 endif
794 endif
795
796 EOF
797         }
798 }
799
800 sub gen_package_source() {
801         parse_package_metadata($ARGV[0]) or exit 1;
802         foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
803                 my $pkg = $package{$name};
804                 if ($pkg->{name} && $pkg->{source}) {
805                         print "$pkg->{name}: ";
806                         print "$pkg->{source}\n";
807                 }
808         }
809 }
810
811 sub parse_command() {
812         my $cmd = shift @ARGV;
813         for ($cmd) {
814                 /^target_config$/ and return gen_target_config();
815                 /^package_mk$/ and return gen_package_mk();
816                 /^package_config$/ and return gen_package_config();
817                 /^kconfig/ and return gen_kconfig_overrides();
818                 /^package_source$/ and return gen_package_source();
819         }
820         print <<EOF
821 Available Commands:
822         $0 target_config [file]         Target metadata in Kconfig format
823         $0 package_mk [file]            Package metadata in makefile format
824         $0 package_config [file]        Package metadata in Kconfig format
825         $0 kconfig [file] [config]      Kernel config overrides
826         $0 package_source [file]        Package source file information
827
828 EOF
829 }
830
831 parse_command();