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