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