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