summaryrefslogtreecommitdiff
path: root/SConstruct
blob: 932b22f4eb6fdaa78154fe7c47899028090f9992 (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import os
import sys
import re
import string
from stat import *

package = 'lighttpd'
version = '1.4.40'

def checkCHeaders(autoconf, hdrs):
	p = re.compile('[^A-Z0-9]')
	for hdr in hdrs:
		if not hdr:
			continue
		_hdr = Split(hdr)
		if autoconf.CheckCHeader(_hdr):
			autoconf.env.Append(CPPFLAGS = [ '-DHAVE_' + p.sub('_', _hdr[-1].upper()) ])

def checkFuncs(autoconf, funcs):
	p = re.compile('[^A-Z0-9]')
	for func in funcs:
		if autoconf.CheckFunc(func):
			autoconf.env.Append(CPPFLAGS = [ '-DHAVE_' + p.sub('_', func.upper()) ])

def checkTypes(autoconf, types):
	p = re.compile('[^A-Z0-9]')
	for type in types:
		if autoconf.CheckType(type, '#include <sys/types.h>'):
			autoconf.env.Append(CPPFLAGS = [ '-DHAVE_' + p.sub('_', type.upper()) ])

def checkGmtOffInStructTm(context):
	source = """
#include <time.h>
int main() {
	struct tm a;
	a.tm_gmtoff = 0;
	return 0;
}
"""
	context.Message('Checking for tm_gmtoff in struct tm...')
	result = context.TryLink(source, '.c')
	context.Result(result)

	return result

def checkIPv6(context):
	source = """
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

int main() {
	struct sockaddr_in6 s; struct in6_addr t=in6addr_any; int i=AF_INET6; s; t.s6_addr[0] = 0;
	return 0;
}
"""
	context.Message('Checking for IPv6 support...')
	result = context.TryLink(source, '.c')
	context.Result(result)

	return result

def checkWeakSymbols(context):
	source = """
__attribute__((weak)) void __dummy(void *x) { }
int main() {
	void *x;
	__dummy(x);
}
"""
	context.Message('Checking for weak symbol support...')
	result = context.TryLink(source, '.c')
	context.Result(result)

	return result

def checkProgram(env, withname, progname):
	withname = 'with_' + withname
	binpath = None

	if env[withname] != 1:
		binpath = env[withname]
	else:
		prog = env.Detect(progname)
		if prog:
			binpath = env.WhereIs(prog)

	if binpath:
		mode = os.stat(binpath)[ST_MODE]
		if S_ISDIR(mode):
			print >> sys.stderr, "* error: path `%s' is a directory" % (binpath)
			env.Exit(-1)
		if not S_ISREG(mode):
			print >> sys.stderr, "* error: path `%s' is not a file or not exists" % (binpath)
			env.Exit(-1)

	if not binpath:
		print >> sys.stderr, "* error: can't find program `%s'" % (progname)
		env.Exit(-1)

	return binpath

VariantDir('sconsbuild/build', 'src', duplicate = 0)
VariantDir('sconsbuild/tests', 'tests', duplicate = 0)

vars = Variables() #('config.py')
vars.AddVariables(
	('prefix', 'prefix', '/usr/local'),
	('bindir', 'binary directory', '${prefix}/bin'),
	('sbindir', 'binary directory', '${prefix}/sbin'),
	('libdir', 'library directory', '${prefix}/lib'),
	PackageVariable('with_mysql', 'enable mysql support', 'no'),
	PackageVariable('with_xml', 'enable xml support', 'no'),
	PackageVariable('with_pcre', 'enable pcre support', 'yes'),
	PathVariable('CC', 'path to the c-compiler', None),
	BoolVariable('build_dynamic', 'enable dynamic build', 'yes'),
	BoolVariable('build_static', 'enable static build', 'no'),
	BoolVariable('build_fullstatic', 'enable fullstatic build', 'no'),
	BoolVariable('with_sqlite3', 'enable sqlite3 support', 'no'),
	BoolVariable('with_memcached', 'enable memcached support', 'no'),
	BoolVariable('with_fam', 'enable FAM/gamin support', 'no'),
	BoolVariable('with_openssl', 'enable openssl support', 'no'),
	BoolVariable('with_gzip', 'enable gzip compression', 'no'),
	BoolVariable('with_bzip2', 'enable bzip2 compression', 'no'),
	BoolVariable('with_lua', 'enable lua support for mod_cml', 'no'),
	BoolVariable('with_ldap', 'enable ldap auth support', 'no'))

env = Environment(
	ENV = os.environ,
	variables = vars,
	CPPPATH = Split('#sconsbuild/build')
)

env.Help(vars.GenerateHelpText(env))

if env.subst('${CC}') is not '':
	env['CC'] = env.subst('${CC}')

env['package'] = package
env['version'] = version
if env['CC'] == 'gcc':
	## we need x-open 6 and bsd 4.3 features
	env.Append(CCFLAGS = Split('-Wall -O2 -g -W -pedantic -Wunused -Wshadow -std=gnu99'))

# cache configure checks
if 1:
	autoconf = Configure(env, custom_tests = {
		'CheckGmtOffInStructTm': checkGmtOffInStructTm,
		'CheckIPv6': checkIPv6,
		'CheckWeakSymbols': checkWeakSymbols,
	})

	if 'CFLAGS' in os.environ:
		autoconf.env.Append(CCFLAGS = os.environ['CFLAGS'])
		print(">> Appending custom build flags : " + os.environ['CFLAGS'])

	if 'LDFLAGS' in os.environ:
		autoconf.env.Append(LINKFLAGS = os.environ['LDFLAGS'])
		print(">> Appending custom link flags : " + os.environ['LDFLAGS'])

	if 'LIBS' in os.environ:
		autoconf.env.Append(APPEND_LIBS = os.environ['LIBS'])
		print(">> Appending custom libraries : " + os.environ['LIBS'])
	else:
		autoconf.env.Append(APPEND_LIBS = '')

	autoconf.headerfile = "foo.h"
	checkCHeaders(autoconf, string.split("""
			arpa/inet.h
			crypt.h
			fcntl.h
			getopt.h
			inttypes.h
			netinet/in.h
			poll.h
			pwd.h
			stdint.h
			stdlib.h
			string.h
			sys/devpoll.h
			sys/epoll.h
			sys/event.h
			sys/filio.h
			sys/mman.h
			sys/poll.h
			sys/port.h
			sys/prctl.h
			sys/resource.h
			sys/select.h
			sys/sendfile.h
			sys/socket.h
			sys/time.h
			sys/time.h sys/types.h sys/resource.h
			sys/types.h netinet/in.h
			sys/types.h sys/event.h
			sys/types.h sys/mman.h
			sys/types.h sys/select.h
			sys/types.h sys/socket.h
			sys/types.h sys/uio.h
			sys/types.h sys/un.h
			sys/uio.h
			sys/un.h
			sys/wait.h
			syslog.h
			unistd.h
			winsock2.h""", "\n"))

	checkFuncs(autoconf, Split('fork stat lstat strftime dup2 getcwd inet_ntoa inet_ntop memset mmap munmap strchr \
			strdup strerror strstr strtol sendfile getopt socket \
			gethostbyname poll epoll_ctl getrlimit chroot \
			getuid select signal pathconf madvise prctl\
			writev sigaction sendfile64 send_file kqueue port_create localtime_r posix_fadvise issetugid inet_pton \
			memset_s explicit_bzero'))

	checkTypes(autoconf, Split('pid_t size_t off_t'))

	autoconf.env.Append( LIBSQLITE3 = '', LIBXML2 = '', LIBMYSQL = '', LIBZ = '',
		LIBBZ2 = '', LIBCRYPT = '', LIBMEMCACHED = '', LIBFCGI = '', LIBPCRE = '',
		LIBLDAP = '', LIBLBER = '', LIBLUA = '', LIBDL = '', LIBUUID = '')

	if env['with_fam']:
		if autoconf.CheckLibWithHeader('fam', 'fam.h', 'C'):
			autoconf.env.Append(CPPFLAGS = [ '-DHAVE_FAM_H', '-DHAVE_LIBFAM' ], LIBS = 'fam')
			checkFuncs(autoconf, ['FAMNoExists']);

	if autoconf.CheckLib('crypt'):
		autoconf.env.Append(CPPFLAGS = [ '-DHAVE_LIBCRYPT' ], LIBCRYPT = 'crypt')
		oldlib = env['LIBS']
		env['LIBS'] += ['crypt']
		checkFuncs(autoconf, ['crypt', 'crypt_r']);
		env['LIBS'] = oldlib
	else:
		checkFuncs(autoconf, ['crypt', 'crypt_r']);

	if autoconf.CheckLibWithHeader('uuid', 'uuid/uuid.h', 'C'):
		autoconf.env.Append(CPPFLAGS = [ '-DHAVE_UUID_UUID_H', '-DHAVE_LIBUUID' ], LIBUUID = 'uuid')

	if env['with_openssl']:
		if autoconf.CheckLibWithHeader('ssl', 'openssl/ssl.h', 'C'):
			autoconf.env.Append(CPPFLAGS = [ '-DHAVE_OPENSSL_SSL_H', '-DHAVE_LIBSSL'] , LIBS = [ 'ssl', 'crypto' ])

	if env['with_gzip']:
		if autoconf.CheckLibWithHeader('z', 'zlib.h', 'C'):
			autoconf.env.Append(CPPFLAGS = [ '-DHAVE_ZLIB_H', '-DHAVE_LIBZ' ], LIBZ = 'z')

	if env['with_ldap']:
		if autoconf.CheckLibWithHeader('ldap', 'ldap.h', 'C'):
			autoconf.env.Append(CPPFLAGS = [ '-DHAVE_LDAP_H', '-DHAVE_LIBLDAP' ], LIBLDAP = 'ldap')
		if autoconf.CheckLibWithHeader('lber', 'lber.h', 'C'):
			autoconf.env.Append(CPPFLAGS = [ '-DHAVE_LBER_H', '-DHAVE_LIBLBER' ], LIBLBER = 'lber')

	if env['with_bzip2']:
		if autoconf.CheckLibWithHeader('bz2', 'bzlib.h', 'C'):
			autoconf.env.Append(CPPFLAGS = [ '-DHAVE_BZLIB_H', '-DHAVE_LIBBZ2' ], LIBBZ2 = 'bz2')

	if env['with_memcached']:
		if autoconf.CheckLibWithHeader('memcached', 'libmemcached/memcached.h', 'C'):
			autoconf.env.Append(CPPFLAGS = [ '-DUSE_MEMCACHED' ], LIBMEMCACHED = 'memcached')

	if env['with_sqlite3']:
		if autoconf.CheckLibWithHeader('sqlite3', 'sqlite3.h', 'C'):
			autoconf.env.Append(CPPFLAGS = [ '-DHAVE_SQLITE3_H', '-DHAVE_LIBSQLITE3' ], LIBSQLITE3 = 'sqlite3')

	ol = env['LIBS']
	if autoconf.CheckLibWithHeader('fcgi', 'fastcgi.h', 'C'):
		autoconf.env.Append(LIBFCGI = 'fcgi')
	env['LIBS'] = ol

	ol = env['LIBS']
	if autoconf.CheckLibWithHeader('dl', 'dlfcn.h', 'C'):
		autoconf.env.Append(LIBDL = 'dl')
	env['LIBS'] = ol

	if autoconf.CheckType('socklen_t', '#include <unistd.h>\n#include <sys/socket.h>\n#include <sys/types.h>'):
		autoconf.env.Append(CPPFLAGS = [ '-DHAVE_SOCKLEN_T' ])

	if autoconf.CheckType('struct sockaddr_storage', '#include <sys/socket.h>\n'):
		autoconf.env.Append(CPPFLAGS = [ '-DHAVE_STRUCT_SOCKADDR_STORAGE' ])

	if autoconf.CheckGmtOffInStructTm():
		autoconf.env.Append(CPPFLAGS = [ '-DHAVE_STRUCT_TM_GMTOFF' ])

	if autoconf.CheckIPv6():
		autoconf.env.Append(CPPFLAGS = [ '-DHAVE_IPV6' ])

	if autoconf.CheckWeakSymbols():
		autoconf.env.Append(CPPFLAGS = [ '-DHAVE_WEAK_SYMBOLS' ])

	env = autoconf.Finish()

def TryLua(env, name):
	result = False
	oldlibs = env['LIBS']
	try:
		print("Searching for lua: " + name + " >= 5.0")
		env.ParseConfig("pkg-config '" + name + " >= 5.0' --cflags --libs")
		env.Append(LIBLUA = env['LIBS'][len(oldlibs):])
		env.Append(CPPFLAGS = [ '-DHAVE_LUA_H' ])
		result = True
	except:
		pass
	env['LIBS'] = oldlibs
	return result

if env['with_lua']:
	found_lua = False
	for lua_name in ['lua5.3', 'lua-5.3', 'lua5.2', 'lua-5.2', 'lua5.1', 'lua-5.1', 'lua']:
		if TryLua(env, lua_name):
			found_lua = True
			break
	if not found_lua:
		raise RuntimeError("Couldn't find any lua implementation")

if env['with_pcre']:
	pcre_config = checkProgram(env, 'pcre', 'pcre-config')
	env.ParseConfig(pcre_config + ' --cflags --libs')
	env.Append(CPPFLAGS = [ '-DHAVE_PCRE_H', '-DHAVE_LIBPCRE' ], LIBPCRE = 'pcre')

if env['with_xml']:
	xml2_config = checkProgram(env, 'xml', 'xml2-config')
	oldlib = env['LIBS']
	env['LIBS'] = []
	env.ParseConfig(xml2_config + ' --cflags --libs')
	env.Append(CPPFLAGS = [ '-DHAVE_LIBXML_H', '-DHAVE_LIBXML2' ], LIBXML2 = env['LIBS'])
	env['LIBS'] = oldlib

if env['with_mysql']:
	mysql_config = checkProgram(env, 'mysql', 'mysql_config')
	oldlib = env['LIBS']
	env['LIBS'] = []
	env.ParseConfig(mysql_config + ' --cflags --libs')
	env.Append(CPPFLAGS = [ '-DHAVE_MYSQL_H', '-DHAVE_LIBMYSQL' ], LIBMYSQL = 'mysqlclient')
	env['LIBS'] = oldlib

if re.compile("cygwin|mingw").search(env['PLATFORM']):
	env.Append(COMMON_LIB = 'bin')
elif re.compile("darwin|aix").search(env['PLATFORM']):
	env.Append(COMMON_LIB = 'lib')
else:
	env.Append(COMMON_LIB = False)

versions = string.split(version, '.')
version_id = int(versions[0]) << 16 | int(versions[1]) << 8 | int(versions[2])
env.Append(CPPFLAGS = [
		'-DLIGHTTPD_VERSION_ID=' + hex(version_id),
		'-DPACKAGE_NAME=\\"' + package + '\\"',
		'-DPACKAGE_VERSION=\\"' + version + '\\"',
		'-DLIBRARY_DIR="\\"${libdir}\\""',
		'-D_FILE_OFFSET_BITS=64', '-D_LARGEFILE_SOURCE', '-D_LARGE_FILES'
		] )

SConscript('src/SConscript', exports = 'env', variant_dir = 'sconsbuild/build', duplicate = 0)
SConscript('tests/SConscript', exports = 'env', variant_dir = 'sconsbuild/tests')