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