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