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