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_openssl']: 161 modules['mod_openssl'] = { 'src' : [ 'mod_openssl.c' ], 'lib' : [ env['LIBSSL'], env['LIBCRYPTO'] ] } 162 163staticenv = env.Clone(CPPFLAGS=[ env['CPPFLAGS'], '-DLIGHTTPD_STATIC' ]) 164 165## all the core-sources + the modules 166staticsrc = src + common_src 167 168staticlib = copy(env['LIBS']) 169staticinit = '' 170for 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'] 175 176def WriteStaticPluginHeader(target, source, env): 177 do_write = True 178 data = env['STATICINIT'] 179 # only touch the file if content actually changes 180 try: 181 with open(target[0].abspath, 'r') as f: 182 do_write = (data != f.read()) 183 except IOError: 184 pass 185 if do_write: 186 with open(target[0].abspath, 'w+') as f: 187 f.write(env['STATICINIT']) 188 189env['STATICINIT'] = staticinit 190staticheader = env.AlwaysBuild(env.Command('plugin-static.h', [], WriteStaticPluginHeader)) 191 192## turn all src-files into objects 193staticobj = [] 194static_plugin_obj = None 195for cfile in staticsrc: 196 if cfile == 'plugin.c': 197 static_plugin_obj = [ staticenv.Object('static-' + cfile.replace('.c', ''), cfile) ] 198 staticobj += static_plugin_obj 199 else: 200 staticobj += [ staticenv.Object('static-' + cfile.replace('.c', ''), cfile) ] 201env.Depends(static_plugin_obj, 'plugin-static.h') 202 203## includes all modules, but links dynamically against other libs 204staticbin = staticenv.Program('../static/build/lighttpd', 205 staticobj, 206 LIBS = GatherLibs(env, staticlib) 207 ) 208 209## you might have to adjust the list of libs and the order for your setup 210## this is tricky, be warned 211fullstaticlib = [] 212 213## try to calculate the libs for fullstatic with ldd 214## 1. find the lib 215## 2. check the deps 216## 3. add them to the libs 217#searchlibs = os.pathsep.join([ '/lib/', '/usr/lib/', '/usr/local/lib/' ]) 218searchlibs = [] 219searchpathre = re.compile(r'\bSEARCH_DIR\("=([^"]+)"\)') 220f = os.popen('ld --verbose | grep SEARCH_DIR', 'r') 221for aword in searchpathre.findall(f.read()): 222 searchlibs += [ aword] 223f.close 224 225lddre = re.compile(r'^\s+lib([^=-]+)(?:-[\.0-9]+)?\.so\.[0-9]+ =>', re.MULTILINE) 226for libs in staticlib: 227 if type(libs) is types.StringType: libs = [ libs ] 228 for lib in libs: 229 fullstaticlib += [ lib ] 230 solibpath = WhereIsFile('lib' + lib + '.so', paths = searchlibs) 231 if solibpath is None: 232 continue 233 234 f = os.popen('ldd ' + solibpath, 'r') 235 for aword in lddre.findall(f.read()): 236 fullstaticlib += [ aword ] 237 f.close 238 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 241import platform 242fullstatic_libs = GatherLibs(env, fullstaticlib) 243fullstatic_linkflags = [staticenv['LINKFLAGS'], '-static'] 244if (('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') 247if 'gcc_s' in fullstatic_libs: 248 fullstatic_linkflags += ['-static-libgcc'] 249 fullstatic_libs.remove('gcc_s') 250 251## includes all modules, linked statically 252fullstaticbin = staticenv.Program('../fullstatic/build/lighttpd', 253 staticobj, 254 LIBS = fullstatic_libs, 255 LINKFLAGS= fullstatic_linkflags 256 ) 257 258Alias('static', staticbin) 259Alias('fullstatic', fullstaticbin) 260 261implib = 'lighttpd.exe.a' 262bin_targets = ['lighttpd'] 263bin_linkflags = [ env['LINKFLAGS'] ] 264if env['COMMON_LIB'] == 'lib': 265 common_lib = env.SharedLibrary('liblighttpd', common_src, LINKFLAGS = [ env['LINKFLAGS'], '-Wl,--export-dynamic' ]) 266else: 267 src += common_src 268 common_lib = [] 269 if env['COMMON_LIB'] == 'bin': 270 bin_linkflags += [ '-Wl,--export-all-symbols', '-Wl,--out-implib=build/' + implib ] 271 bin_targets += [ implib ] 272 else: 273 bin_linkflags += [ '-Wl,--export-dynamic' ] 274 275instbin = env.Program(bin_targets, src, LINKFLAGS = bin_linkflags, 276 LIBS = GatherLibs( 277 env, 278 common_lib, 279 env['LIBCRYPTO'], 280 env['LIBDL'], 281 env['LIBPCRE'], 282 ) 283) 284env.Depends(instbin, configparser) 285 286if env['COMMON_LIB'] == 'bin': 287 common_lib = instbin[1] 288 289env['SHLIBPREFIX'] = '' 290instlib = [] 291for 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)) 296env.Alias('modules', instlib) 297 298inst = [] 299 300if 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': 305 Default(common_lib) 306 inst += env.Install('${bindir}', common_lib) 307 308if env['build_static']: 309 Default(staticbin) 310 inst += env.Install('${sbindir}', staticbin) 311 312if env['build_fullstatic']: 313 Default(fullstaticbin) 314 inst += env.Install('${sbindir}', fullstaticbin) 315 316env.Alias('dynamic', instbin) 317# default all to be installed 318env.Alias('install', inst) 319 320pkgdir = '.' 321tarname = env['package'] + '-' + env['version'] 322