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