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