xref: /lighttpd1.4/src/SConscript (revision 2925a095)
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 fdevent_fdnode.c gw_backend.c \
61	stat_cache.c http_etag.c array.c \
62	algo_md5.c algo_sha1.c algo_splaytree.c \
63	configfile-glue.c \
64	http-header-glue.c \
65	http_cgi.c \
66	http_date.c \
67	request.c \
68	sock_addr.c \
69	rand.c \
70	fdlog_maint.c \
71	fdlog.c \
72	sys-setjmp.c \
73	ck.c \
74")
75
76src = Split("server.c response.c connections.c h2.c reqpool.c \
77	plugin.c \
78	sock_addr_cache.c \
79	ls-hpack/lshpack.c \
80	algo_xxhash.c \
81	fdevent_impl.c \
82	http_range.c \
83	network.c \
84	network_write.c \
85	data_config.c \
86	configfile.c configparser.c")
87
88builtin_mods = Split(" \
89  mod_rewrite.c \
90  mod_redirect.c \
91  mod_access.c \
92  mod_alias.c \
93  mod_indexfile.c \
94  mod_staticfile.c \
95  mod_setenv.c \
96  mod_expire.c \
97  mod_simple_vhost.c \
98  mod_evhost.c \
99  mod_fastcgi.c \
100  mod_scgi.c \
101  ")
102
103lemon = env.Program('lemon', 'lemon.c', LIBS = GatherLibs(env))
104
105def Lemon(env, input):
106	parser = env.Command([input.replace('.y', '.c'),input.replace('.y', '.h')], input, lemon[0].path + ' -q -dsconsbuild/build -Tsrc/lempar.c $SOURCE')
107	env.Depends(parser, lemon)
108
109configparser = Lemon(env, 'configparser.y')
110
111## the modules and how they are built
112modules = {
113	'mod_accesslog' : { 'src' : [ 'mod_accesslog.c' ] },
114	'mod_ajp13' : { 'src' : [ 'mod_ajp13.c' ] },
115	'mod_auth' : { 'src' : [ 'mod_auth.c', 'mod_auth_api.c' ], 'lib' : [ env['LIBCRYPTO'] ] },
116	'mod_authn_file' : { 'src' : [ 'mod_authn_file.c' ], 'lib' : [ env['LIBCRYPT'], env['LIBCRYPTO'] ] },
117	'mod_cgi' : { 'src' : [ 'mod_cgi.c' ] },
118	'mod_deflate' : { 'src' : [ 'mod_deflate.c' ], 'lib' : [ env['LIBZ'], env['LIBZSTD'], env['LIBBZ2'], env['LIBBROTLI'], env['LIBDEFLATE'], 'm' ] },
119	'mod_dirlisting' : { 'src' : [ 'mod_dirlisting.c' ] },
120	'mod_extforward' : { 'src' : [ 'mod_extforward.c' ] },
121	'mod_proxy' : { 'src' : [ 'mod_proxy.c' ] },
122	'mod_rrdtool' : { 'src' : [ 'mod_rrdtool.c' ] },
123	'mod_sockproxy' : { 'src' : [ 'mod_sockproxy.c' ] },
124	'mod_ssi' : { 'src' : [ 'mod_ssi.c' ] },
125	'mod_status' : { 'src' : [ 'mod_status.c' ] },
126	'mod_userdir' : { 'src' : [ 'mod_userdir.c' ] },
127	'mod_vhostdb' : { 'src' : [ 'mod_vhostdb.c', 'mod_vhostdb_api.c' ] },
128	'mod_webdav' : { 'src' : [ 'mod_webdav.c' ], 'lib' : [ env['LIBXML2'], env['LIBSQLITE3'], env['LIBUUID'] ] },
129	'mod_wstunnel' : { 'src' : [ 'mod_wstunnel.c' ], 'lib' : [ env['LIBCRYPTO'] ] },
130}
131
132if env['with_maxminddb']:
133	modules['mod_maxminddb'] = { 'src' : [ 'mod_maxminddb.c' ], 'lib' : [ env['LIBMAXMINDDB'] ] }
134
135if env['with_krb5']:
136	modules['mod_authn_gssapi'] = { 'src' : [ 'mod_authn_gssapi.c' ], 'lib' : [ env['LIBKRB5'], env['LIBGSSAPI_KRB5'] ] }
137
138if env['with_ldap']:
139	modules['mod_authn_ldap'] = { 'src' : [ 'mod_authn_ldap.c' ], 'lib' : [ env['LIBLDAP'], env['LIBLBER'] ] }
140	modules['mod_vhostdb_ldap'] = { 'src' : [ 'mod_vhostdb_ldap.c' ], 'lib' : [ env['LIBLDAP'], env['LIBLBER'] ] }
141
142if env['with_lua']:
143	modules['mod_magnet'] = {
144		'src' : [ 'mod_magnet.c', 'mod_magnet_cache.c', 'algo_hmac.c' ],
145		'lib' : [ env['LIBLUA'], env['LIBCRYPTO'] ]
146	}
147
148if env['with_pam']:
149	modules['mod_authn_pam'] = { 'src' : [ 'mod_authn_pam.c' ], 'lib' : [ env['LIBPAM'] ] }
150
151if env['with_mysql']:
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_authn_dbi'] = { 'src' : [ 'mod_authn_dbi.c' ], 'lib' : [ env['LIBCRYPT'], env['LIBDBI'], env['LIBCRYPTO'] ] }
159	modules['mod_vhostdb_dbi'] = { 'src' : [ 'mod_vhostdb_dbi.c' ], 'lib' : [ env['LIBDBI'] ] }
160
161if env['with_sasl']:
162	modules['mod_authn_sasl'] = { 'src' : [ 'mod_authn_sasl.c' ], 'lib' : [ env['LIBSASL'] ] }
163
164if env['with_openssl']:
165	modules['mod_openssl'] = { 'src' : [ 'mod_openssl.c' ], 'lib' : [ env['LIBSSL'], env['LIBSSLCRYPTO'] ] }
166
167if env['with_wolfssl']:
168	modules['mod_wolfssl'] = { 'src' : [ 'mod_wolfssl.c' ], 'lib' : [ env['LIBWOLFSSL'], 'm' ] }
169
170if env['with_mbedtls']:
171	modules['mod_mbedtls'] = { 'src' : [ 'mod_mbedtls.c' ], 'lib' : [ env['LIBMBEDTLS'], env['LIBMBEDX509'], env['LIBMBEDCRYPTO'] ] }
172
173if env['with_nss']:
174	modules['mod_nss'] = { 'src' : [ 'mod_nss.c' ], 'lib' : [ env['LIBNSS'] ] }
175
176if env['with_gnutls']:
177	modules['mod_gnutls'] = { 'src' : [ 'mod_gnutls.c' ], 'lib' : [ env['LIBGNUTLS'] ] }
178
179staticenv = env.Clone(CPPFLAGS=[ env['CPPFLAGS'], '-DLIGHTTPD_STATIC' ])
180
181## all the core-sources + the modules
182staticsrc = src + common_src
183
184staticlib = copy(env['LIBS'])
185staticinit = ''
186staticsrc += builtin_mods
187for module in builtin_mods:
188	staticinit += "PLUGIN_INIT(%s)\n"%module[0:-2]
189for module in modules.keys():
190	staticsrc += modules[module]['src']
191	staticinit += "PLUGIN_INIT(%s)\n"%module
192	if 'lib' in modules[module]:
193		staticlib += modules[module]['lib']
194
195def WriteStaticPluginHeader(target, source, env):
196	do_write = True
197	data = env['STATICINIT']
198	# only touch the file if content actually changes
199	try:
200		with open(target[0].abspath, 'r') as f:
201			do_write = (data != f.read())
202	except IOError:
203		pass
204	if do_write:
205		with open(target[0].abspath, 'w+') as f:
206			f.write(env['STATICINIT'])
207
208env['STATICINIT'] = staticinit
209staticheader = env.AlwaysBuild(env.Command('plugin-static.h', [], WriteStaticPluginHeader))
210
211## turn all src-files into objects
212staticobj = []
213static_plugin_obj = None
214for cfile in staticsrc:
215	if cfile == 'plugin.c':
216		static_plugin_obj = [ staticenv.Object('static-' + cfile.replace('.c', ''), cfile) ]
217		staticobj += static_plugin_obj
218	else:
219		staticobj += [ staticenv.Object('static-' + cfile.replace('.c', ''), cfile) ]
220env.Depends(static_plugin_obj, 'plugin-static.h')
221
222## includes all modules, but links dynamically against other libs
223staticbin = staticenv.Program('../static/build/lighttpd',
224	staticobj,
225	LIBS = GatherLibs(env, env['LIBPCRE'], staticlib)
226	)
227
228## you might have to adjust the list of libs and the order for your setup
229## this is tricky, be warned
230fullstaticlib = []
231
232## try to calculate the libs for fullstatic with ldd
233## 1. find the lib
234## 2. check the deps
235## 3. add them to the libs
236#searchlibs = os.pathsep.join([ '/lib/', '/usr/lib/', '/usr/local/lib/' ])
237searchlibs = []
238searchpathre = re.compile(r'\bSEARCH_DIR\("=([^"]+)"\)')
239f = os.popen('ld --verbose | grep SEARCH_DIR', 'r')
240for aword in searchpathre.findall(f.read()):
241	searchlibs += [ aword]
242f.close
243
244lddre = re.compile(r'^\s+lib([^=-]+)(?:-[\.0-9]+)?\.so\.[0-9]+ =>', re.MULTILINE)
245for libs in staticlib:
246	if isinstance(libs, string_types): libs = [ libs ]
247	for lib in libs:
248		fullstaticlib += [ lib ]
249		solibpath = WhereIsFile('lib' + lib + '.so', paths = searchlibs)
250		if solibpath is None:
251			continue
252
253		f = os.popen('ldd ' + solibpath, 'r')
254		for aword in lddre.findall(f.read()):
255			fullstaticlib += [ aword ]
256		f.close
257
258## glibc pthread needs to be linked completely (especially nptl-init.o)
259## or not at all, or else pthread structures may not be initialized correctly
260import platform
261fullstatic_libs = GatherLibs(env, env['LIBPCRE'], fullstaticlib)
262fullstatic_linkflags = [staticenv['LINKFLAGS'], '-static']
263if (('pthread' in fullstatic_libs) or ('pcre' in fullstatic_libs)) and (platform.system() == 'Linux'):
264	fullstatic_linkflags += ['-Wl,--whole-archive','-lpthread','-Wl,--no-whole-archive']
265	if 'pthread' in fullstatic_libs:
266		fullstatic_libs.remove('pthread')
267if 'gcc_s' in fullstatic_libs:
268	fullstatic_linkflags += ['-static-libgcc']
269	fullstatic_libs.remove('gcc_s')
270
271## includes all modules, linked statically
272fullstaticbin = staticenv.Program('../fullstatic/build/lighttpd',
273	staticobj,
274	LIBS = fullstatic_libs,
275	LINKFLAGS= fullstatic_linkflags
276	)
277
278Alias('static', staticbin)
279Alias('fullstatic', fullstaticbin)
280
281implib = 'lighttpd.exe.a'
282bin_targets = ['lighttpd']
283bin_linkflags = [ env['LINKFLAGS'] ]
284if env['COMMON_LIB'] == 'lib':
285	common_lib = env.SharedLibrary('liblighttpd', common_src, LINKFLAGS = [ env['LINKFLAGS'], '-Wl,--export-dynamic' ])
286else:
287	src += common_src
288	common_lib = []
289	if env['COMMON_LIB'] == 'bin':
290		bin_linkflags += [ '-Wl,--export-all-symbols', '-Wl,--out-implib=build/' + implib ]
291		bin_targets += [ implib ]
292	else:
293		bin_linkflags += [ '-Wl,--export-dynamic' ]
294
295instbin = env.Program(bin_targets, src + builtin_mods, LINKFLAGS = bin_linkflags,
296	LIBS = GatherLibs(
297		env,
298		common_lib,
299		env['LIBCRYPTO'],
300		env['LIBDL'],
301		env['LIBPCRE'],
302		env['LIBXXHASH'],
303	)
304)
305env.Depends(instbin, configparser)
306
307if env['COMMON_LIB'] == 'bin':
308	common_lib = instbin[1]
309
310env['SHLIBPREFIX'] = ''
311instlib = []
312for module in modules.keys():
313	libs = [ common_lib ]
314	if 'lib' in modules[module]:
315		libs +=  modules[module]['lib']
316	instlib += env.SharedLibrary(module, modules[module]['src'], LIBS = GatherLibs(env, libs))
317env.Alias('modules', instlib)
318
319inst = []
320
321if env['build_dynamic']:
322	Default(instbin[0], instlib)
323	inst += env.Install('${sbindir}', instbin[0])
324	inst += env.Install('${libdir}', instlib)
325	if env['COMMON_LIB'] == 'lib':
326		Default(common_lib)
327		inst += env.Install('${bindir}', common_lib)
328
329if env['build_static']:
330	Default(staticbin)
331	inst += env.Install('${sbindir}', staticbin)
332
333if env['build_fullstatic']:
334	Default(fullstaticbin)
335	inst += env.Install('${sbindir}', fullstaticbin)
336
337env.Alias('dynamic', instbin)
338# default all to be installed
339env.Alias('install', inst)
340
341pkgdir = '.'
342tarname = env['package'] + '-' + env['version']
343