fix infinite recursion in 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 parse_target_metadata() {
16         my $file = shift @ARGV;
17         my ($target, @target, $profile);
18         my %target;
19
20         open FILE, "<$file" or do {
21                 warn "Can't open file '$file': $!\n";
22                 return;
23         };
24         while (<FILE>) {
25                 chomp;
26                 /^Target:\s*(.+)\s*$/ and do {
27                         my $name = $1;
28                         $target = {
29                                 id => $name,
30                                 board => $name,
31                                 boardconf => confstr($name),
32                                 conf => confstr($name),
33                                 profiles => [],
34                                 features => [],
35                                 depends => [],
36                                 subtargets => []
37                         };
38                         push @target, $target;
39                         $target{$name} = $target;
40                         if ($name =~ /([^\/]+)\/([^\/]+)/) {
41                                 push @{$target{$1}->{subtargets}}, $2;
42                                 $target->{board} = $1;
43                                 $target->{boardconf} = confstr($1);
44                                 $target->{subtarget} = 1;
45                                 $target->{parent} = $target{$1};
46                         }
47                 };
48                 /^Target-Kernel:\s*(\d+\.\d+)\s*$/ and $target->{kernel} = $1;
49                 /^Target-Name:\s*(.+)\s*$/ and $target->{name} = $1;
50                 /^Target-Path:\s*(.+)\s*$/ and $target->{path} = $1;
51                 /^Target-Arch:\s*(.+)\s*$/ and $target->{arch} = $1;
52                 /^Target-Features:\s*(.+)\s*$/ and $target->{features} = [ split(/\s+/, $1) ];
53                 /^Target-Depends:\s*(.+)\s*$/ and $target->{depends} = [ split(/\s+/, $1) ];
54                 /^Target-Description:/ and $target->{desc} = get_multiline(*FILE);
55                 /^Linux-Version:\s*(.+)\s*$/ and $target->{version} = $1;
56                 /^Linux-Release:\s*(.+)\s*$/ and $target->{release} = $1;
57                 /^Linux-Kernel-Arch:\s*(.+)\s*$/ and $target->{karch} = $1;
58                 /^Default-Packages:\s*(.+)\s*$/ and $target->{packages} = [ split(/\s+/, $1) ];
59                 /^Target-Profile:\s*(.+)\s*$/ and do {
60                         $profile = {
61                                 id => $1,
62                                 name => $1,
63                                 packages => []
64                         };
65                         push @{$target->{profiles}}, $profile;
66                 };
67                 /^Target-Profile-Name:\s*(.+)\s*$/ and $profile->{name} = $1;
68                 /^Target-Profile-Packages:\s*(.*)\s*$/ and $profile->{packages} = [ split(/\s+/, $1) ];
69                 /^Target-Profile-Description:\s*(.*)\s*/ and $profile->{desc} = get_multiline(*FILE);
70                 /^Target-Profile-Config:/ and $profile->{config} = get_multiline(*FILE, "\t");
71                 /^Target-Profile-Kconfig:/ and $profile->{kconfig} = 1;
72         }
73         close FILE;
74         foreach my $target (@target) {
75                 next if @{$target->{subtargets}} > 0;
76                 @{$target->{profiles}} > 0 or $target->{profiles} = [
77                         {
78                                 id => 'Default',
79                                 name => 'Default',
80                                 packages => []
81                         }
82                 ];
83         }
84         return @target;
85 }
86
87 sub gen_kconfig_overrides() {
88         my %config;
89         my %kconfig;
90         my $package;
91         my $pkginfo = shift @ARGV;
92         my $cfgfile = shift @ARGV;
93
94         # parameter 2: build system config
95         open FILE, "<$cfgfile" or return;
96         while (<FILE>) {
97                 /^(CONFIG_.+?)=(.+)$/ and $config{$1} = 1;
98         }
99         close FILE;
100
101         # parameter 1: package metadata
102         open FILE, "<$pkginfo" or return;
103         while (<FILE>) {
104                 /^Package:\s*(.+?)\s*$/ and $package = $1;
105                 /^Kernel-Config:\s*(.+?)\s*$/ and do {
106                         my @config = split /\s+/, $1;
107                         foreach my $config (@config) {
108                                 my $val = 'm';
109                                 my $override;
110                                 if ($config =~ /^(.+?)=(.+)$/) {
111                                         $config = $1;
112                                         $override = 1;
113                                         $val = $2;
114                                 }
115                                 if ($config{"CONFIG_PACKAGE_$package"} and ($config ne 'n')) {
116                                         $kconfig{$config} = $val;
117                                 } elsif (!$override) {
118                                         $kconfig{$config} or $kconfig{$config} = 'n';
119                                 }
120                         }
121                 };
122         };
123         close FILE;
124
125         foreach my $kconfig (sort keys %kconfig) {
126                 if ($kconfig{$kconfig} eq 'n') {
127                         print "# $kconfig is not set\n";
128                 } else {
129                         print "$kconfig=$kconfig{$kconfig}\n";
130                 }
131         }
132 }
133
134 sub merge_package_lists($$) {
135         my $list1 = shift;
136         my $list2 = shift;
137         my @l = ();
138         my %pkgs;
139
140         foreach my $pkg (@$list1, @$list2) {
141                 $pkgs{$pkg} = 1;
142         }
143         foreach my $pkg (keys %pkgs) {
144                 push @l, $pkg unless ($pkg =~ /^-/ or $pkgs{"-$pkg"});
145         }
146         return sort(@l);
147 }
148
149 sub target_config_features(@) {
150         my $ret;
151
152         while ($_ = shift @_) {
153                 /broken/ and $ret .= "\tdepends BROKEN\n";
154                 /display/ and $ret .= "\tselect DISPLAY_SUPPORT\n";
155                 /gpio/ and $ret .= "\tselect GPIO_SUPPORT\n";
156                 /pci/ and $ret .= "\tselect PCI_SUPPORT\n";
157                 /usb/ and $ret .= "\tselect USB_SUPPORT\n";
158                 /pcmcia/ and $ret .= "\tselect PCMCIA_SUPPORT\n";
159                 /squashfs/ and $ret .= "\tselect USES_SQUASHFS\n";
160                 /jffs2/ and $ret .= "\tselect USES_JFFS2\n";
161                 /ext2/ and $ret .= "\tselect USES_EXT2\n";
162                 /tgz/ and $ret .= "\tselect USES_TGZ\n";
163                 /cpiogz/ and $ret .= "\tselect USES_CPIOGZ\n";
164                 /fpu/ and $ret .= "\tselect HAS_FPU\n";
165         }
166         return $ret;
167 }
168
169 sub target_name($) {
170         my $target = shift;
171         my $parent = $target->{parent};
172         if ($parent) {
173                 return $target->{parent}->{name}." - ".$target->{name};
174         } else {
175                 return $target->{name};
176         }
177 }
178
179 sub kver($) {
180         my $v = shift;
181         $v =~ tr/\./_/;
182         $v =~ /(\d+_\d+_\d+)(_\d+)?/ and $v = $1;
183         return $v;
184 }
185
186 sub print_target($) {
187         my $target = shift;
188         my $features = target_config_features(@{$target->{features}});
189         my $help = $target->{desc};
190         my $kernel = $target->{kernel};
191         my $confstr;
192         $kernel =~ tr/./_/;
193
194         chomp $features;
195         $features .= "\n";
196         if ($help =~ /\w+/) {
197                 $help =~ s/^\s*/\t  /mg;
198                 $help = "\thelp\n$help";
199         } else {
200                 undef $help;
201         }
202
203         my $v = kver($target->{version});
204         $confstr = <<EOF;
205 config TARGET_$target->{conf}
206         bool "$target->{name}"
207         select LINUX_$kernel
208         select LINUX_$v
209 EOF
210         if ($target->{subtarget}) {
211                 $confstr .= "\tdepends TARGET_$target->{boardconf}\n";
212         }
213         if (@{$target->{subtargets}} > 0) {
214                 $confstr .= "\tselect HAS_SUBTARGETS\n";
215         } else {
216                 $confstr .= "\tselect $target->{arch}\n";
217                 foreach my $dep (@{$target->{depends}}) {
218                         my $mode = "depends";
219                         my $flags;
220                         my $name;
221
222                         $dep =~ /^([@\+\-]+)(.+)$/;
223                         $flags = $1;
224                         $name = $2;
225
226                         next if $name =~ /:/;
227                         $flags =~ /-/ and $mode = "deselect";
228                         $flags =~ /\+/ and $mode = "select";
229                         $flags =~ /@/ and $confstr .= "\t$mode $name\n";
230                 }
231                 $confstr .= $features;
232         }
233
234         $confstr .= "$help\n\n";
235         print $confstr;
236 }
237
238 sub gen_target_config() {
239         my @target = parse_target_metadata();
240
241         my @target_sort = sort {
242                 target_name($a) cmp target_name($b);
243         } @target;
244
245
246         print <<EOF;
247 choice
248         prompt "Target System"
249         default TARGET_brcm_2_4
250         reset if !DEVEL
251         
252 EOF
253
254         foreach my $target (@target_sort) {
255                 next if $target->{subtarget};
256                 print_target($target);
257         }
258
259         print <<EOF;
260 endchoice
261
262 choice
263         prompt "Subtarget" if HAS_SUBTARGETS
264
265 EOF
266         foreach my $target (@target) {
267                 next unless $target->{subtarget};
268                 print_target($target);
269         }
270
271 print <<EOF;
272 endchoice
273
274 choice
275         prompt "Target Profile"
276
277 EOF
278
279         foreach my $target (@target) {
280                 my $profiles = $target->{profiles};
281
282                 foreach my $profile (@$profiles) {
283                         print <<EOF;
284 config TARGET_$target->{conf}_$profile->{id}
285         bool "$profile->{name}"
286         depends TARGET_$target->{conf}
287 $profile->{config}
288 EOF
289                         $profile->{kconfig} and print "\tselect PROFILE_KCONFIG\n";
290                         my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
291                         foreach my $pkg (@pkglist) {
292                                 print "\tselect DEFAULT_$pkg\n";
293                         }
294                         print "\n";
295                 }
296         }
297
298         print <<EOF;
299 endchoice
300
301 config HAS_SUBTARGETS
302         bool
303
304 config TARGET_BOARD
305         string
306
307 EOF
308         foreach my $target (@target) {
309                 $target->{subtarget} or print "\t\tdefault \"".$target->{board}."\" if TARGET_".$target->{conf}."\n";
310         }
311
312         my %kver;
313         foreach my $target (@target) {
314                 my $v = kver($target->{version});
315                 next if $kver{$v};
316                 $kver{$v} = 1;
317                 print <<EOF;
318 config LINUX_$v
319         bool
320 EOF
321         }
322 }
323
324 my %dep_check;
325 sub __find_package_dep($$) {
326         my $pkg = shift;
327         my $name = shift;
328         my $deps = ($pkg->{vdepends} or $pkg->{depends});
329
330         return 0 unless defined $deps;
331         foreach my $dep (@{$deps}) {
332                 next if $dep_check{$dep};
333                 $dep_check{$dep} = 1;
334                 return 1 if $dep eq $name;
335                 return 1 if ($package{$dep} and (__find_package_dep($package{$dep},$name) == 1));
336         }
337         return 0;
338 }
339
340 # wrapper to avoid infinite recursion
341 sub find_package_dep($$) {
342         my $pkg = shift;
343         my $name = shift;
344
345         %dep_check = ();
346         return __find_package_dep($pkg, $name);
347 }
348
349 sub package_depends($$) {
350         my $a = shift;
351         my $b = shift;
352         my $ret;
353
354         return 0 if ($a->{submenu} ne $b->{submenu});
355         if (find_package_dep($a, $b->{name}) == 1) {
356                 $ret = 1;
357         } elsif (find_package_dep($b, $a->{name}) == 1) {
358                 $ret = -1;
359         } else {
360                 return 0;
361         }
362         return $ret;
363 }
364
365 sub mconf_depends($$) {
366         my $depends = shift;
367         my $only_dep = shift;
368         my $res;
369         my $dep = shift;
370         my $seen = shift;
371         $dep or $dep = {};
372         $seen or $seen = {};
373
374         $depends or return;
375         my @depends = @$depends;
376         foreach my $depend (@depends) {
377                 my $m = "depends";
378                 $depend =~ s/^([@\+]+)//;
379                 my $flags = $1;
380                 my $condition;
381                 my $vdep;
382
383                 if ($depend =~ /^(.+):(.+)$/) {
384                         $condition = $1;
385                         $depend = $2;
386                 }
387                 next if $seen->{$depend};
388                 $seen->{$depend} = 1;
389                 if ($vdep = $package{$depend}->{vdepends}) {
390                         $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
391                 } else {
392                         $flags =~ /\+/ and do {
393                                 # Menuconfig will not treat 'select FOO' as a real dependency
394                                 # thus if FOO depends on other config options, these dependencies
395                                 # will not be checked. To fix this, we simply emit all of FOO's
396                                 # depends here as well.
397                                 $package{$depend} and mconf_depends($package{$depend}->{depends}, 1, $dep, $seen);
398
399                                 $m = "select";
400                                 next if $only_dep;
401                         };
402                         $flags =~ /@/ or $depend = "PACKAGE_$depend";
403                         $condition and $depend = "$depend if $condition";
404                 }
405                 $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
406         }
407         foreach my $depend (keys %$dep) {
408                 my $m = $dep->{$depend};
409                 $res .= "\t\t$m $depend\n";
410         }
411         return $res;
412 }
413
414 sub print_package_config_category($) {
415         my $cat = shift;
416         my %menus;
417         my %menu_dep;
418
419         return unless $category{$cat};
420
421         print "menu \"$cat\"\n\n";
422         my %spkg = %{$category{$cat}};
423
424         foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
425                 foreach my $pkg (@{$spkg{$spkg}}) {
426                         my $menu = $pkg->{submenu};
427                         if ($menu) {
428                                 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
429                         } else {
430                                 $menu = 'undef';
431                         }
432                         $menus{$menu} or $menus{$menu} = [];
433                         push @{$menus{$menu}}, $pkg;
434                         print "\tconfig DEFAULT_".$pkg->{name}."\n";
435                         print "\t\tbool\n\n";
436                 }
437         }
438         my @menus = sort {
439                 ($a eq 'undef' ?  1 : 0) or
440                 ($b eq 'undef' ? -1 : 0) or
441                 ($a cmp $b)
442         } keys %menus;
443
444         foreach my $menu (@menus) {
445                 my @pkgs = sort {
446                         package_depends($a, $b) or
447                         ($a->{name} cmp $b->{name})
448                 } @{$menus{$menu}};
449                 if ($menu ne 'undef') {
450                         $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
451                         print "menu \"$menu\"\n";
452                 }
453                 foreach my $pkg (@pkgs) {
454                         my $title = $pkg->{name};
455                         my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
456                         if ($c > 0) {
457                                 $title .= ("." x $c). " ". $pkg->{title};
458                         }
459                         print "\t";
460                         $pkg->{menu} and print "menu";
461                         print "config PACKAGE_".$pkg->{name}."\n";
462                         print "\t\t".($pkg->{tristate} ? 'tristate' : 'bool')." \"$title\"\n";
463                         print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
464                         foreach my $default (split /\s*,\s*/, $pkg->{default}) {
465                                 print "\t\tdefault $default\n";
466                         }
467                         print mconf_depends($pkg->{depends}, 0);
468                         print "\t\thelp\n";
469                         print $pkg->{description};
470                         print "\n";
471
472                         $pkg->{config} and print $pkg->{config}."\n";
473                 }
474                 if ($menu ne 'undef') {
475                         print "endmenu\n";
476                         $menu_dep{$menu} and print "endif\n";
477                 }
478         }
479         print "endmenu\n\n";
480
481         undef $category{$cat};
482 }
483
484 sub gen_package_config() {
485         parse_package_metadata($ARGV[0]) or exit 1;
486         print "menuconfig UCI_PRECONFIG\n\tbool \"Image configuration\"\n" if %preconfig;
487         foreach my $preconfig (keys %preconfig) {
488                 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
489                         my $conf = $preconfig{$preconfig}->{$cfg}->{id};
490                         $conf =~ tr/\.-/__/;
491                         print <<EOF
492         config UCI_PRECONFIG_$conf
493                 string "$preconfig{$preconfig}->{$cfg}->{label}" if UCI_PRECONFIG
494                 depends PACKAGE_$preconfig
495                 default "$preconfig{$preconfig}->{$cfg}->{default}"
496
497 EOF
498                 }
499         }
500         print_package_config_category 'Base system';
501         foreach my $cat (keys %category) {
502                 print_package_config_category $cat;
503         }
504 }
505
506 sub gen_package_mk() {
507         my %conf;
508         my %dep;
509         my %done;
510         my $line;
511
512         parse_package_metadata($ARGV[0]) or exit 1;
513         foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
514                 my $config;
515                 my $pkg = $package{$name};
516                 my @srcdeps;
517
518                 next if defined $pkg->{vdepends};
519
520                 if ($ENV{SDK}) {
521                         $conf{$pkg->{src}} or do {
522                                 $config = 'm';
523                                 $conf{$pkg->{src}} = 1;
524                         };
525                 } else {
526                         $config = "\$(CONFIG_PACKAGE_$name)"
527                 }
528                 if ($config) {
529                         print "package-$config += $pkg->{subdir}$pkg->{src}\n";
530                         $pkg->{prereq} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
531                 }
532
533                 next if $done{$pkg->{src}};
534                 $done{$pkg->{src}} = 1;
535
536                 foreach my $spkg (@{$srcpackage{$pkg->{src}}}) {
537                         foreach my $dep (@{$spkg->{depends}}, @{$spkg->{builddepends}}) {
538                                 $dep =~ /@/ or do {
539                                         $dep =~ s/\+//g;
540                                         push @srcdeps, $dep;
541                                 };
542                         }
543                 }
544
545                 my $hasdeps = 0;
546                 my $depline = "";
547                 foreach my $deps (@srcdeps) {
548                         my $idx;
549                         my $condition;
550
551                         if ($deps =~ /^(.+):(.+)/) {
552                                 $condition = $1;
553                                 $deps = $2;
554                         }
555
556                         my $pkg_dep = $package{$deps};
557                         my @deps;
558
559                         if ($pkg_dep->{vdepends}) {
560                                 @deps = @{$pkg_dep->{vdepends}};
561                         } else {
562                                 @deps = ($deps);
563                         }
564
565                         foreach my $dep (@deps) {
566                                 $pkg_dep = $package{$deps};
567                                 if (defined $pkg_dep->{src}) {
568                                         ($pkg->{src} ne $pkg_dep->{src}) and $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
569                                 } elsif (defined($srcpackage{$dep})) {
570                                         $idx = $subdir{$dep}.$dep;
571                                 }
572                                 undef $idx if $idx =~ /^(kernel)|(base-files)$/;
573                                 if ($idx) {
574                                         next if $pkg->{src} eq $pkg_dep->{src};
575                                         next if $dep{$pkg->{src}."->".$idx};
576                                         next if $dep{$pkg->{src}."->($dep)".$idx};
577                                         my $depstr;
578
579                                         if ($pkg_dep->{vdepends}) {
580                                                 $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
581                                                 $dep{$pkg->{src}."->($dep)".$idx} = 1;
582                                         } else {
583                                                 $depstr = "\$(curdir)/$idx/compile";
584                                                 $dep{$pkg->{src}."->".$idx} = 1;
585                                         }
586                                         if ($condition) {
587                                                 $depline .= " \$(if \$(CONFIG_$condition),$depstr)";
588                                         } else {
589                                                 $depline .= " $depstr";
590                                         }
591                                 }
592                         }
593                 }
594                 if ($depline) {
595                         $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/compile += $depline\n";
596                 }
597         }
598
599         if ($line ne "") {
600                 print "\n$line";
601         }
602         foreach my $preconfig (keys %preconfig) {
603                 my $cmds;
604                 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
605                         my $conf = $preconfig{$preconfig}->{$cfg}->{id};
606                         $conf =~ tr/\.-/__/;
607                         $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
608                 }
609                 next unless $cmds;
610                 print <<EOF
611
612 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
613         ( \\
614 $cmds \\
615         ) > \$@
616         
617 ifneq (\$(UCI_PRECONFIG)\$(CONFIG_UCI_PRECONFIG),)
618   package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
619 endif
620 EOF
621         }
622 }
623
624
625 sub parse_command() {
626         my $cmd = shift @ARGV;
627         for ($cmd) {
628                 /^target_config$/ and return gen_target_config();
629                 /^package_mk$/ and return gen_package_mk();
630                 /^package_config$/ and return gen_package_config();
631                 /^kconfig/ and return gen_kconfig_overrides();
632         }
633         print <<EOF
634 Available Commands:
635         $0 target_config [file]         Target metadata in Kconfig format
636         $0 package_mk [file]            Package metadata in makefile format
637         $0 package_config [file]        Package metadata in Kconfig format
638         $0 kconfig [file] [config]      Kernel config overrides
639
640 EOF
641 }
642
643 parse_command();