summaryrefslogtreecommitdiff
path: root/source4/build/smb_build/config_mk.pm
blob: 7b23e8393435e35b57a1737c9af3222ab109575b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# Samba Build System
# - config.mk parsing functions
#
#  Copyright (C) Stefan (metze) Metzmacher 2004
#  Copyright (C) Jelmer Vernooij 2005
#  Released under the GNU GPL
#

package smb_build::config_mk;
use smb_build::input;
use File::Basename;

use strict;

my $section_types = {
	"EXT_LIB" => {
		"LIBS"			=> "list",
		"CFLAGS"		=> "list",
		"CPPFLAGS"		=> "list",
		"LDFLAGS"		=> "list",
		},
	"PYTHON" => {
		SWIG_FILE => "string",
		"PRIVATE_DEPENDENCIES"	=> "list",
		"PUBLIC_DEPENDENCIES"	=> "list",
		"ENABLE"		=> "bool",
		"LDFLAGS"		=> "list",
		"CFLAGS"		=> "list",
	},
	"SUBSYSTEM" => {
		"PRIVATE_DEPENDENCIES"	=> "list",
		"PUBLIC_DEPENDENCIES"	=> "list",

		"ENABLE"		=> "bool",

		"PRIVATE_PROTO_HEADER"	=> "string",

		"CFLAGS"		=> "list",
		"LDFLAGS"		=> "list",
		"STANDARD_VISIBILITY"	=> "string",
		"INIT_FUNCTION_SENTINEL" => "string"
		},
	"MODULE" => {
		"SUBSYSTEM"		=> "string",

		"INIT_FUNCTION"		=> "string",

		"PRIVATE_DEPENDENCIES"	=> "list",

		"ALIASES" => "list",

		"ENABLE"		=> "bool",

		"OUTPUT_TYPE"		=> "list",

		"PRIVATE_PROTO_HEADER"	=> "string",

		"CFLAGS"		=> "list"
		},
	"BINARY" => {

		"PRIVATE_DEPENDENCIES"	=> "list",

		"ENABLE"		=> "bool",

		"INSTALLDIR"		=> "string",
		"PRIVATE_PROTO_HEADER"	=> "string",

		"CFLAGS"		=> "list",
		"LDFLAGS"		=> "list",
		"STANDARD_VISIBILITY"	=> "string",

		"USE_HOSTCC"		=> "bool"
		},
	"LIBRARY" => {
		"LIBRARY_REALNAME" => "string",

		"INIT_FUNCTION_TYPE"	=> "string",
		"INIT_FUNCTION_SENTINEL" => "string",
		"OUTPUT_TYPE"		=> "list",

		"PRIVATE_DEPENDENCIES"	=> "list",
		"PUBLIC_DEPENDENCIES"	=> "list",

		"ENABLE"		=> "bool",

		"PRIVATE_PROTO_HEADER"	=> "string",

		"CFLAGS"		=> "list",
		"LDFLAGS"		=> "list",
		"STANDARD_VISIBILITY"	=> "string"
		}
};

use vars qw(@parsed_files);

@parsed_files = ();

sub _read_config_file
{
	use File::Basename;
	use Cwd;

	my $srcdir = shift;
	my $builddir = shift;
	my $filename = shift;
	my @dirlist;

	# We need to change our working directory because config.mk files can
	# give shell commands as the argument to "include". These shell
	# commands can take arguments that are relative paths and we don't have
	# a way of sensibly rewriting these.
	my $cwd = getcwd;
	chomp $cwd;

	if ($srcdir ne $builddir) {
		# Push the builddir path on the front, so we prefer builddir
		# to srcdir when the file exists in both.
		@dirlist = ($builddir, $srcdir);
	} else {
		@dirlist = ($srcdir);
	}

	foreach my $d (@dirlist) {
		my @lines;
		my $basedir;

		chdir $cwd;
		chdir $d;

		# We need to catch the exception from open in the case where
		# the filename is actually a shell pipeline. Why is this
		# different to opening a regular file? Because this is perl!
		eval {
			open(CONFIG_MK, "./$filename");
			@lines = <CONFIG_MK>;
			close(CONFIG_MK);
		};

		chdir $cwd;
		next unless (@lines);

		# I blame abartlett for this crazy hack -- jpeach
		if ($filename =~ /\|$/) {
			$basedir = $builddir;
		} else {
			$basedir = dirname($filename);
			push(@parsed_files, $filename);
		}
		$basedir =~ s!^($builddir|$srcdir)[/]!!;
		return ($filename, $basedir, @lines);
	}

	chdir $cwd;
	return;
}

