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