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