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