more danube 2 ifxmips transitions
[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 }
163 return $ret;
164 }
165
166 sub target_name($) {
167 my $target = shift;
168 my $parent = $target->{parent};
169 if ($parent) {
170 return $target->{parent}->{name}." - ".$target->{name};
171 } else {
172 return $target->{name};
173 }
174 }
175
176 sub kver($) {
177 my $v = shift;
178 $v =~ tr/\./_/;
179 $v =~ /(\d+_\d+_\d+)(_\d+)?/ and $v = $1;
180 return $v;
181 }
182
183 sub print_target($) {
184 my $target = shift;
185 my $features = target_config_features(@{$target->{features}});
186 my $help = $target->{desc};
187 my $kernel = $target->{kernel};
188 my $confstr;
189 $kernel =~ tr/./_/;
190
191 chomp $features;
192 $features .= "\n";
193 if ($help =~ /\w+/) {
194 $help =~ s/^\s*/\t /mg;
195 $help = "\thelp\n$help";
196 } else {
197 undef $help;
198 }
199
200 my $v = kver($target->{version});
201 $confstr = <<EOF;
202 config TARGET_$target->{conf}
203 bool "$target->{name}"
204 select LINUX_$kernel
205 select LINUX_$v
206 EOF
207 if ($target->{subtarget}) {
208 $confstr .= "\tdepends TARGET_$target->{boardconf}\n";
209 }
210 if (@{$target->{subtargets}} > 0) {
211 $confstr .= "\tselect HAS_SUBTARGETS\n";
212 } else {
213 $confstr .= "\tselect $target->{arch}\n";
214 foreach my $dep (@{$target->{depends}}) {
215 my $mode = "depends";
216 my $flags;
217 my $name;
218
219 $dep =~ /^([@\+\-]+)(.+)$/;
220 $flags = $1;
221 $name = $2;
222
223 $flags =~ /-/ and $mode = "deselect";
224 $flags =~ /\+/ and $mode = "select";
225 $flags =~ /@/ and $confstr .= "\t$mode $name\n";
226 }
227 $confstr .= $features;
228 }
229
230 $confstr .= "$help\n\n";
231 print $confstr;
232 }
233
234 sub gen_target_config() {
235 my @target = parse_target_metadata();
236
237 my @target_sort = sort {
238 target_name($a) cmp target_name($b);
239 } @target;
240
241
242 print <<EOF;
243 choice
244 prompt "Target System"
245 default TARGET_brcm_2_4
246 reset if !DEVEL
247
248 EOF
249
250 foreach my $target (@target_sort) {
251 next if $target->{subtarget};
252 print_target($target);
253 }
254
255 print <<EOF;
256 endchoice
257
258 choice
259 prompt "Subtarget" if HAS_SUBTARGETS
260
261 EOF
262 foreach my $target (@target) {
263 next unless $target->{subtarget};
264 print_target($target);
265 }
266
267 print <<EOF;
268 endchoice
269
270 choice
271 prompt "Target Profile"
272
273 EOF
274
275 foreach my $target (@target) {
276 my $profiles = $target->{profiles};
277
278 foreach my $profile (@$profiles) {
279 print <<EOF;
280 config TARGET_$target->{conf}_$profile->{id}
281 bool "$profile->{name}"
282 depends TARGET_$target->{conf}
283 $profile->{config}
284 EOF
285 $profile->{kconfig} and print "\tselect PROFILE_KCONFIG\n";
286 my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
287 foreach my $pkg (@pkglist) {
288 print "\tselect DEFAULT_$pkg\n";
289 }
290 print "\n";
291 }
292 }
293
294 print <<EOF;
295 endchoice
296
297 config HAS_SUBTARGETS
298 bool
299
300 config TARGET_BOARD
301 string
302
303 EOF
304 foreach my $target (@target) {
305 $target->{subtarget} or print "\t\tdefault \"".$target->{board}."\" if TARGET_".$target->{conf}."\n";
306 }
307
308 my %kver;
309 foreach my $target (@target) {
310 my $v = kver($target->{version});
311 next if $kver{$v};
312 $kver{$v} = 1;
313 print <<EOF;
314 config LINUX_$v
315 bool
316 EOF
317 }
318 }
319
320 my %dep_check;
321 sub __find_package_dep($$) {
322 my $pkg = shift;
323 my $name = shift;
324 my $deps = ($pkg->{vdepends} or $pkg->{depends});
325
326 return 0 unless defined $deps;
327 foreach my $dep (@{$deps}) {
328 next if $dep_check{$dep};
329 $dep_check{$dep} = 1;
330 return 1 if $dep eq $name;
331 return 1 if ($package{$dep} and (__find_package_dep($package{$dep},$name) == 1));
332 }
333 return 0;
334 }
335
336 # wrapper to avoid infinite recursion
337 sub find_package_dep($$) {
338 my $pkg = shift;
339 my $name = shift;
340
341 %dep_check = ();
342 return __find_package_dep($pkg, $name);
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 my $dep = shift;
366 $dep or $dep = {};
367
368 $depends or return;
369 my @depends = @$depends;
370 foreach my $depend (@depends) {
371 my $m = "depends";
372 $depend =~ s/^([@\+]+)//;
373 my $flags = $1;
374 my $vdep;
375
376 if ($vdep = $package{$depend}->{vdepends}) {
377 $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
378 } else {
379 $flags =~ /\+/ and do {
380 next if $only_dep;
381 $m = "select";
382
383 # Menuconfig will not treat 'select FOO' as a real dependency
384 # thus if FOO depends on other config options, these dependencies
385 # will not be checked. To fix this, we simply emit all of FOO's
386 # depends here as well.
387 $package{$depend} and mconf_depends($package{$depend}->{depends}, 1, $dep);
388 };
389 $flags =~ /@/ or $depend = "PACKAGE_$depend";
390 }
391 $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
392 }
393 foreach my $depend (keys %$dep) {
394 my $m = $dep->{$depend};
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\t".($pkg->{tristate} ? 'tristate' : 'bool')." \"$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($ARGV[0]) or exit 1;
472 print "menuconfig UCI_PRECONFIG\n\tbool \"Image configuration\"\n";
473 foreach my $preconfig (keys %preconfig) {
474 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
475 my $conf = $preconfig{$preconfig}->{$cfg}->{id};
476 $conf =~ tr/\.-/__/;
477 print <<EOF
478 config UCI_PRECONFIG_$conf
479 string "$preconfig{$preconfig}->{$cfg}->{label}" if UCI_PRECONFIG
480 depends PACKAGE_$preconfig
481 default "$preconfig{$preconfig}->{$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($ARGV[0]) or exit 1;
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->{subdir}$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 .= " \$(curdir)/$idx/compile";
534 $dep{$pkg->{src}."->".$idx} = 1;
535 }
536 }
537 if ($depline) {
538 $line .= "\$(curdir)/".$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 (keys %{$preconfig{$preconfig}}) {
548 my $conf = $preconfig{$preconfig}->{$cfg}->{id};
549 $conf =~ tr/\.-/__/;
550 $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$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 package/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_config$/ and return gen_target_config();
572 /^package_mk$/ and return gen_package_mk();
573 /^package_config$/ and return gen_package_config();
574 /^kconfig/ and return gen_kconfig_overrides();
575 }
576 print <<EOF
577 Available Commands:
578 $0 target_config [file] Target metadata in Kconfig format
579 $0 package_mk [file] Package metadata in makefile format
580 $0 package_config [file] Package metadata in Kconfig format
581 $0 kconfig [file] [config] Kernel config overrides
582
583 EOF
584 }
585
586 parse_command();
This page took 0.09407 seconds and 5 git commands to generate.