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