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