brcm2708: force uart as the default console for procd and remove nonexistent console
[oweals/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 print_package_overrides() {
683         keys %overrides > 0 or return;
684         print "\tconfig OVERRIDE_PKGS\n";
685         print "\t\tstring\n";
686         print "\t\tdefault \"".join(" ", keys %overrides)."\"\n\n";
687 }
688
689 sub gen_package_config() {
690         parse_package_metadata($ARGV[0]) or exit 1;
691         print "menuconfig IMAGEOPT\n\tbool \"Image configuration\"\n\tdefault n\n";
692         foreach my $preconfig (keys %preconfig) {
693                 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
694                         my $conf = $preconfig{$preconfig}->{$cfg}->{id};
695                         $conf =~ tr/\.-/__/;
696                         print <<EOF
697         config UCI_PRECONFIG_$conf
698                 string "$preconfig{$preconfig}->{$cfg}->{label}" if IMAGEOPT
699                 depends on PACKAGE_$preconfig
700                 default "$preconfig{$preconfig}->{$cfg}->{default}"
701
702 EOF
703                 }
704         }
705         print "source \"package/*/image-config.in\"\n";
706         if (scalar glob "package/feeds/*/*/image-config.in") {
707             print "source \"package/feeds/*/*/image-config.in\"\n";
708         }
709         print_package_features();
710         print_package_config_category 'Base system';
711         foreach my $cat (sort {uc($a) cmp uc($b)} keys %category) {
712                 print_package_config_category $cat;
713         }
714         print_package_overrides();
715 }
716
717 sub get_conditional_dep($$) {
718         my $condition = shift;
719         my $depstr = shift;
720         if ($condition) {
721                 if ($condition =~ /^!(.+)/) {
722                         return "\$(if \$(CONFIG_$1),,$depstr)";
723                 } else {
724                         return "\$(if \$(CONFIG_$condition),$depstr)";
725                 }
726         } else {
727                 return $depstr;
728         }
729 }
730
731 sub gen_package_mk() {
732         my %conf;
733         my %dep;
734         my %done;
735         my $line;
736
737         parse_package_metadata($ARGV[0]) or exit 1;
738         foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
739                 my $config;
740                 my $pkg = $package{$name};
741                 my @srcdeps;
742
743                 next if defined $pkg->{vdepends};
744
745                 $config = "\$(CONFIG_PACKAGE_$name)";
746                 if ($config) {
747                         $pkg->{buildonly} and $config = "";
748                         print "package-$config += $pkg->{subdir}$pkg->{src}\n";
749                         if ($pkg->{variant}) {
750                                 if (!defined($done{$pkg->{src}}) or $pkg->{variant_default}) {
751                                         print "\$(curdir)/$pkg->{subdir}$pkg->{src}/default-variant := $pkg->{variant}\n";
752                                 }
753                                 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/variants += \$(if $config,$pkg->{variant})\n"
754                         }
755                         $pkg->{prereq} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
756                 }
757
758                 next if $done{$pkg->{src}};
759                 $done{$pkg->{src}} = 1;
760
761                 if (@{$pkg->{buildtypes}} > 0) {
762                         print "buildtypes-$pkg->{subdir}$pkg->{src} = ".join(' ', @{$pkg->{buildtypes}})."\n";
763                 }
764
765                 foreach my $spkg (@{$srcpackage{$pkg->{src}}}) {
766                         foreach my $dep (@{$spkg->{depends}}, @{$spkg->{builddepends}}) {
767                                 $dep =~ /@/ or do {
768                                         $dep =~ s/\+//g;
769                                         push @srcdeps, $dep;
770                                 };
771                         }
772                 }
773                 foreach my $type (@{$pkg->{buildtypes}}) {
774                         my @extra_deps;
775                         my %deplines;
776
777                         next unless $pkg->{"builddepends/$type"};
778                         foreach my $dep (@{$pkg->{"builddepends/$type"}}) {
779                                 my $suffix = "";
780                                 my $condition;
781
782                                 if ($dep =~ /^(.+):(.+)/) {
783                                         $condition = $1;
784                                         $dep = $2;
785                                 }
786                                 if ($dep =~ /^(.+)(\/.+)/) {
787                                         $dep = $1;
788                                         $suffix = $2;
789                                 }
790
791                                 my $idx = "";
792                                 my $pkg_dep = $package{$dep};
793                                 if (defined($pkg_dep) && defined($pkg_dep->{src})) {
794                                         $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
795                                 } elsif (defined($srcpackage{$dep})) {
796                                         $idx = $subdir{$dep}.$dep;
797                                 } else {
798                                         next;
799                                 }
800                                 my $depstr = "\$(curdir)/$idx$suffix/compile";
801                                 my $depline = get_conditional_dep($condition, $depstr);
802                                 if ($depline) {
803                                         $deplines{$depline}++;
804                                 }
805                         }
806                         my $depline = join(" ", sort keys %deplines);
807                         if ($depline) {
808                                 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/$type/compile += $depline\n";
809                         }
810                 }
811
812                 my $hasdeps = 0;
813                 my %deplines;
814                 foreach my $deps (@srcdeps) {
815                         my $idx;
816                         my $condition;
817                         my $prefix = "";
818                         my $suffix = "";
819
820                         if ($deps =~ /^(.+):(.+)/) {
821                                 $condition = $1;
822                                 $deps = $2;
823                         }
824                         if ($deps =~ /^(.+)(\/.+)/) {
825                                 $deps = $1;
826                                 $suffix = $2;
827                         }
828
829                         my $pkg_dep = $package{$deps};
830                         my @deps;
831
832                         if ($pkg_dep->{vdepends}) {
833                                 @deps = @{$pkg_dep->{vdepends}};
834                         } else {
835                                 @deps = ($deps);
836                         }
837
838                         foreach my $dep (@deps) {
839                                 $pkg_dep = $package{$deps};
840                                 if (defined $pkg_dep->{src}) {
841                                         ($pkg->{src} ne $pkg_dep->{src}.$suffix) and $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
842                                 } elsif (defined($srcpackage{$dep})) {
843                                         $idx = $subdir{$dep}.$dep;
844                                 }
845                                 undef $idx if $idx eq 'base-files';
846                                 if ($idx) {
847                                         $idx .= $suffix;
848
849                                         my $depline;
850                                         next if $pkg->{src} eq $pkg_dep->{src}.$suffix;
851                                         next if $dep{$condition.":".$pkg->{src}."->".$idx};
852                                         next if $dep{$pkg->{src}."->($dep)".$idx} and $pkg_dep->{vdepends};
853                                         my $depstr;
854
855                                         if ($pkg_dep->{vdepends}) {
856                                                 $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
857                                                 $dep{$pkg->{src}."->($dep)".$idx} = 1;
858                                         } else {
859                                                 $depstr = "\$(curdir)/$idx/compile";
860                                                 $dep{$pkg->{src}."->".$idx} = 1;
861                                         }
862                                         $depline = get_conditional_dep($condition, $depstr);
863                                         if ($depline) {
864                                                 $deplines{$depline}++;
865                                         }
866                                 }
867                         }
868                 }
869                 my $depline = join(" ", sort keys %deplines);
870                 if ($depline) {
871                         $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/compile += $depline\n";
872                 }
873         }
874
875         if ($line ne "") {
876                 print "\n$line";
877         }
878         foreach my $preconfig (keys %preconfig) {
879                 my $cmds;
880                 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
881                         my $conf = $preconfig{$preconfig}->{$cfg}->{id};
882                         $conf =~ tr/\.-/__/;
883                         $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
884                 }
885                 next unless $cmds;
886                 print <<EOF
887
888 ifndef DUMP_TARGET_DB
889 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
890         ( \\
891 $cmds \\
892         ) > \$@
893         
894 ifneq (\$(IMAGEOPT)\$(CONFIG_IMAGEOPT),)
895   package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
896 endif
897 endif
898
899 EOF
900         }
901 }
902
903 sub gen_package_source() {
904         parse_package_metadata($ARGV[0]) or exit 1;
905         foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
906                 my $pkg = $package{$name};
907                 if ($pkg->{name} && $pkg->{source}) {
908                         print "$pkg->{name}: ";
909                         print "$pkg->{source}\n";
910                 }
911         }
912 }
913
914 sub gen_package_feeds() {
915         parse_package_metadata($ARGV[0]) or exit 1;
916         foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
917                 my $pkg = $package{$name};
918                 if ($pkg->{name} && $pkg->{feed}) {
919                         print "Package/$name/feed = $pkg->{feed}\n";
920                 }
921         }
922 }
923
924 sub gen_package_license($) {
925         my $level = shift;
926         parse_package_metadata($ARGV[0]) or exit 1;
927         foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
928                 my $pkg = $package{$name};
929                 if ($pkg->{name}) {
930                         if ($pkg->{license}) {
931                                 print "$pkg->{name}: ";
932                                 print "$pkg->{license}\n";
933                                 if ($pkg->{licensefiles} && $level == 0) {
934                                         print "\tFiles: $pkg->{licensefiles}\n";
935                                 }
936                         } else {
937                                 if ($level == 1) {
938                                         print "$pkg->{name}: Missing license! ";
939                                         print "Please fix $pkg->{makefile}\n";
940                                 }
941                         }
942                 }
943         }
944 }
945
946 sub gen_version_filtered_list() {
947         foreach my $item (version_filter_list(@ARGV)) {
948                 print "$item\n";
949         }
950 }
951
952 sub parse_command() {
953         my $cmd = shift @ARGV;
954         for ($cmd) {
955                 /^target_config$/ and return gen_target_config();
956                 /^package_mk$/ and return gen_package_mk();
957                 /^package_config$/ and return gen_package_config();
958                 /^kconfig/ and return gen_kconfig_overrides();
959                 /^package_source$/ and return gen_package_source();
960                 /^package_feeds$/ and return gen_package_feeds();
961                 /^package_license$/ and return gen_package_license(0);
962                 /^package_licensefull$/ and return gen_package_license(1);
963                 /^version_filter$/ and return gen_version_filtered_list();
964         }
965         print <<EOF
966 Available Commands:
967         $0 target_config [file]                 Target metadata in Kconfig format
968         $0 package_mk [file]                    Package metadata in makefile format
969         $0 package_config [file]                Package metadata in Kconfig format
970         $0 kconfig [file] [config] [patchver]   Kernel config overrides
971         $0 package_source [file]                Package source file information
972         $0 package_feeds [file]                 Package feed information in makefile format
973         $0 package_license [file]               Package license information
974         $0 package_licensefull [file]           Package license information (full list)
975         $0 version_filter [patchver] [list...]  Filter list of version tagged strings
976
977 EOF
978 }
979
980 parse_command();