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