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