metadata.pl: do not strip whitespaces from multiline data
[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 clear_packages parse_package_metadata get_multiline);
6
7 our %package;
8 our %preconfig;
9 our %srcpackage;
10 our %category;
11 our %subdir;
12
13 sub get_multiline {
14         my $fh = shift;
15         my $prefix = shift;
16         my $str;
17         while (<$fh>) {
18                 last if /^@@/;
19                 $str .= (($_ and $prefix) ? $prefix . $_ : $_);
20         }
21
22         return $str ? $str : "";
23 }
24
25 sub clear_packages() {
26         %subdir = ();
27         %preconfig = ();
28         %package = ();
29         %srcpackage = ();
30         %category = ();
31 }
32
33 sub parse_package_metadata($) {
34         my $file = shift;
35         my $pkg;
36         my $makefile;
37         my $preconfig;
38         my $subdir;
39         my $src;
40
41         open FILE, "<$file" or do {
42                 warn "Cannot open '$file': $!\n";
43                 return undef;
44         };
45         while (<FILE>) {
46                 chomp;
47                 /^Source-Makefile: \s*((.+\/)([^\/]+)\/Makefile)\s*$/ and do {
48                         $makefile = $1;
49                         $subdir = $2;
50                         $src = $3;
51                         $subdir =~ s/^package\///;
52                         $subdir{$src} = $subdir;
53                         $srcpackage{$src} = [];
54                         undef $pkg;
55                 };
56                 next unless $src;
57                 /^Package:\s*(.+?)\s*$/ and do {
58                         $pkg = {};
59                         $pkg->{src} = $src;
60                         $pkg->{makefile} = $makefile;
61                         $pkg->{name} = $1;
62                         $pkg->{title} = "";
63                         $pkg->{default} = "m if ALL";
64                         $pkg->{depends} = [];
65                         $pkg->{builddepends} = [];
66                         $pkg->{buildtypes} = [];
67                         $pkg->{subdir} = $subdir;
68                         $pkg->{tristate} = 1;
69                         $package{$1} = $pkg;
70                         push @{$srcpackage{$src}}, $pkg;
71                 };
72                 /^Version: \s*(.+)\s*$/ and $pkg->{version} = $1;
73                 /^Title: \s*(.+)\s*$/ and $pkg->{title} = $1;
74                 /^Menu: \s*(.+)\s*$/ and $pkg->{menu} = $1;
75                 /^Submenu: \s*(.+)\s*$/ and $pkg->{submenu} = $1;
76                 /^Submenu-Depends: \s*(.+)\s*$/ and $pkg->{submenudep} = $1;
77                 /^Source: \s*(.+)\s*$/ and $pkg->{source} = $1;
78                 /^Default: \s*(.+)\s*$/ and $pkg->{default} = $1;
79                 /^Provides: \s*(.+)\s*$/ and do {
80                         my @vpkg = split /\s+/, $1;
81                         foreach my $vpkg (@vpkg) {
82                                 $package{$vpkg} or $package{$vpkg} = {
83                                         name => $vpkg,
84                                         vdepends => [],
85                                         src => $src,
86                                         subdir => $subdir,
87                                         makefile => $makefile
88                                 };
89                                 push @{$package{$vpkg}->{vdepends}}, $pkg->{name};
90                         }
91                 };
92                 /^Depends: \s*(.+)\s*$/ and $pkg->{depends} = [ split /\s+/, $1 ];
93                 /^Build-Only: \s*(.+)\s*$/ and $pkg->{buildonly} = 1;
94                 /^Build-Depends: \s*(.+)\s*$/ and $pkg->{builddepends} = [ split /\s+/, $1 ];
95                 /^Build-Depends\/(\w+): \s*(.+)\s*$/ and $pkg->{"builddepends/$1"} = [ split /\s+/, $2 ];
96                 /^Build-Types:\s*(.+)\s*$/ and $pkg->{buildtypes} = [ split /\s+/, $1 ];
97                 /^Category: \s*(.+)\s*$/ and do {
98                         $pkg->{category} = $1;
99                         defined $category{$1} or $category{$1} = {};
100                         defined $category{$1}->{$src} or $category{$1}->{$src} = [];
101                         push @{$category{$1}->{$src}}, $pkg;
102                 };
103                 /^Description: \s*(.*)\s*$/ and $pkg->{description} = "\t\t $1\n". get_multiline(*FILE, "\t\t ");
104                 /^Type: \s*(.+)\s*$/ and do {
105                         $pkg->{type} = [ split /\s+/, $1 ];
106                         undef $pkg->{tristate};
107                         foreach my $type (@{$pkg->{type}}) {
108                                 $type =~ /ipkg/ and $pkg->{tristate} = 1;
109                         }
110                 };
111                 /^Config:\s*(.*)\s*$/ and $pkg->{config} = "$1\n".get_multiline(*FILE, "\t");
112                 /^Prereq-Check:/ and $pkg->{prereq} = 1;
113                 /^Preconfig:\s*(.+)\s*$/ and do {
114                         my $pkgname = $pkg->{name};
115                         $preconfig{$pkgname} or $preconfig{$pkgname} = {};
116                         if (exists $preconfig{$pkgname}->{$1}) {
117                                 $preconfig = $preconfig{$pkgname}->{$1};
118                         } else {
119                                 $preconfig = {
120                                         id => $1
121                                 };
122                                 $preconfig{$pkgname}->{$1} = $preconfig;
123                         }
124                 };
125                 /^Preconfig-Type:\s*(.*?)\s*$/ and $preconfig->{type} = $1;
126                 /^Preconfig-Label:\s*(.*?)\s*$/ and $preconfig->{label} = $1;
127                 /^Preconfig-Default:\s*(.*?)\s*$/ and $preconfig->{default} = $1;
128         }
129         close FILE;
130         return 1;
131 }
132
133 1;