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