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