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