7ad6600a8c4510053d215830dc49734c462612ed
[ira/wip.git] / source / build / smb_build / config_mk.pm
1 # Samba Build System
2 # - config.mk parsing functions
3 #
4 #  Copyright (C) Stefan (metze) Metzmacher 2004
5 #  Copyright (C) Jelmer Vernooij 2005
6 #  Released under the GNU GPL
7 #
8
9 package smb_build::config_mk;
10 use smb_build::input;
11 use File::Basename;
12
13 use strict;
14
15 my $section_types = {
16         "EXT_LIB" => {
17                 "LIBS"                  => "list",
18                 "CFLAGS"                => "list",
19                 "CPPFLAGS"              => "list",
20                 "LDFLAGS"               => "list",
21                 },
22         "PYTHON" => {
23                 SWIG_FILE => "string",
24                 "PRIVATE_DEPENDENCIES"  => "list",
25                 "PUBLIC_DEPENDENCIES"   => "list",
26                 "OBJ_FILES" => "list",
27                 "ENABLE"                => "bool",
28                 "LDFLAGS"               => "list",
29         },
30         "SUBSYSTEM" => {
31                 "OBJ_FILES"             => "list",
32
33                 "PRIVATE_DEPENDENCIES"  => "list",
34                 "PUBLIC_DEPENDENCIES"   => "list",
35
36                 "ENABLE"                => "bool",
37
38                 "MANPAGE"               => "string",
39
40                 "PUBLIC_PROTO_HEADER"   => "string",
41                 "PRIVATE_PROTO_HEADER"  => "string",
42
43                 "PUBLIC_HEADERS"        => "list",
44
45                 "CFLAGS"                => "list",
46                 "LDFLAGS"               => "list",
47                 "STANDARD_VISIBILITY"   => "string",
48                 "INIT_FUNCTION_SENTINEL" => "string"
49                 },
50         "MODULE" => {
51                 "SUBSYSTEM"             => "string",
52
53                 "INIT_FUNCTION"         => "string",
54                 "OBJ_FILES"             => "list",
55
56                 "PRIVATE_DEPENDENCIES"  => "list",
57
58                 "ALIASES" => "list",
59
60                 "ENABLE"                => "bool",
61
62                 "OUTPUT_TYPE"           => "list",
63
64                 "MANPAGE"               => "string",
65                 "PRIVATE_PROTO_HEADER"  => "string",
66
67                 "CFLAGS"                => "list"
68                 },
69         "BINARY" => {
70                 "OBJ_FILES"             => "list",
71
72                 "PRIVATE_DEPENDENCIES"  => "list",
73
74                 "ENABLE"                => "bool",
75
76                 "MANPAGE"               => "string",
77                 "INSTALLDIR"            => "string",
78                 "PRIVATE_PROTO_HEADER"  => "string",
79
80                 "CFLAGS"                => "list",
81                 "LDFLAGS"               => "list",
82                 "STANDARD_VISIBILITY"   => "string",
83
84                 "USE_HOSTCC"            => "bool"
85                 },
86         "LIBRARY" => {
87                 "VERSION"               => "string",
88                 "SO_VERSION"            => "string",
89                 "LIBRARY_REALNAME" => "string",
90                 
91                 "INIT_FUNCTION_TYPE"    => "string",
92                 "INIT_FUNCTION_SENTINEL" => "string",
93                 "OUTPUT_TYPE"           => "list",
94
95                 "OBJ_FILES"             => "list",
96
97                 "DESCRIPTION"           => "string",
98
99                 "PRIVATE_DEPENDENCIES"  => "list",
100                 "PUBLIC_DEPENDENCIES"   => "list",
101
102                 "ENABLE"                => "bool",
103
104                 "MANPAGE"               => "string",
105
106                 "PUBLIC_HEADERS"        => "list",
107
108                 "PUBLIC_PROTO_HEADER"   => "string",
109                 "PRIVATE_PROTO_HEADER"  => "string",
110
111                 "CFLAGS"                => "list",
112                 "LDFLAGS"               => "list",
113                 "STANDARD_VISIBILITY"   => "string"
114                 }
115 };
116
117 use vars qw(@parsed_files);
118
119 @parsed_files = ();
120
121 sub _read_config_file
122 {
123         use File::Basename;
124         use Cwd;
125
126         my $srcdir = shift;
127         my $builddir = shift;
128         my $filename = shift;
129         my @dirlist;
130
131         # We need to change our working directory because config.mk files can
132         # give shell commands as the argument to "include". These shell
133         # commands can take arguments that are relative paths and we don't have
134         # a way of sensibly rewriting these.
135         my $cwd = getcwd;
136         chomp $cwd;
137
138         if ($srcdir ne $builddir) {
139                 # Push the builddir path on the front, so we prefer builddir
140                 # to srcdir when the file exists in both.
141                 @dirlist = ($builddir, $srcdir);
142         } else {
143                 @dirlist = ($srcdir);
144         }
145
146         foreach my $d (@dirlist) {
147                 my @lines;
148                 my $basedir;
149
150                 chdir $cwd;
151                 chdir $d;
152
153                 # We need to catch the exception from open in the case where
154                 # the filename is actually a shell pipeline. Why is this
155                 # different to opening a regular file? Because this is perl!
156                 eval {
157                         open(CONFIG_MK, "./$filename");
158                         @lines = <CONFIG_MK>;
159                         close(CONFIG_MK);
160                 };
161
162                 chdir $cwd;
163                 next unless (@lines);
164
165                 # I blame abartlett for this crazy hack -- jpeach
166                 if ($filename =~ /\|$/) {
167                         $basedir = $builddir;
168                 } else {
169                         $basedir = dirname($filename);
170                         push(@parsed_files, $filename);
171                 }
172                 $basedir =~ s!^($builddir|$srcdir)[/]!!;
173                 return ($filename, $basedir, @lines);
174         }
175
176         chdir $cwd;
177         return;
178 }
179
180 ###########################################################
181 # The parsing function which parses the file
182 #
183 # $result = _parse_config_mk($input, $srcdir, $builddir, $filename)
184 #
185 # $filename -   the path of the config.mk file
186 #               which should be parsed
187 sub run_config_mk($$$$)
188 {
189         sub run_config_mk($$$$);
190         my ($input, $srcdir, $builddir, $filename) = @_;
191         my $result;
192         my $linenum = -1;
193         my $infragment = 0;
194         my $section = "GLOBAL";
195         my $makefile = "";
196
197         my $basedir;
198
199         my $parsing_file;
200         my @lines;
201
202         $ENV{builddir} = $builddir;
203         $ENV{srcdir} = $srcdir;
204
205         ($parsing_file, $basedir, @lines) =
206             _read_config_file($srcdir, $builddir, $filename);
207
208         die ("$0: can't open '$filename'")
209                 unless ($parsing_file and $basedir and @lines);
210
211         my $line = "";
212         my $prev = "";
213
214         # Emit a line that lets us match up final makefile output with the
215         # corresponding input files. The curlies are so you can match the
216         # BEGIN/END pairs in a text editor.
217         $makefile .= "# BEGIN{ $parsing_file\n";
218
219         foreach (@lines) {
220                 $linenum++;
221
222                 # lines beginning with '#' are ignored
223                 next if (/^\#.*$/);
224                 
225                 if (/^(.*)\\$/) {
226                         $prev .= $1;
227                         next;
228                 } else {
229                         $line = "$prev$_";
230                         $prev = "";
231                 }
232
233                 if ($line =~ /^\[([-a-zA-Z0-9_:]+)\][\t ]*$/) 
234                 {
235                         $section = $1;
236                         $infragment = 0;
237                         next;
238                 }
239
240                 # include
241                 if ($line =~ /^include (.*)$/) {
242                         my $subfile= $1;
243                         my $subdir = dirname($filename);
244                         $subdir =~ s/^\.$//g;
245                         $subdir =~ s/^\.\///g;
246                         $subdir .= "/" if ($subdir ne "");
247                         $makefile .= run_config_mk($input, $srcdir, $builddir, $subdir.$subfile);
248                         next;
249                 }
250
251                 # empty line
252                 if ($line =~ /^[ \t]*$/) {
253                         $section = "GLOBAL";
254                         if ($infragment) { $makefile.="\n"; }
255                         next;
256                 }
257
258                 # global stuff is considered part of the makefile
259                 if ($section eq "GLOBAL") {
260                         if (!$infragment) { $makefile.="\n"; }
261                         $makefile .= $line;
262                         $infragment = 1;
263                         next;
264                 }
265
266                 
267                 # Assignment
268                 if ($line =~ /^([a-zA-Z0-9_]+)[\t ]*=(.*)$/) {
269                         $result->{$section}{$1}{VAL} = $2;
270                         $result->{$section}{$1}{KEY} = $1;
271                 
272                         next;
273                 }
274
275                 die("$parsing_file:$linenum: Bad line while parsing $parsing_file");
276         }
277
278         $makefile .= "# }END $parsing_file\n";
279
280         foreach my $section (keys %{$result}) {
281                 my ($type, $name) = split(/::/, $section, 2);
282
283                 my $sectype = $section_types->{$type};
284                 if (not defined($sectype)) {
285                         die($parsing_file.":[".$section."] unknown section type \"".$type."\"!");
286                 }
287
288                 $input->{$name}{NAME} = $name;
289                 $input->{$name}{TYPE} = $type;
290                 $input->{$name}{MK_FILE} = $parsing_file;
291                 $input->{$name}{BASEDIR} = $basedir;
292
293                 foreach my $key (values %{$result->{$section}}) {
294                         $key->{VAL} = smb_build::input::strtrim($key->{VAL});
295                         my $vartype = $sectype->{$key->{KEY}};
296                         if (not defined($vartype)) {
297                                 die($parsing_file.":[".$section."]: unknown attribute type \"$key->{KEY}\"!");
298                         }
299                         if ($vartype eq "string") {
300                                 $input->{$name}{$key->{KEY}} = $key->{VAL};
301                         } elsif ($vartype eq "list") {
302                                 $input->{$name}{$key->{KEY}} = [smb_build::input::str2array($key->{VAL})];
303                         } elsif ($vartype eq "bool") {
304                                 if (($key->{VAL} ne "YES") and ($key->{VAL} ne "NO")) {
305                                         die("Invalid value for bool attribute $key->{KEY}: $key->{VAL} in section $section");
306                                 }
307                                 $input->{$name}{$key->{KEY}} = $key->{VAL};
308                         }
309                 }
310         }
311
312         return $makefile;
313 }
314
315 1;