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