xref: /lighttpd1.4/src/SConscript (revision d61714dd)
1import itertools
2import os
3import re
4import types
5from collections import OrderedDict
6from copy import copy
7
8# search any file, not just executables
9def WhereIsFile(file, paths):
10	for d in paths:
11		f = os.path.join(d, file)
12		if os.path.isfile(f):
13			try:
14				st = os.stat(f)
15			except OSError:
16				# os.stat() raises OSError, not IOError if the file
17				# doesn't exist, so in this case we let IOError get
18				# raised so as to not mask possibly serious disk or
19				# network issues.
20				continue
21			return os.path.normpath(f)
22	return None
23
24def FlattenLibs(libs):
25	if isinstance(libs, basestring):
26		return [libs]
27	else:
28		return [item for sublibs in libs for item in FlattenLibs(sublibs)]
29
30# removes all but the *LAST* occurance of a lib in the list
31def RemoveDuplicateLibs(libs):
32	libs = FlattenLibs(libs)
33	# remove empty strings from list
34	libs = list(filter(lambda x: x != '', libs))
35	return list(reversed(OrderedDict.fromkeys(reversed(libs))))
36
37Import('env')
38
39def WorkaroundFreeBSDLibOrder(libs):
40	# lib(re)ssl includes (weak) arc4random functions
41	# which "on purpose" might conflict with those in libc
42	# => link libc first solves this
43	# (required for FreeBSD11 fullstatic build)
44	import platform
45	if ('c' in libs) and (platform.system() == 'FreeBSD'):
46		return ['c'] + libs
47	return libs
48
49def GatherLibs(env, *libs):
50	libs = RemoveDuplicateLibs(env['LIBS'] + list(libs) + [env['APPEND_LIBS']])
51	return WorkaroundFreeBSDLibOrder(libs)
52
53common_src = Split("base64.c buffer.c log.c \
54	keyvalue.c chunk.c  \
55	http_chunk.c stream.c fdevent.c gw_backend.c \
56	stat_cache.c plugin.c joblist.c etag.c array.c \
57	data_string.c data_array.c \
58	data_integer.c algo_sha1.c md5.c \
59	vector.c \
60	fdevent_select.c fdevent_libev.c \
61	fdevent_poll.c fdevent_linux_sysepoll.c \
62	fdevent_solaris_devpoll.c fdevent_solaris_port.c \
63	fdevent_freebsd_kqueue.c \
64	data_config.c \
65	crc32.c \
66	connections-glue.c \
67	configfile-glue.c \
68	http-header-glue.c \
69	http_auth.c \
70	http_vhostdb.c \
71	sock_addr.c \
72	splaytree.c \
73	rand.c \
74	status_counter.c safe_memclear.c \
75")
76
77src = Split("server.c response.c connections.c \
78	inet_ntop_cache.c \
79	network.c \
80	network_write.c \
81	configfile.c configparser.c request.c proc_open.c")
82
83lemon = env.Program('lemon', 'lemon.c', LIBS = GatherLibs(env))
84
85def Lemon(env, input):
86	parser = env.Command([input.replace('.y', '.c'),input.replace('.y', '.h')], input, '(cd sconsbuild/build; ../../' + lemon[0].path + ' -q ../../$SOURCE ../../src/lempar.c)')
87	env.Depends(parser, lemon)
88
89configparser = Lemon(env, 'configparser.y')
90mod_ssi_exprparser = Lemon(env, 'mod_ssi_exprparser.y')
91
92## the modules and how they are built
93modules = {
94	'mod_access' : { 'src' : [ 'mod_access.c' ] },
95	'mod_accesslog' : { 'src' : [ 'mod_accesslog.c' ] },
96	'mod_alias' : { 'src' : [ 'mod_alias.c' ] },
97	'mod_auth' : { 'src' : [ 'mod_auth.c' ] },
98	'mod_authn_file' : { 'src' : [ 'mod_authn_file.c' ], 'lib' : [ env['LIBCRYPT'], env['LIBCRYPTO'] ] },
99	'mod_cgi' : { 'src' : [ 'mod_cgi.c' ] },
100	'mod_compress' : { 'src' : [ 'mod_compress.c' ], 'lib' : [ env['LIBZ'], env['LIBBZ2'] ] },
101	'mod_deflate' : { 'src' : [ 'mod_deflate.c' ], 'lib' : [ env['LIBZ'], env['LIBBZ2'] ] },
102	'mod_dirlisting' : { 'src' : [ 'mod_dirlisting.c' ], 'lib' : [ env['LIBPCRE'] ] },
103	'mod_evasive' : { 'src' : [ 'mod_evasive.c' ] },
104	'mod_evhost' : { 'src' : [ 'mod_evhost.c' ] },
105	'mod_expire' : { 'src' : [ 'mod_expire.c' ] },
106	'mod_extforward' : { 'src' : [ 'mod_extforward.c' ] },
107	'mod_fastcgi' : { 'src' : [ 'mod_fastcgi.c' ] },
108	'mod_flv_streaming' : { 'src' : [ 'mod_flv_streaming.c' ] },
109	'mod_indexfile' : { 'src' : [ 'mod_indexfile.c' ] },
110	'mod_proxy' : { 'src' : [ 'mod_proxy.c' ] },
111	'mod_redirect' : { 'src' : [ 'mod_redirect.c' ], 'lib' : [ env['LIBPCRE'] ] },
112	'mod_rewrite' : { 'src' : [ 'mod_rewrite.c' ], 'lib' : [ env['LIBPCRE'] ] },
113	'mod_rrdtool' : { 'src' : [ 'mod_rrdtool.c' ] },
114	'mod_scgi' : { 'src' : [ 'mod_scgi.c' ] },
115	'mod_secdownload' : { 'src' : [ 'mod_secdownload.c' ], 'lib' : [ env['LIBCRYPTO'] ] },
116	'mod_setenv' : { 'src' : [ 'mod_setenv.c' ] },
117	'mod_simple_vhost' : { 'src' : [ 'mod_simple_vhost.c' ] },
118	'mod_ssi' : { 'src' : [ 'mod_ssi_exprparser.c', 'mod_ssi_expr.c', 'mod_ssi.c' ] },
119	'mod_staticfile' : { 'src' : [ 'mod_staticfile.c' ] },
120	'mod_status' : { 'src' : [ 'mod_status.c' ] },
121	'mod_uploadprogress' : { 'src' : [ 'mod_uploadprogress.c' ] },
122	'mod_userdir' : { 'src' : [ 'mod_userdir.c' ] },
123	'mod_usertrack' : { 'src' : [ 'mod_usertrack.c' ] },
124	'mod_vhostdb' : { 'src' : [ 'mod_vhostdb.c' ] },
125	'mod_webdav' : { 'src' : [ 'mod_webdav.c' ], 'lib' : [ env['LIBXML2'], env['LIBSQLITE3'], env['LIBUUID'] ] },
126	'mod_wstunnel' : { 'src' : [ 'mod_wstunnel.c' ], 'lib' : [ env['LIBCRYPTO'] ] },
127}
128
129if env['with_geoip']:
130	modules['mod_geoip'] = { 'src' : [ 'mod_geoip.c' ], 'lib' : [ env['LIBGEOIP'] ] }
131
132if env['with_krb5']:
133	modules['mod_authn_gssapi'] = { 'src' : [ 'mod_authn_gssapi.c' ], 'lib' : [ env['LIBKRB5'], env['LIBGSSAPI_KRB5'] ] }
134
135if env['with_ldap']:
136	modules['mod_authn_ldap'] = { 'src' : [ 'mod_authn_ldap.c' ], 'lib' : [ env['LIBLDAP'], env['LIBLBER'] ] }
137	modules['mod_vhostdb_ldap'] = { 'src' : [ 'mod_vhostdb_ldap.c' ], 'lib' : [ env['LIBLDAP'], env['LIBLBER'] ] }
138
139if env['with_lua']:
140	modules['mod_magnet'] = { 'src' : [ 'mod_magnet.c', 'mod_magnet_cache.c' ], 'lib' : [ env['LIBLUA'] ] }
141	modules['mod_cml'] = {
142		'src' : [ 'mod_cml_lua.c', 'mod_cml.c', 'mod_cml_funcs.c' ],
143		'lib' : [ env['LIBMEMCACHED'], env['LIBLUA'] ]
144	}
145
146if env['with_pcre'] and (env['with_memcached'] or env['with_gdbm']):
147	modules['mod_trigger_b4_dl'] = { 'src' : [ 'mod_trigger_b4_dl.c' ], 'lib' : [ env['LIBPCRE'], env['LIBMEMCACHED'], env['LIBGDBM'] ] }
148
149if env['with_mysql']:
150	modules['mod_authn_mysql'] = { 'src' : [ 'mod_authn_mysql.c' ], 'lib' : [ env['LIBCRYPT'], env['LIBMYSQL'] ] }
151	modules['mod_mysql_vhost'] = { 'src' : [ 'mod_mysql_vhost.c' ], 'lib' : [ env['LIBMYSQL'] ] }
152	modules['mod_vhostdb_mysql'] = { 'src' : [ 'mod_vhostdb_mysql.c' ], 'lib' : [ env['LIBMYSQL'] ] }
153
154if env['with_pgsql']:
155	modules['mod_vhostdb_pgsql'] = { 'src' : [ 'mod_vhostdb_pgsql.c' ], 'lib' : [ env['LIBPGSQL'] ] }
156
157if env['with_dbi']:
158	modules['mod_vhostdb_dbi'] = { 'src' : [ 'mod_vhostdb_dbi.c' ], 'lib' : [ env['LIBDBI'] ] }
159
160if env['with_sasl']:
161	modules['mod_authn_sasl'] = { 'src' : [ 'mod_authn_sasl.c' ], 'lib' : [ env['LIBSASL'] ] }
162
163if env['with_openssl']:
164	modules['mod_openssl'] = { 'src' : [ 'mod_openssl.c' ], 'lib' : [ env['LIBSSL'], env['LIBCRYPTO'] ] }
165
166staticenv = env.Clone(CPPFLAGS=[ env['CPPFLAGS'], '-DLIGHTTPD_STATIC' ])
167
168## all the core-sources + the modules
169staticsrc = src + common_src
170
171staticlib = copy(env['LIBS'])
172staticinit = ''
173for module in modules.keys():
174	staticsrc += modules[module]['src']
175	staticinit += "PLUGIN_INIT(%s)\n"%module
176	if modules[module].has_key('lib'):
177		staticlib += modules[module]['lib']
178
179def WriteStaticPluginHeader(target, source, env):
180	do_write = True
181	data = env['STATICINIT']
182	# only touch the file if content actually changes
183	try:
184		with open(target[0].abspath, 'r') as f:
185			do_write = (data != f.read())
186	except IOError:
187		pass
188	if do_write:
189		with open(target[0].abspath, 'w+') as f:
190			f.write(env['STATICINIT'])
191
192env['STATICINIT'] = staticinit
193staticheader = env.AlwaysBuild(env.Command('plugin-static.h', [], WriteStaticPluginHeader))
194
195## turn all src-files into objects
196staticobj = []
197static_plugin_obj = None
198for cfile in staticsrc:
199	if cfile == 'plugin.c':
200		static_plugin_obj = [ staticenv.Object('static-' + cfile.replace('.c', ''), cfile) ]
201		staticobj += static_plugin_obj
202	else:
203		staticobj += [ staticenv.Object('static-' + cfile.replace('.c', ''), cfile) ]
204env.Depends(static_plugin_obj, 'plugin-static.h')
205
206## includes all modules, but links dynamically against other libs
207staticbin = staticenv.Program('../static/build/lighttpd',
208	staticobj,
209	LIBS = GatherLibs(env, staticlib)
210	)
211
212## you might have to adjust the list of libs and the order for your setup
213## this is tricky, be warned
214fullstaticlib = []
215
216## try to calculate the libs for fullstatic with ldd
217## 1. find the lib
218## 2. check the deps
219## 3. add them to the libs
220#searchlibs = os.pathsep.join([ '/lib/', '/usr/lib/', '/usr/local/lib/' ])
221searchlibs = []
222searchpathre = re.compile(r'\bSEARCH_DIR\("=([^"]+)"\)')
223f = os.popen('ld --verbose | grep SEARCH_DIR', 'r')
224for aword in searchpathre.findall(f.read()):
225	searchlibs += [ aword]
226f.close
227
228lddre = re.compile(r'^\s+lib([^=-]+)(?:-[\.0-9]+)?\.so\.[0-9]+ =>', re.MULTILINE)
229for libs in staticlib:
230	if type(libs) is types.StringType: libs = [ libs ]
231	for lib in libs:
232		fullstaticlib += [ lib ]
233		solibpath = WhereIsFile('lib' + lib + '.so', paths = searchlibs)
234		if solibpath is None:
235			continue
236
237		f = os.popen('ldd ' + solibpath, 'r')
238		for aword in lddre.findall(f.read()):
239			fullstaticlib += [ aword ]
240		f.close
241
242## glibc pthread needs to be linked completely (especially nptl-init.o)
243## or not at all, or else pthread structures may not be initialized correctly
244import platform
245fullstatic_libs = GatherLibs(env, fullstaticlib)
246fullstatic_linkflags = [staticenv['LINKFLAGS'], '-static']
247if (('pthread' in fullstatic_libs) or ('pcre' in fullstatic_libs)) and (platform.system() == 'Linux'):
248	fullstatic_linkflags += ['-Wl,--whole-archive','-lpthread','-Wl,--no-whole-archive']
249	fullstatic_libs.remove('pthread')
250if 'gcc_s' in fullstatic_libs:
251	fullstatic_linkflags += ['-static-libgcc']
252	fullstatic_libs.remove('gcc_s')
253
254## includes all modules, linked statically
255fullstaticbin = staticenv.Program('../fullstatic/build/lighttpd',
256	staticobj,
257	LIBS = fullstatic_libs,
258	LINKFLAGS= fullstatic_linkflags
259	)
260
261Alias('static', staticbin)
262Alias('fullstatic', fullstaticbin)
263
264implib = 'lighttpd.exe.a'
265bin_targets = ['lighttpd']
266bin_linkflags = [ env['LINKFLAGS'] ]
267if env['COMMON_LIB'] == 'lib':
268	common_lib = env.SharedLibrary('liblighttpd', common_src, LINKFLAGS = [ env['LINKFLAGS'], '-Wl,--export-dynamic' ])
269else:
270	src += common_src
271	common_lib = []
272	if env['COMMON_LIB'] == 'bin':
273		bin_linkflags += [ '-Wl,--export-all-symbols', '-Wl,--out-implib=build/' + implib ]
274		bin_targets += [ implib ]
275	else:
276		bin_linkflags += [ '-Wl,--export-dynamic' ]
277
278instbin = env.Program(bin_targets, src, LINKFLAGS = bin_linkflags,
279	LIBS = GatherLibs(
280		env,
281		common_lib,
282		env['LIBCRYPTO'],
283		env['LIBDL'],
284		env['LIBPCRE'],
285	)
286)
287env.Depends(instbin, configparser)
288
289if env['COMMON_LIB'] == 'bin':
290	common_lib = instbin[1]
291
292env['SHLIBPREFIX'] = ''
293instlib = []
294for module in modules.keys():
295	libs = [ common_lib ]
296	if modules[module].has_key('lib'):
297		libs +=  modules[module]['lib']
298	instlib += env.SharedLibrary(module, modules[module]['src'], LIBS = GatherLibs(env, libs))
299env.Alias('modules', instlib)
300
301inst = []
302
303if env['build_dynamic']:
304	Default(instbin[0], instlib)
305	inst += env.Install('${sbindir}', instbin[0])
306	inst += env.Install('${libdir}', instlib)
307	if env['COMMON_LIB'] == 'lib':
308		Default(common_lib)
309		inst += env.Install('${bindir}', common_lib)
310
311if env['build_static']:
312	Default(staticbin)
313	inst += env.Install('${sbindir}', staticbin)
314
315if env['build_fullstatic']:
316	Default(fullstaticbin)
317	inst += env.Install('${sbindir}', fullstaticbin)
318
319env.Alias('dynamic', instbin)
320# default all to be installed
321env.Alias('install', inst)
322
323pkgdir = '.'
324tarname = env['package'] + '-' + env['version']
325