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