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