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