metadata: move 'buildtypes' from binary to source packages
[openwrt/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 packages => [],
217 buildtypes => [],
218 };
219 $override = "";
220 undef $pkg;
221 };
222 /^Override: \s*(.+?)\s*$/ and do {
223 $override = $1;
224 $overrides{$src} = 1;
225 };
226 next unless $src;
227 /^Package:\s*(.+?)\s*$/ and do {
228 undef $feature;
229 $pkg = {};
230 $pkg->{ignore} = $ignore{$src};
231 $pkg->{src} = $src;
232 $pkg->{makefile} = $makefile;
233 $pkg->{name} = $1;
234 $pkg->{title} = "";
235 $pkg->{depends} = [];
236 $pkg->{mdepends} = [];
237 $pkg->{builddepends} = [];
238 $pkg->{subdir} = $subdir;
239 $pkg->{tristate} = 1;
240 $pkg->{override} = $override;
241 $package{$1} = $pkg;
242 push @{$srcpackage{$src}{packages}}, $pkg;
243 };
244 /^Feature:\s*(.+?)\s*$/ and do {
245 undef $pkg;
246 $feature = {};
247 $feature->{name} = $1;
248 $feature->{priority} = 0;
249 };
250 $feature and do {
251 /^Target-Name:\s*(.+?)\s*$/ and do {
252 $features{$1} or $features{$1} = [];
253 push @{$features{$1}}, $feature unless $ignore{$src};
254 };
255 /^Target-Title:\s*(.+?)\s*$/ and $feature->{target_title} = $1;
256 /^Feature-Priority:\s*(\d+)\s*$/ and $feature->{priority} = $1;
257 /^Feature-Name:\s*(.+?)\s*$/ and $feature->{title} = $1;
258 /^Feature-Description:/ and $feature->{description} = get_multiline(\*FILE, "\t\t\t");
259 next;
260 };
261 /^Build-Types:\s*(.+)\s*$/ and $srcpackage{$src}{buildtypes} = [ split /\s+/, $1 ];
262 next unless $pkg;
263 /^Version: \s*(.+)\s*$/ and $pkg->{version} = $1;
264 /^Title: \s*(.+)\s*$/ and $pkg->{title} = $1;
265 /^Menu: \s*(.+)\s*$/ and $pkg->{menu} = $1;
266 /^Submenu: \s*(.+)\s*$/ and $pkg->{submenu} = $1;
267 /^Submenu-Depends: \s*(.+)\s*$/ and $pkg->{submenudep} = $1;
268 /^Source: \s*(.+)\s*$/ and $pkg->{source} = $1;
269 /^License: \s*(.+)\s*$/ and $pkg->{license} = $1;
270 /^LicenseFiles: \s*(.+)\s*$/ and $pkg->{licensefiles} = $1;
271 /^Default: \s*(.+)\s*$/ and $pkg->{default} = $1;
272 /^Provides: \s*(.+)\s*$/ and do {
273 my @vpkg = split /\s+/, $1;
274 foreach my $vpkg (@vpkg) {
275 $package{$vpkg} or $package{$vpkg} = {
276 name => $vpkg,
277 vdepends => [],
278 src => $src,
279 subdir => $subdir,
280 makefile => $makefile
281 };
282 push @{$package{$vpkg}->{vdepends}}, $pkg->{name};
283 }
284 };
285 /^Menu-Depends: \s*(.+)\s*$/ and $pkg->{mdepends} = [ split /\s+/, $1 ];
286 /^Depends: \s*(.+)\s*$/ and $pkg->{depends} = [ split /\s+/, $1 ];
287 /^Conflicts: \s*(.+)\s*$/ and $pkg->{conflicts} = [ split /\s+/, $1 ];
288 /^Hidden: \s*(.+)\s*$/ and $pkg->{hidden} = 1;
289 /^Build-Variant: \s*([\w\-]+)\s*/ and $pkg->{variant} = $1;
290 /^Default-Variant: .*/ and $pkg->{variant_default} = 1;
291 /^Build-Only: \s*(.+)\s*$/ and $pkg->{buildonly} = 1;
292 /^Build-Depends: \s*(.+)\s*$/ and $pkg->{builddepends} = [ split /\s+/, $1 ];
293 /^Build-Depends\/(\w+): \s*(.+)\s*$/ and $pkg->{"builddepends/$1"} = [ split /\s+/, $2 ];
294 /^Repository:\s*(.+?)\s*$/ and $pkg->{repository} = $1;
295 /^Category: \s*(.+)\s*$/ and do {
296 $pkg->{category} = $1;
297 defined $category{$1} or $category{$1} = {};
298 defined $category{$1}->{$src} or $category{$1}->{$src} = [];
299 push @{$category{$1}->{$src}}, $pkg;
300 };
301 /^Description: \s*(.*)\s*$/ and $pkg->{description} = "\t\t $1\n". get_multiline(*FILE, "\t\t ");
302 /^Type: \s*(.+)\s*$/ and do {
303 $pkg->{type} = [ split /\s+/, $1 ];
304 undef $pkg->{tristate};
305 foreach my $type (@{$pkg->{type}}) {
306 $type =~ /ipkg/ and $pkg->{tristate} = 1;
307 }
308 };
309 /^Config:\s*(.*)\s*$/ and $pkg->{config} = "$1\n".get_multiline(*FILE, "\t");
310 /^Prereq-Check:/ and $pkg->{prereq} = 1;
311 /^Preconfig:\s*(.+)\s*$/ and do {
312 my $pkgname = $pkg->{name};
313 $preconfig{$pkgname} or $preconfig{$pkgname} = {};
314 if (exists $preconfig{$pkgname}->{$1}) {
315 $preconfig = $preconfig{$pkgname}->{$1};
316 } else {
317 $preconfig = {
318 id => $1
319 };
320 $preconfig{$pkgname}->{$1} = $preconfig unless $ignore{$src};
321 }
322 };
323 /^Preconfig-Type:\s*(.*?)\s*$/ and $preconfig->{type} = $1;
324 /^Preconfig-Label:\s*(.*?)\s*$/ and $preconfig->{label} = $1;
325 /^Preconfig-Default:\s*(.*?)\s*$/ and $preconfig->{default} = $1;
326 /^Require-User:\s*(.*?)\s*$/ and do {
327 my @ugspecs = split /\s+/, $1;
328
329 for my $ugspec (@ugspecs) {
330 my @ugspec = split /:/, $ugspec, 2;
331 parse_package_metadata_usergroup($makefile, "user", \%usernames, \%userids, $ugspec[0]) or return 0;
332 if (@ugspec > 1) {
333 parse_package_metadata_usergroup($makefile, "group", \%groupnames, \%groupids, $ugspec[1]) or return 0;
334 }
335 }
336 };
337 }
338 close FILE;
339 return 1;
340 }
341
342 1;