[SCons] define with_krb5 for SCons build
[lighttpd.git] / src / SConscript
blobfca71503e2fe1353a3de8418431445e227e1837c
1 import os
2 import re
3 import types
4 import itertools
5 from collections import OrderedDict
7 # search any file, not just executables
8 def 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
23 def FlattenLibs(libs):
24         if isinstance(libs, basestring):
25                 return [libs]
26         else:
27                 return [item for sublibs in libs for item in FlattenLibs(sublibs)]
29 # removes all but the *LAST* occurance of a lib in the list
30 def 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))))
36 Import('env')
38 def GatherLibs(env, *libs):
39         return RemoveDuplicateLibs(env['LIBS'] + list(libs) + [env['APPEND_LIBS']])
41 common_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         status_counter.c safe_memclear.c network_darwin_sendfile.c \
66 src = Split("server.c response.c connections.c network.c \
67         configfile.c configparser.c request.c proc_open.c")
69 lemon = env.Program('lemon', 'lemon.c', LIBS = GatherLibs(env))
71 def Lemon(env, input):
72         parser = env.Command([input.replace('.y', '.c'),input.replace('.y', '.h')], input, '(cd sconsbuild/build; ../../' + lemon[0].path + ' -q ../../$SOURCE ../../src/lempar.c)')
73         env.Depends(parser, lemon)
75 configparser = Lemon(env, 'configparser.y')
76 mod_ssi_exprparser = Lemon(env, 'mod_ssi_exprparser.y')
78 ## the modules and how they are built
79 modules = {
80         'mod_access' : { 'src' : [ 'mod_access.c' ] },
81         'mod_alias' : { 'src' : [ 'mod_alias.c' ] },
82         'mod_cgi' : { 'src' : [ 'mod_cgi.c' ] },
83         'mod_fastcgi' : { 'src' : [ 'mod_fastcgi.c' ] },
84         'mod_scgi' : { 'src' : [ 'mod_scgi.c' ] },
85         'mod_extforward' : { 'src' : [ 'mod_extforward.c' ] },
86         'mod_staticfile' : { 'src' : [ 'mod_staticfile.c' ] },
87         'mod_dirlisting' : { 'src' : [ 'mod_dirlisting.c' ], 'lib' : [ env['LIBPCRE'] ] },
88         'mod_indexfile' : { 'src' : [ 'mod_indexfile.c' ] },
89         'mod_setenv' : { 'src' : [ 'mod_setenv.c' ] },
90         'mod_rrdtool' : { 'src' : [ 'mod_rrdtool.c' ] },
91         'mod_usertrack' : { 'src' : [ 'mod_usertrack.c' ] },
92         'mod_proxy' : { 'src' : [ 'mod_proxy.c' ] },
93         'mod_userdir' : { 'src' : [ 'mod_userdir.c' ] },
94         'mod_secdownload' : { 'src' : [ 'mod_secdownload.c' ] },
95         'mod_accesslog' : { 'src' : [ 'mod_accesslog.c' ] },
96         'mod_simple_vhost' : { 'src' : [ 'mod_simple_vhost.c' ] },
97         'mod_evhost' : { 'src' : [ 'mod_evhost.c' ] },
98         'mod_expire' : { 'src' : [ 'mod_expire.c' ] },
99         'mod_status' : { 'src' : [ 'mod_status.c' ] },
100         'mod_compress' : { 'src' : [ 'mod_compress.c' ], 'lib' : [ env['LIBZ'], env['LIBBZ2'] ] },
101         'mod_deflate' : { 'src' : [ 'mod_deflate.c' ], 'lib' : [ env['LIBZ'], env['LIBBZ2'] ] },
102         'mod_redirect' : { 'src' : [ 'mod_redirect.c' ], 'lib' : [ env['LIBPCRE'] ] },
103         'mod_rewrite' : { 'src' : [ 'mod_rewrite.c' ], 'lib' : [ env['LIBPCRE'] ] },
104         'mod_auth' : { 'src' : [ 'mod_auth.c' ] },
105         'mod_authn_file' : { 'src' : [ 'mod_authn_file.c' ], 'lib' : [ env['LIBCRYPT'] ] },
106         'mod_authn_gssapi' : { 'src' : [ 'mod_authn_gssapi.c' ], 'lib' : [ env['LIBRESOLV'], env['LIBKRB5'], env['LIBGSSAPI_KRB5'] ] },
107         'mod_authn_ldap' : { 'src' : [ 'mod_authn_ldap.c' ], 'lib' : [ env['LIBLDAP'], env['LIBLBER'] ] },
108         'mod_authn_mysql' : { 'src' : [ 'mod_authn_mysql.c' ], 'lib' : [ env['LIBCRYPT'], env['LIBMYSQL'] ] },
109         'mod_webdav' : { 'src' : [ 'mod_webdav.c' ], 'lib' : [ env['LIBXML2'], env['LIBSQLITE3'], env['LIBUUID'] ] },
110         'mod_mysql_vhost' : { 'src' : [ 'mod_mysql_vhost.c' ], 'lib' : [ env['LIBMYSQL'] ] },
111         'mod_uploadprogress' : { 'src' : [ 'mod_uploadprogress.c' ] },
112         'mod_evasive' : { 'src' : [ 'mod_evasive.c' ] },
113         'mod_ssi' : { 'src' : [ 'mod_ssi_exprparser.c', 'mod_ssi_expr.c', 'mod_ssi.c' ] },
114         'mod_flv_streaming' : { 'src' : [ 'mod_flv_streaming.c' ] },
115         'mod_cml': {
116                 'src' : [ 'mod_cml_lua.c', 'mod_cml.c', 'mod_cml_funcs.c' ],
117                 'lib' : [ env['LIBPCRE'], env['LIBMEMCACHED'], env['LIBLUA'] ]
118         },
121 if env['with_memcached']:
122         modules['mod_trigger_b4_dl'] = { 'src' : [ 'mod_trigger_b4_dl.c' ], 'lib' : [ env['LIBPCRE'], env['LIBMEMCACHED'] ] }
124 if env['with_lua']:
125         modules['mod_magnet'] = { 'src' : [ 'mod_magnet.c', 'mod_magnet_cache.c' ], 'lib' : [ env['LIBLUA'] ] }
127 if env['with_geoip']:
128         modules['mod_geoip'] = { 'src' : [ 'mod_geoip.c' ], 'lib' : [ env['LIBGEOIP'] ] },
130 staticenv = env.Clone(CPPFLAGS=[ env['CPPFLAGS'], '-DLIGHTTPD_STATIC' ])
132 ## all the core-sources + the modules
133 staticsrc = src + common_src
135 staticlib = env['LIBS']
136 staticinit = ''
137 for module in modules.keys():
138         staticsrc += modules[module]['src']
139         staticinit += "PLUGIN_INIT(%s)\n"%module
140         if modules[module].has_key('lib'):
141                 staticlib += modules[module]['lib']
143 def WriteStaticPluginHeader(target, source, env):
144         do_write = True
145         data = env['STATICINIT']
146         # only touch the file if content actually changes
147         try:
148                 with open(target[0].abspath, 'r') as f:
149                         do_write = (data != f.read())
150         except IOError:
151                 pass
152         if do_write:
153                 with open(target[0].abspath, 'w+') as f:
154                         f.write(env['STATICINIT'])
156 env['STATICINIT'] = staticinit
157 staticheader = env.AlwaysBuild(env.Command('plugin-static.h', [], WriteStaticPluginHeader))
159 ## turn all src-files into objects
160 staticobj = []
161 static_plugin_obj = None
162 for cfile in staticsrc:
163         if cfile == 'plugin.c':
164                 static_plugin_obj = [ staticenv.Object('static-' + cfile.replace('.c', ''), cfile) ]
165                 staticobj += static_plugin_obj
166         else:
167                 staticobj += [ staticenv.Object('static-' + cfile.replace('.c', ''), cfile) ]
168 env.Depends(static_plugin_obj, 'plugin-static.h')
170 ## includes all modules, but links dynamically against other libs
171 staticbin = staticenv.Program('../static/build/lighttpd',
172         staticobj,
173         LIBS = GatherLibs(env, staticlib)
174         )
176 ## you might have to adjust the list of libs and the order for your setup
177 ## this is tricky, be warned
178 fullstaticlib = []
180 ## try to calculate the libs for fullstatic with ldd
181 ## 1. find the lib
182 ## 2. check the deps
183 ## 3. add them to the libs
184 #searchlibs = os.pathsep.join([ '/lib/', '/usr/lib/', '/usr/local/lib/' ])
185 searchlibs = []
186 searchpathre = re.compile(r'\bSEARCH_DIR\("=([^"]+)"\)')
187 f = os.popen('ld --verbose | grep SEARCH_DIR', 'r')
188 for aword in searchpathre.findall(f.read()):
189         searchlibs += [ aword]
190 f.close
192 lddre = re.compile(r'^\s+lib([^=-]+)(?:-[\.0-9]+)?\.so\.[0-9]+ =>', re.MULTILINE)
193 for libs in staticlib:
194         if type(libs) is types.StringType: libs = [ libs ]
195         for lib in libs:
196                 fullstaticlib += [ lib ]
197                 solibpath = WhereIsFile('lib' + lib + '.so', paths = searchlibs)
198                 if solibpath is None:
199                         continue
201                 f = os.popen('ldd ' + solibpath, 'r')
202                 for aword in lddre.findall(f.read()):
203                         fullstaticlib += [ aword ]
204                 f.close
206 ## includes all modules, linked statically
207 fullstaticbin = staticenv.Program('../fullstatic/build/lighttpd',
208         staticobj,
209         LIBS = GatherLibs(env, fullstaticlib),
210         LINKFLAGS= [staticenv['LINKFLAGS'], '-static']
211         )
213 Alias('static', staticbin)
214 Alias('fullstatic', fullstaticbin)
216 implib = 'lighttpd.exe.a'
217 bin_targets = ['lighttpd']
218 bin_linkflags = [ env['LINKFLAGS'] ]
219 if env['COMMON_LIB'] == 'lib':
220         common_lib = env.SharedLibrary('liblighttpd', common_src, LINKFLAGS = [ env['LINKFLAGS'], '-Wl,--export-dynamic' ])
221 else:
222         src += common_src
223         common_lib = []
224         if env['COMMON_LIB'] == 'bin':
225                 bin_linkflags += [ '-Wl,--export-all-symbols', '-Wl,--out-implib=build/' + implib ]
226                 bin_targets += [ implib ]
227         else:
228                 bin_linkflags += [ '-Wl,--export-dynamic' ]
230 instbin = env.Program(bin_targets, src, LINKFLAGS = bin_linkflags, LIBS = GatherLibs(env, env['LIBS'], common_lib, env['LIBDL']))
231 env.Depends(instbin, configparser)
233 if env['COMMON_LIB'] == 'bin':
234         common_lib = instbin[1]
236 env['SHLIBPREFIX'] = ''
237 instlib = []
238 for module in modules.keys():
239         libs = [ common_lib ]
240         if modules[module].has_key('lib'):
241                 libs +=  modules[module]['lib']
242         instlib += env.SharedLibrary(module, modules[module]['src'], LIBS= GatherLibs(env, libs))
243 env.Alias('modules', instlib)
245 inst = []
247 if env['build_dynamic']:
248         Default(instbin[0], instlib)
249         inst += env.Install('${sbindir}', instbin[0])
250         inst += env.Install('${libdir}', instlib)
251         if env['COMMON_LIB'] == 'lib':
252                 Default(common_lib)
253                 inst += env.Install('${bindir}', common_lib)
255 if env['build_static']:
256         Default(staticbin)
257         inst += env.Install('${sbindir}', staticbin)
259 if env['build_fullstatic']:
260         Default(fullstaticbin)
261         inst += env.Install('${sbindir}', fullstaticbin)
263 env.Alias('dynamic', instbin)
264 # default all to be installed
265 env.Alias('install', inst)
267 pkgdir = '.'
268 tarname = env['package'] + '-' + env['version']