]> git.proxmox.com Git - libgit2.git/blob - wscript
Force position independent code in Linux
[libgit2.git] / wscript
1 from __future__ import with_statement
2 from waflib.Context import Context
3 from waflib.Build import BuildContext, CleanContext, \
4 InstallContext, UninstallContext
5
6 # Unix flags
7 CFLAGS_UNIX = ["-O2", "-Wall", "-Wextra", "-fPIC"]
8 CFLAGS_UNIX_DBG = ['-g', '-O0']
9
10 # Windows MSVC flags
11 CFLAGS_WIN32_COMMON = ['/TC', '/W4', '/WX', '/nologo', '/Zi']
12 CFLAGS_WIN32_RELEASE = ['/O2', '/MD']
13
14 # Note: /RTC* cannot be used with optimization on.
15 CFLAGS_WIN32_DBG = ['/Od', '/RTC1', '/RTCc', '/DEBUG', '/MDd']
16 CFLAGS_WIN32_L = ['/RELEASE'] # used for /both/ debug and release builds.
17 # sets the module's checksum in the header.
18 CFLAGS_WIN32_L_DBG = ['/DEBUG']
19
20 ALL_LIBS = ['crypto', 'pthread', 'sqlite3', 'hiredis']
21
22 def options(opt):
23 opt.load('compiler_c')
24 opt.add_option('--sha1', action='store', default='builtin',
25 help="Use the builtin SHA1 routines (builtin), the \
26 PPC optimized version (ppc) or the SHA1 functions from OpenSSL (openssl)")
27 opt.add_option('--debug', action='store_true', default=False,
28 help='Compile with debug symbols')
29 opt.add_option('--msvc', action='store', default=None,
30 help='Force a specific MSVC++ version (7.1, 8.0, 9.0, 10.0), if more than one is installed')
31 opt.add_option('--arch', action='store', default='x86',
32 help='Select target architecture (ia64, x64, x86, x86_amd64, x86_ia64)')
33 opt.add_option('--with-sqlite', action='store_true', default=False,
34 dest='use_sqlite', help='Enable sqlite support')
35 opt.add_option('--with-hiredis', action='store_true', default=False,
36 dest='use_hiredis', help='Enable redis support using hiredis')
37 opt.add_option('--threadsafe', action='store_true', default=False,
38 help='Make libgit2 thread-safe (requires pthreads)')
39
40 def configure(conf):
41
42 # load the MSVC configuration flags
43 if conf.options.msvc:
44 conf.env['MSVC_VERSIONS'] = ['msvc ' + conf.options.msvc]
45
46 conf.env['MSVC_TARGETS'] = [conf.options.arch]
47
48 # default configuration for C programs
49 conf.load('compiler_c')
50
51 dbg = conf.options.debug
52
53 conf.env.CFLAGS = CFLAGS_UNIX + (CFLAGS_UNIX_DBG if dbg else [])
54
55 if conf.env.DEST_OS == 'win32':
56 conf.env.PLATFORM = 'win32'
57
58 if conf.env.CC_NAME == 'msvc':
59 conf.env.CFLAGS = CFLAGS_WIN32_COMMON + \
60 (CFLAGS_WIN32_DBG if dbg else CFLAGS_WIN32_RELEASE)
61 conf.env.LINKFLAGS += CFLAGS_WIN32_L + \
62 (CFLAGS_WIN32_L_DBG if dbg else [])
63 conf.env.DEFINES += ['WIN32', '_DEBUG', '_LIB']
64
65 else:
66 conf.env.PLATFORM = 'unix'
67
68 if conf.options.threadsafe:
69 if conf.env.PLATFORM == 'unix':
70 conf.check_cc(lib='pthread', uselib_store='pthread')
71 conf.env.DEFINES += ['GIT_THREADS']
72
73 # check for sqlite3
74 if conf.options.use_sqlite and conf.check_cc(
75 lib='sqlite3', uselib_store='sqlite3', install_path=None, mandatory=False):
76 conf.env.DEFINES += ['GIT2_SQLITE_BACKEND']
77
78 # check for hiredis
79 if conf.options.use_hiredis and conf.check_cc(
80 lib='hiredis', uselib_store='hiredis', install_path=None, mandatory=False):
81 conf.env.DEFINES += ['GIT2_HIREDIS_BACKEND']
82
83
84 if conf.options.sha1 not in ['openssl', 'ppc', 'builtin']:
85 conf.fatal('Invalid SHA1 option')
86
87 # check for libcrypto (openssl) if we are using its SHA1 functions
88 if conf.options.sha1 == 'openssl':
89 conf.check_cfg(package='libcrypto', args=['--cflags', '--libs'], uselib_store='crypto')
90 conf.env.DEFINES += ['OPENSSL_SHA1']
91
92 elif conf.options.sha1 == 'ppc':
93 conf.env.DEFINES += ['PPC_SHA1']
94
95 conf.env.sha1 = conf.options.sha1
96
97 def build(bld):
98
99 # command '[build|clean|install|uninstall]-static'
100 if bld.variant == 'static':
101 build_library(bld, 'static')
102
103 # command '[build|clean|install|uninstall]-shared'
104 elif bld.variant == 'shared':
105 build_library(bld, 'shared')
106
107 # command '[build|clean]-tests'
108 elif bld.variant == 'test':
109 build_library(bld, 'objects')
110 build_test(bld)
111
112 # command 'build|clean|install|uninstall': by default, run
113 # the same command for both the static and the shared lib
114 else:
115 from waflib import Options
116 Options.commands = [bld.cmd + '-shared', bld.cmd + '-static'] + Options.commands
117
118 def get_libgit2_version(git2_h):
119 import re
120 line = None
121
122 with open(git2_h) as f:
123 line = re.search(r'^#define LIBGIT2_VERSION "(\d+\.\d+\.\d+)"$', f.read(), re.MULTILINE)
124
125 if line is None:
126 raise Exception("Failed to detect libgit2 version")
127
128 return line.group(1)
129
130
131 def build_library(bld, build_type):
132
133 BUILD = {
134 'shared' : bld.shlib,
135 'static' : bld.stlib,
136 'objects' : bld.objects
137 }
138
139 directory = bld.path
140 sources = directory.ant_glob('src/*.c')
141
142 # Find the version of the library, from our header file
143 version = get_libgit2_version(directory.find_node("include/git2.h").abspath())
144
145 # Compile platform-dependant code
146 # E.g. src/unix/*.c
147 # src/win32/*.c
148 sources = sources + directory.ant_glob('src/%s/*.c' % bld.env.PLATFORM)
149 sources = sources + directory.ant_glob('src/backends/*.c')
150 sources = sources + directory.ant_glob('deps/zlib/*.c')
151
152 # SHA1 methods source
153 if bld.env.sha1 == "ppc":
154 sources.append('src/ppc/sha1.c')
155 else:
156 sources.append('src/block-sha1/sha1.c')
157 #------------------------------
158 # Build the main library
159 #------------------------------
160
161 # either as static or shared;
162 BUILD[build_type](
163 source=sources,
164 target='git2',
165 includes=['src', 'include', 'deps/zlib'],
166 install_path='${LIBDIR}',
167 use=ALL_LIBS,
168 vnum=version,
169 )
170
171 # On Unix systems, build the Pkg-config entry file
172 if bld.env.PLATFORM == 'unix' and bld.is_install:
173 bld(rule="""sed -e 's#@prefix@#${PREFIX}#' -e 's#@libdir@#${LIBDIR}#' -e 's#@version@#%s#' < ${SRC} > ${TGT}""" % version,
174 source='libgit2.pc.in',
175 target='libgit2.pc',
176 install_path='${LIBDIR}/pkgconfig',
177 )
178
179 # Install headers
180 bld.install_files('${PREFIX}/include', directory.find_node('include/git2.h'))
181 bld.install_files('${PREFIX}/include/git2', directory.ant_glob('include/git2/*.h'))
182
183 # On Unix systems, let them know about installation
184 if bld.env.PLATFORM == 'unix' and bld.cmd == 'install-shared':
185 bld.add_post_fun(call_ldconfig)
186
187 def call_ldconfig(bld):
188 import distutils.spawn as s
189 ldconf = s.find_executable('ldconfig')
190 if ldconf:
191 bld.exec_command(ldconf)
192
193 def build_test(bld):
194 directory = bld.path
195 resources_path = directory.find_node('tests/resources/').abspath().replace('\\', '/')
196
197 sources = ['tests/test_lib.c', 'tests/test_helpers.c', 'tests/test_main.c']
198 sources = sources + directory.ant_glob('tests/t??-*.c')
199
200 bld.program(
201 source=sources,
202 target='libgit2_test',
203 includes=['src', 'tests', 'include'],
204 defines=['TEST_RESOURCES="%s"' % resources_path],
205 use=['git2'] + ALL_LIBS
206 )
207
208 class _test(BuildContext):
209 cmd = 'test'
210 fun = 'test'
211
212 def test(bld):
213 from waflib import Options
214 Options.commands = ['build-test', 'run-test'] + Options.commands
215
216 class _build_doc(Context):
217 cmd = 'doxygen'
218 fun = 'build_docs'
219
220 def build_docs(ctx):
221 ctx.exec_command("doxygen api.doxygen")
222 ctx.exec_command("git stash")
223 ctx.exec_command("git checkout gh-pages")
224 ctx.exec_command("cp -Rf apidocs/html/* .")
225 ctx.exec_command("git add .")
226 ctx.exec_command("git commit -am 'generated docs'")
227 ctx.exec_command("git push origin gh-pages")
228 ctx.exec_command("git checkout master")
229
230 class _run_test(Context):
231 cmd = 'run-test'
232 fun = 'run_test'
233
234 def run_test(ctx):
235 import shutil, tempfile, sys
236
237 failed = False
238
239 test_path = 'build/test/libgit2_test'
240 if sys.platform == 'win32':
241 test_path += '.exe'
242
243 test_folder = tempfile.mkdtemp()
244 test = ctx.path.find_node(test_path)
245
246 if not test or ctx.exec_command(test.abspath(), cwd=test_folder) != 0:
247 failed = True
248
249 shutil.rmtree(test_folder)
250
251 if failed:
252 ctx.fatal('Test run failed')
253
254
255 CONTEXTS = {
256 'build' : BuildContext,
257 'clean' : CleanContext,
258 'install' : InstallContext,
259 'uninstall' : UninstallContext
260 }
261
262 def build_command(command):
263 ctx, var = command.split('-')
264 class _gen_command(CONTEXTS[ctx]):
265 cmd = command
266 variant = var
267
268 build_command('build-static')
269 build_command('build-shared')
270 build_command('build-test')
271
272 build_command('clean-static')
273 build_command('clean-shared')
274 build_command('clean-test')
275
276 build_command('install-static')
277 build_command('install-shared')
278
279 build_command('uninstall-static')
280 build_command('uninstall-shared')
281