scripts/metadata.pl: add support for generating profile make code for the image builder
[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 %subdir %preconfig %features %overrides clear_packages parse_package_metadata parse_target_metadata get_multiline @ignore);
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 sub get_multiline {
17 my $fh = shift;
18 my $prefix = shift;
19 my $str;
20 while (<$fh>) {
21 last if /^@@/;
22 $str .= (($_ and $prefix) ? $prefix . $_ : $_);
23 }
24
25 return $str ? $str : "";
26 }
27
28 sub confstr($) {
29 my $conf = shift;
30 $conf =~ tr#/\.\-/#___#;
31 return $conf;
32 }
33
34 sub parse_target_metadata($) {
35 my $file = shift;
36 my ($target, @target, $profile);
37 my %target;
38 my $makefile;
39
40 open FILE, "<$file" or do {
41 warn "Can't open file '$file': $!\n";
42 return;
43 };
44 while (<FILE>) {
45 chomp;
46 /^Source-Makefile: \s*((.+\/)([^\/]+)\/Makefile)\s*$/ and $makefile = $1;
47 /^Target:\s*(.+)\s*$/ and do {
48 my $name = $1;
49 $target = {
50 id => $name,
51 board => $name,
52 makefile => $makefile,
53 boardconf => confstr($name),
54 conf => confstr($name),
55 profiles => [],
56 features => [],
57 depends => [],
58 subtargets => []
59 };
60 push @target, $target;
61 $target{$name} = $target;
62 if ($name =~ /([^\/]+)\/([^\/]+)/) {
63 push @{$target{$1}->{subtargets}}, $2;
64 $target->{board} = $1;
65 $target->{boardconf} = confstr($1);
66 $target->{subtarget} = 1;
67 $target->{parent} = $target{$1};
68 }
69 };
70 /^Target-Name:\s*(.+)\s*$/ and $target->{name} = $1;
71 /^Target-Arch:\s*(.+)\s*$/ and $target->{arch} = $1;
72 /^Target-Arch-Packages:\s*(.+)\s*$/ and $target->{arch_packages} = $1;
73 /^Target-Features:\s*(.+)\s*$/ and $target->{features} = [ split(/\s+/, $1) ];
74 /^Target-Depends:\s*(.+)\s*$/ and $target->{depends} = [ split(/\s+/, $1) ];
75 /^Target-Description:/ and $target->{desc} = get_multiline(*FILE);
76 /^Target-Optimization:\s*(.+)\s*$/ and $target->{cflags} = $1;
77 /^CPU-Type:\s*(.+)\s*$/ and $target->{cputype} = $1;
78 /^Linux-Version:\s*(.+)\s*$/ and $target->{version} = $1;
79 /^Linux-Release:\s*(.+)\s*$/ and $target->{release} = $1;
80 /^Linux-Kernel-Arch:\s*(.+)\s*$/ and $target->{karch} = $1;
81 /^Default-Subtarget:\s*(.+)\s*$/ and $target->{def_subtarget} = $1;
82 /^Default-Packages:\s*(.+)\s*$/ and $target->{packages} = [ split(/\s+/, $1) ];
83 /^Target-Profile:\s*(.+)\s*$/ and do {
84 $profile = {
85 id => $1,
86 name => $1,
87 priority => 999,
88 packages => []
89 };
90 push @{$target->{profiles}}, $profile;
91 };
92 /^Target-Profile-Name:\s*(.+)\s*$/ and $profile->{name} = $1;
93 /^Target-Profile-Priority:\s*(\d+)\s*$/ and do {
94 $profile->{priority} = $1;
95 $target->{sort} = 1;
96 };
97 /^Target-Profile-Packages:\s*(.*)\s*$/ and $profile->{packages} = [ split(/\s+/, $1) ];
98 /^Target-Profile-Description:\s*(.*)\s*/ and $profile->{desc} = get_multiline(*FILE);
99 /^Target-Profile-Config:/ and $profile->{config} = get_multiline(*FILE, "\t");
100 /^Target-Profile-Kconfig:/ and $profile->{kconfig} = 1;
101 }
102 close FILE;
103 foreach my $target (@target) {
104 if (@{$target->{subtargets}} > 0) {
105 $target->{profiles} = [];
106 next;
107 }
108 @{$target->{profiles}} > 0 or $target->{profiles} = [
109 {
110 id => 'Default',
111 name => 'Default',
112 packages => []
113 }
114 ];
115
116 $target->{sort} and @{$target->{profiles}} = sort {
117 $a->{priority} <=> $b->{priority} or
118 $a->{name} cmp $b->{name};
119 } @{$target->{profiles}};
120 }
121 return @target;
122 }
123
124 sub clear_packages() {
125 %subdir = ();
126 %preconfig = ();
127 %package = ();
128 %srcpackage = ();
129 %category = ();
130 %features = ();
131 %overrides = ();
132 }
133
134 sub parse_package_metadata($) {
135 my $file = shift;
136 my $pkg;
137 my $feature;
138 my $makefile;
139 my $preconfig;
140 my $subdir;
141 my $src;
142 my $override;
143 my %ignore = map { $_ => 1 } @ignore;
144
145 open FILE, "<$file" or do {
146 warn "Cannot open '$file': $!\n";
147 return undef;
148 };
149 while (<FILE>) {
150 chomp;
151 /^Source-Makefile: \s*((.+\/)([^\/]+)\/Makefile)\s*$/ and do {
152 $makefile = $1;
153 $subdir = $2;
154 $src = $3;
155 $subdir =~ s/^package\///;
156 $subdir{$src} = $subdir;
157 $srcpackage{$src} = [];
158 $override = "";
159 undef $pkg;
160 };
161 /^Override: \s*(.+?)\s*$/ and do {
162 $override = $1;
163 $overrides{$src} = 1;
164 };
165 next unless $src;
166 next if $ignore{$src};
167 /^Package:\s*(.+?)\s*$/ and do {
168 undef $feature;
169 $pkg = {};
170 $pkg->{src} = $src;
171 $pkg->{makefile} = $makefile;
172 $pkg->{name} = $1;
173 $pkg->{title} = "";
174 $pkg->{depends} = [];
175 $pkg->{mdepends} = [];
176 $pkg->{builddepends} = [];
177 $pkg->{buildtypes} = [];
178 $pkg->{subdir} = $subdir;
179 $pkg->{tristate} = 1;
180 $pkg->{override} = $override;
181 $package{$1} = $pkg;
182 push @{$srcpackage{$src}}, $pkg;
183 };
184 /^Feature:\s*(.+?)\s*$/ and do {
185 undef $pkg;
186 $feature = {};
187 $feature->{name} = $1;
188 $feature->{priority} = 0;
189 };
190 $feature and do {
191 /^Target-Name:\s*(.+?)\s*$/ and do {
192 $features{$1} or $features{$1} = [];
193 push @{$features{$1}}, $feature;
194 };
195 /^Target-Title:\s*(.+?)\s*$/ and $feature->{target_title} = $1;
196 /^Feature-Priority:\s*(\d+)\s*$/ and $feature->{priority} = $1;
197 /^Feature-Name:\s*(.+?)\s*$/ and $feature->{title} = $1;
198 /^Feature-Description:/ and $feature->{description} = get_multiline(\*FILE, "\t\t\t");
199 next;
200 };
201 next unless $pkg;
202 /^Version: \s*(.+)\s*$/ and $pkg->{version} = $1;
203 /^Title: \s*(.+)\s*$/ and $pkg->{title} = $1;
204 /^Menu: \s*(.+)\s*$/ and $pkg->{menu} = $1;
205 /^Submenu: \s*(.+)\s*$/ and $pkg->{submenu} = $1;
206 /^Submenu-Depends: \s*(.+)\s*$/ and $pkg->{submenudep} = $1;
207 /^Source: \s*(.+)\s*$/ and $pkg->{source} = $1;
208 /^License: \s*(.+)\s*$/ and $pkg->{license} = $1;
209 /^LicenseFiles: \s*(.+)\s*$/ and $pkg->{licensefiles} = $1;
210 /^Default: \s*(.+)\s*$/ and $pkg->{default} = $1;
211 /^Provides: \s*(.+)\s*$/ and do {
212 my @vpkg = split /\s+/, $1;
213 foreach my $vpkg (@vpkg) {
214 $package{$vpkg} or $package{$vpkg} = {
215 name => $vpkg,
216 vdepends => [],
217 src => $src,
218 subdir => $subdir,
219 makefile => $makefile
220 };
221 push @{$package{$vpkg}->{vdepends}}, $pkg->{name};
222 }
223 };
224 /^Menu-Depends: \s*(.+)\s*$/ and $pkg->{mdepends} = [ split /\s+/, $1 ];
225 /^Depends: \s*(.+)\s*$/ and $pkg->{depends} = [ split /\s+/, $1 ];
226 /^Conflicts: \s*(.+)\s*$/ and $pkg->{conflicts} = [ split /\s+/, $1 ];
227 /^Hidden: \s*(.+)\s*$/ and $pkg->{hidden} = 1;
228 /^Build-Variant: \s*([\w\-]+)\s*/ and $pkg->{variant} = $1;
229 /^Default-Variant: .*/ and $pkg->{variant_default} = 1;
230 /^Build-Only: \s*(.+)\s*$/ and $pkg->{buildonly} = 1;
231 /^Build-Depends: \s*(.+)\s*$/ and $pkg->{builddepends} = [ split /\s+/, $1 ];
232 /^Build-Depends\/(\w+): \s*(.+)\s*$/ and $pkg->{"builddepends/$1"} = [ split /\s+/, $2 ];
233 /^Build-Types:\s*(.+)\s*$/ and $pkg->{buildtypes} = [ split /\s+/, $1 ];
234 /^Repository:\s*(.+?)\s*$/ and $pkg->{repository} = $1;
235 /^Category: \s*(.+)\s*$/ and do {
236 $pkg->{category} = $1;
237 defined $category{$1} or $category{$1} = {};
238 defined $category{$1}->{$src} or $category{$1}->{$src} = [];
239 push @{$category{$1}->{$src}}, $pkg;
240 };
241 /^Description: \s*(.*)\s*$/ and $pkg->{description} = "\t\t $1\n". get_multiline(*FILE, "\t\t ");
242 /^Type: \s*(.+)\s*$/ and do {
243 $pkg->{type} = [ split /\s+/, $1 ];
244 undef $pkg->{tristate};
245 foreach my $type (@{$pkg->{type}}) {
246 $type =~ /ipkg/ and $pkg->{tristate} = 1;
247 }
248 };
249 /^Config:\s*(.*)\s*$/ and $pkg->{config} = "$1\n".get_multiline(*FILE, "\t");
250 /^Prereq-Check:/ and $pkg->{prereq} = 1;
251 /^Preconfig:\s*(.+)\s*$/ and do {
252 my $pkgname = $pkg->{name};
253 $preconfig{$pkgname} or $preconfig{$pkgname} = {};
254 if (exists $preconfig{$pkgname}->{$1}) {
255 $preconfig = $preconfig{$pkgname}->{$1};
256 } else {
257 $preconfig = {
258 id => $1
259 };
260 $preconfig{$pkgname}->{$1} = $preconfig;
261 }
262 };
263 /^Preconfig-Type:\s*(.*?)\s*$/ and $preconfig->{type} = $1;
264 /^Preconfig-Label:\s*(.*?)\s*$/ and $preconfig->{label} = $1;
265 /^Preconfig-Default:\s*(.*?)\s*$/ and $preconfig->{default} = $1;
266 }
267 close FILE;
268 return 1;
269 }
270
271 1;