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