1import itertools 2import os 3import re 4import types 5from collections import OrderedDict 6from copy import copy 7 8# search any file, not just executables 9def WhereIsFile(file, paths): 10 for d in paths: 11 f = os.path.join(d, file) 12 if os.path.isfile(f): 13 try: 14 st = os.stat(f) 15 except OSError: 16 # os.stat() raises OSError, not IOError if the file 17 # doesn't exist, so in this case we let IOError get 18 # raised so as to not mask possibly serious disk or 19 # network issues. 20 continue 21 return os.path.normpath(f) 22 return None 23 24def FlattenLibs(libs): 25 if isinstance(libs, basestring): 26 return [libs] 27 else: 28 return [item for sublibs in libs for item in FlattenLibs(sublibs)] 29 30# removes all but the *LAST* occurance of a lib in the list 31def RemoveDuplicateLibs(libs): 32 libs = FlattenLibs(libs) 33 # remove empty strings from list 34 libs = list(filter(lambda x: x != '', libs)) 35 return list(reversed(OrderedDict.fromkeys(reversed(libs)))) 36 37Import('env') 38 39def WorkaroundFreeBSDLibOrder(libs): 40 # lib(re)ssl includes (weak) arc4random functions 41 # which "on purpose" might conflict with those in libc 42 # => link libc first solves this 43 # (required for FreeBSD11 fullstatic build) 44 import platform 45 if ('c' in libs) and (platform.system() == 'FreeBSD'): 46 return ['c'] + libs 47 return libs 48 49def GatherLibs(env, *libs): 50 libs = RemoveDuplicateLibs(env['LIBS'] + list(libs) + [env['APPEND_LIBS']]) 51 return WorkaroundFreeBSDLibOrder(libs) 52 53common_src = Split("base64.c buffer.c log.c \ 54 keyvalue.c chunk.c \ 55 http_chunk.c stream.c fdevent.c gw_backend.c \ 56 stat_cache.c plugin.c joblist.c etag.c array.c \ 57 data_string.c data_array.c \ 58 data_integer.c algo_sha1.c md5.c \ 59 vector.c \ 60 fdevent_select.c fdevent_libev.c \ 61 fdevent_poll.c fdevent_linux_sysepoll.c \ 62 fdevent_solaris_devpoll.c fdevent_solaris_port.c \ 63 fdevent_freebsd_kqueue.c \ 64 data_config.c \ 65 crc32.c \ 66 connections-glue.c \ 67 configfile-glue.c \ 68 http-header-glue.c \ 69 http_auth.c \ 70 http_vhostdb.c \ 71 sock_addr.c \ 72 splaytree.c \ 73 rand.c \ 74 status_counter.c safe_memclear.c \ 75") 76 77src = Split("server.c response.c connections.c \ 78 inet_ntop_cache.c \ 79 network.c \ 80 network_writev.c \ 81 network_write_mmap.c network_write_no_mmap.c \ 82 network_write.c network_linux_sendfile.c \ 83 network_freebsd_sendfile.c \ 84 network_solaris_sendfilev.c \ 85 network_darwin_sendfile.c \ 86 configfile.c configparser.c request.c proc_open.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_openssl']: 166 modules['mod_openssl'] = { 'src' : [ 'mod_openssl.c' ], 'lib' : [ env['LIBSSL'], env['LIBCRYPTO'] ] } 167 168staticenv = env.Clone(CPPFLAGS=[ env['CPPFLAGS'], '-DLIGHTTPD_STATIC' ]) 169 170## all the core-sources + the modules 171staticsrc = src + common_src 172 173staticlib = copy(env['LIBS']) 174staticinit = '' 175for module in modules.keys(): 176 staticsrc += modules[module]['src'] 177 staticinit += "PLUGIN_INIT(%s)\n"%module 178 if modules[module].has_key('lib'): 179 staticlib += modules[module]['lib'] 180 181def WriteStaticPluginHeader(target, source, env): 182 do_write = True 183 data = env['STATICINIT'] 184 # only touch the file if content actually changes 185 try: 186 with open(target[0].abspath, 'r') as f: 187 do_write = (data != f.read()) 188 except IOError: 189 pass 190 if do_write: 191 with open(target[0].abspath, 'w+') as f: 192 f.write(env['STATICINIT']) 193 194env['STATICINIT'] = staticinit 195staticheader = env.AlwaysBuild(env.Command('plugin-static.h', [], WriteStaticPluginHeader)) 196 197## turn all src-files into objects 198staticobj = [] 199static_plugin_obj = None 200for cfile in staticsrc: 201 if cfile == 'plugin.c': 202 static_plugin_obj = [ staticenv.Object('static-' + cfile.replace('.c', ''), cfile) ] 203 staticobj += static_plugin_obj 204 else: 205 staticobj += [ staticenv.Object('static-' + cfile.replace('.c', ''), cfile) ] 206env.Depends(static_plugin_obj, 'plugin-static.h') 207 208## includes all modules, but links dynamically against other libs 209staticbin = staticenv.Program('../static/build/lighttpd', 210 staticobj, 211 LIBS = GatherLibs(env, staticlib) 212 ) 213 214## you might have to adjust the list of libs and the order for your setup 215## this is tricky, be warned 216fullstaticlib = [] 217 218## try to calculate the libs for fullstatic with ldd 219## 1. find the lib 220## 2. check the deps 221## 3. add them to the libs 222#searchlibs = os.pathsep.join([ '/lib/', '/usr/lib/', '/usr/local/lib/' ]) 223searchlibs = [] 224searchpathre = re.compile(r'\bSEARCH_DIR\("=([^"]+)"\)') 225f = os.popen('ld --verbose | grep SEARCH_DIR', 'r') 226for aword in searchpathre.findall(f.read()): 227 searchlibs += [ aword] 228f.close 229 230lddre = re.compile(r'^\s+lib([^=-]+)(?:-[\.0-9]+)?\.so\.[0-9]+ =>', re.MULTILINE) 231for libs in staticlib: 232 if type(libs) is types.StringType: libs = [ libs ] 233 for lib in libs: 234 fullstaticlib += [ lib ] 235 solibpath = WhereIsFile('lib' + lib + '.so', paths = searchlibs) 236 if solibpath is None: 237 continue 238 239 f = os.popen('ldd ' + solibpath, 'r') 240 for aword in lddre.findall(f.read()): 241 fullstaticlib += [ aword ] 242 f.close 243 244## glibc pthread needs to be linked completely (especially nptl-init.o) 245## or not at all, or else pthread structures may not be initialized correctly 246import platform 247fullstatic_libs = GatherLibs(env, fullstaticlib) 248fullstatic_linkflags = [staticenv['LINKFLAGS'], '-static'] 249if (('pthread' in fullstatic_libs) or ('pcre' in fullstatic_libs)) and (platform.system() == 'Linux'): 250 fullstatic_linkflags += ['-Wl,--whole-archive','-lpthread','-Wl,--no-whole-archive'] 251 fullstatic_libs.remove('pthread') 252if 'gcc_s' in fullstatic_libs: 253 fullstatic_linkflags += ['-static-libgcc'] 254 fullstatic_libs.remove('gcc_s') 255 256## includes all modules, linked statically 257fullstaticbin = staticenv.Program('../fullstatic/build/lighttpd', 258 staticobj, 259 LIBS = fullstatic_libs, 260 LINKFLAGS= fullstatic_linkflags 261 ) 262 263Alias('static', staticbin) 264Alias('fullstatic', fullstaticbin) 265 266implib = 'lighttpd.exe.a' 267bin_targets = ['lighttpd'] 268bin_linkflags = [ env['LINKFLAGS'] ] 269if env['COMMON_LIB'] == 'lib': 270 common_lib = env.SharedLibrary('liblighttpd', common_src, LINKFLAGS = [ env['LINKFLAGS'], '-Wl,--export-dynamic' ]) 271else: 272 src += common_src 273 common_lib = [] 274 if env['COMMON_LIB'] == 'bin': 275 bin_linkflags += [ '-Wl,--export-all-symbols', '-Wl,--out-implib=build/' + implib ] 276 bin_targets += [ implib ] 277 else: 278 bin_linkflags += [ '-Wl,--export-dynamic' ] 279 280instbin = env.Program(bin_targets, src, LINKFLAGS = bin_linkflags, 281 LIBS = GatherLibs( 282 env, 283 common_lib, 284 env['LIBCRYPTO'], 285 env['LIBDL'], 286 env['LIBPCRE'], 287 ) 288) 289env.Depends(instbin, configparser) 290 291if env['COMMON_LIB'] == 'bin': 292 common_lib = instbin[1] 293 294env['SHLIBPREFIX'] = '' 295instlib = [] 296for module in modules.keys(): 297 libs = [ common_lib ] 298 if modules[module].has_key('lib'): 299 libs += modules[module]['lib'] 300 instlib += env.SharedLibrary(module, modules[module]['src'], LIBS = GatherLibs(env, libs)) 301env.Alias('modules', instlib) 302 303inst = [] 304 305if env['build_dynamic']: 306 Default(instbin[0], instlib) 307 inst += env.Install('${sbindir}', instbin[0]) 308 inst += env.Install('${libdir}', instlib) 309 if env['COMMON_LIB'] == 'lib': 310 Default(common_lib) 311 inst += env.Install('${bindir}', common_lib) 312 313if env['build_static']: 314 Default(staticbin) 315 inst += env.Install('${sbindir}', staticbin) 316 317if env['build_fullstatic']: 318 Default(fullstaticbin) 319 inst += env.Install('${sbindir}', fullstaticbin) 320 321env.Alias('dynamic', instbin) 322# default all to be installed 323env.Alias('install', inst) 324 325pkgdir = '.' 326tarname = env['package'] + '-' + env['version'] 327