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