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