###########################################################
# The parsing function which parses the file
#
# $result = _parse_config_mk($input, $srcdir, $builddir, $filename)
#
# $filename -	the path of the config.mk file
#		which should be parsed
sub run_config_mk($$$$)
{
	sub run_config_mk($$$$);
	my ($input, $srcdir, $builddir, $filename) = @_;
	my $result;
	my $linenum = -1;
	my $infragment = 0;
	my $section = "GLOBAL";
	my $makefile = "";

	my $basedir;

	my $parsing_file;
	my @lines;

	$ENV{builddir} = $builddir;
	$ENV{srcdir} = $srcdir;

	($parsing_file, $basedir, @lines) =
	    _read_config_file($srcdir, $builddir, $filename);

	die ("$0: can't open '$filename'")
		unless ($parsing_file and $basedir and @lines);

	my $line = "";
	my $prev = "";

	# Emit a line that lets us match up final makefile output with the
	# corresponding input files. The curlies are so you can match the
	# BEGIN/END pairs in a text editor.
	$makefile .= "# BEGIN{ $parsing_file\n";

	foreach (@lines) {
		$linenum++;

		# lines beginning with '#' are ignored
		next if (/^\#.*$/);
		
		if (/^(.*)\\$/) {
			$prev .= $1;
			next;
		} else {
			$line = "$prev$_";
			$prev = "";
		}

		if ($line =~ /^\[([-a-zA-Z0-9_:]+)\][\t ]*$/) 
		{
			$section = $1;
			$infragment = 0;

			$result->{$section}{EXISTS}{KEY} = "EXISTS";
			$result->{$section}{EXISTS}{VAL} = 1;
			next;
		}

		# include
		if ($line =~ /^mkinclude (.*)$/) {
			my $subfile= $1;
			my $subdir = dirname($filename);
			$subdir =~ s/^\.$//g;
			$subdir =~ s/^\.\///g;
			$subdir .= "/" if ($subdir ne "");
			$makefile .= "basedir := $subdir\n";
			$makefile .= run_config_mk($input, $srcdir, $builddir, $subdir.$subfile);
			next;
		}

		# empty line
		if ($line =~ /^[ \t]*$/) {
			$section = "GLOBAL";
			if ($infragment) { $makefile.="\n"; }
			next;
		}

		# global stuff is considered part of the makefile
		if ($section eq "GLOBAL") {
			if (!$infragment) { $makefile.="\n"; }
			$makefile .= $line;
			$infragment = 1;
			next;
		}
		
		# Assignment
		if ($line =~ /^([a-zA-Z0-9_]+)[\t ]*=(.*)$/) {
			$result->{$section}{$1}{VAL} = $2;
			$result->{$section}{$1}{KEY} = $1;
		
			next;
		}

		die("$parsing_file:$linenum: Bad line while parsing $parsing_file");
	}

	$makefile .= "# }END $parsing_file\n";

	foreach my $section (keys %{$result}) {
		my ($type, $name) = split(/::/, $section, 2);

		my $sectype = $section_types->{$type};
		if (not defined($sectype)) {
			die($parsing_file.":[".$section."] unknown section type \"".$type."\"!");
		}

		$input->{$name}{NAME} = $name;
		$input->{$name}{TYPE} = $type;
		$input->{$name}{MK_FILE} = $parsing_file;
		$input->{$name}{BASEDIR} = $basedir;

		foreach my $key (values %{$result->{$section}}) {
			next if ($key->{KEY} eq "EXISTS");
			$key->{VAL} = smb_build::input::strtrim($key->{VAL});
			my $vartype = $sectype->{$key->{KEY}};
			if (not defined($vartype)) {
				die($parsing_file.":[".$section."]: unknown attribute type \"$key->{KEY}\"!");
			}
			if ($vartype eq "string") {
				$input->{$name}{$key->{KEY}} = $key->{VAL};
			} elsif ($vartype eq "list") {
				$input->{$name}{$key->{KEY}} = [smb_build::input::str2array($key->{VAL})];
			} elsif ($vartype eq "bool") {
				if (($key->{VAL} ne "YES") and ($key->{VAL} ne "NO")) {
					die("Invalid value for bool attribute $key->{KEY}: $key->{VAL} in section $section");
				}
				$input->{$name}{$key->{KEY}} = $key->{VAL};
			}
		}
	}

	return $makefile;
}

1;