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