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