92c989d3a292416e548cd3e306471ecb29f3da5e
[openwrt/staging/florian.git] / scripts / package-metadata.pl
1 #!/usr/bin/env perl
2 use FindBin;
3 use lib "$FindBin::Bin";
4 use strict;
5 use metadata;
6 use Getopt::Long;
7
8 my %board;
9
10 sub version_to_num($) {
11 my $str = shift;
12 my $num = 0;
13
14 if (defined($str) && $str =~ /^\d+(?:\.\d+)+$/)
15 {
16 my @n = (split(/\./, $str), 0, 0, 0, 0);
17 $num = ($n[0] << 24) | ($n[1] << 16) | ($n[2] << 8) | $n[3];
18 }
19
20 return $num;
21 }
22
23 sub version_filter_list(@) {
24 my $cmpver = version_to_num(shift @_);
25 my @items;
26
27 foreach my $item (@_)
28 {
29 if ($item =~ s/@(lt|le|gt|ge|eq|ne)(\d+(?:\.\d+)+)\b//)
30 {
31 my $op = $1;
32 my $symver = version_to_num($2);
33
34 if ($symver > 0 && $cmpver > 0)
35 {
36 next unless (($op eq 'lt' && $cmpver < $symver) ||
37 ($op eq 'le' && $cmpver <= $symver) ||
38 ($op eq 'gt' && $cmpver > $symver) ||
39 ($op eq 'ge' && $cmpver >= $symver) ||
40 ($op eq 'eq' && $cmpver == $symver) ||
41 ($op eq 'ne' && $cmpver != $symver));
42 }
43 }
44
45 push @items, $item;
46 }
47
48 return @items;
49 }
50
51 sub gen_kconfig_overrides() {
52 my %config;
53 my %kconfig;
54 my $package;
55 my $pkginfo = shift @ARGV;
56 my $cfgfile = shift @ARGV;
57 my $patchver = shift @ARGV;
58
59 # parameter 2: build system config
60 open FILE, "<$cfgfile" or return;
61 while (<FILE>) {
62 /^(CONFIG_.+?)=(.+)$/ and $config{$1} = 1;
63 }
64 close FILE;
65
66 # parameter 1: package metadata
67 open FILE, "<$pkginfo" or return;
68 while (<FILE>) {
69 /^Package:\s*(.+?)\s*$/ and $package = $1;
70 /^Kernel-Config:\s*(.+?)\s*$/ and do {
71 my @config = split /\s+/, $1;
72 foreach my $config (version_filter_list($patchver, @config)) {
73 my $val = 'm';
74 my $override;
75 if ($config =~ /^(.+?)=(.+)$/) {
76 $config = $1;
77 $override = 1;
78 $val = $2;
79 }
80 if ($config{"CONFIG_PACKAGE_$package"} and ($config ne 'n')) {
81 next if $kconfig{$config} eq 'y';
82 $kconfig{$config} = $val;
83 } elsif (!$override) {
84 $kconfig{$config} or $kconfig{$config} = 'n';
85 }
86 }
87 };
88 };
89 close FILE;
90
91 foreach my $kconfig (sort keys %kconfig) {
92 if ($kconfig{$kconfig} eq 'n') {
93 print "# $kconfig is not set\n";
94 } else {
95 print "$kconfig=$kconfig{$kconfig}\n";
96 }
97 }
98 }
99
100 my %dep_check;
101 sub __find_package_dep($$) {
102 my $pkg = shift;
103 my $name = shift;
104 my $deps = ($pkg->{vdepends} or $pkg->{depends});
105
106 return 0 unless defined $deps;
107 foreach my $dep (@{$deps}) {
108 next if $dep_check{$dep};
109 $dep_check{$dep} = 1;
110 return 1 if $dep eq $name;
111 return 1 if ($package{$dep} and (__find_package_dep($package{$dep},$name) == 1));
112 }
113 return 0;
114 }
115
116 # wrapper to avoid infinite recursion
117 sub find_package_dep($$) {
118 my $pkg = shift;
119 my $name = shift;
120
121 %dep_check = ();
122 return __find_package_dep($pkg, $name);
123 }
124
125 sub package_depends($$) {
126 my $a = shift;
127 my $b = shift;
128 my $ret;
129
130 return 0 if ($a->{submenu} ne $b->{submenu});
131 if (find_package_dep($a, $b->{name}) == 1) {
132 $ret = 1;
133 } elsif (find_package_dep($b, $a->{name}) == 1) {
134 $ret = -1;
135 } else {
136 return 0;
137 }
138 return $ret;
139 }
140
141 sub mconf_depends {
142 my $pkgname = shift;
143 my $depends = shift;
144 my $only_dep = shift;
145 my $res;
146 my $dep = shift;
147 my $seen = shift;
148 my $parent_condition = shift;
149 $dep or $dep = {};
150 $seen or $seen = {};
151 my @t_depends;
152
153 $depends or return;
154 my @depends = @$depends;
155 foreach my $depend (@depends) {
156 my $m = "depends on";
157 my $flags = "";
158 $depend =~ s/^([@\+]+)// and $flags = $1;
159 my $vdep;
160 my $condition = $parent_condition;
161
162 next if $condition eq $depend;
163 next if $seen->{"$parent_condition:$depend"};
164 next if $seen->{":$depend"};
165 $seen->{"$parent_condition:$depend"} = 1;
166 if ($depend =~ /^(.+):(.+)$/) {
167 if ($1 ne "PACKAGE_$pkgname") {
168 if ($condition) {
169 $condition = "$condition && $1";
170 } else {
171 $condition = $1;
172 }
173 }
174 $depend = $2;
175 }
176 next if $package{$depend} and $package{$depend}->{buildonly};
177 if ($flags =~ /\+/) {
178 if ($vdep = $package{$depend}->{vdepends}) {
179 my @vdeps = @$vdep;
180 $depend = shift @vdeps;
181 if (@vdeps > 1) {
182 $condition = '!('.join("||", map { "PACKAGE_".$_ } @vdeps).')';
183 } elsif (@vdeps > 0) {
184 $condition = '!PACKAGE_'.$vdeps[0];
185 }
186 }
187
188 # Menuconfig will not treat 'select FOO' as a real dependency
189 # thus if FOO depends on other config options, these dependencies
190 # will not be checked. To fix this, we simply emit all of FOO's
191 # depends here as well.
192 $package{$depend} and push @t_depends, [ $package{$depend}->{depends}, $condition ];
193
194 $m = "select";
195 next if $only_dep;
196 } else {
197 if ($vdep = $package{$depend}->{vdepends}) {
198 $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
199 }
200 }
201 $flags =~ /@/ or $depend = "PACKAGE_$depend";
202 if ($condition) {
203 if ($m =~ /select/) {
204 next if $depend eq $condition;
205 $depend = "$depend if $condition";
206 } else {
207 $depend = "!($condition) || $depend" unless $dep->{$condition} eq 'select';
208 }
209 }
210 $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
211 }
212
213 foreach my $tdep (@t_depends) {
214 mconf_depends($pkgname, $tdep->[0], 1, $dep, $seen, $tdep->[1]);
215 }
216
217 foreach my $depend (keys %$dep) {
218 my $m = $dep->{$depend};
219 $res .= "\t\t$m $depend\n";
220 }
221 return $res;
222 }
223
224 sub mconf_conflicts {
225 my $pkgname = shift;
226 my $depends = shift;
227 my $res = "";
228
229 foreach my $depend (@$depends) {
230 next unless $package{$depend};
231 $res .= "\t\tdepends on m || (PACKAGE_$depend != y)\n";
232 }
233 return $res;
234 }
235
236 sub print_package_config_category($) {
237 my $cat = shift;
238 my %menus;
239 my %menu_dep;
240
241 return unless $category{$cat};
242
243 print "menu \"$cat\"\n\n";
244 my %spkg = %{$category{$cat}};
245
246 foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
247 foreach my $pkg (@{$spkg{$spkg}}) {
248 next if $pkg->{buildonly};
249 my $menu = $pkg->{submenu};
250 if ($menu) {
251 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
252 } else {
253 $menu = 'undef';
254 }
255 $menus{$menu} or $menus{$menu} = [];
256 push @{$menus{$menu}}, $pkg;
257 }
258 }
259 my @menus = sort {
260 ($a eq 'undef' ? 1 : 0) or
261 ($b eq 'undef' ? -1 : 0) or
262 ($a cmp $b)
263 } keys %menus;
264
265 foreach my $menu (@menus) {
266 my @pkgs = sort {
267 package_depends($a, $b) or
268 ($a->{name} cmp $b->{name})
269 } @{$menus{$menu}};
270 if ($menu ne 'undef') {
271 $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
272 print "menu \"$menu\"\n";
273 }
274 foreach my $pkg (@pkgs) {
275 my $title = $pkg->{name};
276 my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
277 if ($c > 0) {
278 $title .= ("." x $c). " ". $pkg->{title};
279 }
280 $title = "\"$title\"";
281 print "\t";
282 $pkg->{menu} and print "menu";
283 print "config PACKAGE_".$pkg->{name}."\n";
284 $pkg->{hidden} and $title = "";
285 print "\t\t".($pkg->{tristate} ? 'tristate' : 'bool')." $title\n";
286 print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
287 unless ($pkg->{hidden}) {
288 my @def = ("ALL");
289 if (!exists($pkg->{repository})) {
290 push @def, "ALL_NONSHARED";
291 }
292 if ($pkg->{name} =~ /^kmod-/) {
293 push @def, "ALL_KMODS";
294 }
295 $pkg->{default} ||= "m if " . join("||", @def);
296 }
297 if ($pkg->{default}) {
298 foreach my $default (split /\s*,\s*/, $pkg->{default}) {
299 print "\t\tdefault $default\n";
300 }
301 }
302 print mconf_depends($pkg->{name}, $pkg->{depends}, 0);
303 print mconf_depends($pkg->{name}, $pkg->{mdepends}, 0);
304 print mconf_conflicts($pkg->{name}, $pkg->{conflicts});
305 print "\t\thelp\n";
306 print $pkg->{description};
307 print "\n";
308
309 $pkg->{config} and print $pkg->{config}."\n";
310 }
311 if ($menu ne 'undef') {
312 print "endmenu\n";
313 $menu_dep{$menu} and print "endif\n";
314 }
315 }
316 print "endmenu\n\n";
317
318 undef $category{$cat};
319 }
320
321 sub print_package_features() {
322 keys %features > 0 or return;
323 print "menu \"Package features\"\n";
324 foreach my $n (keys %features) {
325 my @features = sort { $b->{priority} <=> $a->{priority} or $a->{title} cmp $b->{title} } @{$features{$n}};
326 print <<EOF;
327 choice
328 prompt "$features[0]->{target_title}"
329 default FEATURE_$features[0]->{name}
330 EOF
331
332 foreach my $feature (@features) {
333 print <<EOF;
334 config FEATURE_$feature->{name}
335 bool "$feature->{title}"
336 EOF
337 $feature->{description} =~ /\w/ and do {
338 print "\t\thelp\n".$feature->{description}."\n";
339 };
340 }
341 print "endchoice\n"
342 }
343 print "endmenu\n\n";
344 }
345
346 sub print_package_overrides() {
347 keys %overrides > 0 or return;
348 print "\tconfig OVERRIDE_PKGS\n";
349 print "\t\tstring\n";
350 print "\t\tdefault \"".join(" ", keys %overrides)."\"\n\n";
351 }
352
353 sub gen_package_config() {
354 parse_package_metadata($ARGV[0]) or exit 1;
355 print "menuconfig IMAGEOPT\n\tbool \"Image configuration\"\n\tdefault n\n";
356 foreach my $preconfig (keys %preconfig) {
357 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
358 my $conf = $preconfig{$preconfig}->{$cfg}->{id};
359 $conf =~ tr/\.-/__/;
360 print <<EOF
361 config UCI_PRECONFIG_$conf
362 string "$preconfig{$preconfig}->{$cfg}->{label}" if IMAGEOPT
363 depends on PACKAGE_$preconfig
364 default "$preconfig{$preconfig}->{$cfg}->{default}"
365
366 EOF
367 }
368 }
369 print "source \"package/*/image-config.in\"\n";
370 if (scalar glob "package/feeds/*/*/image-config.in") {
371 print "source \"package/feeds/*/*/image-config.in\"\n";
372 }
373 print_package_features();
374 print_package_config_category 'Base system';
375 foreach my $cat (sort {uc($a) cmp uc($b)} keys %category) {
376 print_package_config_category $cat;
377 }
378 print_package_overrides();
379 }
380
381 sub get_conditional_dep($$) {
382 my $condition = shift;
383 my $depstr = shift;
384 if ($condition) {
385 if ($condition =~ /^!(.+)/) {
386 return "\$(if \$(CONFIG_$1),,$depstr)";
387 } else {
388 return "\$(if \$(CONFIG_$condition),$depstr)";
389 }
390 } else {
391 return $depstr;
392 }
393 }
394
395 sub gen_package_mk() {
396 my %conf;
397 my %dep;
398 my %done;
399 my $line;
400
401 parse_package_metadata($ARGV[0]) or exit 1;
402 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
403 my $config;
404 my $pkg = $package{$name};
405 my @srcdeps;
406
407 next if defined $pkg->{vdepends};
408
409 $config = "\$(CONFIG_PACKAGE_$name)";
410 if ($config) {
411 $pkg->{buildonly} and $config = "";
412 print "package-$config += $pkg->{subdir}$pkg->{src}\n";
413 if ($pkg->{variant}) {
414 if (!defined($done{$pkg->{src}}) or $pkg->{variant_default}) {
415 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/default-variant := $pkg->{variant}\n";
416 }
417 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/variants += \$(if $config,$pkg->{variant})\n"
418 }
419 $pkg->{prereq} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
420 }
421
422 next if $done{$pkg->{src}};
423 $done{$pkg->{src}} = 1;
424
425 if (@{$pkg->{buildtypes}} > 0) {
426 print "buildtypes-$pkg->{subdir}$pkg->{src} = ".join(' ', @{$pkg->{buildtypes}})."\n";
427 }
428
429 foreach my $spkg (@{$srcpackage{$pkg->{src}}}) {
430 foreach my $dep (@{$spkg->{depends}}, @{$spkg->{builddepends}}) {
431 $dep =~ /@/ or do {
432 $dep =~ s/\+//g;
433 push @srcdeps, $dep;
434 };
435 }
436 }
437 foreach my $type (@{$pkg->{buildtypes}}) {
438 my @extra_deps;
439 my %deplines;
440
441 next unless $pkg->{"builddepends/$type"};
442 foreach my $dep (@{$pkg->{"builddepends/$type"}}) {
443 my $suffix = "";
444 my $condition;
445
446 if ($dep =~ /^(.+):(.+)/) {
447 $condition = $1;
448 $dep = $2;
449 }
450 if ($dep =~ /^(.+)(\/.+)/) {
451 $dep = $1;
452 $suffix = $2;
453 }
454
455 my $idx = "";
456 my $pkg_dep = $package{$dep};
457 if (defined($pkg_dep) && defined($pkg_dep->{src})) {
458 $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
459 } elsif (defined($srcpackage{$dep})) {
460 $idx = $subdir{$dep}.$dep;
461 } else {
462 next;
463 }
464 my $depstr = "\$(curdir)/$idx$suffix/compile";
465 my $depline = get_conditional_dep($condition, $depstr);
466 if ($depline) {
467 $deplines{$depline}++;
468 }
469 }
470 my $depline = join(" ", sort keys %deplines);
471 if ($depline) {
472 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/$type/compile += $depline\n";
473 }
474 }
475
476 my $hasdeps = 0;
477 my %deplines;
478 foreach my $deps (@srcdeps) {
479 my $idx;
480 my $condition;
481 my $prefix = "";
482 my $suffix = "";
483
484 if ($deps =~ /^(.+):(.+)/) {
485 $condition = $1;
486 $deps = $2;
487 }
488 if ($deps =~ /^(.+)(\/.+)/) {
489 $deps = $1;
490 $suffix = $2;
491 }
492
493 my $pkg_dep = $package{$deps};
494 my @deps;
495
496 if ($pkg_dep->{vdepends}) {
497 @deps = @{$pkg_dep->{vdepends}};
498 } else {
499 @deps = ($deps);
500 }
501
502 foreach my $dep (@deps) {
503 $pkg_dep = $package{$deps};
504 if (defined $pkg_dep->{src}) {
505 ($pkg->{src} ne $pkg_dep->{src}.$suffix) and $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
506 } elsif (defined($srcpackage{$dep})) {
507 $idx = $subdir{$dep}.$dep;
508 }
509 undef $idx if $idx eq 'base-files';
510 if ($idx) {
511 $idx .= $suffix;
512
513 my $depline;
514 next if $pkg->{src} eq $pkg_dep->{src}.$suffix;
515 next if $dep{$condition.":".$pkg->{src}."->".$idx};
516 next if $dep{$pkg->{src}."->($dep)".$idx} and $pkg_dep->{vdepends};
517 my $depstr;
518
519 if ($pkg_dep->{vdepends}) {
520 $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
521 $dep{$pkg->{src}."->($dep)".$idx} = 1;
522 } else {
523 $depstr = "\$(curdir)/$idx/compile";
524 $dep{$pkg->{src}."->".$idx} = 1;
525 }
526 $depline = get_conditional_dep($condition, $depstr);
527 if ($depline) {
528 $deplines{$depline}++;
529 }
530 }
531 }
532 }
533 my $depline = join(" ", sort keys %deplines);
534 if ($depline) {
535 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/compile += $depline\n";
536 }
537 }
538
539 if ($line ne "") {
540 print "\n$line";
541 }
542 foreach my $preconfig (keys %preconfig) {
543 my $cmds;
544 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
545 my $conf = $preconfig{$preconfig}->{$cfg}->{id};
546 $conf =~ tr/\.-/__/;
547 $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
548 }
549 next unless $cmds;
550 print <<EOF
551
552 ifndef DUMP_TARGET_DB
553 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
554 ( \\
555 $cmds \\
556 ) > \$@
557
558 ifneq (\$(IMAGEOPT)\$(CONFIG_IMAGEOPT),)
559 package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
560 endif
561 endif
562
563 EOF
564 }
565 }
566
567 sub gen_package_source() {
568 parse_package_metadata($ARGV[0]) or exit 1;
569 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
570 my $pkg = $package{$name};
571 if ($pkg->{name} && $pkg->{source}) {
572 print "$pkg->{name}: ";
573 print "$pkg->{source}\n";
574 }
575 }
576 }
577
578 sub gen_package_subdirs() {
579 parse_package_metadata($ARGV[0]) or exit 1;
580 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
581 my $pkg = $package{$name};
582 if ($pkg->{name} && $pkg->{repository}) {
583 print "Package/$name/subdir = $pkg->{repository}\n";
584 }
585 }
586 }
587
588 sub gen_package_license($) {
589 my $level = shift;
590 parse_package_metadata($ARGV[0]) or exit 1;
591 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
592 my $pkg = $package{$name};
593 if ($pkg->{name}) {
594 if ($pkg->{license}) {
595 print "$pkg->{name}: ";
596 print "$pkg->{license}\n";
597 if ($pkg->{licensefiles} && $level == 0) {
598 print "\tFiles: $pkg->{licensefiles}\n";
599 }
600 } else {
601 if ($level == 1) {
602 print "$pkg->{name}: Missing license! ";
603 print "Please fix $pkg->{makefile}\n";
604 }
605 }
606 }
607 }
608 }
609
610 sub gen_version_filtered_list() {
611 foreach my $item (version_filter_list(@ARGV)) {
612 print "$item\n";
613 }
614 }
615
616 sub parse_command() {
617 GetOptions("ignore=s", \@ignore);
618 my $cmd = shift @ARGV;
619 for ($cmd) {
620 /^mk$/ and return gen_package_mk();
621 /^config$/ and return gen_package_config();
622 /^kconfig/ and return gen_kconfig_overrides();
623 /^source$/ and return gen_package_source();
624 /^subdirs$/ and return gen_package_subdirs();
625 /^license$/ and return gen_package_license(0);
626 /^licensefull$/ and return gen_package_license(1);
627 /^version_filter$/ and return gen_version_filtered_list();
628 }
629 die <<EOF
630 Available Commands:
631 $0 mk [file] Package metadata in makefile format
632 $0 config [file] Package metadata in Kconfig format
633 $0 kconfig [file] [config] [patchver] Kernel config overrides
634 $0 source [file] Package source file information
635 $0 subdirs [file] Package subdir information in makefile format
636 $0 license [file] Package license information
637 $0 licensefull [file] Package license information (full list)
638 $0 version_filter [patchver] [list...] Filter list of version tagged strings
639
640 Options:
641 --ignore <name> Ignore the source package <name>
642 EOF
643 }
644
645 parse_command();