ar7: drop unused 4.1 support
[openwrt/openwrt.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;
180 $depend = undef;
181
182 foreach my $v (@$vdep) {
183 if ($package{$v} && $package{$v}->{variant_default}) {
184 $depend = $v;
185 } else {
186 push @vdeps, $v;
187 }
188 }
189
190 if (!$depend) {
191 $depend = shift @vdeps;
192 }
193
194 if (@vdeps > 1) {
195 $condition = ($condition ? "$condition && " : '') . '!('.join("||", map { "PACKAGE_".$_ } @vdeps).')';
196 } elsif (@vdeps > 0) {
197 $condition = ($condition ? "$condition && " : '') . '!PACKAGE_'.$vdeps[0];
198 }
199 }
200
201 # Menuconfig will not treat 'select FOO' as a real dependency
202 # thus if FOO depends on other config options, these dependencies
203 # will not be checked. To fix this, we simply emit all of FOO's
204 # depends here as well.
205 $package{$depend} and push @t_depends, [ $package{$depend}->{depends}, $condition ];
206
207 $m = "select";
208 next if $only_dep;
209
210 $flags =~ /@/ or $depend = "PACKAGE_$depend";
211 } else {
212 if ($vdep = $package{$depend}->{vdepends}) {
213 $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
214 } else {
215 $flags =~ /@/ or $depend = "PACKAGE_$depend";
216 }
217 }
218
219 if ($condition) {
220 if ($m =~ /select/) {
221 next if $depend eq $condition;
222 $depend = "$depend if $condition";
223 } else {
224 next if $dep->{"$depend if $condition"};
225 $depend = "!($condition) || $depend" unless $dep->{$condition} eq 'select';
226 }
227 }
228 $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
229 }
230
231 foreach my $tdep (@t_depends) {
232 mconf_depends($pkgname, $tdep->[0], 1, $dep, $seen, $tdep->[1]);
233 }
234
235 foreach my $depend (keys %$dep) {
236 my $m = $dep->{$depend};
237 $res .= "\t\t$m $depend\n";
238 }
239 return $res;
240 }
241
242 sub mconf_conflicts {
243 my $pkgname = shift;
244 my $depends = shift;
245 my $res = "";
246
247 foreach my $depend (@$depends) {
248 next unless $package{$depend};
249 $res .= "\t\tdepends on m || (PACKAGE_$depend != y)\n";
250 }
251 return $res;
252 }
253
254 sub print_package_config_category($) {
255 my $cat = shift;
256 my %menus;
257 my %menu_dep;
258
259 return unless $category{$cat};
260
261 print "menu \"$cat\"\n\n";
262 my %spkg = %{$category{$cat}};
263
264 foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
265 foreach my $pkg (@{$spkg{$spkg}}) {
266 next if $pkg->{buildonly};
267 my $menu = $pkg->{submenu};
268 if ($menu) {
269 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
270 } else {
271 $menu = 'undef';
272 }
273 $menus{$menu} or $menus{$menu} = [];
274 push @{$menus{$menu}}, $pkg;
275 }
276 }
277 my @menus = sort {
278 ($a eq 'undef' ? 1 : 0) or
279 ($b eq 'undef' ? -1 : 0) or
280 ($a cmp $b)
281 } keys %menus;
282
283 foreach my $menu (@menus) {
284 my @pkgs = sort {
285 package_depends($a, $b) or
286 ($a->{name} cmp $b->{name})
287 } @{$menus{$menu}};
288 if ($menu ne 'undef') {
289 $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
290 print "menu \"$menu\"\n";
291 }
292 foreach my $pkg (@pkgs) {
293 next if $pkg->{ignore};
294 my $title = $pkg->{name};
295 my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
296 if ($c > 0) {
297 $title .= ("." x $c). " ". $pkg->{title};
298 }
299 $title = "\"$title\"";
300 print "\t";
301 $pkg->{menu} and print "menu";
302 print "config PACKAGE_".$pkg->{name}."\n";
303 $pkg->{hidden} and $title = "";
304 print "\t\t".($pkg->{tristate} ? 'tristate' : 'bool')." $title\n";
305 print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
306 unless ($pkg->{hidden}) {
307 my @def = ("ALL");
308 if (!exists($pkg->{repository})) {
309 push @def, "ALL_NONSHARED";
310 }
311 if ($pkg->{name} =~ /^kmod-/) {
312 push @def, "ALL_KMODS";
313 }
314 $pkg->{default} ||= "m if " . join("||", @def);
315 }
316 if ($pkg->{default}) {
317 foreach my $default (split /\s*,\s*/, $pkg->{default}) {
318 print "\t\tdefault $default\n";
319 }
320 }
321 print mconf_depends($pkg->{name}, $pkg->{depends}, 0);
322 print mconf_depends($pkg->{name}, $pkg->{mdepends}, 0);
323 print mconf_conflicts($pkg->{name}, $pkg->{conflicts});
324 print "\t\thelp\n";
325 print $pkg->{description};
326 print "\n";
327
328 $pkg->{config} and print $pkg->{config}."\n";
329 }
330 if ($menu ne 'undef') {
331 print "endmenu\n";
332 $menu_dep{$menu} and print "endif\n";
333 }
334 }
335 print "endmenu\n\n";
336
337 undef $category{$cat};
338 }
339
340 sub print_package_features() {
341 keys %features > 0 or return;
342 print "menu \"Package features\"\n";
343 foreach my $n (keys %features) {
344 my @features = sort { $b->{priority} <=> $a->{priority} or $a->{title} cmp $b->{title} } @{$features{$n}};
345 print <<EOF;
346 choice
347 prompt "$features[0]->{target_title}"
348 default FEATURE_$features[0]->{name}
349 EOF
350
351 foreach my $feature (@features) {
352 print <<EOF;
353 config FEATURE_$feature->{name}
354 bool "$feature->{title}"
355 EOF
356 $feature->{description} =~ /\w/ and do {
357 print "\t\thelp\n".$feature->{description}."\n";
358 };
359 }
360 print "endchoice\n"
361 }
362 print "endmenu\n\n";
363 }
364
365 sub print_package_overrides() {
366 keys %overrides > 0 or return;
367 print "\tconfig OVERRIDE_PKGS\n";
368 print "\t\tstring\n";
369 print "\t\tdefault \"".join(" ", sort keys %overrides)."\"\n\n";
370 }
371
372 sub gen_package_config() {
373 parse_package_metadata($ARGV[0]) or exit 1;
374 print "menuconfig IMAGEOPT\n\tbool \"Image configuration\"\n\tdefault n\n";
375 foreach my $preconfig (keys %preconfig) {
376 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
377 my $conf = $preconfig{$preconfig}->{$cfg}->{id};
378 $conf =~ tr/\.-/__/;
379 print <<EOF
380 config UCI_PRECONFIG_$conf
381 string "$preconfig{$preconfig}->{$cfg}->{label}" if IMAGEOPT
382 depends on PACKAGE_$preconfig
383 default "$preconfig{$preconfig}->{$cfg}->{default}"
384
385 EOF
386 }
387 }
388 print "source \"package/*/image-config.in\"\n";
389 if (scalar glob "package/feeds/*/*/image-config.in") {
390 print "source \"package/feeds/*/*/image-config.in\"\n";
391 }
392 print_package_features();
393 print_package_config_category 'Base system';
394 foreach my $cat (sort {uc($a) cmp uc($b)} keys %category) {
395 print_package_config_category $cat;
396 }
397 print_package_overrides();
398 }
399
400 sub get_conditional_dep($$) {
401 my $condition = shift;
402 my $depstr = shift;
403 if ($condition) {
404 if ($condition =~ /^!(.+)/) {
405 return "\$(if \$(CONFIG_$1),,$depstr)";
406 } else {
407 return "\$(if \$(CONFIG_$condition),$depstr)";
408 }
409 } else {
410 return $depstr;
411 }
412 }
413
414 sub gen_package_mk() {
415 my %conf;
416 my %dep;
417 my %done;
418 my $line;
419
420 parse_package_metadata($ARGV[0]) or exit 1;
421 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
422 my $config;
423 my $pkg = $package{$name};
424 my @srcdeps;
425
426 next if defined $pkg->{vdepends};
427
428 $config = "\$(CONFIG_PACKAGE_$name)";
429 if ($config) {
430 $pkg->{buildonly} and $config = "";
431 print "package-$config += $pkg->{subdir}$pkg->{src}\n";
432 if ($pkg->{variant}) {
433 if (!defined($done{$pkg->{src}}) or $pkg->{variant_default}) {
434 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/default-variant := $pkg->{variant}\n";
435 }
436 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/variants += \$(if $config,$pkg->{variant})\n"
437 }
438 $pkg->{prereq} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
439 }
440
441 next if $done{$pkg->{src}};
442 $done{$pkg->{src}} = 1;
443
444 if (@{$pkg->{buildtypes}} > 0) {
445 print "buildtypes-$pkg->{subdir}$pkg->{src} = ".join(' ', @{$pkg->{buildtypes}})."\n";
446 }
447
448 foreach my $spkg (@{$srcpackage{$pkg->{src}}}) {
449 foreach my $dep (@{$spkg->{depends}}, @{$spkg->{builddepends}}) {
450 $dep =~ /@/ or do {
451 $dep =~ s/\+//g;
452 push @srcdeps, $dep;
453 };
454 }
455 }
456 foreach my $type (@{$pkg->{buildtypes}}) {
457 my @extra_deps;
458 my %deplines;
459
460 next unless $pkg->{"builddepends/$type"};
461 foreach my $dep (@{$pkg->{"builddepends/$type"}}) {
462 my $suffix = "";
463 my $deptype = "";
464 my $condition;
465
466 if ($dep =~ /^(.+):(.+)/) {
467 $condition = $1;
468 $dep = $2;
469 }
470 if ($dep =~ /^(.+)\/(.+)/) {
471 $dep = $1;
472 $deptype = $2;
473 $suffix = "/$2";
474 }
475
476 my $idx = "";
477 my $pkg_dep = $package{$dep};
478 if (defined($pkg_dep) && defined($pkg_dep->{src})) {
479 unless (!$deptype || grep { $_ eq $deptype } @{$pkg_dep->{buildtypes}}) {
480 warn sprintf "WARNING: Makefile '%s' has a %s build dependency on '%s/%s' but '%s' does not implement a '%s' build type\n",
481 $pkg->{makefile}, $type, $pkg_dep->{src}, $deptype, $pkg_dep->{makefile}, $deptype;
482 next;
483 }
484 $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
485 } elsif (defined($srcpackage{$dep})) {
486 $idx = $subdir{$dep}.$dep;
487 } else {
488 next;
489 }
490 my $depstr = "\$(curdir)/$idx$suffix/compile";
491 my $depline = get_conditional_dep($condition, $depstr);
492 if ($depline) {
493 $deplines{$depline}++;
494 }
495 }
496 my $depline = join(" ", sort keys %deplines);
497 if ($depline) {
498 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/$type/compile += $depline\n";
499 }
500 }
501
502 my $hasdeps = 0;
503 my %deplines;
504 foreach my $deps (@srcdeps) {
505 my $idx;
506 my $condition;
507 my $prefix = "";
508 my $suffix = "";
509 my $deptype = "";
510
511 if ($deps =~ /^(.+):(.+)/) {
512 $condition = $1;
513 $deps = $2;
514 }
515 if ($deps =~ /^(.+)\/(.+)/) {
516 $deps = $1;
517 $deptype = $2;
518 $suffix = "/$2";
519 }
520
521 my $pkg_dep = $package{$deps};
522 my @deps;
523
524 if ($pkg_dep->{vdepends}) {
525 @deps = @{$pkg_dep->{vdepends}};
526 } else {
527 @deps = ($deps);
528 }
529
530 foreach my $dep (@deps) {
531 $pkg_dep = $package{$deps};
532 if (defined $pkg_dep->{src}) {
533 unless (!$deptype || grep { $_ eq $deptype } @{$pkg_dep->{buildtypes}}) {
534 warn sprintf "WARNING: Makefile '%s' has a build dependency on '%s/%s' but '%s' does not implement a '%s' build type\n",
535 $pkg->{makefile}, $pkg_dep->{src}, $deptype, $pkg_dep->{makefile}, $deptype;
536 next;
537 }
538 unless ($pkg->{src} ne $pkg_dep->{sec}.$suffix) {
539 warn sprintf "WARNING: Makefile '%s' has a build dependency on itself\n",
540 $pkg->{makefile};
541 next;
542 }
543 $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
544 } elsif (defined($srcpackage{$dep})) {
545 $idx = $subdir{$dep}.$dep;
546 }
547 undef $idx if $idx eq 'base-files';
548 if ($idx) {
549 $idx .= $suffix;
550
551 my $depline;
552 next if $pkg->{src} eq $pkg_dep->{src}.$suffix;
553 next if $dep{$condition.":".$pkg->{src}."->".$idx};
554 next if $dep{$pkg->{src}."->($dep)".$idx} and $pkg_dep->{vdepends};
555 my $depstr;
556
557 if ($pkg_dep->{vdepends}) {
558 $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
559 $dep{$pkg->{src}."->($dep)".$idx} = 1;
560 } else {
561 $depstr = "\$(curdir)/$idx/compile";
562 $dep{$pkg->{src}."->".$idx} = 1;
563 }
564 $depline = get_conditional_dep($condition, $depstr);
565 if ($depline) {
566 $deplines{$depline}++;
567 }
568 }
569 }
570 }
571 my $depline = join(" ", sort keys %deplines);
572 if ($depline) {
573 $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/compile += $depline\n";
574 }
575 }
576
577 if ($line ne "") {
578 print "\n$line";
579 }
580 foreach my $preconfig (keys %preconfig) {
581 my $cmds;
582 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
583 my $conf = $preconfig{$preconfig}->{$cfg}->{id};
584 $conf =~ tr/\.-/__/;
585 $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
586 }
587 next unless $cmds;
588 print <<EOF
589
590 ifndef DUMP_TARGET_DB
591 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
592 ( \\
593 $cmds \\
594 ) > \$@
595
596 ifneq (\$(IMAGEOPT)\$(CONFIG_IMAGEOPT),)
597 package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
598 endif
599 endif
600
601 EOF
602 }
603 }
604
605 sub gen_package_source() {
606 parse_package_metadata($ARGV[0]) or exit 1;
607 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
608 my $pkg = $package{$name};
609 if ($pkg->{name} && $pkg->{source}) {
610 print "$pkg->{name}: ";
611 print "$pkg->{source}\n";
612 }
613 }
614 }
615
616 sub gen_package_subdirs() {
617 parse_package_metadata($ARGV[0]) or exit 1;
618 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
619 my $pkg = $package{$name};
620 if ($pkg->{name} && $pkg->{repository}) {
621 print "Package/$name/subdir = $pkg->{repository}\n";
622 }
623 }
624 }
625
626 sub gen_package_license($) {
627 my $level = shift;
628 parse_package_metadata($ARGV[0]) or exit 1;
629 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
630 my $pkg = $package{$name};
631 if ($pkg->{name}) {
632 if ($pkg->{license}) {
633 print "$pkg->{name}: ";
634 print "$pkg->{license}\n";
635 if ($pkg->{licensefiles} && $level == 0) {
636 print "\tFiles: $pkg->{licensefiles}\n";
637 }
638 } else {
639 if ($level == 1) {
640 print "$pkg->{name}: Missing license! ";
641 print "Please fix $pkg->{makefile}\n";
642 }
643 }
644 }
645 }
646 }
647
648 sub gen_version_filtered_list() {
649 foreach my $item (version_filter_list(@ARGV)) {
650 print "$item\n";
651 }
652 }
653
654 sub gen_usergroup_list() {
655 parse_package_metadata($ARGV[0]) or exit 1;
656 for my $name (keys %usernames) {
657 print "user $name $usernames{$name}{id} $usernames{$name}{makefile}\n";
658 }
659 for my $name (keys %groupnames) {
660 print "group $name $groupnames{$name}{id} $groupnames{$name}{makefile}\n";
661 }
662 }
663
664 sub parse_command() {
665 GetOptions("ignore=s", \@ignore);
666 my $cmd = shift @ARGV;
667 for ($cmd) {
668 /^mk$/ and return gen_package_mk();
669 /^config$/ and return gen_package_config();
670 /^kconfig/ and return gen_kconfig_overrides();
671 /^source$/ and return gen_package_source();
672 /^subdirs$/ and return gen_package_subdirs();
673 /^license$/ and return gen_package_license(0);
674 /^licensefull$/ and return gen_package_license(1);
675 /^usergroup$/ and return gen_usergroup_list();
676 /^version_filter$/ and return gen_version_filtered_list();
677 }
678 die <<EOF
679 Available Commands:
680 $0 mk [file] Package metadata in makefile format
681 $0 config [file] Package metadata in Kconfig format
682 $0 kconfig [file] [config] [patchver] Kernel config overrides
683 $0 source [file] Package source file information
684 $0 subdirs [file] Package subdir information in makefile format
685 $0 license [file] Package license information
686 $0 licensefull [file] Package license information (full list)
687 $0 usergroup [file] Package usergroup allocation list
688 $0 version_filter [patchver] [list...] Filter list of version tagged strings
689
690 Options:
691 --ignore <name> Ignore the source package <name>
692 EOF
693 }
694
695 parse_command();