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