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 joblist.c etag.c array.c \ 62 data_string.c data_array.c \ 63 data_integer.c algo_sha1.c md5.c \ 64 vector.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 data_config.c \ 70 crc32.c \ 71 connections-glue.c \ 72 configfile-glue.c \ 73 http-header-glue.c \ 74 http_auth.c \ 75 http_vhostdb.c \ 76 sock_addr.c \ 77 splaytree.c \ 78 rand.c \ 79 status_counter.c safe_memclear.c \ 80") 81 82src = Split("server.c response.c connections.c \ 83 inet_ntop_cache.c \ 84 network.c \ 85 network_write.c \ 86 configfile.c configparser.c request.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') 95mod_ssi_exprparser = Lemon(env, 'mod_ssi_exprparser.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_alias' : { 'src' : [ 'mod_alias.c' ] }, 102 'mod_auth' : { 'src' : [ 'mod_auth.c' ] }, 103 'mod_authn_file' : { 'src' : [ 'mod_authn_file.c' ], 'lib' : [ env['LIBCRYPT'], env['LIBCRYPTO'] ] }, 104 'mod_cgi' : { 'src' : [ 'mod_cgi.c' ] }, 105 'mod_compress' : { 'src' : [ 'mod_compress.c' ], 'lib' : [ env['LIBZ'], env['LIBBZ2'] ] }, 106 'mod_deflate' : { 'src' : [ 'mod_deflate.c' ], 'lib' : [ env['LIBZ'], env['LIBBZ2'] ] }, 107 'mod_dirlisting' : { 'src' : [ 'mod_dirlisting.c' ], 'lib' : [ env['LIBPCRE'] ] }, 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_flv_streaming' : { 'src' : [ 'mod_flv_streaming.c' ] }, 114 'mod_indexfile' : { 'src' : [ 'mod_indexfile.c' ] }, 115 'mod_proxy' : { 'src' : [ 'mod_proxy.c' ] }, 116 'mod_redirect' : { 'src' : [ 'mod_redirect.c' ], 'lib' : [ env['LIBPCRE'] ] }, 117 'mod_rewrite' : { 'src' : [ 'mod_rewrite.c' ], 'lib' : [ env['LIBPCRE'] ] }, 118 'mod_rrdtool' : { 'src' : [ 'mod_rrdtool.c' ] }, 119 'mod_scgi' : { 'src' : [ 'mod_scgi.c' ] }, 120 'mod_secdownload' : { 'src' : [ 'mod_secdownload.c' ], 'lib' : [ env['LIBCRYPTO'] ] }, 121 'mod_setenv' : { 'src' : [ 'mod_setenv.c' ] }, 122 'mod_simple_vhost' : { 'src' : [ 'mod_simple_vhost.c' ] }, 123 'mod_ssi' : { 'src' : [ 'mod_ssi_exprparser.c', 'mod_ssi_expr.c', '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' ] }, 129 'mod_vhostdb' : { 'src' : [ 'mod_vhostdb.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_geoip']: 135 modules['mod_geoip'] = { 'src' : [ 'mod_geoip.c' ], 'lib' : [ env['LIBGEOIP'] ] } 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'] = { 'src' : [ 'mod_magnet.c', 'mod_magnet_cache.c' ], 'lib' : [ env['LIBLUA'] ] } 146 modules['mod_cml'] = { 147 'src' : [ 'mod_cml_lua.c', 'mod_cml.c', 'mod_cml_funcs.c' ], 148 'lib' : [ env['LIBMEMCACHED'], env['LIBLUA'] ] 149 } 150 151if env['with_pcre'] and (env['with_memcached'] or env['with_gdbm']): 152 modules['mod_trigger_b4_dl'] = { 'src' : [ 'mod_trigger_b4_dl.c' ], 'lib' : [ env['LIBPCRE'], env['LIBMEMCACHED'], env['LIBGDBM'] ] } 153 154if env['with_mysql']: 155 modules['mod_authn_mysql'] = { 'src' : [ 'mod_authn_mysql.c' ], 'lib' : [ env['LIBCRYPT'], env['LIBMYSQL'] ] } 156 modules['mod_mysql_vhost'] = { 'src' : [ 'mod_mysql_vhost.c' ], 'lib' : [ env['LIBMYSQL'] ] } 157 modules['mod_vhostdb_mysql'] = { 'src' : [ 'mod_vhostdb_mysql.c' ], 'lib' : [ env['LIBMYSQL'] ] } 158 159if env['with_pgsql']: 160 modules['mod_vhostdb_pgsql'] = { 'src' : [ 'mod_vhostdb_pgsql.c' ], 'lib' : [ env['LIBPGSQL'] ] } 161 162if env['with_dbi']: 163 modules['mod_vhostdb_dbi'] = { 'src' : [ 'mod_vhostdb_dbi.c' ], 'lib' : [ env['LIBDBI'] ] } 164 165if env['with_sasl']: 166 modules['mod_authn_sasl'] = { 'src' : [ 'mod_authn_sasl.c' ], 'lib' : [ env['LIBSASL'] ] } 167 168if env['with_openssl']: 169 modules['mod_openssl'] = { 'src' : [ 'mod_openssl.c' ], 'lib' : [ env['LIBSSL'], env['LIBCRYPTO'] ] } 170 171staticenv = env.Clone(CPPFLAGS=[ env['CPPFLAGS'], '-DLIGHTTPD_STATIC' ]) 172 173## all the core-sources + the modules 174staticsrc = src + common_src 175 176staticlib = copy(env['LIBS']) 177staticinit = '' 178for module in modules.keys(): 179 staticsrc += modules[module]['src'] 180 staticinit += "PLUGIN_INIT(%s)\n"%module 181 if 'lib' in modules[module]: 182 staticlib += modules[module]['lib'] 183 184def WriteStaticPluginHeader(target, source, env): 185 do_write = True 186 data = env['STATICINIT'] 187 # only touch the file if content actually changes 188 try: 189 with open(target[0].abspath, 'r') as f: 190 do_write = (data != f.read()) 191 except IOError: 192 pass 193 if do_write: 194 with open(target[0].abspath, 'w+') as f: 195 f.write(env['STATICINIT']) 196 197env['STATICINIT'] = staticinit 198staticheader = env.AlwaysBuild(env.Command('plugin-static.h', [], WriteStaticPluginHeader)) 199 200## turn all src-files into objects 201staticobj = [] 202static_plugin_obj = None 203for cfile in staticsrc: 204 if cfile == 'plugin.c': 205 static_plugin_obj = [ staticenv.Object('static-' + cfile.replace('.c', ''), cfile) ] 206 staticobj += static_plugin_obj 207 else: 208 staticobj += [ staticenv.Object('static-' + cfile.replace('.c', ''), cfile) ] 209env.Depends(static_plugin_obj, 'plugin-static.h') 210 211## includes all modules, but links dynamically against other libs 212staticbin = staticenv.Program('../static/build/lighttpd', 213 staticobj, 214 LIBS = GatherLibs(env, staticlib) 215 ) 216 217## you might have to adjust the list of libs and the order for your setup 218## this is tricky, be warned 219fullstaticlib = [] 220 221## try to calculate the libs for fullstatic with ldd 222## 1. find the lib 223## 2. check the deps 224## 3. add them to the libs 225#searchlibs = os.pathsep.join([ '/lib/', '/usr/lib/', '/usr/local/lib/' ]) 226searchlibs = [] 227searchpathre = re.compile(r'\bSEARCH_DIR\("=([^"]+)"\)') 228f = os.popen('ld --verbose | grep SEARCH_DIR', 'r') 229for aword in searchpathre.findall(f.read()): 230 searchlibs += [ aword] 231f.close 232 233lddre = re.compile(r'^\s+lib([^=-]+)(?:-[\.0-9]+)?\.so\.[0-9]+ =>', re.MULTILINE) 234for libs in staticlib: 235 if isinstance(libs, string_types): libs = [ libs ] 236 for lib in libs: 237 fullstaticlib += [ lib ] 238 solibpath = WhereIsFile('lib' + lib + '.so', paths = searchlibs) 239 if solibpath is None: 240 continue 241 242 f = os.popen('ldd ' + solibpath, 'r') 243 for aword in lddre.findall(f.read()): 244 fullstaticlib += [ aword ] 245 f.close 246 247## glibc pthread needs to be linked completely (especially nptl-init.o) 248## or not at all, or else pthread structures may not be initialized correctly 249import platform 250fullstatic_libs = GatherLibs(env, fullstaticlib) 251fullstatic_linkflags = [staticenv['LINKFLAGS'], '-static'] 252if (('pthread' in fullstatic_libs) or ('pcre' in fullstatic_libs)) and (platform.system() == 'Linux'): 253 fullstatic_linkflags += ['-Wl,--whole-archive','-lpthread','-Wl,--no-whole-archive'] 254 fullstatic_libs.remove('pthread') 255if 'gcc_s' in fullstatic_libs: 256 fullstatic_linkflags += ['-static-libgcc'] 257 fullstatic_libs.remove('gcc_s') 258 259## includes all modules, linked statically 260fullstaticbin = staticenv.Program('../fullstatic/build/lighttpd', 261 staticobj, 262 LIBS = fullstatic_libs, 263 LINKFLAGS= fullstatic_linkflags 264 ) 265 266Alias('static', staticbin) 267Alias('fullstatic', fullstaticbin) 268 269implib = 'lighttpd.exe.a' 270bin_targets = ['lighttpd'] 271bin_linkflags = [ env['LINKFLAGS'] ] 272if env['COMMON_LIB'] == 'lib': 273 common_lib = env.SharedLibrary('liblighttpd', common_src, LINKFLAGS = [ env['LINKFLAGS'], '-Wl,--export-dynamic' ]) 274else: 275 src += common_src 276 common_lib = [] 277 if env['COMMON_LIB'] == 'bin': 278 bin_linkflags += [ '-Wl,--export-all-symbols', '-Wl,--out-implib=build/' + implib ] 279 bin_targets += [ implib ] 280 else: 281 bin_linkflags += [ '-Wl,--export-dynamic' ] 282 283instbin = env.Program(bin_targets, src, LINKFLAGS = bin_linkflags, 284 LIBS = GatherLibs( 285 env, 286 common_lib, 287 env['LIBCRYPTO'], 288 env['LIBDL'], 289 env['LIBPCRE'], 290 ) 291) 292env.Depends(instbin, configparser) 293 294if env['COMMON_LIB'] == 'bin': 295 common_lib = instbin[1] 296 297env['SHLIBPREFIX'] = '' 298instlib = [] 299for module in modules.keys(): 300 libs = [ common_lib ] 301 if 'lib' in modules[module]: 302 libs += modules[module]['lib'] 303 instlib += env.SharedLibrary(module, modules[module]['src'], LIBS = GatherLibs(env, libs)) 304env.Alias('modules', instlib) 305 306inst = [] 307 308if env['build_dynamic']: 309 Default(instbin[0], instlib) 310 inst += env.Install('${sbindir}', instbin[0]) 311 inst += env.Install('${libdir}', instlib) 312 if env['COMMON_LIB'] == 'lib': 313 Default(common_lib) 314 inst += env.Install('${bindir}', common_lib) 315 316if env['build_static']: 317 Default(staticbin) 318 inst += env.Install('${sbindir}', staticbin) 319 320if env['build_fullstatic']: 321 Default(fullstaticbin) 322 inst += env.Install('${sbindir}', fullstaticbin) 323 324env.Alias('dynamic', instbin) 325# default all to be installed 326env.Alias('install', inst) 327 328pkgdir = '.' 329tarname = env['package'] + '-' + env['version'] 330