5 from collections import OrderedDict
8 # search any file, not just executables
9 def WhereIsFile(file, paths):
11 f = os.path.join(d, file)
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
21 return os.path.normpath(f)
24 def FlattenLibs(libs):
25 if isinstance(libs, basestring):
28 return [item for sublibs in libs for item in FlattenLibs(sublibs)]
30 # removes all but the *LAST* occurance of a lib in the list
31 def 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))))
39 def 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)
45 if ('c' in libs) and (platform.system() == 'FreeBSD'):
49 def GatherLibs(env, *libs):
50 libs = RemoveDuplicateLibs(env['LIBS'] + list(libs) + [env['APPEND_LIBS']])
51 return WorkaroundFreeBSDLibOrder(libs)
53 common_src = Split("base64.c buffer.c log.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 \
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 \
74 status_counter.c safe_memclear.c \
77 src = Split("server.c response.c connections.c \
81 configfile.c configparser.c request.c proc_open.c")
83 lemon = env.Program('lemon', 'lemon.c', LIBS = GatherLibs(env))
85 def 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)
89 configparser = Lemon(env, 'configparser.y')
90 mod_ssi_exprparser = Lemon(env, 'mod_ssi_exprparser.y')
92 ## the modules and how they are built
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'] ] },
129 if env['with_geoip']:
130 modules['mod_geoip'] = { 'src' : [ 'mod_geoip.c' ], 'lib' : [ env['LIBGEOIP'] ] }
133 modules['mod_authn_gssapi'] = { 'src' : [ 'mod_authn_gssapi.c' ], 'lib' : [ env['LIBKRB5'], env['LIBGSSAPI_KRB5'] ] }
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'] ] }
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'] ]
146 if 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'] ] }
149 if 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'] ] }
154 if env['with_pgsql']:
155 modules['mod_vhostdb_pgsql'] = { 'src' : [ 'mod_vhostdb_pgsql.c' ], 'lib' : [ env['LIBPGSQL'] ] }
158 modules['mod_vhostdb_dbi'] = { 'src' : [ 'mod_vhostdb_dbi.c' ], 'lib' : [ env['LIBDBI'] ] }
160 if env['with_openssl']:
161 modules['mod_openssl'] = { 'src' : [ 'mod_openssl.c' ], 'lib' : [ env['LIBSSL'], env['LIBCRYPTO'] ] }
163 staticenv = env.Clone(CPPFLAGS=[ env['CPPFLAGS'], '-DLIGHTTPD_STATIC' ])
165 ## all the core-sources + the modules
166 staticsrc = src + common_src
168 staticlib = copy(env['LIBS'])
170 for module in modules.keys():
171 staticsrc += modules[module]['src']
172 staticinit += "PLUGIN_INIT(%s)\n"%module
173 if modules[module].has_key('lib'):
174 staticlib += modules[module]['lib']
176 def WriteStaticPluginHeader(target, source, env):
178 data = env['STATICINIT']
179 # only touch the file if content actually changes
181 with open(target[0].abspath, 'r') as f:
182 do_write = (data != f.read())
186 with open(target[0].abspath, 'w+') as f:
187 f.write(env['STATICINIT'])
189 env['STATICINIT'] = staticinit
190 staticheader = env.AlwaysBuild(env.Command('plugin-static.h', [], WriteStaticPluginHeader))
192 ## turn all src-files into objects
194 static_plugin_obj = None
195 for cfile in staticsrc:
196 if cfile == 'plugin.c':
197 static_plugin_obj = [ staticenv.Object('static-' + cfile.replace('.c', ''), cfile) ]
198 staticobj += static_plugin_obj
200 staticobj += [ staticenv.Object('static-' + cfile.replace('.c', ''), cfile) ]
201 env.Depends(static_plugin_obj, 'plugin-static.h')
203 ## includes all modules, but links dynamically against other libs
204 staticbin = staticenv.Program('../static/build/lighttpd',
206 LIBS = GatherLibs(env, staticlib)
209 ## you might have to adjust the list of libs and the order for your setup
210 ## this is tricky, be warned
213 ## try to calculate the libs for fullstatic with ldd
216 ## 3. add them to the libs
217 #searchlibs = os.pathsep.join([ '/lib/', '/usr/lib/', '/usr/local/lib/' ])
219 searchpathre = re.compile(r'\bSEARCH_DIR\("=([^"]+)"\)')
220 f = os.popen('ld --verbose | grep SEARCH_DIR', 'r')
221 for aword in searchpathre.findall(f.read()):
222 searchlibs += [ aword]
225 lddre = re.compile(r'^\s+lib([^=-]+)(?:-[\.0-9]+)?\.so\.[0-9]+ =>', re.MULTILINE)
226 for libs in staticlib:
227 if type(libs) is types.StringType: libs = [ libs ]
229 fullstaticlib += [ lib ]
230 solibpath = WhereIsFile('lib' + lib + '.so', paths = searchlibs)
231 if solibpath is None:
234 f = os.popen('ldd ' + solibpath, 'r')
235 for aword in lddre.findall(f.read()):
236 fullstaticlib += [ aword ]
239 ## glibc pthread needs to be linked completely (especially nptl-init.o)
240 ## or not at all, or else pthread structures may not be initialized correctly
242 fullstatic_libs = GatherLibs(env, fullstaticlib)
243 fullstatic_linkflags = [staticenv['LINKFLAGS'], '-static']
244 if (('pthread' in fullstatic_libs) or ('pcre' in fullstatic_libs)) and (platform.system() == 'Linux'):
245 fullstatic_linkflags += ['-Wl,--whole-archive','-lpthread','-Wl,--no-whole-archive']
246 fullstatic_libs.remove('pthread')
247 if 'gcc_s' in fullstatic_libs:
248 fullstatic_linkflags += ['-static-libgcc']
249 fullstatic_libs.remove('gcc_s')
251 ## includes all modules, linked statically
252 fullstaticbin = staticenv.Program('../fullstatic/build/lighttpd',
254 LIBS = fullstatic_libs,
255 LINKFLAGS= fullstatic_linkflags
258 Alias('static', staticbin)
259 Alias('fullstatic', fullstaticbin)
261 implib = 'lighttpd.exe.a'
262 bin_targets = ['lighttpd']
263 bin_linkflags = [ env['LINKFLAGS'] ]
264 if env['COMMON_LIB'] == 'lib':
265 common_lib = env.SharedLibrary('liblighttpd', common_src, LINKFLAGS = [ env['LINKFLAGS'], '-Wl,--export-dynamic' ])
269 if env['COMMON_LIB'] == 'bin':
270 bin_linkflags += [ '-Wl,--export-all-symbols', '-Wl,--out-implib=build/' + implib ]
271 bin_targets += [ implib ]
273 bin_linkflags += [ '-Wl,--export-dynamic' ]
275 instbin = env.Program(bin_targets, src, LINKFLAGS = bin_linkflags,
284 env.Depends(instbin, configparser)
286 if env['COMMON_LIB'] == 'bin':
287 common_lib = instbin[1]
289 env['SHLIBPREFIX'] = ''
291 for module in modules.keys():
292 libs = [ common_lib ]
293 if modules[module].has_key('lib'):
294 libs += modules[module]['lib']
295 instlib += env.SharedLibrary(module, modules[module]['src'], LIBS = GatherLibs(env, libs))
296 env.Alias('modules', instlib)
300 if env['build_dynamic']:
301 Default(instbin[0], instlib)
302 inst += env.Install('${sbindir}', instbin[0])
303 inst += env.Install('${libdir}', instlib)
304 if env['COMMON_LIB'] == 'lib':
306 inst += env.Install('${bindir}', common_lib)
308 if env['build_static']:
310 inst += env.Install('${sbindir}', staticbin)
312 if env['build_fullstatic']:
313 Default(fullstaticbin)
314 inst += env.Install('${sbindir}', fullstaticbin)
316 env.Alias('dynamic', instbin)
317 # default all to be installed
318 env.Alias('install', inst)
321 tarname = env['package'] + '-' + env['version']