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