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