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