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