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