add a gpio feature for devices supporting the generic GPIO interface
[openwrt.git] / scripts / metadata.pl
1 #!/usr/bin/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         }
165         return $ret;
166 }
167
168 sub target_name($) {
169         my $target = shift;
170         my $parent = $target->{parent};
171         if ($parent) {
172                 return $target->{parent}->{name}." - ".$target->{name};
173         } else {
174                 return $target->{name};
175         }
176 }
177
178 sub kver($) {
179         my $v = shift;
180         $v =~ tr/\./_/;
181         $v =~ /(\d+_\d+_\d+)(_\d+)?/ and $v = $1;
182         return $v;
183 }
184
185 sub print_target($) {
186         my $target = shift;
187         my $features = target_config_features(@{$target->{features}});
188         my $help = $target->{desc};
189         my $kernel = $target->{kernel};
190         my $confstr;
191         $kernel =~ tr/./_/;
192
193         chomp $features;
194         $features .= "\n";
195         if ($help =~ /\w+/) {
196                 $help =~ s/^\s*/\t  /mg;
197                 $help = "\thelp\n$help";
198         } else {
199                 undef $help;
200         }
201
202         my $v = kver($target->{version});
203         $confstr = <<EOF;
204 config TARGET_$target->{conf}
205         bool "$target->{name}"
206         select LINUX_$kernel
207         select LINUX_$v
208 EOF
209         if ($target->{subtarget}) {
210                 $confstr .= "\tdepends TARGET_$target->{boardconf}\n";
211         }
212         if (@{$target->{subtargets}} > 0) {
213                 $confstr .= "\tselect HAS_SUBTARGETS\n";
214         } else {
215                 $confstr .= "\tselect $target->{arch}\n";
216                 foreach my $dep (@{$target->{depends}}) {
217                         my $mode = "depends";
218                         my $flags;
219                         my $name;
220
221                         $dep =~ /^([@\+\-]+)(.+)$/;
222                         $flags = $1;
223                         $name = $2;
224
225                         $flags =~ /-/ and $mode = "deselect";
226                         $flags =~ /\+/ and $mode = "select";
227                         $flags =~ /@/ and $confstr .= "\t$mode $name\n";
228                 }
229                 $confstr .= $features;
230         }
231
232         $confstr .= "$help\n\n";
233         print $confstr;
234 }
235
236 sub gen_target_config() {
237         my @target = parse_target_metadata();
238
239         my @target_sort = sort {
240                 target_name($a) cmp target_name($b);
241         } @target;
242
243
244         print <<EOF;
245 choice
246         prompt "Target System"
247         default TARGET_brcm_2_4
248         reset if !DEVEL
249         
250 EOF
251
252         foreach my $target (@target_sort) {
253                 next if $target->{subtarget};
254                 print_target($target);
255         }
256
257         print <<EOF;
258 endchoice
259
260 choice
261         prompt "Subtarget" if HAS_SUBTARGETS
262
263 EOF
264         foreach my $target (@target) {
265                 next unless $target->{subtarget};
266                 print_target($target);
267         }
268
269 print <<EOF;
270 endchoice
271
272 choice
273         prompt "Target Profile"
274
275 EOF
276
277         foreach my $target (@target) {
278                 my $profiles = $target->{profiles};
279
280                 foreach my $profile (@$profiles) {
281                         print <<EOF;
282 config TARGET_$target->{conf}_$profile->{id}
283         bool "$profile->{name}"
284         depends TARGET_$target->{conf}
285 $profile->{config}
286 EOF
287                         $profile->{kconfig} and print "\tselect PROFILE_KCONFIG\n";
288                         my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
289                         foreach my $pkg (@pkglist) {
290                                 print "\tselect DEFAULT_$pkg\n";
291                         }
292                         print "\n";
293                 }
294         }
295
296         print <<EOF;
297 endchoice
298
299 config HAS_SUBTARGETS
300         bool
301
302 config TARGET_BOARD
303         string
304
305 EOF
306         foreach my $target (@target) {
307                 $target->{subtarget} or print "\t\tdefault \"".$target->{board}."\" if TARGET_".$target->{conf}."\n";
308         }
309
310         my %kver;
311         foreach my $target (@target) {
312                 my $v = kver($target->{version});
313                 next if $kver{$v};
314                 $kver{$v} = 1;
315                 print <<EOF;
316 config LINUX_$v
317         bool
318 EOF
319         }
320 }
321
322 my %dep_check;
323 sub __find_package_dep($$) {
324         my $pkg = shift;
325         my $name = shift;
326         my $deps = ($pkg->{vdepends} or $pkg->{depends});
327
328         return 0 unless defined $deps;
329         foreach my $dep (@{$deps}) {
330                 next if $dep_check{$dep};
331                 $dep_check{$dep} = 1;
332                 return 1 if $dep eq $name;
333                 return 1 if ($package{$dep} and (__find_package_dep($package{$dep},$name) == 1));
334         }
335         return 0;
336 }
337
338 # wrapper to avoid infinite recursion
339 sub find_package_dep($$) {
340         my $pkg = shift;
341         my $name = shift;
342
343         %dep_check = ();
344         return __find_package_dep($pkg, $name);
345 }
346
347 sub package_depends($$) {
348         my $a = shift;
349         my $b = shift;
350         my $ret;
351
352         return 0 if ($a->{submenu} ne $b->{submenu});
353         if (find_package_dep($a, $b->{name}) == 1) {
354                 $ret = 1;
355         } elsif (find_package_dep($b, $a->{name}) == 1) {
356                 $ret = -1;
357         } else {
358                 return 0;
359         }
360         return $ret;
361 }
362
363 sub mconf_depends($$) {
364         my $depends = shift;
365         my $only_dep = shift;
366         my $res;
367         my $dep = shift;
368         $dep or $dep = {};
369
370         $depends or return;
371         my @depends = @$depends;
372         foreach my $depend (@depends) {
373                 my $m = "depends";
374                 $depend =~ s/^([@\+]+)//;
375                 my $flags = $1;
376                 my $vdep;
377
378                 if ($vdep = $package{$depend}->{vdepends}) {
379                         $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
380                 } else {
381                         $flags =~ /\+/ and do {
382                                 next if $only_dep;
383                                 $m = "select";
384
385                                 # Menuconfig will not treat 'select FOO' as a real dependency
386                                 # thus if FOO depends on other config options, these dependencies
387                                 # will not be checked. To fix this, we simply emit all of FOO's
388                                 # depends here as well.
389                                 $package{$depend} and mconf_depends($package{$depend}->{depends}, 1, $dep);
390                         };
391                         $flags =~ /@/ or $depend = "PACKAGE_$depend";
392                 }
393                 $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
394         }
395         foreach my $depend (keys %$dep) {
396                 my $m = $dep->{$depend};
397                 $res .= "\t\t$m $depend\n";
398         }
399         return $res;
400 }
401
402 sub print_package_config_category($) {
403         my $cat = shift;
404         my %menus;
405         my %menu_dep;
406
407         return unless $category{$cat};
408
409         print "menu \"$cat\"\n\n";
410         my %spkg = %{$category{$cat}};
411
412         foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
413                 foreach my $pkg (@{$spkg{$spkg}}) {
414                         my $menu = $pkg->{submenu};
415                         if ($menu) {
416                                 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
417                         } else {
418                                 $menu = 'undef';
419                         }
420                         $menus{$menu} or $menus{$menu} = [];
421                         push @{$menus{$menu}}, $pkg;
422                         print "\tconfig DEFAULT_".$pkg->{name}."\n";
423                         print "\t\tbool\n\n";
424                 }
425         }
426         my @menus = sort {
427                 ($a eq 'undef' ?  1 : 0) or
428                 ($b eq 'undef' ? -1 : 0) or
429                 ($a cmp $b)
430         } keys %menus;
431
432         foreach my $menu (@menus) {
433                 my @pkgs = sort {
434                         package_depends($a, $b) or
435                         ($a->{name} cmp $b->{name})
436                 } @{$menus{$menu}};
437                 if ($menu ne 'undef') {
438                         $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
439                         print "menu \"$menu\"\n";
440                 }
441                 foreach my $pkg (@pkgs) {
442                         my $title = $pkg->{name};
443                         my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
444                         if ($c > 0) {
445                                 $title .= ("." x $c). " ". $pkg->{title};
446                         }
447                         print "\t";
448                         $pkg->{menu} and print "menu";
449                         print "config PACKAGE_".$pkg->{name}."\n";
450                         print "\t\t".($pkg->{tristate} ? 'tristate' : 'bool')." \"$title\"\n";
451                         print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
452                         foreach my $default (split /\s*,\s*/, $pkg->{default}) {
453                                 print "\t\tdefault $default\n";
454                         }
455                         print mconf_depends($pkg->{depends}, 0);
456                         print "\t\thelp\n";
457                         print $pkg->{description};
458                         print "\n";
459
460                         $pkg->{config} and print $pkg->{config}."\n";
461                 }
462                 if ($menu ne 'undef') {
463                         print "endmenu\n";
464                         $menu_dep{$menu} and print "endif\n";
465                 }
466         }
467         print "endmenu\n\n";
468
469         undef $category{$cat};
470 }
471
472 sub gen_package_config() {
473         parse_package_metadata($ARGV[0]) or exit 1;
474         print "menuconfig UCI_PRECONFIG\n\tbool \"Image configuration\"\n";
475         foreach my $preconfig (keys %preconfig) {
476                 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
477                         my $conf = $preconfig{$preconfig}->{$cfg}->{id};
478                         $conf =~ tr/\.-/__/;
479                         print <<EOF
480         config UCI_PRECONFIG_$conf
481                 string "$preconfig{$preconfig}->{$cfg}->{label}" if UCI_PRECONFIG
482                 depends PACKAGE_$preconfig
483                 default "$preconfig{$preconfig}->{$cfg}->{default}"
484
485 EOF
486                 }
487         }
488         print_package_config_category 'Base system';
489         foreach my $cat (keys %category) {
490                 print_package_config_category $cat;
491         }
492 }
493
494 sub gen_package_mk() {
495         my %conf;
496         my %dep;
497         my $line;
498
499         parse_package_metadata($ARGV[0]) or exit 1;
500         foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
501                 my $config;
502                 my $pkg = $package{$name};
503
504                 next if defined $pkg->{vdepends};
505                 if ($ENV{SDK}) {
506                         $conf{$pkg->{src}} or do {
507                                 $config = 'm';
508                                 $conf{$pkg->{src}} = 1;
509                         };
510                 } else {
511                         $config = "\$(CONFIG_PACKAGE_$name)"
512                 }
513                 if ($config) {
514                         print "package-$config += $pkg->{subdir}$pkg->{src}\n";
515                         $pkg->{prereq} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
516                 }
517
518                 my $hasdeps = 0;
519                 my $depline = "";
520                 foreach my $dep (@{$pkg->{depends}}, @{$pkg->{builddepends}}) {
521                         next if $dep =~ /@/;
522                         $dep =~ s/\+//;
523                         my $idx;
524                         my $pkg_dep = $package{$dep};
525                         next if defined $pkg_dep->{vdepends};
526
527                         if (defined $pkg_dep->{src}) {
528                                 ($pkg->{src} ne $pkg_dep->{src}) and $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
529                         } elsif (defined($srcpackage{$dep})) {
530                                 $idx = $subdir{$dep}.$dep;
531                         }
532                         undef $idx if $idx =~ /^(kernel)|(base-files)$/;
533                         if ($idx) {
534                                 next if $dep{$pkg->{src}."->".$idx};
535                                 $depline .= " \$(curdir)/$idx/compile";
536                                 $dep{$pkg->{src}."->".$idx} = 1;
537                         }
538                 }
539                 if ($depline) {
540                         $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/compile += $depline\n";
541                 }
542         }
543
544         if ($line ne "") {
545                 print "\n$line";
546         }
547         foreach my $preconfig (keys %preconfig) {
548                 my $cmds;
549                 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
550                         my $conf = $preconfig{$preconfig}->{$cfg}->{id};
551                         $conf =~ tr/\.-/__/;
552                         $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
553                 }
554                 next unless $cmds;
555                 print <<EOF
556
557 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
558         ( \\
559 $cmds \\
560         ) > \$@
561         
562 ifneq (\$(UCI_PRECONFIG)\$(CONFIG_UCI_PRECONFIG),)
563   package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
564 endif
565 EOF
566         }
567 }
568
569
570 sub parse_command() {
571         my $cmd = shift @ARGV;
572         for ($cmd) {
573                 /^target_config$/ and return gen_target_config();
574                 /^package_mk$/ and return gen_package_mk();
575                 /^package_config$/ and return gen_package_config();
576                 /^kconfig/ and return gen_kconfig_overrides();
577         }
578         print <<EOF
579 Available Commands:
580         $0 target_config [file]         Target metadata in Kconfig format
581         $0 package_mk [file]            Package metadata in makefile format
582         $0 package_config [file]        Package metadata in Kconfig format
583         $0 kconfig [file] [config]      Kernel config overrides
584
585 EOF
586 }
587
588 parse_command();