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