fix [6191]
[openwrt/openwrt.git] / scripts / metadata.pl
1 #!/usr/bin/perl
2 use strict;
3 my %package;
4 my %category;
5
6 sub parse_target_metadata() {
7 my ($target, @target, $profile);
8 while (<>) {
9 chomp;
10 /^Target:\s*((.+)-(\d+\.\d+))\s*$/ and do {
11 my $conf = uc $3.'_'.$2;
12 $conf =~ tr/\.-/__/;
13 $target = {
14 id => $1,
15 conf => $conf,
16 board => $2,
17 kernel => $3,
18 profiles => []
19 };
20 push @target, $target;
21 };
22 /^Target-Name:\s*(.+)\s*$/ and $target->{name} = $1;
23 /^Target-Path:\s*(.+)\s*$/ and $target->{path} = $1;
24 /^Target-Arch:\s*(.+)\s*$/ and $target->{arch} = $1;
25 /^Target-Features:\s*(.+)\s*$/ and $target->{features} = [ split(/\s+/, $1) ];
26 /^Target-Description:/ and do {
27 my $desc;
28 while (<>) {
29 last if /^@@/;
30 $desc .= $_;
31 }
32 $target->{desc} = $desc;
33 };
34 /^Linux-Version:\s*(.+)\s*$/ and $target->{version} = $1;
35 /^Linux-Release:\s*(.+)\s*$/ and $target->{release} = $1;
36 /^Linux-Kernel-Arch:\s*(.+)\s*$/ and $target->{karch} = $1;
37 /^Default-Packages:\s*(.+)\s*$/ and $target->{packages} = [ split(/\s+/, $1) ];
38 /^Target-Profile:\s*(.+)\s*$/ and do {
39 $profile = {
40 id => $1,
41 name => $1,
42 packages => []
43 };
44 push @{$target->{profiles}}, $profile;
45 };
46 /^Target-Profile-Name:\s*(.+)\s*$/ and $profile->{name} = $1;
47 /^Target-Profile-Packages:\s*(.*)\s*$/ and $profile->{packages} = [ split(/\s+/, $1) ];
48 /^Target-Profile-Description:/ and do {
49 my $desc;
50 while (<>) {
51 last if /^@@/;
52 $desc .= $_;
53 }
54 $profile->{desc} = $desc;
55 };
56 }
57 foreach my $target (@target) {
58 @{$target->{profiles}} > 0 or $target->{profiles} = [
59 {
60 id => 'Default',
61 name => 'Default',
62 packages => []
63 }
64 ];
65 }
66 return @target;
67 }
68
69 sub parse_package_metadata() {
70 my $pkg;
71 my $makefile;
72 my $src;
73 while (<>) {
74 chomp;
75 /^Source-Makefile: \s*(.+\/([^\/]+)\/Makefile)\s*$/ and do {
76 $makefile = $1;
77 $src = $2;
78 undef $pkg;
79 };
80 /^Package: \s*(.+)\s*$/ and do {
81 $pkg = {};
82 $pkg->{src} = $src;
83 $pkg->{makefile} = $makefile;
84 $pkg->{name} = $1;
85 $pkg->{default} = "m if ALL";
86 $package{$1} = $pkg;
87 };
88 /^Version: \s*(.+)\s*$/ and $pkg->{version} = $1;
89 /^Title: \s*(.+)\s*$/ and $pkg->{title} = $1;
90 /^Menu: \s*(.+)\s*$/ and $pkg->{menu} = $1;
91 /^Submenu: \s*(.+)\s*$/ and $pkg->{submenu} = $1;
92 /^Submenu-Depends: \s*(.+)\s*$/ and $pkg->{submenudep} = $1;
93 /^Default: \s*(.+)\s*$/ and $pkg->{default} = $1;
94 /^Provides: \s*(.+)\s*$/ and do {
95 my @vpkg = split /\s+/, $1;
96 foreach my $vpkg (@vpkg) {
97 $package{$vpkg} or $package{$vpkg} = { vdepends => [] };
98 push @{$package{$vpkg}->{vdepends}}, $pkg->{name};
99 }
100 };
101 /^Depends: \s*(.+)\s*$/ and do {
102 my @dep = split /\s+/, $1;
103 $pkg->{depends} = \@dep;
104 };
105 /^Category: \s*(.+)\s*$/ and do {
106 $pkg->{category} = $1;
107 defined $category{$1} or $category{$1} = {};
108 defined $category{$1}->{$src} or $category{$1}->{$src} = [];
109 push @{$category{$1}->{$src}}, $pkg;
110 };
111 /^Description: \s*(.*)\s*$/ and do {
112 my $desc = "\t\t$1\n\n";
113 my $line;
114 while ($line = <>) {
115 last if $line =~ /^@@/;
116 $desc .= "\t\t$line";
117 }
118 $pkg->{description} = $desc;
119 };
120 /^Config: \s*(.*)\s*$/ and do {
121 my $conf = "$1\n";
122 my $line;
123 while ($line = <>) {
124 last if $line =~ /^@@/;
125 $conf .= "$line";
126 }
127 $pkg->{config} = $conf;
128 }
129 }
130 return %category;
131 }
132
133
134 sub gen_target_mk() {
135 my @target = parse_target_metadata();
136
137 @target = sort {
138 $a->{id} cmp $b->{id}
139 } @target;
140
141 foreach my $target (@target) {
142 my ($profiles_def, $profiles_eval);
143 my $conf = uc $target->{kernel}.'_'.$target->{board};
144 $conf =~ tr/\.-/__/;
145
146 foreach my $profile (@{$target->{profiles}}) {
147 $profiles_def .= "
148 define Profile/$conf\_$profile->{id}
149 ID:=$profile->{id}
150 NAME:=$profile->{name}
151 PACKAGES:=".join(" ", @{$profile->{packages}})."
152 endef";
153 $profiles_eval .= "
154 \$(eval \$(call Profile,$conf\_$profile->{id}))"
155 }
156 print "
157 ifeq (\$(CONFIG_LINUX_$conf),y)
158 define Target
159 KERNEL:=$target->{kernel}
160 BOARD:=$target->{board}
161 BOARDNAME:=$target->{name}
162 LINUX_VERSION:=$target->{version}
163 LINUX_RELEASE:=$target->{release}
164 LINUX_KARCH:=$target->{karch}
165 DEFAULT_PACKAGES:=".join(" ", @{$target->{packages}})."
166 endef$profiles_def
167 endif$profiles_eval
168
169 "
170 }
171 print "\$(eval \$(call Target))\n";
172 }
173
174 sub target_config_features(@) {
175 my $ret;
176
177 while ($_ = shift @_) {
178 /broken/ and $ret .= "\tdepends BROKEN\n";
179 /pci/ and $ret .= "\tselect PCI_SUPPORT\n";
180 /usb/ and $ret .= "\tselect USB_SUPPORT\n";
181 /atm/ and $ret .= "\tselect ATM_SUPPORT\n";
182 /pcmcia/ and $ret .= "\tselect PCMCIA_SUPPORT\n";
183 /video/ and $ret .= "\tselect VIDEO_SUPPORT\n";
184 /squashfs/ and $ret .= "\tselect USES_SQUASHFS\n";
185 /jffs2/ and $ret .= "\tselect USES_JFFS2\n";
186 /ext2/ and $ret .= "\tselect USES_EXT2\n";
187 }
188 return $ret;
189 }
190
191
192 sub gen_target_config() {
193 my @target = parse_target_metadata();
194
195 @target = sort {
196 $a->{name} cmp $b->{name}
197 } @target;
198
199
200 print <<EOF;
201 choice
202 prompt "Target System"
203 default LINUX_2_4_BRCM
204
205 EOF
206
207 foreach my $target (@target) {
208 my $features = target_config_features(@{$target->{features}});
209 my $help = $target->{desc};
210 my $kernel = $target->{kernel};
211 $kernel =~ tr/./_/;
212
213 chomp $features;
214 $features .= "\n";
215 if ($help =~ /\w+/) {
216 $help =~ s/^\s*/\t /mg;
217 $help = "\thelp\n$help";
218 } else {
219 undef $help;
220 }
221
222 print <<EOF
223 config LINUX_$target->{conf}
224 bool "$target->{name}"
225 select $target->{arch}
226 select LINUX_$kernel
227 $features$help
228
229 EOF
230 }
231
232 print <<EOF;
233 if DEVEL
234
235 config LINUX_2_6_ARM
236 bool "UNSUPPORTED little-endian arm platform"
237 depends BROKEN
238 select LINUX_2_6
239 select arm
240
241 config LINUX_2_6_CRIS
242 bool "UNSUPPORTED cris platform"
243 depends BROKEN
244 select LINUX_2_6
245 select cris
246
247 config LINUX_2_6_M68K
248 bool "UNSUPPORTED m68k platform"
249 depends BROKEN
250 select LINUX_2_6
251 select m68k
252
253 config LINUX_2_6_SH3
254 bool "UNSUPPORTED little-endian sh3 platform"
255 depends BROKEN
256 select LINUX_2_6
257 select sh3
258
259 config LINUX_2_6_SH3EB
260 bool "UNSUPPORTED big-endian sh3 platform"
261 depends BROKEN
262 select LINUX_2_6
263 select sh3eb
264
265 config LINUX_2_6_SH4
266 bool "UNSUPPORTED little-endian sh4 platform"
267 depends BROKEN
268 select LINUX_2_6
269 select sh4
270
271 config LINUX_2_6_SH4EB
272 bool "UNSUPPORTED big-endian sh4 platform"
273 depends BROKEN
274 select LINUX_2_6
275 select sh4eb
276
277 config LINUX_2_6_SPARC
278 bool "UNSUPPORTED sparc platform"
279 depends BROKEN
280 select LINUX_2_6
281 select sparc
282
283 endif
284
285 endchoice
286
287 choice
288 prompt "Target Profile"
289
290 EOF
291
292 foreach my $target (@target) {
293 my $profiles = $target->{profiles};
294
295 foreach my $profile (@$profiles) {
296 print <<EOF;
297 config LINUX_$target->{conf}_$profile->{id}
298 bool "$profile->{name}"
299 depends LINUX_$target->{conf}
300 EOF
301 my %pkgs;
302 foreach my $pkg (@{$target->{packages}}, @{$profile->{packages}}) {
303 $pkgs{$pkg} = 1;
304 }
305 foreach my $pkg (keys %pkgs) {
306 print "\tselect DEFAULT_$pkg\n" unless ($pkg =~ /^-/ or $pkgs{"-$pkg"});
307 }
308 print "\n";
309 }
310 }
311
312 print "endchoice\n";
313 }
314
315 sub find_package_dep($$) {
316 my $pkg = shift;
317 my $name = shift;
318 my $deps = ($pkg->{vdepends} or $pkg->{depends});
319
320 return 0 unless defined $deps;
321 foreach my $dep (@{$deps}) {
322 return 1 if $dep eq $name;
323 return 1 if ($package{$dep} and (find_package_dep($package{$dep},$name) == 1));
324 }
325 return 0;
326 }
327
328 sub package_depends($$) {
329 my $a = shift;
330 my $b = shift;
331 my $ret;
332
333 return 0 if ($a->{submenu} ne $b->{submenu});
334 if (find_package_dep($a, $b->{name}) == 1) {
335 $ret = 1;
336 } elsif (find_package_dep($b, $a->{name}) == 1) {
337 $ret = -1;
338 } else {
339 return 0;
340 }
341 return $ret;
342 }
343
344 sub print_package_config_category($) {
345 my $cat = shift;
346 my %menus;
347 my %menu_dep;
348
349 return unless $category{$cat};
350
351 print "menu \"$cat\"\n\n";
352 my %spkg = %{$category{$cat}};
353
354 foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
355 foreach my $pkg (@{$spkg{$spkg}}) {
356 my $menu = $pkg->{submenu};
357 if ($menu) {
358 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
359 } else {
360 $menu = 'undef';
361 }
362 $menus{$menu} or $menus{$menu} = [];
363 push @{$menus{$menu}}, $pkg;
364 print "\tconfig DEFAULT_".$pkg->{name}."\n";
365 print "\t\tbool\n\n";
366 }
367 }
368 my @menus = sort {
369 ($a eq 'undef' ? 1 : 0) or
370 ($b eq 'undef' ? -1 : 0) or
371 ($a cmp $b)
372 } keys %menus;
373
374 foreach my $menu (@menus) {
375 my @pkgs = sort {
376 package_depends($a, $b) or
377 ($a->{name} cmp $b->{name})
378 } @{$menus{$menu}};
379 if ($menu ne 'undef') {
380 $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
381 print "menu \"$menu\"\n";
382 }
383 foreach my $pkg (@pkgs) {
384 my $title = $pkg->{name};
385 my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
386 if ($c > 0) {
387 $title .= ("." x $c). " ". $pkg->{title};
388 }
389 print "\t";
390 $pkg->{menu} and print "menu";
391 print "config PACKAGE_".$pkg->{name}."\n";
392 print "\t\ttristate \"$title\"\n";
393 print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
394 foreach my $default (split /\s*,\s*/, $pkg->{default}) {
395 print "\t\tdefault $default\n";
396 }
397 foreach my $depend (@{$pkg->{depends}}) {
398 my $m = "depends";
399 $depend =~ s/^([@\+]+)//;
400 my $flags = $1;
401 my $vdep;
402
403 if ($vdep = $package{$depend}->{vdepends}) {
404 $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
405 } else {
406 $flags =~ /@/ or $depend = "PACKAGE_$depend";
407 $flags =~ /\+/ and $m = "select";
408 }
409 print "\t\t$m $depend\n";
410 }
411 print "\t\thelp\n";
412 print $pkg->{description};
413 print "\n";
414
415 $pkg->{config} and print $pkg->{config}."\n";
416 }
417 if ($menu ne 'undef') {
418 print "endmenu\n";
419 $menu_dep{$menu} and print "endif\n";
420 }
421 }
422 print "endmenu\n\n";
423
424 undef $category{$cat};
425 }
426
427 sub gen_package_config() {
428 parse_package_metadata();
429 print_package_config_category 'Base system';
430 foreach my $cat (keys %category) {
431 print_package_config_category $cat;
432 }
433 }
434
435 sub parse_command() {
436 my $cmd = shift @ARGV;
437 for ($cmd) {
438 /^target_mk$/ and return gen_target_mk();
439 /^target_config$/ and return gen_target_config();
440 /^package_config$/ and return gen_package_config();
441 }
442 print <<EOF
443 Available Commands:
444 $0 target_mk [file] Target metadata in makefile format
445 $0 target_config [file] Target metadata in Kconfig format
446 $0 package_config [file] Package metadata in Kconfig format
447 EOF
448 }
449
450 parse_command();