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