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