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