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