1 from __future__ import with_statement
2 from waflib.Context import Context
3 from waflib.Build import BuildContext, CleanContext, \
4 InstallContext, UninstallContext
7 CFLAGS_UNIX = ["-O2", "-Wall", "-Wextra", "-fPIC"]
8 CFLAGS_UNIX_DBG = ['-g', '-O0']
11 CFLAGS_WIN32_COMMON = ['/TC', '/W4', '/WX', '/nologo', '/Zi']
12 CFLAGS_WIN32_RELEASE = ['/O2', '/MD']
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']
20 ALL_LIBS = ['crypto', 'pthread', 'sqlite3', 'hiredis']
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)')
42 # load the MSVC configuration flags
44 conf.env['MSVC_VERSIONS'] = ['msvc ' + conf.options.msvc]
46 conf.env['MSVC_TARGETS'] = [conf.options.arch]
48 # default configuration for C programs
49 conf.load('compiler_c')
51 dbg = conf.options.debug
53 conf.env.CFLAGS = CFLAGS_UNIX + (CFLAGS_UNIX_DBG if dbg else [])
55 if conf.env.DEST_OS == 'win32':
56 conf.env.PLATFORM = 'win32'
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']
66 conf.env.PLATFORM = 'unix'
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']
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']
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']
84 if conf.options.sha1 not in ['openssl', 'ppc', 'builtin']:
85 conf.fatal('Invalid SHA1 option')
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']
92 elif conf.options.sha1 == 'ppc':
93 conf.env.DEFINES += ['PPC_SHA1']
95 conf.env.sha1 = conf.options.sha1
99 # command '[build|clean|install|uninstall]-static'
100 if bld.variant == 'static':
101 build_library(bld, 'static')
103 # command '[build|clean|install|uninstall]-shared'
104 elif bld.variant == 'shared':
105 build_library(bld, 'shared')
107 # command '[build|clean]-tests'
108 elif bld.variant == 'test':
109 build_library(bld, 'objects')
112 # command 'build|clean|install|uninstall': by default, run
113 # the same command for both the static and the shared lib
115 from waflib import Options
116 Options.commands = [bld.cmd + '-shared', bld.cmd + '-static'] + Options.commands
118 def get_libgit2_version(git2_h):
122 with open(git2_h) as f:
123 line = re.search(r'^#define LIBGIT2_VERSION "(\d+\.\d+\.\d+)"$', f.read(), re.MULTILINE)
126 raise Exception("Failed to detect libgit2 version")
131 def build_library(bld, build_type):
134 'shared' : bld.shlib,
135 'static' : bld.stlib,
136 'objects' : bld.objects
140 sources = directory.ant_glob('src/*.c')
142 # Find the version of the library, from our header file
143 version = get_libgit2_version(directory.find_node("include/git2.h").abspath())
145 # Compile platform-dependant code
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')
152 # SHA1 methods source
153 if bld.env.sha1 == "ppc":
154 sources.append('src/ppc/sha1.c')
156 sources.append('src/block-sha1/sha1.c')
157 #------------------------------
158 # Build the main library
159 #------------------------------
161 # either as static or shared;
165 includes=['src', 'include', 'deps/zlib'],
166 install_path='${LIBDIR}',
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',
176 install_path='${LIBDIR}/pkgconfig',
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'))
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)
187 def call_ldconfig(bld):
188 import distutils.spawn as s
189 ldconf = s.find_executable('ldconfig')
191 bld.exec_command(ldconf)
195 resources_path = directory.find_node('tests/resources/').abspath().replace('\\', '/')
197 sources = ['tests/test_lib.c', 'tests/test_helpers.c', 'tests/test_main.c']
198 sources = sources + directory.ant_glob('tests/t??-*.c')
202 target='libgit2_test',
203 includes=['src', 'tests', 'include'],
204 defines=['TEST_RESOURCES="%s"' % resources_path],
205 use=['git2'] + ALL_LIBS
208 class _test(BuildContext):
213 from waflib import Options
214 Options.commands = ['build-test', 'run-test'] + Options.commands
216 class _build_doc(Context):
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")
230 class _run_test(Context):
235 import shutil, tempfile, sys
239 test_path = 'build/test/libgit2_test'
240 if sys.platform == 'win32':
243 test_folder = tempfile.mkdtemp()
244 test = ctx.path.find_node(test_path)
246 if not test or ctx.exec_command(test.abspath(), cwd=test_folder) != 0:
249 shutil.rmtree(test_folder)
252 ctx.fatal('Test run failed')
256 'build' : BuildContext,
257 'clean' : CleanContext,
258 'install' : InstallContext,
259 'uninstall' : UninstallContext
262 def build_command(command):
263 ctx, var = command.split('-')
264 class _gen_command(CONTEXTS[ctx]):
268 build_command('build-static')
269 build_command('build-shared')
270 build_command('build-test')
272 build_command('clean-static')
273 build_command('clean-shared')
274 build_command('clean-test')
276 build_command('install-static')
277 build_command('install-shared')
279 build_command('uninstall-static')
280 build_command('uninstall-shared')