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