ar71xx: fix DDR write buffer flushing issues with 4.4
[librecmc/librecmc.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 $profile->{config}
291 EOF
292                         $profile->{kconfig} and print "\tselect PROFILE_KCONFIG\n";
293                         my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
294                         foreach my $pkg (@pkglist) {
295                                 print "\tselect DEFAULT_$pkg\n";
296                                 $defaults{$pkg} = 1;
297                         }
298                         my $help = $profile->{desc};
299                         if ($help =~ /\w+/) {
300                                 $help =~ s/^\s*/\t  /mg;
301                                 $help = "\thelp\n$help";
302                         } else {
303                                 undef $help;
304                         }
305                         print "$help\n";
306                 }
307         }
308
309         print <<EOF;
310 endchoice
311
312 config HAS_SUBTARGETS
313         bool
314
315 config TARGET_BOARD
316         string
317
318 EOF
319         foreach my $target (@target) {
320                 $target->{subtarget} or print "\t\tdefault \"".$target->{board}."\" if TARGET_".$target->{conf}."\n";
321         }
322         print <<EOF;
323 config TARGET_SUBTARGET
324         string
325         default "generic" if !HAS_SUBTARGETS
326
327 EOF
328
329         foreach my $target (@target) {
330                 foreach my $subtarget (@{$target->{subtargets}}) {
331                         print "\t\tdefault \"$subtarget\" if TARGET_".$target->{conf}."_$subtarget\n";
332                 }
333         }
334         print <<EOF;
335 config TARGET_PROFILE
336         string
337 EOF
338         foreach my $target (@target) {
339                 my $profiles = $target->{profiles};
340                 foreach my $profile (@$profiles) {
341                         print "\tdefault \"$profile->{id}\" if TARGET_$target->{conf}_$profile->{id}\n";
342                 }
343         }
344
345         print <<EOF;
346
347 config TARGET_ARCH_PACKAGES
348         string
349         
350 EOF
351         foreach my $target (@target) {
352                 next if @{$target->{subtargets}} > 0;
353                 print "\t\tdefault \"".($target->{arch_packages} || $target->{board})."\" if TARGET_".$target->{conf}."\n";
354         }
355         print <<EOF;
356
357 config DEFAULT_TARGET_OPTIMIZATION
358         string
359 EOF
360         foreach my $target (@target) {
361                 next if @{$target->{subtargets}} > 0;
362                 print "\tdefault \"".$target->{cflags}."\" if TARGET_".$target->{conf}."\n";
363         }
364         print "\tdefault \"-Os -pipe -funit-at-a-time\"\n";
365         print <<EOF;
366
367 config CPU_TYPE
368         string
369 EOF
370         foreach my $target (@target) {
371                 next if @{$target->{subtargets}} > 0;
372                 print "\tdefault \"".$target->{cputype}."\" if TARGET_".$target->{conf}."\n";
373         }
374         print "\tdefault \"\"\n";
375
376         my %kver;
377         foreach my $target (@target) {
378                 my $v = kver($target->{version});
379                 next if $kver{$v};
380                 $kver{$v} = 1;
381                 print <<EOF;
382
383 config LINUX_$v
384         bool
385
386 EOF
387         }
388         foreach my $def (sort keys %defaults) {
389                 print "\tconfig DEFAULT_".$def."\n";
390                 print "\t\tbool\n\n";
391         }
392 }
393
394 my %dep_check;
395 sub __find_package_dep($$) {
396         my $pkg = shift;
397         my $name = shift;
398         my $deps = ($pkg->{vdepends} or $pkg->{depends});
399
400         return 0 unless defined $deps;
401         foreach my $dep (@{$deps}) {
402                 next if $dep_check{$dep};
403                 $dep_check{$dep} = 1;
404                 return 1 if $dep eq $name;
405                 return 1 if ($package{$dep} and (__find_package_dep($package{$dep},$name) == 1));
406         }
407         return 0;
408 }
409
410 # wrapper to avoid infinite recursion
411 sub find_package_dep($$) {
412         my $pkg = shift;
413         my $name = shift;
414
415         %dep_check = ();
416         return __find_package_dep($pkg, $name);
417 }
418
419 sub package_depends($$) {
420         my $a = shift;
421         my $b = shift;
422         my $ret;
423
424         return 0 if ($a->{submenu} ne $b->{submenu});
425         if (find_package_dep($a, $b->{name}) == 1) {
426                 $ret = 1;
427         } elsif (find_package_dep($b, $a->{name}) == 1) {
428                 $ret = -1;
429         } else {
430                 return 0;
431         }
432         return $ret;
433 }
434
435 sub mconf_depends {
436         my $pkgname = shift;
437         my $depends = shift;
438         my $only_dep = shift;
439         my $res;
440         my $dep = shift;
441         my $seen = shift;
442         my $parent_condition = shift;
443         $dep or $dep = {};
444         $seen or $seen = {};
445         my @t_depends;
446
447         $depends or return;
448         my @depends = @$depends;
449         foreach my $depend (@depends) {
450                 my $m = "depends on";
451                 my $flags = "";
452                 $depend =~ s/^([@\+]+)// and $flags = $1;
453                 my $vdep;
454                 my $condition = $parent_condition;
455
456                 next if $condition eq $depend;
457                 next if $seen->{"$parent_condition:$depend"};
458                 next if $seen->{":$depend"};
459                 $seen->{"$parent_condition:$depend"} = 1;
460                 if ($depend =~ /^(.+):(.+)$/) {
461                         if ($1 ne "PACKAGE_$pkgname") {
462                                 if ($condition) {
463                                         $condition = "$condition && $1";
464                                 } else {
465                                         $condition = $1;
466                                 }
467                         }
468                         $depend = $2;
469                 }
470                 next if $package{$depend} and $package{$depend}->{buildonly};
471                 if ($flags =~ /\+/) {
472                         if ($vdep = $package{$depend}->{vdepends}) {
473                                 my @vdeps = @$vdep;
474                                 $depend = shift @vdeps;
475                                 if (@vdeps > 1) {
476                                         $condition = '!('.join("||", map { "PACKAGE_".$_ } @vdeps).')';
477                                 } elsif (@vdeps > 0) {
478                                         $condition = '!PACKAGE_'.$vdeps[0];
479                                 }
480                         }
481
482                         # Menuconfig will not treat 'select FOO' as a real dependency
483                         # thus if FOO depends on other config options, these dependencies
484                         # will not be checked. To fix this, we simply emit all of FOO's
485                         # depends here as well.
486                         $package{$depend} and push @t_depends, [ $package{$depend}->{depends}, $condition ];
487
488                         $m = "select";
489                         next if $only_dep;
490                 } else {
491                         if ($vdep = $package{$depend}->{vdepends}) {
492                                 $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
493                         }
494                 }
495                 $flags =~ /@/ or $depend = "PACKAGE_$depend";
496                 if ($condition) {
497                         if ($m =~ /select/) {
498                                 next if $depend eq $condition;
499                                 $depend = "$depend if $condition";
500                         } else {
501                                 $depend = "!($condition) || $depend" unless $dep->{$condition} eq 'select';
502                         }
503                 }
504                 $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
505         }
506
507         foreach my $tdep (@t_depends) {
508                 mconf_depends($pkgname, $tdep->[0], 1, $dep, $seen, $tdep->[1]);
509         }
510
511         foreach my $depend (keys %$dep) {
512                 my $m = $dep->{$depend};
513                 $res .= "\t\t$m $depend\n";
514         }
515         return $res;
516 }
517
518 sub mconf_conflicts {
519         my $pkgname = shift;
520         my $depends = shift;
521         my $res = "";
522
523         foreach my $depend (@$depends) {
524                 next unless $package{$depend};
525                 $res .= "\t\tdepends on m || (PACKAGE_$depend != y)\n";
526         }
527         return $res;
528 }
529
530 sub print_package_config_category($) {
531         my $cat = shift;
532         my %menus;
533         my %menu_dep;
534
535         return unless $category{$cat};
536
537         print "menu \"$cat\"\n\n";
538         my %spkg = %{$category{$cat}};
539
540         foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
541                 foreach my $pkg (@{$spkg{$spkg}}) {
542                         next if $pkg->{buildonly};
543                         my $menu = $pkg->{submenu};
544                         if ($menu) {
545                                 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
546                         } else {
547                                 $menu = 'undef';
548                         }
549                         $menus{$menu} or $menus{$menu} = [];
550                         push @{$menus{$menu}}, $pkg;
551                 }
552         }
553         my @menus = sort {
554                 ($a eq 'undef' ?  1 : 0) or
555                 ($b eq 'undef' ? -1 : 0) or
556                 ($a cmp $b)
557         } keys %menus;
558
559         foreach my $menu (@menus) {
560                 my @pkgs = sort {
561                         package_depends($a, $b) or
562                         ($a->{name} cmp $b->{name})
563                 } @{$menus{$menu}};
564                 if ($menu ne 'undef') {
565                         $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
566                         print "menu \"$menu\"\n";
567                 }
568                 foreach my $pkg (@pkgs) {
569                         my $title = $pkg->{name};
570                         my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
571                         if ($c > 0) {
572                                 $title .= ("." x $c). " ". $pkg->{title};
573                         }
574                         $title = "\"$title\"";
575                         print "\t";
576                         $pkg->{menu} and print "menu";
577                         print "config PACKAGE_".$pkg->{name}."\n";
578                         $pkg->{hidden} and $title = "";
579                         print "\t\t".($pkg->{tristate} ? 'tristate' : 'bool')." $title\n";
580                         print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
581                         unless ($pkg->{hidden}) {
582                                 my @def = ("ALL");
583                                 if (!exists($pkg->{repository})) {
584                                         push @def, "ALL_NONSHARED";
585                                 }
586                                 if ($pkg->{name} =~ /^kmod-/) {
587                                         push @def, "ALL_KMODS";
588                                 }
589                                 $pkg->{default} ||= "m if " . join("||", @def);
590                         }
591                         if ($pkg->{default}) {
592                                 foreach my $default (split /\s*,\s*/, $pkg->{default}) {
593                                         print "\t\tdefault $default\n";
594                                 }
595                         }
596                         print mconf_depends($pkg->{name}, $pkg->{depends}, 0);
597                         print mconf_depends($pkg->{name}, $pkg->{mdepends}, 0);
598                         print mconf_conflicts($pkg->{name}, $pkg->{conflicts});
599                         print "\t\thelp\n";
600                         print $pkg->{description};
601                         print "\n";
602
603                         $pkg->{config} and print $pkg->{config}."\n";
604                 }
605                 if ($menu ne 'undef') {
606                         print "endmenu\n";
607                         $menu_dep{$menu} and print "endif\n";
608                 }
609         }
610         print "endmenu\n\n";
611
612         undef $category{$cat};
613 }
614
615 sub print_package_features() {
616         keys %features > 0 or return;
617         print "menu \"Package features\"\n";
618         foreach my $n (keys %features) {
619                 my @features = sort { $b->{priority} <=> $a->{priority} or $a->{title} cmp $b->{title} } @{$features{$n}};
620                 print <<EOF;
621 choice
622         prompt "$features[0]->{target_title}"
623         default FEATURE_$features[0]->{name}
624 EOF
625
626                 foreach my $feature (@features) {
627                         print <<EOF;
628         config FEATURE_$feature->{name}
629                 bool "$feature->{title}"
630 EOF
631                         $feature->{description} =~ /\w/ and do {
632                                 print "\t\thelp\n".$feature->{description}."\n";
633                         };
634                 }
635                 print "endchoice\n"
636         }
637         print "endmenu\n\n";
638 }
639
640 sub print_package_overrides() {
641         keys %overrides > 0 or return;
642         print "\tconfig OVERRIDE_PKGS\n";
643         print "\t\tstring\n";
644         print "\t\tdefault \"".join(" ", keys %overrides)."\"\n\n";
645 }
646
647 sub gen_package_config() {
648         parse_package_metadata($ARGV[0]) or exit 1;
649         print "menuconfig IMAGEOPT\n\tbool \"Image configuration\"\n\tdefault n\n";
650         foreach my $preconfig (keys %preconfig) {
651                 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
652                         my $conf = $preconfig{$preconfig}->{$cfg}->{id};
653                         $conf =~ tr/\.-/__/;
654                         print <<EOF
655         config UCI_PRECONFIG_$conf
656                 string "$preconfig{$preconfig}->{$cfg}->{label}" if IMAGEOPT
657                 depends on PACKAGE_$preconfig
658                 default "$preconfig{$preconfig}->{$cfg}->{default}"
659
660 EOF
661                 }
662         }
663         print "source \"package/*/image-config.in\"\n";
664         if (scalar glob "package/feeds/*/*/image-config.in") {
665             print "source \"package/feeds/*/*/image-config.in\"\n";
666         }
667         print_package_features();
668         print_package_config_category 'Base system';
669         foreach my $cat (sort {uc($a) cmp uc($b)} keys %category) {
670                 print_package_config_category $cat;
671         }
672         print_package_overrides();
673 }
674
675 sub get_conditional_dep($$) {
676         my $condition = shift;
677         my $depstr = shift;
678         if ($condition) {
679                 if ($condition =~ /^!(.+)/) {
680                         return "\$(if \$(CONFIG_$1),,$depstr)";
681                 } else {
682                         return "\$(if \$(CONFIG_$condition),$depstr)";
683                 }
684         } else {
685                 return $depstr;
686         }
687 }
688
689 sub gen_package_mk() {
690         my %conf;
691         my %dep;
692         my %done;
693         my $line;
694
695         parse_package_metadata($ARGV[0]) or exit 1;
696         foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
697                 my $config;
698                 my $pkg = $package{$name};
699                 my @srcdeps;
700
701                 next if defined $pkg->{vdepends};
702
703                 $config = "\$(CONFIG_PACKAGE_$name)";
704                 if ($config) {
705                         $pkg->{buildonly} and $config = "";
706                         print "package-$config += $pkg->{subdir}$pkg->{src}\n";
707                         if ($pkg->{variant}) {
708                                 if (!defined($done{$pkg->{src}}) or $pkg->{variant_default}) {
709                                         print "\$(curdir)/$pkg->{subdir}$pkg->{src}/default-variant := $pkg->{variant}\n";
710                                 }
711                                 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/variants += \$(if $config,$pkg->{variant})\n"
712                         }
713                         $pkg->{prereq} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
714                 }
715
716                 next if $done{$pkg->{src}};
717                 $done{$pkg->{src}} = 1;
718
719                 if (@{$pkg->{buildtypes}} > 0) {
720                         print "buildtypes-$pkg->{subdir}$pkg->{src} = ".join(' ', @{$pkg->{buildtypes}})."\n";
721                 }
722
723                 foreach my $spkg (@{$srcpackage{$pkg->{src}}}) {
724                         foreach my $dep (@{$spkg->{depends}}, @{$spkg->{builddepends}}) {
725                                 $dep =~ /@/ or do {
726                                         $dep =~ s/\+//g;
727                                         push @srcdeps, $dep;
728                                 };
729                         }
730                 }
731                 foreach my $type (@{$pkg->{buildtypes}}) {
732                         my @extra_deps;
733                         my %deplines;
734
735                         next unless $pkg->{"builddepends/$type"};
736                         foreach my $dep (@{$pkg->{"builddepends/$type"}}) {
737                                 my $suffix = "";
738                                 my $condition;
739
740                                 if ($dep =~ /^(.+):(.+)/) {
741                                         $condition = $1;
742                                         $dep = $2;
743                                 }
744                                 if ($dep =~ /^(.+)(\/.+)/) {
745                                         $dep = $1;
746                                         $suffix = $2;
747                                 }
748
749                                 my $idx = "";
750                                 my $pkg_dep = $package{$dep};
751                                 if (defined($pkg_dep) && defined($pkg_dep->{src})) {
752                                         $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
753                                 } elsif (defined($srcpackage{$dep})) {
754                                         $idx = $subdir{$dep}.$dep;
755                                 } else {
756                                         next;
757                                 }
758                                 my $depstr = "\$(curdir)/$idx$suffix/compile";
759                                 my $depline = get_conditional_dep($condition, $depstr);
760                                 if ($depline) {
761                                         $deplines{$depline}++;
762                                 }
763                         }
764                         my $depline = join(" ", sort keys %deplines);
765                         if ($depline) {
766                                 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/$type/compile += $depline\n";
767                         }
768                 }
769
770                 my $hasdeps = 0;
771                 my %deplines;
772                 foreach my $deps (@srcdeps) {
773                         my $idx;
774                         my $condition;
775                         my $prefix = "";
776                         my $suffix = "";
777
778                         if ($deps =~ /^(.+):(.+)/) {
779                                 $condition = $1;
780                                 $deps = $2;
781                         }
782                         if ($deps =~ /^(.+)(\/.+)/) {
783                                 $deps = $1;
784                                 $suffix = $2;
785                         }
786
787                         my $pkg_dep = $package{$deps};
788                         my @deps;
789
790                         if ($pkg_dep->{vdepends}) {
791                                 @deps = @{$pkg_dep->{vdepends}};
792                         } else {
793                                 @deps = ($deps);
794                         }
795
796                         foreach my $dep (@deps) {
797                                 $pkg_dep = $package{$deps};
798                                 if (defined $pkg_dep->{src}) {
799                                         ($pkg->{src} ne $pkg_dep->{src}.$suffix) and $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
800                                 } elsif (defined($srcpackage{$dep})) {
801                                         $idx = $subdir{$dep}.$dep;
802                                 }
803                                 undef $idx if $idx eq 'base-files';
804                                 if ($idx) {
805                                         $idx .= $suffix;
806
807                                         my $depline;
808                                         next if $pkg->{src} eq $pkg_dep->{src}.$suffix;
809                                         next if $dep{$condition.":".$pkg->{src}."->".$idx};
810                                         next if $dep{$pkg->{src}."->($dep)".$idx} and $pkg_dep->{vdepends};
811                                         my $depstr;
812
813                                         if ($pkg_dep->{vdepends}) {
814                                                 $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
815                                                 $dep{$pkg->{src}."->($dep)".$idx} = 1;
816                                         } else {
817                                                 $depstr = "\$(curdir)/$idx/compile";
818                                                 $dep{$pkg->{src}."->".$idx} = 1;
819                                         }
820                                         $depline = get_conditional_dep($condition, $depstr);
821                                         if ($depline) {
822                                                 $deplines{$depline}++;
823                                         }
824                                 }
825                         }
826                 }
827                 my $depline = join(" ", sort keys %deplines);
828                 if ($depline) {
829                         $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/compile += $depline\n";
830                 }
831         }
832
833         if ($line ne "") {
834                 print "\n$line";
835         }
836         foreach my $preconfig (keys %preconfig) {
837                 my $cmds;
838                 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
839                         my $conf = $preconfig{$preconfig}->{$cfg}->{id};
840                         $conf =~ tr/\.-/__/;
841                         $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
842                 }
843                 next unless $cmds;
844                 print <<EOF
845
846 ifndef DUMP_TARGET_DB
847 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
848         ( \\
849 $cmds \\
850         ) > \$@
851         
852 ifneq (\$(IMAGEOPT)\$(CONFIG_IMAGEOPT),)
853   package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
854 endif
855 endif
856
857 EOF
858         }
859 }
860
861 sub gen_package_source() {
862         parse_package_metadata($ARGV[0]) or exit 1;
863         foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
864                 my $pkg = $package{$name};
865                 if ($pkg->{name} && $pkg->{source}) {
866                         print "$pkg->{name}: ";
867                         print "$pkg->{source}\n";
868                 }
869         }
870 }
871
872 sub gen_package_subdirs() {
873         parse_package_metadata($ARGV[0]) or exit 1;
874         foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
875                 my $pkg = $package{$name};
876                 if ($pkg->{name} && $pkg->{repository}) {
877                         print "Package/$name/subdir = $pkg->{repository}\n";
878                 }
879         }
880 }
881
882 sub gen_package_license($) {
883         my $level = shift;
884         parse_package_metadata($ARGV[0]) or exit 1;
885         foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
886                 my $pkg = $package{$name};
887                 if ($pkg->{name}) {
888                         if ($pkg->{license}) {
889                                 print "$pkg->{name}: ";
890                                 print "$pkg->{license}\n";
891                                 if ($pkg->{licensefiles} && $level == 0) {
892                                         print "\tFiles: $pkg->{licensefiles}\n";
893                                 }
894                         } else {
895                                 if ($level == 1) {
896                                         print "$pkg->{name}: Missing license! ";
897                                         print "Please fix $pkg->{makefile}\n";
898                                 }
899                         }
900                 }
901         }
902 }
903
904 sub gen_version_filtered_list() {
905         foreach my $item (version_filter_list(@ARGV)) {
906                 print "$item\n";
907         }
908 }
909
910 sub gen_profile_mk() {
911         my $file = shift @ARGV;
912         my $target = shift @ARGV;
913         my @targets = parse_target_metadata($file);
914         foreach my $cur (@targets) {
915                 next unless $cur->{id} eq $target;
916                 print "PROFILE_NAMES = ".join(" ", map { $_->{id} } @{$cur->{profiles}})."\n";
917                 foreach my $profile (@{$cur->{profiles}}) {
918                         print $profile->{id}.'_NAME:='.$profile->{name}."\n";
919                         print $profile->{id}.'_PACKAGES:='.join(' ', @{$profile->{packages}})."\n";
920                 }
921         }
922 }
923
924 sub parse_command() {
925         GetOptions("ignore=s", \@ignore);
926         my $cmd = shift @ARGV;
927         for ($cmd) {
928                 /^target_config$/ and return gen_target_config();
929                 /^profile_mk$/ and return gen_profile_mk();
930                 /^package_mk$/ and return gen_package_mk();
931                 /^package_config$/ and return gen_package_config();
932                 /^kconfig/ and return gen_kconfig_overrides();
933                 /^package_source$/ and return gen_package_source();
934                 /^package_subdirs$/ and return gen_package_subdirs();
935                 /^package_license$/ and return gen_package_license(0);
936                 /^package_licensefull$/ and return gen_package_license(1);
937                 /^version_filter$/ and return gen_version_filtered_list();
938         }
939         print <<EOF
940 Available Commands:
941         $0 target_config [file]                 Target metadata in Kconfig format
942         $0 profile_mk [file] [target]           Profile metadata in makefile format
943         $0 package_mk [file]                    Package metadata in makefile format
944         $0 package_config [file]                Package metadata in Kconfig format
945         $0 kconfig [file] [config] [patchver]   Kernel config overrides
946         $0 package_source [file]                Package source file information
947         $0 package_subdirs [file]               Package subdir information in makefile format
948         $0 package_license [file]               Package license information
949         $0 package_licensefull [file]           Package license information (full list)
950         $0 version_filter [patchver] [list...]  Filter list of version tagged strings
951
952 Options:
953         --ignore <name>                         Ignore the source package <name>
954 EOF
955 }
956
957 parse_command();