1import os 2import re 3import types 4import itertools 5from collections import OrderedDict 6 7# search any file, not just executables 8def WhereIsFile(file, paths): 9 for d in paths: 10 f = os.path.join(d, file) 11 if os.path.isfile(f): 12 try: 13 st = os.stat(f) 14 except OSError: 15 # os.stat() raises OSError, not IOError if the file 16 # doesn't exist, so in this case we let IOError get 17 # raised so as to not mask possibly serious disk or 18 # network issues. 19 continue 20 return os.path.normpath(f) 21 return None 22 23def FlattenLibs(libs): 24 if isinstance(libs, basestring): 25 return [libs] 26 else: 27 return [item for sublibs in libs for item in FlattenLibs(sublibs)] 28 29# removes all but the *LAST* occurance of a lib in the list 30def RemoveDuplicateLibs(libs): 31 libs = FlattenLibs(libs) 32 # remove empty strings from list 33 libs = list(filter(lambda x: x != '', libs)) 34 return list(reversed(OrderedDict.fromkeys(reversed(libs)))) 35 36Import('env') 37 38def WorkaroundFreeBSDLibOrder(libs): 39 # lib(re)ssl includes (weak) arc4random functions 40 # which "on purpose" might conflict with those in libc 41 # => link libc first solves this 42 # (required for FreeBSD11 fullstatic build) 43 import platform 44 if ('c' in libs) and (platform.system() == 'FreeBSD'): 45 return ['c'] + libs 46 return libs 47 48def GatherLibs(env, *libs): 49 libs = RemoveDuplicateLibs(env['LIBS'] + list(libs) + [env['APPEND_LIBS']]) 50 return WorkaroundFreeBSDLibOrder(libs) 51 52common_src = Split("base64.c buffer.c log.c \ 53 keyvalue.c chunk.c \ 54 http_chunk.c stream.c fdevent.c \ 55 stat_cache.c plugin.c joblist.c etag.c array.c \ 56 data_string.c data_array.c \ 57 data_integer.c md5.c data_fastcgi.c \ 58 vector.c \ 59 fdevent_select.c fdevent_libev.c \ 60 fdevent_poll.c fdevent_linux_sysepoll.c \ 61 fdevent_solaris_devpoll.c fdevent_solaris_port.c \ 62 fdevent_freebsd_kqueue.c \ 63 data_config.c \ 64 inet_ntop_cache.c crc32.c \ 65 connections-glue.c \ 66 configfile-glue.c \ 67 http-header-glue.c \ 68 http_auth.c \ 69 splaytree.c network_writev.c \ 70 network_write_mmap.c network_write_no_mmap.c \ 71 network_write.c network_linux_sendfile.c \ 72 network_freebsd_sendfile.c \ 73 network_solaris_sendfilev.c network_openssl.c \ 74 rand.c \ 75 status_counter.c safe_memclear.c network_darwin_sendfile.c \ 76") 77 78src = Split("server.c response.c connections.c network.c \ 79 configfile.c configparser.c request.c proc_open.c") 80 81lemon = env.Program('lemon', 'lemon.c', LIBS = GatherLibs(env)) 82 83def Lemon(env, input): 84 parser = env.Command([input.replace('.y', '.c'),input.replace('.y', '.h')], input, '(cd sconsbuild/build; ../../' + lemon[0].path + ' -q ../../$SOURCE ../../src/lempar.c)') 85 env.Depends(parser, lemon) 86 87configparser = Lemon(env, 'configparser.y') 88mod_ssi_exprparser = Lemon(env, 'mod_ssi_exprparser.y') 89 90## the modules and how they are built 91modules = { 92 'mod_access' : { 'src' : [ 'mod_access.c' ] }, 93 'mod_alias' : { 'src' : [ 'mod_alias.c' ] }, 94 'mod_cgi' : { 'src' : [ 'mod_cgi.c' ] }, 95 'mod_fastcgi' : { 'src' : [ 'mod_fastcgi.c' ] }, 96 'mod_scgi' : { 'src' : [ 'mod_scgi.c' ] }, 97 'mod_extforward' : { 'src' : [ 'mod_extforward.c' ] }, 98 'mod_staticfile' : { 'src' : [ 'mod_staticfile.c' ] }, 99 'mod_dirlisting' : { 'src' : [ 'mod_dirlisting.c' ], 'lib' : [ env['LIBPCRE'] ] }, 100 'mod_indexfile' : { 'src' : [ 'mod_indexfile.c' ] }, 101 'mod_setenv' : { 'src' : [ 'mod_setenv.c' ] }, 102 'mod_rrdtool' : { 'src' : [ 'mod_rrdtool.c' ] }, 103 'mod_usertrack' : { 'src' : [ 'mod_usertrack.c' ] }, 104 'mod_proxy' : { 'src' : [ 'mod_proxy.c' ] }, 105 'mod_userdir' : { 'src' : [ 'mod_userdir.c' ] }, 106 'mod_secdownload' : { 'src' : [ 'mod_secdownload.c' ] }, 107 'mod_accesslog' : { 'src' : [ 'mod_accesslog.c' ] }, 108 'mod_simple_vhost' : { 'src' : [ 'mod_simple_vhost.c' ] }, 109 'mod_evhost' : { 'src' : [ 'mod_evhost.c' ] }, 110 'mod_expire' : { 'src' : [ 'mod_expire.c' ] }, 111 'mod_status' : { 'src' : [ 'mod_status.c' ] }, 112 'mod_compress' : { 'src' : [ 'mod_compress.c' ], 'lib' : [ env['LIBZ'], env['LIBBZ2'] ] }, 113 'mod_deflate' : { 'src' : [ 'mod_deflate.c' ], 'lib' : [ env['LIBZ'], env['LIBBZ2'] ] }, 114 'mod_redirect' : { 'src' : [ 'mod_redirect.c' ], 'lib' : [ env['LIBPCRE'] ] }, 115 'mod_rewrite' : { 'src' : [ 'mod_rewrite.c' ], 'lib' : [ env['LIBPCRE'] ] }, 116 'mod_auth' : { 'src' : [ 'mod_auth.c' ] }, 117 'mod_authn_file' : { 'src' : [ 'mod_authn_file.c' ], 'lib' : [ env['LIBCRYPT'] ] }, 118 'mod_webdav' : { 'src' : [ 'mod_webdav.c' ], 'lib' : [ env['LIBXML2'], env['LIBSQLITE3'], env['LIBUUID'] ] }, 119 'mod_uploadprogress' : { 'src' : [ 'mod_uploadprogress.c' ] }, 120 'mod_evasive' : { 'src' : [ 'mod_evasive.c' ] }, 121 'mod_ssi' : { 'src' : [ 'mod_ssi_exprparser.c', 'mod_ssi_expr.c', 'mod_ssi.c' ] }, 122 'mod_flv_streaming' : { 'src' : [ 'mod_flv_streaming.c' ] }, 123} 124 125if env['with_geoip']: 126 modules['mod_geoip'] = { 'src' : [ 'mod_geoip.c' ], 'lib' : [ env['LIBGEOIP'] ] } 127 128if env['with_krb5']: 129 modules['mod_authn_gssapi'] = { 'src' : [ 'mod_authn_gssapi.c' ], 'lib' : [ env['LIBKRB5'], env['LIBGSSAPI_KRB5'] ] } 130 131if env['with_ldap']: 132 modules['mod_authn_ldap'] = { 'src' : [ 'mod_authn_ldap.c' ], 'lib' : [ env['LIBLDAP'], env['LIBLBER'] ] } 133 134if env['with_lua']: 135 modules['mod_magnet'] = { 'src' : [ 'mod_magnet.c', 'mod_magnet_cache.c' ], 'lib' : [ env['LIBLUA'] ] } 136 modules['mod_cml'] = { 137 'src' : [ 'mod_cml_lua.c', 'mod_cml.c', 'mod_cml_funcs.c' ], 138 'lib' : [ env['LIBPCRE'], env['LIBMEMCACHED'], env['LIBLUA'] ] 139 } 140 141if env['with_pcre'] and (env['with_memcached'] or env['with_gdbm']): 142 modules['mod_trigger_b4_dl'] = { 'src' : [ 'mod_trigger_b4_dl.c' ], 'lib' : [ env['LIBPCRE'], env['LIBMEMCACHED'] ] } 143 144if env['with_mysql']: 145 modules['mod_authn_mysql'] = { 'src' : [ 'mod_authn_mysql.c' ], 'lib' : [ env['LIBCRYPT'], env['LIBMYSQL'] ] } 146 modules['mod_mysql_vhost'] = { 'src' : [ 'mod_mysql_vhost.c' ], 'lib' : [ env['LIBMYSQL'] ] } 147 148staticenv = env.Clone(CPPFLAGS=[ env['CPPFLAGS'], '-DLIGHTTPD_STATIC' ]) 149 150## all the core-sources + the modules 151staticsrc = src + common_src 152 153staticlib = env['LIBS'] 154staticinit = '' 155for module in modules.keys(): 156 staticsrc += modules[module]['src'] 157 staticinit += "PLUGIN_INIT(%s)\n"%module 158 if modules[module].has_key('lib'): 159 staticlib += modules[module]['lib'] 160 161def WriteStaticPluginHeader(target, source, env): 162 do_write = True 163 data = env['STATICINIT'] 164 # only touch the file if content actually changes 165 try: 166 with open(target[0].abspath, 'r') as f: 167 do_write = (data != f.read()) 168 except IOError: 169 pass 170 if do_write: 171 with open(target[0].abspath, 'w+') as f: 172 f.write(env['STATICINIT']) 173 174env['STATICINIT'] = staticinit 175staticheader = env.AlwaysBuild(env.Command('plugin-static.h', [], WriteStaticPluginHeader)) 176 177## turn all src-files into objects 178staticobj = [] 179static_plugin_obj = None 180for cfile in staticsrc: 181 if cfile == 'plugin.c': 182 static_plugin_obj = [ staticenv.Object('static-' + cfile.replace('.c', ''), cfile) ] 183 staticobj += static_plugin_obj 184 else: 185 staticobj += [ staticenv.Object('static-' + cfile.replace('.c', ''), cfile) ] 186env.Depends(static_plugin_obj, 'plugin-static.h') 187 188## includes all modules, but links dynamically against other libs 189staticbin = staticenv.Program('../static/build/lighttpd', 190 staticobj, 191 LIBS = GatherLibs(env, staticlib) 192 ) 193 194## you might have to adjust the list of libs and the order for your setup 195## this is tricky, be warned 196fullstaticlib = [] 197 198## try to calculate the libs for fullstatic with ldd 199## 1. find the lib 200## 2. check the deps 201## 3. add them to the libs 202#searchlibs = os.pathsep.join([ '/lib/', '/usr/lib/', '/usr/local/lib/' ]) 203searchlibs = [] 204searchpathre = re.compile(r'\bSEARCH_DIR\("=([^"]+)"\)') 205f = os.popen('ld --verbose | grep SEARCH_DIR', 'r') 206for aword in searchpathre.findall(f.read()): 207 searchlibs += [ aword] 208f.close 209 210lddre = re.compile(r'^\s+lib([^=-]+)(?:-[\.0-9]+)?\.so\.[0-9]+ =>', re.MULTILINE) 211for libs in staticlib: 212 if type(libs) is types.StringType: libs = [ libs ] 213 for lib in libs: 214 fullstaticlib += [ lib ] 215 solibpath = WhereIsFile('lib' + lib + '.so', paths = searchlibs) 216 if solibpath is None: 217 continue 218 219 f = os.popen('ldd ' + solibpath, 'r') 220 for aword in lddre.findall(f.read()): 221 fullstaticlib += [ aword ] 222 f.close 223 224## includes all modules, linked statically 225fullstaticbin = staticenv.Program('../fullstatic/build/lighttpd', 226 staticobj, 227 LIBS = GatherLibs(env, fullstaticlib), 228 LINKFLAGS= [staticenv['LINKFLAGS'], '-static'] 229 ) 230 231Alias('static', staticbin) 232Alias('fullstatic', fullstaticbin) 233 234implib = 'lighttpd.exe.a' 235bin_targets = ['lighttpd'] 236bin_linkflags = [ env['LINKFLAGS'] ] 237if env['COMMON_LIB'] == 'lib': 238 common_lib = env.SharedLibrary('liblighttpd', common_src, LINKFLAGS = [ env['LINKFLAGS'], '-Wl,--export-dynamic' ]) 239else: 240 src += common_src 241 common_lib = [] 242 if env['COMMON_LIB'] == 'bin': 243 bin_linkflags += [ '-Wl,--export-all-symbols', '-Wl,--out-implib=build/' + implib ] 244 bin_targets += [ implib ] 245 else: 246 bin_linkflags += [ '-Wl,--export-dynamic' ] 247 248instbin = env.Program(bin_targets, src, LINKFLAGS = bin_linkflags, LIBS = GatherLibs(env, env['LIBS'], common_lib, env['LIBDL'])) 249env.Depends(instbin, configparser) 250 251if env['COMMON_LIB'] == 'bin': 252 common_lib = instbin[1] 253 254env['SHLIBPREFIX'] = '' 255instlib = [] 256for module in modules.keys(): 257 libs = [ common_lib ] 258 if modules[module].has_key('lib'): 259 libs += modules[module]['lib'] 260 instlib += env.SharedLibrary(module, modules[module]['src'], LIBS= GatherLibs(env, libs)) 261env.Alias('modules', instlib) 262 263inst = [] 264 265if env['build_dynamic']: 266 Default(instbin[0], instlib) 267 inst += env.Install('${sbindir}', instbin[0]) 268 inst += env.Install('${libdir}', instlib) 269 if env['COMMON_LIB'] == 'lib': 270 Default(common_lib) 271 inst += env.Install('${bindir}', common_lib) 272 273if env['build_static']: 274 Default(staticbin) 275 inst += env.Install('${sbindir}', staticbin) 276 277if env['build_fullstatic']: 278 Default(fullstaticbin) 279 inst += env.Install('${sbindir}', fullstaticbin) 280 281env.Alias('dynamic', instbin) 282# default all to be installed 283env.Alias('install', inst) 284 285pkgdir = '.' 286tarname = env['package'] + '-' + env['version'] 287