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