1 from waflib.Context import Context
2 from waflib.Build import BuildContext, CleanContext, \
3 InstallContext, UninstallContext
6 CFLAGS_UNIX = ["-O2", "-Wall", "-Wextra"]
7 CFLAGS_UNIX_DBG = ['-g']
10 CFLAGS_WIN32_COMMON = ['/TC', '/W4', '/WX', '/nologo', '/Zi']
11 CFLAGS_WIN32_RELEASE = ['/O2', '/MD']
13 # Note: /RTC* cannot be used with optimization on.
14 CFLAGS_WIN32_DBG = ['/Od', '/RTC1', '/RTCc', '/DEBUG', '/MDd']
15 CFLAGS_WIN32_L = ['/RELEASE'] # used for /both/ debug and release builds.
16 # sets the module's checksum in the header.
17 CFLAGS_WIN32_L_DBG = ['/DEBUG']
19 ALL_LIBS = ['crypto', 'pthread', 'sqlite3']
22 opt.load('compiler_c')
23 opt.add_option('--sha1', action='store', default='builtin',
24 help="Use the builtin SHA1 routines (builtin), the \
25 PPC optimized version (ppc) or the SHA1 functions from OpenSSL (openssl)")
26 opt.add_option('--debug', action='store_true', default=False,
27 help='Compile with debug symbols')
28 opt.add_option('--msvc', action='store', default=None,
29 help='Force a specific MSVC++ version (7.1, 8.0, 9.0, 10.0), if more than one is installed')
30 opt.add_option('--arch', action='store', default='x86',
31 help='Select target architecture (ia64, x64, x86, x86_amd64, x86_ia64)')
32 opt.add_option('--without-sqlite', action='store_false', default=True,
33 dest='use_sqlite', help='Disable sqlite support')
37 # load the MSVC configuration flags
39 conf.env['MSVC_VERSIONS'] = ['msvc ' + conf.options.msvc]
41 conf.env['MSVC_TARGETS'] = [conf.options.arch]
43 # default configuration for C programs
44 conf.load('compiler_c')
46 dbg = conf.options.debug
48 conf.env.CFLAGS = CFLAGS_UNIX + (CFLAGS_UNIX_DBG if dbg else [])
50 if conf.env.DEST_OS == 'win32':
51 conf.env.PLATFORM = 'win32'
53 if conf.env.CC_NAME == 'msvc':
54 conf.env.CFLAGS = CFLAGS_WIN32_COMMON + \
55 (CFLAGS_WIN32_DBG if dbg else CFLAGS_WIN32_RELEASE)
56 conf.env.LINKFLAGS += CFLAGS_WIN32_L + \
57 (CFLAGS_WIN32_L_DBG if dbg else [])
58 conf.env.DEFINES += ['WIN32', '_DEBUG', '_LIB']
61 conf.env.PLATFORM = 'unix'
62 conf.check_cc(lib='pthread', uselib_store='pthread')
64 # Do not build ZLib with GZIP support
65 conf.env.DEFINES += ['NO_GZIP']
68 if conf.options.use_sqlite and conf.check_cc(
69 lib='sqlite3', uselib_store='sqlite3', install_path=None, mandatory=False):
70 conf.env.DEFINES += ['GIT2_SQLITE_BACKEND']
72 if conf.options.sha1 not in ['openssl', 'ppc', 'builtin']:
73 conf.fatal('Invalid SHA1 option')
75 # check for libcrypto (openssl) if we are using its SHA1 functions
76 if conf.options.sha1 == 'openssl':
77 conf.check_cfg(package='libcrypto', args=['--cflags', '--libs'], uselib_store='crypto')
78 conf.env.DEFINES += ['OPENSSL_SHA1']
80 elif conf.options.sha1 == 'ppc':
81 conf.env.DEFINES += ['PPC_SHA1']
83 conf.env.sha1 = conf.options.sha1
87 # command '[build|clean|install|uninstall]-static'
88 if bld.variant == 'static':
89 build_library(bld, 'static')
91 # command '[build|clean|install|uninstall]-shared'
92 elif bld.variant == 'shared':
93 build_library(bld, 'shared')
95 # command '[build|clean]-tests'
96 elif bld.variant == 'test':
97 build_library(bld, 'objects')
100 # command 'build|clean|install|uninstall': by default, run
101 # the same command for both the static and the shared lib
103 from waflib import Options
104 Options.commands = [bld.cmd + '-shared', bld.cmd + '-static'] + Options.commands
106 def get_libgit2_version(git2_h):
110 with open(git2_h) as f:
111 line = re.search(r'^#define LIBGIT2_VERSION "(\d+\.\d+\.\d+)"$', f.read(), re.MULTILINE)
114 raise Exception("Failed to detect libgit2 version")
119 def build_library(bld, build_type):
122 'shared' : bld.shlib,
123 'static' : bld.stlib,
124 'objects' : bld.objects
128 sources = directory.ant_glob('src/*.c')
130 # Find the version of the library, from our header file
131 version = get_libgit2_version(directory.find_node("include/git2.h").abspath())
133 # Compile platform-dependant code
136 sources = sources + directory.ant_glob('src/%s/*.c' % bld.env.PLATFORM)
137 sources = sources + directory.ant_glob('src/backends/*.c')
138 sources = sources + directory.ant_glob('deps/zlib/*.c')
140 # SHA1 methods source
141 if bld.env.sha1 == "ppc":
142 sources.append('src/ppc/sha1.c')
144 sources.append('src/block-sha1/sha1.c')
145 #------------------------------
146 # Build the main library
147 #------------------------------
149 # either as static or shared;
153 includes=['src', 'include', 'deps/zlib'],
154 install_path='${LIBDIR}',
159 # On Unix systems, build the Pkg-config entry file
160 if bld.env.PLATFORM == 'unix' and bld.is_install:
161 bld(rule="""sed -e 's#@prefix@#${PREFIX}#' -e 's#@libdir@#${LIBDIR}#' -e 's#@version@#%s#' < ${SRC} > ${TGT}""" % version,
162 source='libgit2.pc.in',
164 install_path='${LIBDIR}/pkgconfig',
168 bld.install_files('${PREFIX}/include', directory.find_node('include/git2.h'))
169 bld.install_files('${PREFIX}/include/git2', directory.ant_glob('include/git2/*.h'))
171 # On Unix systems, let them know about installation
172 if bld.env.PLATFORM == 'unix' and bld.cmd == 'install-shared':
173 bld.add_post_fun(call_ldconfig)
175 def call_ldconfig(bld):
176 import distutils.spawn as s
177 ldconf = s.find_executable('ldconfig')
179 bld.exec_command(ldconf)
183 resources_path = directory.find_node('tests/resources/').abspath().replace('\\', '/')
185 sources = ['tests/test_lib.c', 'tests/test_helpers.c', 'tests/test_main.c']
186 sources = sources + directory.ant_glob('tests/t??-*.c')
190 target='libgit2_test',
191 includes=['src', 'tests', 'include'],
192 defines=['TEST_RESOURCES="%s"' % resources_path],
193 use=['git2'] + ALL_LIBS
196 class _test(BuildContext):
201 from waflib import Options
202 Options.commands = ['build-test', 'run-test'] + Options.commands
204 class _build_doc(Context):
209 ctx.exec_command("doxygen api.doxygen")
210 ctx.exec_command("git stash")
211 ctx.exec_command("git checkout gh-pages")
212 ctx.exec_command("cp -Rf apidocs/html/* .")
213 ctx.exec_command("git add .")
214 ctx.exec_command("git commit -am 'generated docs'")
215 ctx.exec_command("git push origin gh-pages")
216 ctx.exec_command("git checkout master")
218 class _run_test(Context):
223 import shutil, tempfile, sys
227 test_path = 'build/test/libgit2_test'
228 if sys.platform == 'win32':
231 test_folder = tempfile.mkdtemp()
232 test = ctx.path.find_node(test_path)
234 if not test or ctx.exec_command(test.abspath(), cwd=test_folder) != 0:
237 shutil.rmtree(test_folder)
240 ctx.fatal('Test run failed')
244 'build' : BuildContext,
245 'clean' : CleanContext,
246 'install' : InstallContext,
247 'uninstall' : UninstallContext
250 def build_command(command):
251 ctx, var = command.split('-')
252 class _gen_command(CONTEXTS[ctx]):
256 build_command('build-static')
257 build_command('build-shared')
258 build_command('build-test')
260 build_command('clean-static')
261 build_command('clean-shared')
262 build_command('clean-test')
264 build_command('install-static')
265 build_command('install-shared')
267 build_command('uninstall-static')
268 build_command('uninstall-shared')