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