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