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