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