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