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