1import os
2import sys
3import re
4import string
5from stat import *
6
7package = 'lighttpd'
8version = '1.4.32'
9
10def checkCHeaders(autoconf, hdrs):
11	p = re.compile('[^A-Z0-9]')
12	for hdr in hdrs:
13		if not hdr:
14			continue
15		_hdr = Split(hdr)
16 		if autoconf.CheckCHeader(_hdr):
17 			autoconf.env.Append(CPPFLAGS = [ '-DHAVE_' + p.sub('_', _hdr[-1].upper()) ])
18
19def checkFuncs(autoconf, funcs):
20	p = re.compile('[^A-Z0-9]')
21	for func in funcs:
22		if autoconf.CheckFunc(func):
23			autoconf.env.Append(CPPFLAGS = [ '-DHAVE_' + p.sub('_', func.upper()) ])
24
25def checkTypes(autoconf, types):
26	p = re.compile('[^A-Z0-9]')
27	for type in types:
28		if autoconf.CheckType(type, '#include <sys/types.h>'):
29			autoconf.env.Append(CPPFLAGS = [ '-DHAVE_' + p.sub('_', type.upper()) ])
30
31def checkProgram(env, withname, progname):
32	withname = 'with_' + withname
33	binpath = None
34
35	if env[withname] != 1:
36		binpath = env[withname]
37	else:
38		prog = env.Detect(progname)
39		if prog:
40			binpath = env.WhereIs(prog)
41
42	if binpath:
43		mode = os.stat(binpath)[ST_MODE]
44		if S_ISDIR(mode):
45			print >> sys.stderr, "* error: path `%s' is a directory" % (binpath)
46			env.Exit(-1)
47		if not S_ISREG(mode):
48			print >> sys.stderr, "* error: path `%s' is not a file or not exists" % (binpath)
49			env.Exit(-1)
50
51	if not binpath:
52		print >> sys.stderr, "* error: can't find program `%s'" % (progname)
53		env.Exit(-1)
54
55	return binpath
56
57def checkStructMember(context):
58	struct_member = """
59#include <time.h>
60int main() {
61	struct tm a;
62	a.tm_gmtoff = 0;
63	return 0;
64}
65"""
66	context.Message('Checking for tm_gmtoff in struct tm...')
67	result = context.TryLink(struct_member, '.c')
68	context.Result(result)
69
70	return result
71
72
73BuildDir('build', 'src', duplicate = 0)
74
75opts = Options('config.py')
76opts.AddOptions(
77	('prefix', 'prefix', '/usr/local'),
78	('bindir', 'binary directory', '${prefix}/bin'),
79	('sbindir', 'binary directory', '${prefix}/sbin'),
80	('libdir', 'library directory', '${prefix}/lib'),
81	PackageOption('with_mysql', 'enable mysql support', 'no'),
82	PackageOption('with_xml', 'enable xml support', 'no'),
83	PackageOption('with_pcre', 'enable pcre support', 'yes'),
84	PathOption('CC', 'path to the c-compiler', None),
85	BoolOption('build_dynamic', 'enable dynamic build', 'yes'),
86	BoolOption('build_static', 'enable static build', 'no'),
87	BoolOption('build_fullstatic', 'enable fullstatic build', 'no'),
88	BoolOption('with_sqlite3', 'enable sqlite3 support', 'no'),
89	BoolOption('with_memcache', 'enable memcache support', 'no'),
90	BoolOption('with_fam', 'enable FAM/gamin support', 'no'),
91	BoolOption('with_openssl', 'enable memcache support', 'no'),
92	BoolOption('with_gzip', 'enable gzip compression', 'no'),
93	BoolOption('with_bzip2', 'enable bzip2 compression', 'no'),
94	BoolOption('with_lua', 'enable lua support for mod_cml', 'no'),
95	BoolOption('with_ldap', 'enable ldap auth support', 'no'))
96
97env = Environment(
98	env = os.environ,
99	options = opts,
100	CPPPATH = Split('#build')
101)
102
103env.Help(opts.GenerateHelpText(env))
104
105if env.subst('${CC}') is not '':
106	env['CC'] = env.subst('${CC}')
107
108env['package'] = package
109env['version'] = version
110if env['CC'] == 'gcc':
111	## we need x-open 6 and bsd 4.3 features
112	env.Append(CCFLAGS = Split('-Wall -O2 -g -W -pedantic -Wunused -Wshadow -std=gnu99'))
113
114# cache configure checks
115if 1:
116	autoconf = Configure(env, custom_tests = {'CheckStructMember': checkStructMember })
117	autoconf.headerfile = "foo.h"
118	checkCHeaders(autoconf, string.split("""
119			arpa/inet.h
120			fcntl.h
121			netinet/in.h
122			sys/types.h netinet/in.h
123			stdlib.h
124			string.h
125			sys/socket.h
126			sys/types.h sys/socket.h
127		 	sys/time.h
128			unistd.h
129			sys/sendfile.h
130			sys/uio.h
131			sys/types.h sys/uio.h
132			getopt.h
133			sys/epoll.h
134			sys/select.h
135			sys/types.h sys/select.h
136			poll.h
137			sys/poll.h
138			sys/devpoll.h
139			sys/filio.h
140			sys/mman.h
141			sys/types.h sys/mman.h
142			sys/event.h
143			sys/types.h sys/event.h
144			sys/port.h
145			winsock2.h
146			pwd.h
147			sys/syslimits.h
148			sys/resource.h
149			sys/time.h sys/types.h sys/resource.h
150			sys/un.h
151			sys/types.h sys/un.h
152			syslog.h
153			stdint.h
154			inttypes.h
155			sys/prctl.h
156			sys/wait.h""", "\n"))
157
158	checkFuncs(autoconf, Split('fork stat lstat strftime dup2 getcwd inet_ntoa inet_ntop memset mmap munmap strchr \
159			strdup strerror strstr strtol sendfile  getopt socket \
160			gethostbyname poll epoll_ctl getrlimit chroot \
161			getuid select signal pathconf madvise prctl\
162			writev sigaction sendfile64 send_file kqueue port_create localtime_r posix_fadvise issetugid inet_pton'))
163
164	checkTypes(autoconf, Split('pid_t size_t off_t'))
165
166	autoconf.env.Append( LIBSQLITE3 = '', LIBXML2 = '', LIBMYSQL = '', LIBZ = '',
167		LIBBZ2 = '', LIBCRYPT = '', LIBMEMCACHE = '', LIBFCGI = '', LIBPCRE = '',
168		LIBLDAP = '', LIBLBER = '', LIBLUA = '', LIBLUALIB = '', LIBDL = '')
169
170	if env['with_fam']:
171		if autoconf.CheckLibWithHeader('fam', 'fam.h', 'C'):
172			autoconf.env.Append(CPPFLAGS = [ '-DHAVE_FAM_H', '-DHAVE_LIBFAM' ], LIBS = 'fam')
173			checkFuncs(autoconf, ['FAMNoExists']);
174
175
176	if autoconf.CheckLibWithHeader('crypt', 'crypt.h', 'C'):
177		autoconf.env.Append(CPPFLAGS = [ '-DHAVE_CRYPT_H', '-DHAVE_LIBCRYPT' ], LIBCRYPT = 'crypt')
178
179	if autoconf.CheckLibWithHeader('uuid', 'uuid/uuid.h', 'C'):
180		autoconf.env.Append(CPPFLAGS = [ '-DHAVE_UUID_UUID_H', '-DHAVE_LIBUUID' ], LIBUUID = 'uuid')
181
182	if env['with_openssl']:
183		if autoconf.CheckLibWithHeader('ssl', 'openssl/ssl.h', 'C'):
184			autoconf.env.Append(CPPFLAGS = [ '-DHAVE_OPENSSL_SSL_H', '-DHAVE_LIBSSL'] , LIBS = [ 'ssl', 'crypto' ])
185
186	if env['with_gzip']:
187		if autoconf.CheckLibWithHeader('z', 'zlib.h', 'C'):
188			autoconf.env.Append(CPPFLAGS = [ '-DHAVE_ZLIB_H', '-DHAVE_LIBZ' ], LIBZ = 'z')
189
190	if env['with_ldap']:
191		if autoconf.CheckLibWithHeader('ldap', 'ldap.h', 'C'):
192			autoconf.env.Append(CPPFLAGS = [ '-DHAVE_LDAP_H', '-DHAVE_LIBLDAP' ], LIBLDAP = 'ldap')
193		if autoconf.CheckLibWithHeader('lber', 'lber.h', 'C'):
194			autoconf.env.Append(CPPFLAGS = [ '-DHAVE_LBER_H', '-DHAVE_LIBLBER' ], LIBLBER = 'lber')
195
196	if env['with_bzip2']:
197		if autoconf.CheckLibWithHeader('bz2', 'bzlib.h', 'C'):
198			autoconf.env.Append(CPPFLAGS = [ '-DHAVE_BZLIB_H', '-DHAVE_LIBBZ2' ], LIBBZ2 = 'bz2')
199
200	if env['with_memcache']:
201		if autoconf.CheckLibWithHeader('memcache', 'memcache.h', 'C'):
202			autoconf.env.Append(CPPFLAGS = [ '-DHAVE_MEMCACHE_H', '-DHAVE_LIBMEMCACHE' ], LIBMEMCACHE = 'memcache')
203
204	if env['with_sqlite3']:
205		if autoconf.CheckLibWithHeader('sqlite3', 'sqlite3.h', 'C'):
206			autoconf.env.Append(CPPFLAGS = [ '-DHAVE_SQLITE3_H', '-DHAVE_LIBSQLITE3' ], LIBSQLITE3 = 'sqlite3')
207
208	ol = env['LIBS']
209	if autoconf.CheckLibWithHeader('fcgi', 'fastcgi.h', 'C'):
210		autoconf.env.Append(LIBFCGI = 'fcgi')
211	env['LIBS'] = ol
212
213	ol = env['LIBS']
214	if autoconf.CheckLibWithHeader('dl', 'dlfcn.h', 'C'):
215		autoconf.env.Append(LIBDL = 'dl')
216	env['LIBS'] = ol
217
218	if autoconf.CheckType('socklen_t', '#include <unistd.h>\n#include <sys/socket.h>\n#include <sys/types.h>'):
219		autoconf.env.Append(CPPFLAGS = [ '-DHAVE_SOCKLEN_T' ])
220
221	if autoconf.CheckType('struct sockaddr_storage', '#include <sys/socket.h>\n'):
222		autoconf.env.Append(CPPFLAGS = [ '-DHAVE_STRUCT_SOCKADDR_STORAGE' ])
223
224	if autoconf.CheckStructMember():
225		autoconf.env.Append(CPPFLAGS = [ '-DHAVE_STRUCT_TM_GMTOFF' ])
226
227	env = autoconf.Finish()
228
229	if env['with_lua']:
230		oldlibs = env['LIBS']
231		env.ParseConfig("pkg-config 'lua >= 5.0' --cflags --libs")
232		lualibs = env['LIBS'][len(oldlibs):]
233		env.Append(LIBLUA = lualibs)
234		env.Append(CPPFLAGS = [ '-DHAVE_LUA_H' ])
235		env['LIBS'] = oldlibs
236
237
238if env['with_pcre']:
239	pcre_config = checkProgram(env, 'pcre', 'pcre-config')
240	env.ParseConfig(pcre_config + ' --cflags --libs')
241	env.Append(CPPFLAGS = [ '-DHAVE_PCRE_H', '-DHAVE_LIBPCRE' ], LIBPCRE = 'pcre')
242
243if env['with_xml']:
244	xml2_config = checkProgram(env, 'xml', 'xml2-config')
245	oldlib = env['LIBS']
246	env['LIBS'] = []
247	env.ParseConfig(xml2_config + ' --cflags --libs')
248	env.Append(CPPFLAGS = [ '-DHAVE_LIBXML_H', '-DHAVE_LIBXML2' ], LIBXML2 = env['LIBS'])
249	env['LIBS'] = oldlib
250
251if env['with_mysql']:
252	mysql_config = checkProgram(env, 'mysql', 'mysql_config')
253	oldlib = env['LIBS']
254	env['LIBS'] = []
255	env.ParseConfig(mysql_config + ' --cflags --libs')
256	env.Append(CPPFLAGS = [ '-DHAVE_MYSQL_H', '-DHAVE_LIBMYSQL' ], LIBMYSQL = 'mysqlclient')
257	env['LIBS'] = oldlib
258
259if re.compile("cygwin|mingw").search(env['PLATFORM']):
260	env.Append(COMMON_LIB = 'bin')
261elif re.compile("darwin|aix").search(env['PLATFORM']):
262	env.Append(COMMON_LIB = 'lib')
263else:
264	env.Append(COMMON_LIB = False)
265
266versions = string.split(version, '.')
267version_id = int(versions[0]) << 16 | int(versions[1]) << 8 | int(versions[2])
268env.Append(CPPFLAGS = [
269		'-DLIGHTTPD_VERSION_ID=' + str(version_id),
270		'-DPACKAGE_NAME=\\"' + package + '\\"',
271		'-DPACKAGE_VERSION=\\"' + version + '\\"',
272		'-DLIBRARY_DIR="\\"${libdir}\\""',
273		'-D_FILE_OFFSET_BITS=64', '-D_LARGEFILE_SOURCE', '-D_LARGE_FILES'
274		] )
275
276SConscript( 'src/SConscript', 'env', build_dir = 'build', duplicate = 0)
277SConscript( 'tests/SConscript', 'env' )
278