remove annoying package rm command
[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 /video/ and $ret .= "\tselect VIDEO_SUPPORT\n";
184 /squashfs/ and $ret .= "\tselect USES_SQUASHFS\n";
185 /jffs2/ and $ret .= "\tselect USES_JFFS2\n";
186 /ext2/ and $ret .= "\tselect USES_EXT2\n";
187 }
188 return $ret;
189 }
190
191
192 sub gen_target_config() {
193 my @target = parse_target_metadata();
194
195 @target = sort {
196 $a->{name} cmp $b->{name}
197 } @target;
198
199
200 print <<EOF;
201 choice
202 prompt "Target System"
203 default LINUX_2_4_BRCM
204
205 EOF
206
207 foreach my $target (@target) {
208 my $features = target_config_features(@{$target->{features}});
209 my $help = $target->{desc};
210 my $kernel = $target->{kernel};
211 $kernel =~ tr/./_/;
212
213 chomp $features;
214 $features .= "\n";
215 if ($help =~ /\w+/) {
216 $help =~ s/^\s*/\t /mg;
217 $help = "\thelp\n$help";
218 } else {
219 undef $help;
220 }
221
222 print <<EOF
223 config LINUX_$target->{conf}
224 bool "$target->{name}"
225 select $target->{arch}
226 select LINUX_$kernel
227 $features$help
228
229 EOF
230 }
231
232 print <<EOF;
233 if DEVEL
234
235 config LINUX_2_6_ARM
236 bool "UNSUPPORTED little-endian arm platform"
237 depends BROKEN
238 select LINUX_2_6
239 select arm
240
241 config LINUX_2_6_CRIS
242 bool "UNSUPPORTED cris platform"
243 depends BROKEN
244 select LINUX_2_6
245 select cris
246
247 config LINUX_2_6_M68K
248 bool "UNSUPPORTED m68k platform"
249 depends BROKEN
250 select LINUX_2_6
251 select m68k
252
253 config LINUX_2_6_SH3
254 bool "UNSUPPORTED little-endian sh3 platform"
255 depends BROKEN
256 select LINUX_2_6
257 select sh3
258
259 config LINUX_2_6_SH3EB
260 bool "UNSUPPORTED big-endian sh3 platform"
261 depends BROKEN
262 select LINUX_2_6
263 select sh3eb
264
265 config LINUX_2_6_SH4
266 bool "UNSUPPORTED little-endian sh4 platform"
267 depends BROKEN
268 select LINUX_2_6
269 select sh4
270
271 config LINUX_2_6_SH4EB
272 bool "UNSUPPORTED big-endian sh4 platform"
273 depends BROKEN
274 select LINUX_2_6
275 select sh4eb
276
277 config LINUX_2_6_SPARC
278 bool "UNSUPPORTED sparc platform"
279 depends BROKEN
280 select LINUX_2_6
281 select sparc
282
283 endif
284
285 endchoice
286
287 choice
288 prompt "Target Profile"
289
290 EOF
291
292 foreach my $target (@target) {
293 my $profiles = $target->{profiles};
294
295 foreach my $profile (@$profiles) {
296 print <<EOF;
297 config LINUX_$target->{conf}_$profile->{id}
298 bool "$profile->{name}"
299 depends LINUX_$target->{conf}
300 EOF
301 my %pkgs;
302 foreach my $pkg (@{$target->{packages}}, @{$profile->{packages}}) {
303 $pkgs{$pkg} = 1;
304 }
305 foreach my $pkg (keys %pkgs) {
306 print "\tselect DEFAULT_$pkg\n" unless ($pkg =~ /^-/ or $pkgs{"-$pkg"});
307 }
308 print "\n";
309 }
310 }
311
312 print "endchoice\n";
313 }
314
315 sub find_package_dep($$) {
316 my $pkg = shift;
317 my $name = shift;
318 my $deps = ($pkg->{vdepends} or $pkg->{depends});
319
320 return 0 unless defined $deps;
321 foreach my $dep (@{$deps}) {
322 return 1 if $dep eq $name;
323 return 1 if ($package{$dep} and (find_package_dep($package{$dep},$name) == 1));
324 }
325 return 0;
326 }
327
328 sub package_depends($$) {
329 my $a = shift;
330 my $b = shift;
331 my $ret;
332
333 return 0 if ($a->{submenu} ne $b->{submenu});
334 if (find_package_dep($a, $b->{name}) == 1) {
335 $ret = 1;
336 } elsif (find_package_dep($b, $a->{name}) == 1) {
337 $ret = -1;
338 } else {
339 return 0;
340 }
341 return $ret;
342 }
343
344 sub mconf_depends($$) {
345 my $depends = shift;
346 my $only_dep = shift;
347 my $res;
348
349 $depends or return;
350 my @depends = @$depends;
351 foreach my $depend (@depends) {
352 my $m = "depends";
353 $depend =~ s/^([@\+]+)//;
354 my $flags = $1;
355 my $vdep;
356
357 if ($vdep = $package{$depend}->{vdepends}) {
358 $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
359 } else {
360 $flags =~ /\+/ and do {
361 next if $only_dep;
362 $m = "select";
363
364 # Menuconfig will not treat 'select FOO' as a real dependency
365 # thus if FOO depends on other config options, these dependencies
366 # will not be checked. To fix this, we simply emit all of FOO's
367 # depends here as well.
368 $package{$depend} and $res .= mconf_depends($package{$depend}->{depends}, 1);
369 };
370 $flags =~ /@/ or $depend = "PACKAGE_$depend";
371 }
372 $res .= "\t\t$m $depend\n";
373 }
374 return $res;
375 }
376
377 sub print_package_config_category($) {
378 my $cat = shift;
379 my %menus;
380 my %menu_dep;
381
382 return unless $category{$cat};
383
384 print "menu \"$cat\"\n\n";
385 my %spkg = %{$category{$cat}};
386
387 foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
388 foreach my $pkg (@{$spkg{$spkg}}) {
389 my $menu = $pkg->{submenu};
390 if ($menu) {
391 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
392 } else {
393 $menu = 'undef';
394 }
395 $menus{$menu} or $menus{$menu} = [];
396 push @{$menus{$menu}}, $pkg;
397 print "\tconfig DEFAULT_".$pkg->{name}."\n";
398 print "\t\tbool\n\n";
399 }
400 }
401 my @menus = sort {
402 ($a eq 'undef' ? 1 : 0) or
403 ($b eq 'undef' ? -1 : 0) or
404 ($a cmp $b)
405 } keys %menus;
406
407 foreach my $menu (@menus) {
408 my @pkgs = sort {
409 package_depends($a, $b) or
410 ($a->{name} cmp $b->{name})
411 } @{$menus{$menu}};
412 if ($menu ne 'undef') {
413 $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
414 print "menu \"$menu\"\n";
415 }
416 foreach my $pkg (@pkgs) {
417 my $title = $pkg->{name};
418 my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
419 if ($c > 0) {
420 $title .= ("." x $c). " ". $pkg->{title};
421 }
422 print "\t";
423 $pkg->{menu} and print "menu";
424 print "config PACKAGE_".$pkg->{name}."\n";
425 print "\t\ttristate \"$title\"\n";
426 print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
427 foreach my $default (split /\s*,\s*/, $pkg->{default}) {
428 print "\t\tdefault $default\n";
429 }
430 print mconf_depends($pkg->{depends}, 0);
431 print "\t\thelp\n";
432 print $pkg->{description};
433 print "\n";
434
435 $pkg->{config} and print $pkg->{config}."\n";
436 }
437 if ($menu ne 'undef') {
438 print "endmenu\n";
439 $menu_dep{$menu} and print "endif\n";
440 }
441 }
442 print "endmenu\n\n";
443
444 undef $category{$cat};
445 }
446
447 sub gen_package_config() {
448 parse_package_metadata();
449 print_package_config_category 'Base system';
450 foreach my $cat (keys %category) {
451 print_package_config_category $cat;
452 }
453 }
454
455 sub parse_command() {
456 my $cmd = shift @ARGV;
457 for ($cmd) {
458 /^target_mk$/ and return gen_target_mk();
459 /^target_config$/ and return gen_target_config();
460 /^package_config$/ and return gen_package_config();
461 }
462 print <<EOF
463 Available Commands:
464 $0 target_mk [file] Target metadata in makefile format
465 $0 target_config [file] Target metadata in Kconfig format
466 $0 package_config [file] Package metadata in Kconfig format
467 EOF
468 }
469
470 parse_command();
This page took 0.0959 seconds and 5 git commands to generate.