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