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