r26600: Provide dir variable with current basedir in Makefile.
[samba.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         $makefile .= "dir = $basedir\n";
219
220         foreach (@lines) {
221                 $linenum++;
222
223                 # lines beginning with '#' are ignored
224                 next if (/^\#.*$/);
225                 
226                 if (/^(.*)\\$/) {
227                         $prev .= $1;
228                         next;
229                 } else {
230                         $line = "$prev$_";
231                         $prev = "";
232                 }
233
234                 if ($line =~ /^\[([-a-zA-Z0-9_:]+)\][\t ]*$/) 
235                 {
236                         $section = $1;
237                         $infragment = 0;
238                         next;
239                 }
240
241                 # include
242                 if ($line =~ /^include (.*)$/) {
243                         my $subfile= $1;
244                         my $subdir = dirname($filename);
245                         $subdir =~ s/^\.$//g;
246                         $subdir =~ s/^\.\///g;
247                         $subdir .= "/" if ($subdir ne "");
248                         $makefile .= run_config_mk($input, $srcdir, $builddir, $subdir.$subfile);
249                         next;
250                 }
251
252                 # empty line
253                 if ($line =~ /^[ \t]*$/) {
254                         $section = "GLOBAL";
255                         if ($infragment) { $makefile.="\n"; }
256                         next;
257                 }
258
259                 # global stuff is considered part of the makefile
260                 if ($section eq "GLOBAL") {
261                         if (!$infragment) { $makefile.="\n"; }
262                         $makefile .= $line;
263                         $infragment = 1;
264                         next;
265                 }
266
267                 
268                 # Assignment
269                 if ($line =~ /^([a-zA-Z0-9_]+)[\t ]*=(.*)$/) {
270                         $result->{$section}{$1}{VAL} = $2;
271                         $result->{$section}{$1}{KEY} = $1;
272                 
273                         next;
274                 }
275
276                 die("$parsing_file:$linenum: Bad line while parsing $parsing_file");
277         }
278
279         $makefile .= "# }END $parsing_file\n";
280
281         foreach my $section (keys %{$result}) {
282                 my ($type, $name) = split(/::/, $section, 2);
283
284                 my $sectype = $section_types->{$type};
285                 if (not defined($sectype)) {
286                         die($parsing_file.":[".$section."] unknown section type \"".$type."\"!");
287                 }
288
289                 $input->{$name}{NAME} = $name;
290                 $input->{$name}{TYPE} = $type;
291                 $input->{$name}{MK_FILE} = $parsing_file;
292                 $input->{$name}{BASEDIR} = $basedir;
293
294                 foreach my $key (values %{$result->{$section}}) {
295                         $key->{VAL} = smb_build::input::strtrim($key->{VAL});
296                         my $vartype = $sectype->{$key->{KEY}};
297                         if (not defined($vartype)) {
298                                 die($parsing_file.":[".$section."]: unknown attribute type \"$key->{KEY}\"!");
299                         }
300                         if ($vartype eq "string") {
301                                 $input->{$name}{$key->{KEY}} = $key->{VAL};
302                         } elsif ($vartype eq "list") {
303                                 $input->{$name}{$key->{KEY}} = [smb_build::input::str2array($key->{VAL})];
304                         } elsif ($vartype eq "bool") {
305                                 if (($key->{VAL} ne "YES") and ($key->{VAL} ne "NO")) {
306                                         die("Invalid value for bool attribute $key->{KEY}: $key->{VAL} in section $section");
307                                 }
308                                 $input->{$name}{$key->{KEY}} = $key->{VAL};
309                         }
310                 }
311         }
312
313         return $makefile;
314 }
315
316 1;