2dc29ea6ff3f0baae5464b527630f02fe1a76b00
[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->{depends};
105
106         return 0 unless defined $deps;
107         foreach my $vpkg (@{$deps}) {
108                 foreach my $dep (@{$vpackage{$vpkg}}) {
109                         next if $dep_check{$dep->{name}};
110                         $dep_check{$dep->{name}} = 1;
111                         return 1 if $dep->{name} eq $name;
112                         return 1 if (__find_package_dep($dep, $name) == 1);
113                 }
114         }
115         return 0;
116 }
117
118 # wrapper to avoid infinite recursion
119 sub find_package_dep($$) {
120         my $pkg = shift;
121         my $name = shift;
122
123         %dep_check = ();
124         return __find_package_dep($pkg, $name);
125 }
126
127 sub package_depends($$) {
128         my $a = shift;
129         my $b = shift;
130         my $ret;
131
132         return 0 if ($a->{submenu} ne $b->{submenu});
133         if (find_package_dep($a, $b->{name}) == 1) {
134                 $ret = 1;
135         } elsif (find_package_dep($b, $a->{name}) == 1) {
136                 $ret = -1;
137         } else {
138                 return 0;
139         }
140         return $ret;
141 }
142
143 sub mconf_depends {
144         my $pkgname = shift;
145         my $depends = shift;
146         my $only_dep = shift;
147         my $res;
148         my $dep = shift;
149         my $seen = shift;
150         my $parent_condition = shift;
151         $dep or $dep = {};
152         $seen or $seen = {};
153         my @t_depends;
154
155         $depends or return;
156         my @depends = @$depends;
157         foreach my $depend (@depends) {
158                 my $m = "depends on";
159                 my $flags = "";
160                 $depend =~ s/^([@\+]+)// and $flags = $1;
161                 my $condition = $parent_condition;
162
163                 next if $condition eq $depend;
164                 next if $seen->{"$parent_condition:$depend"};
165                 next if $seen->{":$depend"};
166                 $seen->{"$parent_condition:$depend"} = 1;
167                 if ($depend =~ /^(.+):(.+)$/) {
168                         if ($1 ne "PACKAGE_$pkgname") {
169                                 if ($condition) {
170                                         $condition = "$condition && $1";
171                                 } else {
172                                         $condition = $1;
173                                 }
174                         }
175                         $depend = $2;
176                 }
177                 if ($flags =~ /\+/) {
178                         my $vdep = $vpackage{$depend};
179                         if ($vdep) {
180                                 my @vdeps;
181
182                                 foreach my $v (@$vdep) {
183                                         next if $v->{buildonly};
184                                         if ($v->{variant_default}) {
185                                                 unshift @vdeps, $v->{name};
186                                         } else {
187                                                 push @vdeps, $v->{name};
188                                         }
189                                 }
190
191                                 $depend = shift @vdeps;
192
193                                 if (@vdeps > 1) {
194                                         $condition = ($condition ? "$condition && " : '') . '!('.join("||", map { "PACKAGE_".$_ } @vdeps).')';
195                                 } elsif (@vdeps > 0) {
196                                         $condition = ($condition ? "$condition && " : '') . '!PACKAGE_'.$vdeps[0];
197                                 }
198                         }
199
200                         # Menuconfig will not treat 'select FOO' as a real dependency
201                         # thus if FOO depends on other config options, these dependencies
202                         # will not be checked. To fix this, we simply emit all of FOO's
203                         # depends here as well.
204                         $package{$depend} and push @t_depends, [ $package{$depend}->{depends}, $condition ];
205
206                         $m = "select";
207                         next if $only_dep;
208
209                         $flags =~ /@/ or $depend = "PACKAGE_$depend";
210                 } else {
211                         my $vdep = $vpackage{$depend};
212                         if ($vdep) {
213                                 $depend = join("||", map { "PACKAGE_".$_->{name} } @$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->{src}{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         print "source \"package/*/image-config.in\"\n";
376         if (scalar glob "package/feeds/*/*/image-config.in") {
377             print "source \"package/feeds/*/*/image-config.in\"\n";
378         }
379         print_package_features();
380         print_package_config_category 'Base system';
381         foreach my $cat (sort {uc($a) cmp uc($b)} keys %category) {
382                 print_package_config_category $cat;
383         }
384         print_package_overrides();
385 }
386
387 sub get_conditional_dep($$) {
388         my $condition = shift;
389         my $depstr = shift;
390         if ($condition) {
391                 if ($condition =~ /^!(.+)/) {
392                         return "\$(if \$(CONFIG_$1),,$depstr)";
393                 } else {
394                         return "\$(if \$(CONFIG_$condition),$depstr)";
395                 }
396         } else {
397                 return $depstr;
398         }
399 }
400
401 sub gen_package_mk() {
402         my $line;
403
404         parse_package_metadata($ARGV[0]) or exit 1;
405         foreach my $srcname (sort {uc($a) cmp uc($b)} keys %srcpackage) {
406                 my $src = $srcpackage{$srcname};
407                 my $variant_default;
408                 my %deplines = ('' => {});
409
410                 foreach my $pkg (@{$src->{packages}}) {
411                         foreach my $dep (@{$pkg->{depends}}) {
412                                 next if ($dep =~ /@/);
413
414                                 my $condition;
415
416                                 $dep =~ s/\+//g;
417                                 if ($dep =~ /^(.+):(.+)/) {
418                                         $condition = $1;
419                                         $dep = $2;
420                                 }
421
422                                 my $vpkg_dep = $vpackage{$dep};
423                                 unless (defined $vpkg_dep) {
424                                         warn sprintf "WARNING: Makefile '%s' has a dependency on '%s', which does not exist\n",
425                                                 $src->{makefile}, $dep;
426                                         next;
427                                 }
428
429                                 # Filter out self-depends
430                                 my @vdeps = grep { $srcname ne $_->{src}{name} } @{$vpkg_dep};
431
432                                 foreach my $vdep (@vdeps) {
433                                         my $depstr = "\$(curdir)/$vdep->{src}{path}/compile";
434                                         if (@vdeps > 1) {
435                                                 $depstr = "\$(if \$(CONFIG_PACKAGE_$vdep->{name}),$depstr)";
436                                         }
437                                         my $depline = get_conditional_dep($condition, $depstr);
438                                         if ($depline) {
439                                                 $deplines{''}{$depline}++;
440                                         }
441                                 }
442                         }
443
444                         my $config = '';
445                         $config = "\$(CONFIG_PACKAGE_$pkg->{name})" unless $pkg->{buildonly};
446
447                         $pkg->{prereq} and print "prereq-$config += $src->{path}\n";
448
449                         next if $pkg->{buildonly};
450
451                         print "package-$config += $src->{path}\n";
452
453                         if ($pkg->{variant}) {
454                                 if (!defined($variant_default) or $pkg->{variant_default}) {
455                                         $variant_default = $pkg->{variant};
456                                 }
457                                 print "\$(curdir)/$src->{path}/variants += \$(if $config,$pkg->{variant})\n";
458                         }
459                 }
460
461                 if (defined($variant_default)) {
462                         print "\$(curdir)/$src->{path}/default-variant := $variant_default\n";
463                 }
464
465                 unless (grep {!$_->{buildonly}} @{$src->{packages}}) {
466                         print "package- += $src->{path}\n";
467                 }
468
469                 if (@{$src->{buildtypes}} > 0) {
470                         print "buildtypes-$src->{path} = ".join(' ', @{$src->{buildtypes}})."\n";
471                 }
472
473                 foreach my $type ('', @{$src->{buildtypes}}) {
474                         my $suffix = '';
475
476                         $suffix = "/$type" if $type;
477
478                         next unless $src->{"builddepends$suffix"};
479
480                         defined $deplines{$suffix} or $deplines{$suffix} = {};
481
482                         foreach my $dep (@{$src->{"builddepends$suffix"}}) {
483                                 my $depsuffix = "";
484                                 my $deptype = "";
485                                 my $condition;
486
487                                 if ($dep =~ /^(.+):(.+)/) {
488                                         $condition = $1;
489                                         $dep = $2;
490                                 }
491                                 if ($dep =~ /^(.+)\/(.+)/) {
492                                         $dep = $1;
493                                         $deptype = $2;
494                                         $depsuffix = "/$2";
495                                 }
496
497                                 next if $srcname.$suffix eq $dep.$depsuffix;
498
499                                 my $src_dep = $srcpackage{$dep};
500                                 unless (defined($src_dep) && (!$deptype || grep { $_ eq $deptype } @{$src_dep->{buildtypes}})) {
501                                         warn sprintf "WARNING: Makefile '%s' has a build dependency on '%s%s', which does not exist\n",
502                                                 $src->{makefile}, $dep, $depsuffix;
503                                         next;
504                                 }
505
506                                 my $depstr = "\$(curdir)/$src_dep->{path}$depsuffix/compile";
507                                 my $depline = get_conditional_dep($condition, $depstr);
508                                 if ($depline) {
509                                         $deplines{$suffix}{$depline}++;
510                                 }
511                         }
512                 }
513
514                 foreach my $suffix (sort keys %deplines) {
515                         my $depline = join(" ", sort keys %{$deplines{$suffix}});
516                         if ($depline) {
517                                 $line .= "\$(curdir)/$src->{path}$suffix/compile += $depline\n";
518                         }
519                 }
520         }
521
522         if ($line ne "") {
523                 print "\n$line";
524         }
525 }
526
527 sub gen_package_source() {
528         parse_package_metadata($ARGV[0]) or exit 1;
529         foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
530                 my $pkg = $package{$name};
531                 if ($pkg->{name} && $pkg->{source}) {
532                         print "$pkg->{name}: ";
533                         print "$pkg->{source}\n";
534                 }
535         }
536 }
537
538 sub gen_package_subdirs() {
539         parse_package_metadata($ARGV[0]) or exit 1;
540         foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
541                 my $pkg = $package{$name};
542                 if ($pkg->{name} && $pkg->{repository}) {
543                         print "Package/$name/subdir = $pkg->{repository}\n";
544                 }
545         }
546 }
547
548 sub gen_package_license($) {
549         my $level = shift;
550         parse_package_metadata($ARGV[0]) or exit 1;
551         foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
552                 my $pkg = $package{$name};
553                 if ($pkg->{name}) {
554                         if ($pkg->{license}) {
555                                 print "$pkg->{name}: ";
556                                 print "$pkg->{license}\n";
557                                 if ($pkg->{licensefiles} && $level == 0) {
558                                         print "\tFiles: $pkg->{licensefiles}\n";
559                                 }
560                         } else {
561                                 if ($level == 1) {
562                                         print "$pkg->{name}: Missing license! ";
563                                         print "Please fix $pkg->{src}{makefile}\n";
564                                 }
565                         }
566                 }
567         }
568 }
569
570 sub gen_version_filtered_list() {
571         foreach my $item (version_filter_list(@ARGV)) {
572                 print "$item\n";
573         }
574 }
575
576 sub gen_usergroup_list() {
577         parse_package_metadata($ARGV[0]) or exit 1;
578         for my $name (keys %usernames) {
579                 print "user $name $usernames{$name}{id} $usernames{$name}{makefile}\n";
580         }
581         for my $name (keys %groupnames) {
582                 print "group $name $groupnames{$name}{id} $groupnames{$name}{makefile}\n";
583         }
584 }
585
586 sub parse_command() {
587         GetOptions("ignore=s", \@ignore);
588         my $cmd = shift @ARGV;
589         for ($cmd) {
590                 /^mk$/ and return gen_package_mk();
591                 /^config$/ and return gen_package_config();
592                 /^kconfig/ and return gen_kconfig_overrides();
593                 /^source$/ and return gen_package_source();
594                 /^subdirs$/ and return gen_package_subdirs();
595                 /^license$/ and return gen_package_license(0);
596                 /^licensefull$/ and return gen_package_license(1);
597                 /^usergroup$/ and return gen_usergroup_list();
598                 /^version_filter$/ and return gen_version_filtered_list();
599         }
600         die <<EOF
601 Available Commands:
602         $0 mk [file]                            Package metadata in makefile format
603         $0 config [file]                        Package metadata in Kconfig format
604         $0 kconfig [file] [config] [patchver]   Kernel config overrides
605         $0 source [file]                        Package source file information
606         $0 subdirs [file]                       Package subdir information in makefile format
607         $0 license [file]                       Package license information
608         $0 licensefull [file]                   Package license information (full list)
609         $0 usergroup [file]                     Package usergroup allocation list
610         $0 version_filter [patchver] [list...]  Filter list of version tagged strings
611
612 Options:
613         --ignore <name>                         Ignore the source package <name>
614 EOF
615 }
616
617 parse_command();