ar71xx/images/senao: fix reproducible issue using tar
[oweals/openwrt.git] / scripts / metadata.pm
1 package metadata;
2 use base 'Exporter';
3 use strict;
4 use warnings;
5 our @EXPORT = qw(%package %srcpackage %category %subdir %preconfig %features %overrides clear_packages parse_package_metadata parse_target_metadata get_multiline @ignore %usernames %groupnames);
6
7 our %package;
8 our %preconfig;
9 our %srcpackage;
10 our %category;
11 our %subdir;
12 our %features;
13 our %overrides;
14 our @ignore;
15
16 our %usernames;
17 our %groupnames;
18 our %userids;
19 our %groupids;
20
21 sub get_multiline {
22         my $fh = shift;
23         my $prefix = shift;
24         my $str;
25         while (<$fh>) {
26                 last if /^@@/;
27                 $str .= (($_ and $prefix) ? $prefix . $_ : $_);
28         }
29
30         return $str ? $str : "";
31 }
32
33 sub confstr($) {
34         my $conf = shift;
35         $conf =~ tr#/\.\-/#___#;
36         return $conf;
37 }
38
39 sub parse_package_metadata_usergroup($$$$$) {
40         my $makefile = shift;
41         my $typename = shift;
42         my $names = shift;
43         my $ids = shift;
44         my $spec = shift;
45         my $name;
46         my $id;
47
48         # the regex for name is taken from is_valid_name() of package shadow
49         if ($spec =~ /^([a-z_][a-z0-9_-]*\$?)$/) {
50                 $name = $spec;
51                 $id = -1;
52         } elsif ($spec =~ /^([a-z_][a-z0-9_-]*\$?)=(\d+)$/) {
53                 $name = $1;
54                 $id = $2;
55         } else {
56                 warn "$makefile: invalid $typename spec $spec\n";
57                 return 0;
58         }
59
60         if ($id =~ /^[1-9]\d*$/) {
61                 if ($id >= 65536) {
62                         warn "$makefile: $typename $name id $id >= 65536";
63                         return 0;
64                 }
65                 if (not exists $ids->{$id}) {
66                         $ids->{$id} = {
67                                 name => $name,
68                                 makefile => $makefile,
69                         };
70                 } elsif ($ids->{$id}{name} ne $name) {
71                         warn "$makefile: $typename $name id $id is already taken by $ids->{$id}{makefile}\n";
72                         return 0;
73                 }
74         } elsif ($id != -1) {
75                 warn "$makefile: $typename $name has invalid id $id\n";
76                 return 0;
77         }
78
79         if (not exists $names->{$name}) {
80                 $names->{$name} = {
81                         id => $id,
82                         makefile => $makefile,
83                 };
84         } elsif ($names->{$name}{id} != $id) {
85                 warn "$makefile: id of $typename $name collides with that defined defined in $names->{$name}{makefile}\n";
86                 return 0;
87         }
88         return 1;
89 }
90
91 sub parse_target_metadata($) {
92         my $file = shift;
93         my ($target, @target, $profile);
94         my %target;
95         my $makefile;
96
97         open FILE, "<$file" or do {
98                 warn "Can't open file '$file': $!\n";
99                 return;
100         };
101         while (<FILE>) {
102                 chomp;
103                 /^Source-Makefile: \s*((.+\/)([^\/]+)\/Makefile)\s*$/ and $makefile = $1;
104                 /^Target:\s*(.+)\s*$/ and do {
105                         my $name = $1;
106                         $target = {
107                                 id => $name,
108                                 board => $name,
109                                 makefile => $makefile,
110                                 boardconf => confstr($name),
111                                 conf => confstr($name),
112                                 profiles => [],
113                                 features => [],
114                                 depends => [],
115                                 subtargets => []
116                         };
117                         push @target, $target;
118                         $target{$name} = $target;
119                         if ($name =~ /([^\/]+)\/([^\/]+)/) {
120                                 push @{$target{$1}->{subtargets}}, $2;
121                                 $target->{board} = $1;
122                                 $target->{boardconf} = confstr($1);
123                                 $target->{subtarget} = 1;
124                                 $target->{parent} = $target{$1};
125                         }
126                 };
127                 /^Target-Name:\s*(.+)\s*$/ and $target->{name} = $1;
128                 /^Target-Arch:\s*(.+)\s*$/ and $target->{arch} = $1;
129                 /^Target-Arch-Packages:\s*(.+)\s*$/ and $target->{arch_packages} = $1;
130                 /^Target-Features:\s*(.+)\s*$/ and $target->{features} = [ split(/\s+/, $1) ];
131                 /^Target-Depends:\s*(.+)\s*$/ and $target->{depends} = [ split(/\s+/, $1) ];
132                 /^Target-Description:/ and $target->{desc} = get_multiline(*FILE);
133                 /^Target-Optimization:\s*(.+)\s*$/ and $target->{cflags} = $1;
134                 /^CPU-Type:\s*(.+)\s*$/ and $target->{cputype} = $1;
135                 /^Linux-Version:\s*(.+)\s*$/ and $target->{version} = $1;
136                 /^Linux-Release:\s*(.+)\s*$/ and $target->{release} = $1;
137                 /^Linux-Kernel-Arch:\s*(.+)\s*$/ and $target->{karch} = $1;
138                 /^Default-Subtarget:\s*(.+)\s*$/ and $target->{def_subtarget} = $1;
139                 /^Default-Packages:\s*(.+)\s*$/ and $target->{packages} = [ split(/\s+/, $1) ];
140                 /^Target-Profile:\s*(.+)\s*$/ and do {
141                         $profile = {
142                                 id => $1,
143                                 name => $1,
144                                 priority => 999,
145                                 packages => []
146                         };
147                         $1 =~ /^DEVICE_/ and $target->{has_devices} = 1;
148                         push @{$target->{profiles}}, $profile;
149                 };
150                 /^Target-Profile-Name:\s*(.+)\s*$/ and $profile->{name} = $1;
151                 /^Target-Profile-Priority:\s*(\d+)\s*$/ and do {
152                         $profile->{priority} = $1;
153                         $target->{sort} = 1;
154                 };
155                 /^Target-Profile-Packages:\s*(.*)\s*$/ and $profile->{packages} = [ split(/\s+/, $1) ];
156                 /^Target-Profile-Description:\s*(.*)\s*/ and $profile->{desc} = get_multiline(*FILE);
157         }
158         close FILE;
159         foreach my $target (@target) {
160                 if (@{$target->{subtargets}} > 0) {
161                         $target->{profiles} = [];
162                         next;
163                 }
164                 @{$target->{profiles}} > 0 or $target->{profiles} = [
165                         {
166                                 id => 'Default',
167                                 name => 'Default',
168                                 packages => []
169                         }
170                 ];
171
172                 $target->{sort} and @{$target->{profiles}} = sort {
173                         $a->{priority} <=> $b->{priority} or
174                         $a->{name} cmp $b->{name};
175                 } @{$target->{profiles}};
176         }
177         return @target;
178 }
179
180 sub clear_packages() {
181         %subdir = ();
182         %preconfig = ();
183         %package = ();
184         %srcpackage = ();
185         %category = ();
186         %features = ();
187         %overrides = ();
188         %usernames = ();
189         %groupnames = ();
190 }
191
192 sub parse_package_metadata($) {
193         my $file = shift;
194         my $pkg;
195         my $feature;
196         my $makefile;
197         my $preconfig;
198         my $subdir;
199         my $src;
200         my $override;
201         my %ignore = map { $_ => 1 } @ignore;
202
203         open FILE, "<$file" or do {
204                 warn "Cannot open '$file': $!\n";
205                 return undef;
206         };
207         while (<FILE>) {
208                 chomp;
209                 /^Source-Makefile: \s*((.+\/)([^\/]+)\/Makefile)\s*$/ and do {
210                         $makefile = $1;
211                         $subdir = $2;
212                         $src = $3;
213                         $subdir =~ s/^package\///;
214                         $subdir{$src} = $subdir;
215                         $srcpackage{$src} = [];
216                         $override = "";
217                         undef $pkg;
218                 };
219                 /^Override: \s*(.+?)\s*$/ and do {
220                         $override = $1;
221                         $overrides{$src} = 1;
222                 };
223                 next unless $src;
224                 /^Package:\s*(.+?)\s*$/ and do {
225                         undef $feature;
226                         $pkg = {};
227                         $pkg->{ignore} = $ignore{$src};
228                         $pkg->{src} = $src;
229                         $pkg->{makefile} = $makefile;
230                         $pkg->{name} = $1;
231                         $pkg->{title} = "";
232                         $pkg->{depends} = [];
233                         $pkg->{mdepends} = [];
234                         $pkg->{builddepends} = [];
235                         $pkg->{buildtypes} = [];
236                         $pkg->{subdir} = $subdir;
237                         $pkg->{tristate} = 1;
238                         $pkg->{override} = $override;
239                         $package{$1} = $pkg;
240                         push @{$srcpackage{$src}}, $pkg;
241                 };
242                 /^Feature:\s*(.+?)\s*$/ and do {
243                         undef $pkg;
244                         $feature = {};
245                         $feature->{name} = $1;
246                         $feature->{priority} = 0;
247                 };
248                 $feature and do {
249                         /^Target-Name:\s*(.+?)\s*$/ and do {
250                                 $features{$1} or $features{$1} = [];
251                                 push @{$features{$1}}, $feature unless $ignore{$src};
252                         };
253                         /^Target-Title:\s*(.+?)\s*$/ and $feature->{target_title} = $1;
254                         /^Feature-Priority:\s*(\d+)\s*$/ and $feature->{priority} = $1;
255                         /^Feature-Name:\s*(.+?)\s*$/ and $feature->{title} = $1;
256                         /^Feature-Description:/ and $feature->{description} = get_multiline(\*FILE, "\t\t\t");
257                         next;
258                 };
259                 next unless $pkg;
260                 /^Version: \s*(.+)\s*$/ and $pkg->{version} = $1;
261                 /^Title: \s*(.+)\s*$/ and $pkg->{title} = $1;
262                 /^Menu: \s*(.+)\s*$/ and $pkg->{menu} = $1;
263                 /^Submenu: \s*(.+)\s*$/ and $pkg->{submenu} = $1;
264                 /^Submenu-Depends: \s*(.+)\s*$/ and $pkg->{submenudep} = $1;
265                 /^Source: \s*(.+)\s*$/ and $pkg->{source} = $1;
266                 /^License: \s*(.+)\s*$/ and $pkg->{license} = $1;
267                 /^LicenseFiles: \s*(.+)\s*$/ and $pkg->{licensefiles} = $1;
268                 /^Default: \s*(.+)\s*$/ and $pkg->{default} = $1;
269                 /^Provides: \s*(.+)\s*$/ and do {
270                         my @vpkg = split /\s+/, $1;
271                         foreach my $vpkg (@vpkg) {
272                                 $package{$vpkg} or $package{$vpkg} = {
273                                         name => $vpkg,
274                                         vdepends => [],
275                                         src => $src,
276                                         subdir => $subdir,
277                                         makefile => $makefile
278                                 };
279                                 push @{$package{$vpkg}->{vdepends}}, $pkg->{name};
280                         }
281                 };
282                 /^Menu-Depends: \s*(.+)\s*$/ and $pkg->{mdepends} = [ split /\s+/, $1 ];
283                 /^Depends: \s*(.+)\s*$/ and $pkg->{depends} = [ split /\s+/, $1 ];
284                 /^Conflicts: \s*(.+)\s*$/ and $pkg->{conflicts} = [ split /\s+/, $1 ];
285                 /^Hidden: \s*(.+)\s*$/ and $pkg->{hidden} = 1;
286                 /^Build-Variant: \s*([\w\-]+)\s*/ and $pkg->{variant} = $1;
287                 /^Default-Variant: .*/ and $pkg->{variant_default} = 1;
288                 /^Build-Only: \s*(.+)\s*$/ and $pkg->{buildonly} = 1;
289                 /^Build-Depends: \s*(.+)\s*$/ and $pkg->{builddepends} = [ split /\s+/, $1 ];
290                 /^Build-Depends\/(\w+): \s*(.+)\s*$/ and $pkg->{"builddepends/$1"} = [ split /\s+/, $2 ];
291                 /^Build-Types:\s*(.+)\s*$/ and $pkg->{buildtypes} = [ split /\s+/, $1 ];
292                 /^Repository:\s*(.+?)\s*$/ and $pkg->{repository} = $1;
293                 /^Category: \s*(.+)\s*$/ and do {
294                         $pkg->{category} = $1;
295                         defined $category{$1} or $category{$1} = {};
296                         defined $category{$1}->{$src} or $category{$1}->{$src} = [];
297                         push @{$category{$1}->{$src}}, $pkg;
298                 };
299                 /^Description: \s*(.*)\s*$/ and $pkg->{description} = "\t\t $1\n". get_multiline(*FILE, "\t\t ");
300                 /^Type: \s*(.+)\s*$/ and do {
301                         $pkg->{type} = [ split /\s+/, $1 ];
302                         undef $pkg->{tristate};
303                         foreach my $type (@{$pkg->{type}}) {
304                                 $type =~ /ipkg/ and $pkg->{tristate} = 1;
305                         }
306                 };
307                 /^Config:\s*(.*)\s*$/ and $pkg->{config} = "$1\n".get_multiline(*FILE, "\t");
308                 /^Prereq-Check:/ and $pkg->{prereq} = 1;
309                 /^Preconfig:\s*(.+)\s*$/ and do {
310                         my $pkgname = $pkg->{name};
311                         $preconfig{$pkgname} or $preconfig{$pkgname} = {};
312                         if (exists $preconfig{$pkgname}->{$1}) {
313                                 $preconfig = $preconfig{$pkgname}->{$1};
314                         } else {
315                                 $preconfig = {
316                                         id => $1
317                                 };
318                                 $preconfig{$pkgname}->{$1} = $preconfig unless $ignore{$src};
319                         }
320                 };
321                 /^Preconfig-Type:\s*(.*?)\s*$/ and $preconfig->{type} = $1;
322                 /^Preconfig-Label:\s*(.*?)\s*$/ and $preconfig->{label} = $1;
323                 /^Preconfig-Default:\s*(.*?)\s*$/ and $preconfig->{default} = $1;
324                 /^Require-User:\s*(.*?)\s*$/ and do {
325                         my @ugspecs = split /\s+/, $1;
326
327                         for my $ugspec (@ugspecs) {
328                                 my @ugspec = split /:/, $ugspec, 2;
329                                 parse_package_metadata_usergroup($makefile, "user", \%usernames, \%userids, $ugspec[0]) or return 0;
330                                 if (@ugspec > 1) {
331                                         parse_package_metadata_usergroup($makefile, "group", \%groupnames, \%groupids, $ugspec[1]) or return 0;
332                                 }
333                         }
334                 };
335         }
336         close FILE;
337         return 1;
338 }
339
340 1;