build: allow packages with build variants to explicitly select a default variant
[openwrt/staging/chunkeey.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 clear_packages parse_package_metadata get_multiline);
6
7 our %package;
8 our %preconfig;
9 our %srcpackage;
10 our %category;
11 our %subdir;
12 our %features;
13
14 sub get_multiline {
15 my $fh = shift;
16 my $prefix = shift;
17 my $str;
18 while (<$fh>) {
19 last if /^@@/;
20 $str .= (($_ and $prefix) ? $prefix . $_ : $_);
21 }
22
23 return $str ? $str : "";
24 }
25
26 sub clear_packages() {
27 %subdir = ();
28 %preconfig = ();
29 %package = ();
30 %srcpackage = ();
31 %category = ();
32 %features = ();
33 }
34
35 sub parse_package_metadata($) {
36 my $file = shift;
37 my $pkg;
38 my $feature;
39 my $makefile;
40 my $preconfig;
41 my $subdir;
42 my $src;
43
44 open FILE, "<$file" or do {
45 warn "Cannot open '$file': $!\n";
46 return undef;
47 };
48 while (<FILE>) {
49 chomp;
50 /^Source-Makefile: \s*((.+\/)([^\/]+)\/Makefile)\s*$/ and do {
51 $makefile = $1;
52 $subdir = $2;
53 $src = $3;
54 $subdir =~ s/^package\///;
55 $subdir{$src} = $subdir;
56 $srcpackage{$src} = [];
57 undef $pkg;
58 };
59 next unless $src;
60 /^Package:\s*(.+?)\s*$/ and do {
61 undef $feature;
62 $pkg = {};
63 $pkg->{src} = $src;
64 $pkg->{makefile} = $makefile;
65 $pkg->{name} = $1;
66 $pkg->{title} = "";
67 $pkg->{depends} = [];
68 $pkg->{mdepends} = [];
69 $pkg->{builddepends} = [];
70 $pkg->{buildtypes} = [];
71 $pkg->{subdir} = $subdir;
72 $pkg->{tristate} = 1;
73 $package{$1} = $pkg;
74 push @{$srcpackage{$src}}, $pkg;
75 };
76 /^Feature:\s*(.+?)\s*$/ and do {
77 undef $pkg;
78 $feature = {};
79 $feature->{name} = $1;
80 $feature->{priority} = 0;
81 };
82 $feature and do {
83 /^Target-Name:\s*(.+?)\s*$/ and do {
84 $features{$1} or $features{$1} = [];
85 push @{$features{$1}}, $feature;
86 };
87 /^Target-Title:\s*(.+?)\s*$/ and $feature->{target_title} = $1;
88 /^Feature-Priority:\s*(\d+)\s*$/ and $feature->{priority} = $1;
89 /^Feature-Name:\s*(.+?)\s*$/ and $feature->{title} = $1;
90 /^Feature-Description:/ and $feature->{description} = get_multiline(\*FILE, "\t\t\t");
91 next;
92 };
93 next unless $pkg;
94 /^Version: \s*(.+)\s*$/ and $pkg->{version} = $1;
95 /^Title: \s*(.+)\s*$/ and $pkg->{title} = $1;
96 /^Menu: \s*(.+)\s*$/ and $pkg->{menu} = $1;
97 /^Submenu: \s*(.+)\s*$/ and $pkg->{submenu} = $1;
98 /^Submenu-Depends: \s*(.+)\s*$/ and $pkg->{submenudep} = $1;
99 /^Source: \s*(.+)\s*$/ and $pkg->{source} = $1;
100 /^Default: \s*(.+)\s*$/ and $pkg->{default} = $1;
101 /^Provides: \s*(.+)\s*$/ and do {
102 my @vpkg = split /\s+/, $1;
103 foreach my $vpkg (@vpkg) {
104 $package{$vpkg} or $package{$vpkg} = {
105 name => $vpkg,
106 vdepends => [],
107 src => $src,
108 subdir => $subdir,
109 makefile => $makefile
110 };
111 push @{$package{$vpkg}->{vdepends}}, $pkg->{name};
112 }
113 };
114 /^Menu-Depends: \s*(.+)\s*$/ and $pkg->{mdepends} = [ split /\s+/, $1 ];
115 /^Depends: \s*(.+)\s*$/ and $pkg->{depends} = [ split /\s+/, $1 ];
116 /^Hidden: \s*(.+)\s*$/ and $pkg->{hidden} = 1;
117 /^Build-Variant: \s*([\w\-]+)\s*/ and $pkg->{variant} = $1;
118 /^Default-Variant: .*/ and $pkg->{variant_default} = 1;
119 /^Build-Only: \s*(.+)\s*$/ and $pkg->{buildonly} = 1;
120 /^Build-Depends: \s*(.+)\s*$/ and $pkg->{builddepends} = [ split /\s+/, $1 ];
121 /^Build-Depends\/(\w+): \s*(.+)\s*$/ and $pkg->{"builddepends/$1"} = [ split /\s+/, $2 ];
122 /^Build-Types:\s*(.+)\s*$/ and $pkg->{buildtypes} = [ split /\s+/, $1 ];
123 /^Feed:\s*(.+?)\s*$/ and $pkg->{feed} = $1;
124 /^Category: \s*(.+)\s*$/ and do {
125 $pkg->{category} = $1;
126 defined $category{$1} or $category{$1} = {};
127 defined $category{$1}->{$src} or $category{$1}->{$src} = [];
128 push @{$category{$1}->{$src}}, $pkg;
129 };
130 /^Description: \s*(.*)\s*$/ and $pkg->{description} = "\t\t $1\n". get_multiline(*FILE, "\t\t ");
131 /^Type: \s*(.+)\s*$/ and do {
132 $pkg->{type} = [ split /\s+/, $1 ];
133 undef $pkg->{tristate};
134 foreach my $type (@{$pkg->{type}}) {
135 $type =~ /ipkg/ and $pkg->{tristate} = 1;
136 }
137 };
138 /^Config:\s*(.*)\s*$/ and $pkg->{config} = "$1\n".get_multiline(*FILE, "\t");
139 /^Prereq-Check:/ and $pkg->{prereq} = 1;
140 /^Preconfig:\s*(.+)\s*$/ and do {
141 my $pkgname = $pkg->{name};
142 $preconfig{$pkgname} or $preconfig{$pkgname} = {};
143 if (exists $preconfig{$pkgname}->{$1}) {
144 $preconfig = $preconfig{$pkgname}->{$1};
145 } else {
146 $preconfig = {
147 id => $1
148 };
149 $preconfig{$pkgname}->{$1} = $preconfig;
150 }
151 };
152 /^Preconfig-Type:\s*(.*?)\s*$/ and $preconfig->{type} = $1;
153 /^Preconfig-Label:\s*(.*?)\s*$/ and $preconfig->{label} = $1;
154 /^Preconfig-Default:\s*(.*?)\s*$/ and $preconfig->{default} = $1;
155 }
156 close FILE;
157 return 1;
158 }
159
160 1;