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