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