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