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