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