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