fix duplicate dependencies
[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 my $dep = shift;
305 $dep or $dep = {};
306
307 $depends or return;
308 my @depends = @$depends;
309 foreach my $depend (@depends) {
310 my $m = "depends";
311 $depend =~ s/^([@\+]+)//;
312 my $flags = $1;
313 my $vdep;
314
315 if ($vdep = $package{$depend}->{vdepends}) {
316 $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
317 } else {
318 $flags =~ /\+/ and do {
319 next if $only_dep;
320 $m = "select";
321
322 # Menuconfig will not treat 'select FOO' as a real dependency
323 # thus if FOO depends on other config options, these dependencies
324 # will not be checked. To fix this, we simply emit all of FOO's
325 # depends here as well.
326 $package{$depend} and mconf_depends($package{$depend}->{depends}, 1, $dep);
327 };
328 $flags =~ /@/ or $depend = "PACKAGE_$depend";
329 }
330 $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
331 }
332 foreach my $depend (keys %$dep) {
333 my $m = $dep->{$depend};
334 $res .= "\t\t$m $depend\n";
335 }
336 return $res;
337 }
338
339 sub print_package_config_category($) {
340 my $cat = shift;
341 my %menus;
342 my %menu_dep;
343
344 return unless $category{$cat};
345
346 print "menu \"$cat\"\n\n";
347 my %spkg = %{$category{$cat}};
348
349 foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
350 foreach my $pkg (@{$spkg{$spkg}}) {
351 my $menu = $pkg->{submenu};
352 if ($menu) {
353 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
354 } else {
355 $menu = 'undef';
356 }
357 $menus{$menu} or $menus{$menu} = [];
358 push @{$menus{$menu}}, $pkg;
359 print "\tconfig DEFAULT_".$pkg->{name}."\n";
360 print "\t\tbool\n\n";
361 }
362 }
363 my @menus = sort {
364 ($a eq 'undef' ? 1 : 0) or
365 ($b eq 'undef' ? -1 : 0) or
366 ($a cmp $b)
367 } keys %menus;
368
369 foreach my $menu (@menus) {
370 my @pkgs = sort {
371 package_depends($a, $b) or
372 ($a->{name} cmp $b->{name})
373 } @{$menus{$menu}};
374 if ($menu ne 'undef') {
375 $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
376 print "menu \"$menu\"\n";
377 }
378 foreach my $pkg (@pkgs) {
379 my $title = $pkg->{name};
380 my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
381 if ($c > 0) {
382 $title .= ("." x $c). " ". $pkg->{title};
383 }
384 print "\t";
385 $pkg->{menu} and print "menu";
386 print "config PACKAGE_".$pkg->{name}."\n";
387 print "\t\t".($pkg->{tristate} ? 'tristate' : 'bool')." \"$title\"\n";
388 print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
389 foreach my $default (split /\s*,\s*/, $pkg->{default}) {
390 print "\t\tdefault $default\n";
391 }
392 print mconf_depends($pkg->{depends}, 0);
393 print "\t\thelp\n";
394 print $pkg->{description};
395 print "\n";
396
397 $pkg->{config} and print $pkg->{config}."\n";
398 }
399 if ($menu ne 'undef') {
400 print "endmenu\n";
401 $menu_dep{$menu} and print "endif\n";
402 }
403 }
404 print "endmenu\n\n";
405
406 undef $category{$cat};
407 }
408
409 sub gen_package_config() {
410 parse_package_metadata($ARGV[0]) or exit 1;
411 print "menuconfig UCI_PRECONFIG\n\tbool \"Image configuration\"\n";
412 foreach my $preconfig (keys %preconfig) {
413 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
414 my $conf = $preconfig{$preconfig}->{$cfg}->{id};
415 $conf =~ tr/\.-/__/;
416 print <<EOF
417 config UCI_PRECONFIG_$conf
418 string "$preconfig{$preconfig}->{$cfg}->{label}" if UCI_PRECONFIG
419 depends PACKAGE_$preconfig
420 default "$preconfig{$preconfig}->{$cfg}->{default}"
421
422 EOF
423 }
424 }
425 print_package_config_category 'Base system';
426 foreach my $cat (keys %category) {
427 print_package_config_category $cat;
428 }
429 }
430
431 sub gen_package_mk() {
432 my %conf;
433 my %dep;
434 my $line;
435
436 parse_package_metadata($ARGV[0]) or exit 1;
437 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
438 my $config;
439 my $pkg = $package{$name};
440
441 next if defined $pkg->{vdepends};
442 if ($ENV{SDK}) {
443 $conf{$pkg->{src}} or do {
444 $config = 'm';
445 $conf{$pkg->{src}} = 1;
446 };
447 } else {
448 $config = "\$(CONFIG_PACKAGE_$name)"
449 }
450 if ($config) {
451 print "package-$config += $pkg->{subdir}$pkg->{src}\n";
452 $pkg->{prereq} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
453 }
454
455 my $hasdeps = 0;
456 my $depline = "";
457 foreach my $dep (@{$pkg->{depends}}, @{$pkg->{builddepends}}) {
458 next if $dep =~ /@/;
459 $dep =~ s/\+//;
460 my $idx;
461 my $pkg_dep = $package{$dep};
462 next if defined $pkg_dep->{vdepends};
463
464 if (defined $pkg_dep->{src}) {
465 ($pkg->{src} ne $pkg_dep->{src}) and $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
466 } elsif (defined($srcpackage{$dep})) {
467 $idx = $subdir{$dep}.$dep;
468 }
469 undef $idx if $idx =~ /^(kernel)|(base-files)$/;
470 if ($idx) {
471 next if $dep{$pkg->{src}."->".$idx};
472 $depline .= " \$(curdir)/$idx/compile";
473 $dep{$pkg->{src}."->".$idx} = 1;
474 }
475 }
476 if ($depline) {
477 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/compile += $depline\n";
478 }
479 }
480
481 if ($line ne "") {
482 print "\n$line";
483 }
484 foreach my $preconfig (keys %preconfig) {
485 my $cmds;
486 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
487 my $conf = $preconfig{$preconfig}->{$cfg}->{id};
488 $conf =~ tr/\.-/__/;
489 $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
490 }
491 next unless $cmds;
492 print <<EOF
493
494 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
495 ( \\
496 $cmds \\
497 ) > \$@
498
499 ifneq (\$(UCI_PRECONFIG)\$(CONFIG_UCI_PRECONFIG),)
500 package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
501 endif
502 EOF
503 }
504 }
505
506
507 sub parse_command() {
508 my $cmd = shift @ARGV;
509 for ($cmd) {
510 /^target_config$/ and return gen_target_config();
511 /^package_mk$/ and return gen_package_mk();
512 /^package_config$/ and return gen_package_config();
513 /^kconfig/ and return gen_kconfig_overrides();
514 }
515 print <<EOF
516 Available Commands:
517 $0 target_config [file] Target metadata in Kconfig format
518 $0 package_mk [file] Package metadata in makefile format
519 $0 package_config [file] Package metadata in Kconfig format
520 $0 kconfig [file] [config] Kernel config overrides
521
522 EOF
523 }
524
525 parse_command();