f3c84e80c3d8feebc4acd02cb3c7158644dada05
[openwrt/staging/chunkeey.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->{ignore} = $src->{ignore};
228 $pkg->{src} = $src;
229 $pkg->{makefile} = $src->{makefile};
230 $pkg->{name} = $1;
231 $pkg->{title} = "";
232 $pkg->{depends} = [];
233 $pkg->{mdepends} = [];
234 $pkg->{tristate} = 1;
235 $pkg->{override} = $override;
236 $package{$1} = $pkg;
237 push @{$src->{packages}}, $pkg;
238 };
239 /^Feature:\s*(.+?)\s*$/ and do {
240 undef $pkg;
241 $feature = {};
242 $feature->{name} = $1;
243 $feature->{priority} = 0;
244 };
245 $feature and do {
246 /^Target-Name:\s*(.+?)\s*$/ and do {
247 $features{$1} or $features{$1} = [];
248 push @{$features{$1}}, $feature unless $src->{ignore};
249 };
250 /^Target-Title:\s*(.+?)\s*$/ and $feature->{target_title} = $1;
251 /^Feature-Priority:\s*(\d+)\s*$/ and $feature->{priority} = $1;
252 /^Feature-Name:\s*(.+?)\s*$/ and $feature->{title} = $1;
253 /^Feature-Description:/ and $feature->{description} = get_multiline(\*FILE, "\t\t\t");
254 next;
255 };
256 /^Build-Depends: \s*(.+)\s*$/ and $src->{builddepends} = [ split /\s+/, $1 ];
257 /^Build-Depends\/(\w+): \s*(.+)\s*$/ and $src->{"builddepends/$1"} = [ split /\s+/, $2 ];
258 /^Build-Types:\s*(.+)\s*$/ and $src->{buildtypes} = [ split /\s+/, $1 ];
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 makefile => $src->{makefile},
277 };
278 push @{$package{$vpkg}->{vdepends}}, $pkg->{name};
279 }
280 };
281 /^Menu-Depends: \s*(.+)\s*$/ and $pkg->{mdepends} = [ split /\s+/, $1 ];
282 /^Depends: \s*(.+)\s*$/ and $pkg->{depends} = [ split /\s+/, $1 ];
283 /^Conflicts: \s*(.+)\s*$/ and $pkg->{conflicts} = [ split /\s+/, $1 ];
284 /^Hidden: \s*(.+)\s*$/ and $pkg->{hidden} = 1;
285 /^Build-Variant: \s*([\w\-]+)\s*/ and $pkg->{variant} = $1;
286 /^Default-Variant: .*/ and $pkg->{variant_default} = 1;
287 /^Build-Only: \s*(.+)\s*$/ and $pkg->{buildonly} = 1;
288 /^Repository:\s*(.+?)\s*$/ and $pkg->{repository} = $1;
289 /^Category: \s*(.+)\s*$/ and do {
290 $pkg->{category} = $1;
291 defined $category{$1} or $category{$1} = {};
292 defined $category{$1}{$src->{name}} or $category{$1}{$src->{name}} = [];
293 push @{$category{$1}{$src->{name}}}, $pkg;
294 };
295 /^Description: \s*(.*)\s*$/ and $pkg->{description} = "\t\t $1\n". get_multiline(*FILE, "\t\t ");
296 /^Type: \s*(.+)\s*$/ and do {
297 $pkg->{type} = [ split /\s+/, $1 ];
298 undef $pkg->{tristate};
299 foreach my $type (@{$pkg->{type}}) {
300 $type =~ /ipkg/ and $pkg->{tristate} = 1;
301 }
302 };
303 /^Config:\s*(.*)\s*$/ and $pkg->{config} = "$1\n".get_multiline(*FILE, "\t");
304 /^Prereq-Check:/ and $pkg->{prereq} = 1;
305 /^Preconfig:\s*(.+)\s*$/ and do {
306 my $pkgname = $pkg->{name};
307 $preconfig{$pkgname} or $preconfig{$pkgname} = {};
308 if (exists $preconfig{$pkgname}->{$1}) {
309 $preconfig = $preconfig{$pkgname}->{$1};
310 } else {
311 $preconfig = {
312 id => $1
313 };
314 $preconfig{$pkgname}{$1} = $preconfig unless $src->{ignore};
315 }
316 };
317 /^Preconfig-Type:\s*(.*?)\s*$/ and $preconfig->{type} = $1;
318 /^Preconfig-Label:\s*(.*?)\s*$/ and $preconfig->{label} = $1;
319 /^Preconfig-Default:\s*(.*?)\s*$/ and $preconfig->{default} = $1;
320 /^Require-User:\s*(.*?)\s*$/ and do {
321 my @ugspecs = split /\s+/, $1;
322
323 for my $ugspec (@ugspecs) {
324 my @ugspec = split /:/, $ugspec, 2;
325 parse_package_metadata_usergroup($src->{makefile}, "user", \%usernames, \%userids, $ugspec[0]) or return 0;
326 if (@ugspec > 1) {
327 parse_package_metadata_usergroup($src->{makefile}, "group", \%groupnames, \%groupids, $ugspec[1]) or return 0;
328 }
329 }
330 };
331 }
332 close FILE;
333 return 1;
334 }
335
336 1;