target.mk: remove the unused Target-Path field
[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);
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 packages => []
88 };
89 push @{$target->{profiles}}, $profile;
90 };
91 /^Target-Profile-Name:\s*(.+)\s*$/ and $profile->{name} = $1;
92 /^Target-Profile-Packages:\s*(.*)\s*$/ and $profile->{packages} = [ split(/\s+/, $1) ];
93 /^Target-Profile-Description:\s*(.*)\s*/ and $profile->{desc} = get_multiline(*FILE);
94 /^Target-Profile-Config:/ and $profile->{config} = get_multiline(*FILE, "\t");
95 /^Target-Profile-Kconfig:/ and $profile->{kconfig} = 1;
96 }
97 close FILE;
98 foreach my $target (@target) {
99 if (@{$target->{subtargets}} > 0) {
100 $target->{profiles} = [];
101 next;
102 }
103 @{$target->{profiles}} > 0 or $target->{profiles} = [
104 {
105 id => 'Default',
106 name => 'Default',
107 packages => []
108 }
109 ];
110 }
111 return @target;
112 }
113
114 sub clear_packages() {
115 %subdir = ();
116 %preconfig = ();
117 %package = ();
118 %srcpackage = ();
119 %category = ();
120 %features = ();
121 %overrides = ();
122 }
123
124 sub parse_package_metadata($) {
125 my $file = shift;
126 my $pkg;
127 my $feature;
128 my $makefile;
129 my $preconfig;
130 my $subdir;
131 my $src;
132 my $override;
133 my %ignore = map { $_ => 1 } @ignore;
134
135 open FILE, "<$file" or do {
136 warn "Cannot open '$file': $!\n";
137 return undef;
138 };
139 while (<FILE>) {
140 chomp;
141 /^Source-Makefile: \s*((.+\/)([^\/]+)\/Makefile)\s*$/ and do {
142 $makefile = $1;
143 $subdir = $2;
144 $src = $3;
145 $subdir =~ s/^package\///;
146 $subdir{$src} = $subdir;
147 $srcpackage{$src} = [];
148 $override = "";
149 undef $pkg;
150 };
151 /^Override: \s*(.+?)\s*$/ and do {
152 $override = $1;
153 $overrides{$src} = 1;
154 };
155 next unless $src;
156 next if $ignore{$src};
157 /^Package:\s*(.+?)\s*$/ and do {
158 undef $feature;
159 $pkg = {};
160 $pkg->{src} = $src;
161 $pkg->{makefile} = $makefile;
162 $pkg->{name} = $1;
163 $pkg->{title} = "";
164 $pkg->{depends} = [];
165 $pkg->{mdepends} = [];
166 $pkg->{builddepends} = [];
167 $pkg->{buildtypes} = [];
168 $pkg->{subdir} = $subdir;
169 $pkg->{tristate} = 1;
170 $pkg->{override} = $override;
171 $package{$1} = $pkg;
172 push @{$srcpackage{$src}}, $pkg;
173 };
174 /^Feature:\s*(.+?)\s*$/ and do {
175 undef $pkg;
176 $feature = {};
177 $feature->{name} = $1;
178 $feature->{priority} = 0;
179 };
180 $feature and do {
181 /^Target-Name:\s*(.+?)\s*$/ and do {
182 $features{$1} or $features{$1} = [];
183 push @{$features{$1}}, $feature;
184 };
185 /^Target-Title:\s*(.+?)\s*$/ and $feature->{target_title} = $1;
186 /^Feature-Priority:\s*(\d+)\s*$/ and $feature->{priority} = $1;
187 /^Feature-Name:\s*(.+?)\s*$/ and $feature->{title} = $1;
188 /^Feature-Description:/ and $feature->{description} = get_multiline(\*FILE, "\t\t\t");
189 next;
190 };
191 next unless $pkg;
192 /^Version: \s*(.+)\s*$/ and $pkg->{version} = $1;
193 /^Title: \s*(.+)\s*$/ and $pkg->{title} = $1;
194 /^Menu: \s*(.+)\s*$/ and $pkg->{menu} = $1;
195 /^Submenu: \s*(.+)\s*$/ and $pkg->{submenu} = $1;
196 /^Submenu-Depends: \s*(.+)\s*$/ and $pkg->{submenudep} = $1;
197 /^Source: \s*(.+)\s*$/ and $pkg->{source} = $1;
198 /^License: \s*(.+)\s*$/ and $pkg->{license} = $1;
199 /^LicenseFiles: \s*(.+)\s*$/ and $pkg->{licensefiles} = $1;
200 /^Default: \s*(.+)\s*$/ and $pkg->{default} = $1;
201 /^Provides: \s*(.+)\s*$/ and do {
202 my @vpkg = split /\s+/, $1;
203 foreach my $vpkg (@vpkg) {
204 $package{$vpkg} or $package{$vpkg} = {
205 name => $vpkg,
206 vdepends => [],
207 src => $src,
208 subdir => $subdir,
209 makefile => $makefile
210 };
211 push @{$package{$vpkg}->{vdepends}}, $pkg->{name};
212 }
213 };
214 /^Menu-Depends: \s*(.+)\s*$/ and $pkg->{mdepends} = [ split /\s+/, $1 ];
215 /^Depends: \s*(.+)\s*$/ and $pkg->{depends} = [ split /\s+/, $1 ];
216 /^Conflicts: \s*(.+)\s*$/ and $pkg->{conflicts} = [ split /\s+/, $1 ];
217 /^Hidden: \s*(.+)\s*$/ and $pkg->{hidden} = 1;
218 /^Build-Variant: \s*([\w\-]+)\s*/ and $pkg->{variant} = $1;
219 /^Default-Variant: .*/ and $pkg->{variant_default} = 1;
220 /^Build-Only: \s*(.+)\s*$/ and $pkg->{buildonly} = 1;
221 /^Build-Depends: \s*(.+)\s*$/ and $pkg->{builddepends} = [ split /\s+/, $1 ];
222 /^Build-Depends\/(\w+): \s*(.+)\s*$/ and $pkg->{"builddepends/$1"} = [ split /\s+/, $2 ];
223 /^Build-Types:\s*(.+)\s*$/ and $pkg->{buildtypes} = [ split /\s+/, $1 ];
224 /^Repository:\s*(.+?)\s*$/ and $pkg->{repository} = $1;
225 /^Category: \s*(.+)\s*$/ and do {
226 $pkg->{category} = $1;
227 defined $category{$1} or $category{$1} = {};
228 defined $category{$1}->{$src} or $category{$1}->{$src} = [];
229 push @{$category{$1}->{$src}}, $pkg;
230 };
231 /^Description: \s*(.*)\s*$/ and $pkg->{description} = "\t\t $1\n". get_multiline(*FILE, "\t\t ");
232 /^Type: \s*(.+)\s*$/ and do {
233 $pkg->{type} = [ split /\s+/, $1 ];
234 undef $pkg->{tristate};
235 foreach my $type (@{$pkg->{type}}) {
236 $type =~ /ipkg/ and $pkg->{tristate} = 1;
237 }
238 };
239 /^Config:\s*(.*)\s*$/ and $pkg->{config} = "$1\n".get_multiline(*FILE, "\t");
240 /^Prereq-Check:/ and $pkg->{prereq} = 1;
241 /^Preconfig:\s*(.+)\s*$/ and do {
242 my $pkgname = $pkg->{name};
243 $preconfig{$pkgname} or $preconfig{$pkgname} = {};
244 if (exists $preconfig{$pkgname}->{$1}) {
245 $preconfig = $preconfig{$pkgname}->{$1};
246 } else {
247 $preconfig = {
248 id => $1
249 };
250 $preconfig{$pkgname}->{$1} = $preconfig;
251 }
252 };
253 /^Preconfig-Type:\s*(.*?)\s*$/ and $preconfig->{type} = $1;
254 /^Preconfig-Label:\s*(.*?)\s*$/ and $preconfig->{label} = $1;
255 /^Preconfig-Default:\s*(.*?)\s*$/ and $preconfig->{default} = $1;
256 }
257 close FILE;
258 return 1;
259 }
260
261 1;