Add support for Ubiquiti Rocket M XW devices
[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         my $makefile;
38
39         open FILE, "<$file" or do {
40                 warn "Can't open file '$file': $!\n";
41                 return;
42         };
43         while (<FILE>) {
44                 chomp;
45                 /^Source-Makefile: \s*((.+\/)([^\/]+)\/Makefile)\s*$/ and $makefile = $1;
46                 /^Target:\s*(.+)\s*$/ and do {
47                         my $name = $1;
48                         $target = {
49                                 id => $name,
50                                 board => $name,
51                                 makefile => $makefile,
52                                 boardconf => confstr($name),
53                                 conf => confstr($name),
54                                 profiles => [],
55                                 features => [],
56                                 depends => [],
57                                 subtargets => []
58                         };
59                         push @target, $target;
60                         $target{$name} = $target;
61                         if ($name =~ /([^\/]+)\/([^\/]+)/) {
62                                 push @{$target{$1}->{subtargets}}, $2;
63                                 $target->{board} = $1;
64                                 $target->{boardconf} = confstr($1);
65                                 $target->{subtarget} = 1;
66                                 $target->{parent} = $target{$1};
67                         }
68                 };
69                 /^Target-Name:\s*(.+)\s*$/ and $target->{name} = $1;
70                 /^Target-Path:\s*(.+)\s*$/ and $target->{path} = $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
134         open FILE, "<$file" or do {
135                 warn "Cannot open '$file': $!\n";
136                 return undef;
137         };
138         while (<FILE>) {
139                 chomp;
140                 /^Source-Makefile: \s*((.+\/)([^\/]+)\/Makefile)\s*$/ and do {
141                         $makefile = $1;
142                         $subdir = $2;
143                         $src = $3;
144                         $subdir =~ s/^package\///;
145                         $subdir{$src} = $subdir;
146                         $srcpackage{$src} = [];
147                         $override = "";
148                         undef $pkg;
149                 };
150                 /^Override: \s*(.+?)\s*$/ and do {
151                         $override = $1;
152                         $overrides{$src} = 1;
153                 };
154                 next unless $src;
155                 /^Package:\s*(.+?)\s*$/ and do {
156                         undef $feature;
157                         $pkg = {};
158                         $pkg->{src} = $src;
159                         $pkg->{makefile} = $makefile;
160                         $pkg->{name} = $1;
161                         $pkg->{title} = "";
162                         $pkg->{depends} = [];
163                         $pkg->{mdepends} = [];
164                         $pkg->{builddepends} = [];
165                         $pkg->{buildtypes} = [];
166                         $pkg->{subdir} = $subdir;
167                         $pkg->{tristate} = 1;
168                         $pkg->{override} = $override;
169                         $package{$1} = $pkg;
170                         push @{$srcpackage{$src}}, $pkg;
171                 };
172                 /^Feature:\s*(.+?)\s*$/ and do {
173                         undef $pkg;
174                         $feature = {};
175                         $feature->{name} = $1;
176                         $feature->{priority} = 0;
177                 };
178                 $feature and do {
179                         /^Target-Name:\s*(.+?)\s*$/ and do {
180                                 $features{$1} or $features{$1} = [];
181                                 push @{$features{$1}}, $feature;
182                         };
183                         /^Target-Title:\s*(.+?)\s*$/ and $feature->{target_title} = $1;
184                         /^Feature-Priority:\s*(\d+)\s*$/ and $feature->{priority} = $1;
185                         /^Feature-Name:\s*(.+?)\s*$/ and $feature->{title} = $1;
186                         /^Feature-Description:/ and $feature->{description} = get_multiline(\*FILE, "\t\t\t");
187                         next;
188                 };
189                 next unless $pkg;
190                 /^Version: \s*(.+)\s*$/ and $pkg->{version} = $1;
191                 /^Title: \s*(.+)\s*$/ and $pkg->{title} = $1;
192                 /^Menu: \s*(.+)\s*$/ and $pkg->{menu} = $1;
193                 /^Submenu: \s*(.+)\s*$/ and $pkg->{submenu} = $1;
194                 /^Submenu-Depends: \s*(.+)\s*$/ and $pkg->{submenudep} = $1;
195                 /^Source: \s*(.+)\s*$/ and $pkg->{source} = $1;
196                 /^License: \s*(.+)\s*$/ and $pkg->{license} = $1;
197                 /^LicenseFiles: \s*(.+)\s*$/ and $pkg->{licensefiles} = $1;
198                 /^Default: \s*(.+)\s*$/ and $pkg->{default} = $1;
199                 /^Provides: \s*(.+)\s*$/ and do {
200                         my @vpkg = split /\s+/, $1;
201                         foreach my $vpkg (@vpkg) {
202                                 $package{$vpkg} or $package{$vpkg} = {
203                                         name => $vpkg,
204                                         vdepends => [],
205                                         src => $src,
206                                         subdir => $subdir,
207                                         makefile => $makefile
208                                 };
209                                 push @{$package{$vpkg}->{vdepends}}, $pkg->{name};
210                         }
211                 };
212                 /^Menu-Depends: \s*(.+)\s*$/ and $pkg->{mdepends} = [ split /\s+/, $1 ];
213                 /^Depends: \s*(.+)\s*$/ and $pkg->{depends} = [ split /\s+/, $1 ];
214                 /^Conflicts: \s*(.+)\s*$/ and $pkg->{conflicts} = [ split /\s+/, $1 ];
215                 /^Hidden: \s*(.+)\s*$/ and $pkg->{hidden} = 1;
216                 /^Build-Variant: \s*([\w\-]+)\s*/ and $pkg->{variant} = $1;
217                 /^Default-Variant: .*/ and $pkg->{variant_default} = 1;
218                 /^Build-Only: \s*(.+)\s*$/ and $pkg->{buildonly} = 1;
219                 /^Build-Depends: \s*(.+)\s*$/ and $pkg->{builddepends} = [ split /\s+/, $1 ];
220                 /^Build-Depends\/(\w+): \s*(.+)\s*$/ and $pkg->{"builddepends/$1"} = [ split /\s+/, $2 ];
221                 /^Build-Types:\s*(.+)\s*$/ and $pkg->{buildtypes} = [ split /\s+/, $1 ];
222                 /^Feed:\s*(.+?)\s*$/ and $pkg->{feed} = $1;
223                 /^Category: \s*(.+)\s*$/ and do {
224                         $pkg->{category} = $1;
225                         defined $category{$1} or $category{$1} = {};
226                         defined $category{$1}->{$src} or $category{$1}->{$src} = [];
227                         push @{$category{$1}->{$src}}, $pkg;
228                 };
229                 /^Description: \s*(.*)\s*$/ and $pkg->{description} = "\t\t $1\n". get_multiline(*FILE, "\t\t ");
230                 /^Type: \s*(.+)\s*$/ and do {
231                         $pkg->{type} = [ split /\s+/, $1 ];
232                         undef $pkg->{tristate};
233                         foreach my $type (@{$pkg->{type}}) {
234                                 $type =~ /ipkg/ and $pkg->{tristate} = 1;
235                         }
236                 };
237                 /^Config:\s*(.*)\s*$/ and $pkg->{config} = "$1\n".get_multiline(*FILE, "\t");
238                 /^Prereq-Check:/ and $pkg->{prereq} = 1;
239                 /^Preconfig:\s*(.+)\s*$/ and do {
240                         my $pkgname = $pkg->{name};
241                         $preconfig{$pkgname} or $preconfig{$pkgname} = {};
242                         if (exists $preconfig{$pkgname}->{$1}) {
243                                 $preconfig = $preconfig{$pkgname}->{$1};
244                         } else {
245                                 $preconfig = {
246                                         id => $1
247                                 };
248                                 $preconfig{$pkgname}->{$1} = $preconfig;
249                         }
250                 };
251                 /^Preconfig-Type:\s*(.*?)\s*$/ and $preconfig->{type} = $1;
252                 /^Preconfig-Label:\s*(.*?)\s*$/ and $preconfig->{label} = $1;
253                 /^Preconfig-Default:\s*(.*?)\s*$/ and $preconfig->{default} = $1;
254         }
255         close FILE;
256         return 1;
257 }
258
259 1;