fix typos
[openwrt.git] / scripts / metadata.pl
1 #!/usr/bin/perl
2 use strict;
3 my %package;
4 my %category;
5
6 sub parse_target_metadata() {
7 my ($target, @target, $profile);
8 while (<>) {
9 chomp;
10 /^Target:\s*((.+)-(\d+\.\d+))\s*$/ and do {
11 my $conf = uc $3.'_'.$2;
12 $conf =~ tr/\.-/__/;
13 $target = {
14 id => $1,
15 conf => $conf,
16 board => $2,
17 kernel => $3,
18 profiles => []
19 };
20 push @target, $target;
21 };
22 /^Target-Name:\s*(.+)\s*$/ and $target->{name} = $1;
23 /^Target-Path:\s*(.+)\s*$/ and $target->{path} = $1;
24 /^Target-Arch:\s*(.+)\s*$/ and $target->{arch} = $1;
25 /^Target-Features:\s*(.+)\s*$/ and $target->{features} = [ split(/\s+/, $1) ];
26 /^Target-Description:/ and do {
27 my $desc;
28 while (<>) {
29 last if /^@@/;
30 $desc .= $_;
31 }
32 $target->{desc} = $desc;
33 };
34 /^Linux-Version:\s*(.+)\s*$/ and $target->{version} = $1;
35 /^Linux-Release:\s*(.+)\s*$/ and $target->{release} = $1;
36 /^Linux-Kernel-Arch:\s*(.+)\s*$/ and $target->{karch} = $1;
37 /^Default-Packages:\s*(.+)\s*$/ and $target->{packages} = [ split(/\s+/, $1) ];
38 /^Target-Profile:\s*(.+)\s*$/ and do {
39 $profile = {
40 id => $1,
41 name => $1,
42 packages => []
43 };
44 push @{$target->{profiles}}, $profile;
45 };
46 /^Target-Profile-Name:\s*(.+)\s*$/ and $profile->{name} = $1;
47 /^Target-Profile-Packages:\s*(.*)\s*$/ and $profile->{packages} = [ split(/\s+/, $1) ];
48 /^Target-Profile-Description:/ and do {
49 my $desc;
50 while (<>) {
51 last if /^@@/;
52 $desc .= $_;
53 }
54 $profile->{desc} = $desc;
55 };
56 }
57 foreach my $target (@target) {
58 @{$target->{profiles}} > 0 or $target->{profiles} = [
59 {
60 id => 'Default',
61 name => 'Default',
62 packages => []
63 }
64 ];
65 }
66 return @target;
67 }
68
69 sub parse_package_metadata() {
70 my $pkg;
71 my $makefile;
72 my $src;
73 while (<>) {
74 chomp;
75 /^Source-Makefile: \s*(.+\/([^\/]+)\/Makefile)\s*$/ and do {
76 $makefile = $1;
77 $src = $2;
78 undef $pkg;
79 };
80 /^Package: \s*(.+)\s*$/ and do {
81 $pkg = {};
82 $pkg->{src} = $src;
83 $pkg->{makefile} = $makefile;
84 $pkg->{name} = $1;
85 $pkg->{default} = "m if ALL";
86 $package{$1} = $pkg;
87 };
88 /^Version: \s*(.+)\s*$/ and $pkg->{version} = $1;
89 /^Title: \s*(.+)\s*$/ and $pkg->{title} = $1;
90 /^Menu: \s*(.+)\s*$/ and $pkg->{menu} = $1;
91 /^Submenu: \s*(.+)\s*$/ and $pkg->{submenu} = $1;
92 /^Submenu-Depends: \s*(.+)\s*$/ and $pkg->{submenudep} = $1;
93 /^Default: \s*(.+)\s*$/ and $pkg->{default} = $1;
94 /^Provides: \s*(.+)\s*$/ and do {
95 my @vpkg = split /\s+/, $1;
96 foreach my $vpkg (@vpkg) {
97 $package{$vpkg} or $package{$vpkg} = { vdepends => [] };
98 push @{$package{$vpkg}->{vdepends}}, $pkg->{name};
99 }
100 };
101 /^Depends: \s*(.+)\s*$/ and do {
102 my @dep = split /\s+/, $1;
103 $pkg->{depends} = \@dep;
104 };
105 /^Category: \s*(.+)\s*$/ and do {
106 $pkg->{category} = $1;
107 defined $category{$1} or $category{$1} = {};
108 defined $category{$1}->{$src} or $category{$1}->{$src} = [];
109 push @{$category{$1}->{$src}}, $pkg;
110 };
111 /^Description: \s*(.*)\s*$/ and do {
112 my $desc = "\t\t$1\n\n";
113 my $line;
114 while ($line = <>) {
115 last if $line =~ /^@@/;
116 $desc .= "\t\t$line";
117 }
118 $pkg->{description} = $desc;
119 };
120 /^Config: \s*(.*)\s*$/ and do {
121 my $conf = "$1\n";
122 my $line;
123 while ($line = <>) {
124 last if $line =~ /^@@/;
125 $conf .= "$line";
126 }
127 $pkg->{config} = $conf;
128 }
129 }
130 return %category;
131 }
132
133
134 sub gen_target_mk() {
135 my @target = parse_target_metadata();
136
137 @target = sort {
138 $a->{id} cmp $b->{id}
139 } @target;
140
141 foreach my $target (@target) {
142 my ($profiles_def, $profiles_eval);
143 my $conf = uc $target->{kernel}.'_'.$target->{board};
144 $conf =~ tr/\.-/__/;
145
146 foreach my $profile (@{$target->{profiles}}) {
147 $profiles_def .= "
148 define Profile/$conf\_$profile->{id}
149 ID:=$profile->{id}
150 NAME:=$profile->{name}
151 PACKAGES:=".join(" ", @{$profile->{packages}})."
152 endef";
153 $profiles_eval .= "
154 \$(eval \$(call Profile,$conf\_$profile->{id}))"
155 }
156 print "
157 ifeq (\$(CONFIG_LINUX_$conf),y)
158 define Target
159 KERNEL:=$target->{kernel}
160 BOARD:=$target->{board}
161 BOARDNAME:=$target->{name}
162 LINUX_VERSION:=$target->{version}
163 LINUX_RELEASE:=$target->{release}
164 LINUX_KARCH:=$target->{karch}
165 DEFAULT_PACKAGES:=".join(" ", @{$target->{packages}})."
166 endef$profiles_def
167 endif$profiles_eval
168
169 "
170 }
171 print "\$(eval \$(call Target))\n";
172 }
173
174 sub target_config_features(@) {
175 my $ret;
176
177 while ($_ = shift @_) {
178 /broken/ and $ret .= "\tdepends BROKEN\n";
179 /pci/ and $ret .= "\tselect PCI_SUPPORT\n";
180 /usb/ and $ret .= "\tselect USB_SUPPORT\n";
181 /atm/ and $ret .= "\tselect ATM_SUPPORT\n";
182 /pcmcia/ and $ret .= "\tselect PCMCIA_SUPPORT\n";
183 /squashfs/ and $ret .= "\tselect USES_SQUASHFS\n";
184 /jffs2/ and $ret .= "\tselect USES_JFFS2\n";
185 /ext2/ and $ret .= "\tselect USES_EXT2\n";
186 }
187 return $ret;
188 }
189
190
191 sub gen_target_config() {
192 my @target = parse_target_metadata();
193
194 @target = sort {
195 $a->{name} cmp $b->{name}
196 } @target;
197
198
199 print <<EOF;
200 choice
201 prompt "Target System"
202 default LINUX_2_4_BRCM
203
204 EOF
205
206 foreach my $target (@target) {
207 my $features = target_config_features(@{$target->{features}});
208 my $help = $target->{desc};
209 my $kernel = $target->{kernel};
210 $kernel =~ tr/./_/;
211
212 chomp $features;
213 $features .= "\n";
214 if ($help =~ /\w+/) {
215 $help =~ s/^\s*/\t /mg;
216 $help = "\thelp\n$help";
217 } else {
218 undef $help;
219 }
220
221 print <<EOF
222 config LINUX_$target->{conf}
223 bool "$target->{name}"
224 select $target->{arch}
225 select LINUX_$kernel
226 $features$help
227
228 EOF
229 }
230
231 print <<EOF;
232 if DEVEL
233
234 config LINUX_2_6_ARM
235 bool "UNSUPPORTED little-endian arm platform"
236 depends BROKEN
237 select LINUX_2_6
238 select arm
239
240 config LINUX_2_6_CRIS
241 bool "UNSUPPORTED cris platform"
242 depends BROKEN
243 select LINUX_2_6
244 select cris
245
246 config LINUX_2_6_M68K
247 bool "UNSUPPORTED m68k platform"
248 depends BROKEN
249 select LINUX_2_6
250 select m68k
251
252 config LINUX_2_6_SH3
253 bool "UNSUPPORTED little-endian sh3 platform"
254 depends BROKEN
255 select LINUX_2_6
256 select sh3
257
258 config LINUX_2_6_SH3EB
259 bool "UNSUPPORTED big-endian sh3 platform"
260 depends BROKEN
261 select LINUX_2_6
262 select sh3eb
263
264 config LINUX_2_6_SH4
265 bool "UNSUPPORTED little-endian sh4 platform"
266 depends BROKEN
267 select LINUX_2_6
268 select sh4
269
270 config LINUX_2_6_SH4EB
271 bool "UNSUPPORTED big-endian sh4 platform"
272 depends BROKEN
273 select LINUX_2_6
274 select sh4eb
275
276 config LINUX_2_6_SPARC
277 bool "UNSUPPORTED sparc platform"
278 depends BROKEN
279 select LINUX_2_6
280 select sparc
281
282 endif
283
284 endchoice
285
286 choice
287 prompt "Target Profile"
288
289 EOF
290
291 foreach my $target (@target) {
292 my $profiles = $target->{profiles};
293
294 foreach my $profile (@$profiles) {
295 print <<EOF;
296 config LINUX_$target->{conf}_$profile->{id}
297 bool "$profile->{name}"
298 depends LINUX_$target->{conf}
299 EOF
300 my %pkgs;
301 foreach my $pkg (@{$target->{packages}}, @{$profile->{packages}}) {
302 $pkgs{$pkg} = 1;
303 }
304 foreach my $pkg (keys %pkgs) {
305 print "\tselect DEFAULT_$pkg\n" unless ($pkg =~ /^-/ or $pkgs{"-$pkg"});
306 }
307 print "\n";
308 }
309 }
310
311 print "endchoice\n";
312 }
313
314 sub find_package_dep($$) {
315 my $pkg = shift;
316 my $name = shift;
317 my $deps = ($pkg->{vdepends} or $pkg->{depends});
318
319 return 0 unless defined $deps;
320 foreach my $dep (@{$deps}) {
321 return 1 if $dep eq $name;
322 return 1 if ($package{$dep} and (find_package_dep($package{$dep},$name) == 1));
323 }
324 return 0;
325 }
326
327 sub package_depends($$) {
328 my $a = shift;
329 my $b = shift;
330 my $ret;
331
332 return 0 if ($a->{submenu} ne $b->{submenu});
333 if (find_package_dep($a, $b->{name}) == 1) {
334 $ret = 1;
335 } elsif (find_package_dep($b, $a->{name}) == 1) {
336 $ret = -1;
337 } else {
338 return 0;
339 }
340 return $ret;
341 }
342
343 sub print_package_config_category($) {
344 my $cat = shift;
345 my %menus;
346 my %menu_dep;
347
348 return unless $category{$cat};
349
350 print "menu \"$cat\"\n\n";
351 my %spkg = %{$category{$cat}};
352
353 foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
354 foreach my $pkg (@{$spkg{$spkg}}) {
355 my $menu = $pkg->{submenu};
356 if ($menu) {
357 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
358 } else {
359 $menu = 'undef';
360 }
361 $menus{$menu} or $menus{$menu} = [];
362 push @{$menus{$menu}}, $pkg;
363 print "\tconfig DEFAULT_".$pkg->{name}."\n";
364 print "\t\tbool\n\n";
365 }
366 }
367 my @menus = sort {
368 ($a eq 'undef' ? 1 : 0) or
369 ($b eq 'undef' ? -1 : 0) or
370 ($a cmp $b)
371 } keys %menus;
372
373 foreach my $menu (@menus) {
374 my @pkgs = sort {
375 package_depends($a, $b) or
376 ($a->{name} cmp $b->{name})
377 } @{$menus{$menu}};
378 if ($menu ne 'undef') {
379 $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
380 print "menu \"$menu\"\n";
381 }
382 foreach my $pkg (@pkgs) {
383 my $title = $pkg->{name};
384 my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
385 if ($c > 0) {
386 $title .= ("." x $c). " ". $pkg->{title};
387 }
388 print "\t";
389 $pkg->{menu} and print "menu";
390 print "config PACKAGE_".$pkg->{name}."\n";
391 print "\t\ttristate \"$title\"\n";
392 print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
393 foreach my $default (split /\s*,\s*/, $pkg->{default}) {
394 print "\t\tdefault $default\n";
395 }
396 foreach my $depend (@{$pkg->{depends}}) {
397 my $m = "depends";
398 $depend =~ s/^([@\+]+)//;
399 my $flags = $1;
400 my $vdep;
401
402 if ($vdep = $package{$depend}->{vdepends}) {
403 $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
404 } else {
405 $flags =~ /@/ or $depend = "PACKAGE_$depend";
406 $flags =~ /\+/ and $m = "select";
407 }
408 print "\t\t$m $depend\n";
409 }
410 print "\t\thelp\n";
411 print $pkg->{description};
412 print "\n";
413
414 $pkg->{config} and print $pkg->{config}."\n";
415 }
416 if ($menu ne 'undef') {
417 print "endmenu\n";
418 $menu_dep{$menu} and print "endif\n";
419 }
420 }
421 print "endmenu\n\n";
422
423 undef $category{$cat};
424 }
425
426 sub gen_package_config() {
427 parse_package_metadata();
428 print_package_config_category 'Base system';
429 foreach my $cat (keys %category) {
430 print_package_config_category $cat;
431 }
432 }
433
434 sub parse_command() {
435 my $cmd = shift @ARGV;
436 for ($cmd) {
437 /^target_mk$/ and return gen_target_mk();
438 /^target_config$/ and return gen_target_config();
439 /^package_config$/ and return gen_package_config();
440 }
441 print <<EOF
442 Available Commands:
443 $0 target_mk [file] Target metadata in makefile format
444 $0 target_config [file] Target metadata in Kconfig format
445 $0 package_config [file] Package metadata in Kconfig format
446 EOF
447 }
448
449 parse_command();
This page took 0.089998 seconds and 5 git commands to generate.