1 from waflib.Context import Context
2 from waflib.Build import BuildContext, CleanContext, \
3 InstallContext, UninstallContext
5 CFLAGS_UNIX = ["-O2", "-Wall", "-Wextra"]
6 CFLAGS_WIN32 = ['/TC', '/W4', '/RTC1', '/nologo']
8 CFLAGS_UNIX_DBG = ['-g']
9 CFLAGS_WIN32_DBG = ['/Zi', '/DEBUG']
10 CFLAGS_WIN32_L_DBG = ['/DEBUG']
12 ALL_LIBS = ['z', 'crypto', 'pthread']
15 opt.load('compiler_c')
16 opt.add_option('--sha1', action='store', default='builtin',
17 help="Use the builtin SHA1 routines (builtin), the \
18 PPC optimized version (ppc) or the SHA1 functions from OpenSSL (openssl)")
19 opt.add_option('--debug', action='store_true', default=False,
20 help='Compile with debug symbols')
21 opt.add_option('--msvc', action='store', default=None,
22 help='Force a specific MSVC++ version (7.1, 8.0, 9.0, 10.0), if more than one is installed')
23 opt.add_option('--arch', action='store', default='x86',
24 help='Select target architecture (ia64, x64, x86, x86_amd64, x86_ia64)')
28 # load the MSVC configuration flags
30 conf.env['MSVC_VERSIONS'] = ['msvc ' + conf.options.msvc]
32 conf.env['MSVC_TARGETS'] = [conf.options.arch]
34 # default configuration for C programs
35 conf.load('compiler_c')
37 dbg = conf.options.debug
40 conf.env.CFLAGS = CFLAGS_UNIX + (CFLAGS_UNIX_DBG if dbg else [])
42 if conf.env.DEST_OS == 'win32':
43 conf.env.PLATFORM = 'win32'
45 if conf.env.CC_NAME == 'msvc':
46 conf.env.CFLAGS = CFLAGS_WIN32 + (CFLAGS_WIN32_DBG if dbg else [])
47 conf.env.LINKFLAGS += CFLAGS_WIN32_L_DBG if dbg else []
48 conf.env.DEFINES += ['WIN32', '_DEBUG', '_LIB', 'ZLIB_WINAPI']
49 zlib_name = 'zlibwapi'
51 elif conf.env.CC_NAME == 'gcc':
52 conf.check(features='c cprogram', lib='pthread', uselib_store='pthread')
55 conf.env.PLATFORM = 'unix'
58 conf.check(features='c cprogram', lib=zlib_name, uselib_store='z', install_path=None)
60 if conf.options.sha1 not in ['openssl', 'ppc', 'builtin']:
61 ctx.fatal('Invalid SHA1 option')
63 # check for libcrypto (openssl) if we are using its SHA1 functions
64 if conf.options.sha1 == 'openssl':
65 conf.check_cfg(package='libcrypto', args=['--cflags', '--libs'], uselib_store='crypto')
66 conf.env.DEFINES += ['OPENSSL_SHA1']
68 elif conf.options.sha1 == 'ppc':
69 conf.env.DEFINES += ['PPC_SHA1']
71 conf.env.sha1 = conf.options.sha1
75 # command '[build|clean|install|uninstall]-static'
76 if bld.variant == 'static':
77 build_library(bld, 'cstlib')
79 # command '[build|clean|install|uninstall]-shared'
80 elif bld.variant == 'shared':
81 build_library(bld, 'cshlib')
83 # command '[build|clean]-tests'
84 elif bld.variant == 'tests':
85 build_library(bld, 'cstlib')
88 # command 'build|clean|install|uninstall': by default, run
89 # the same command for both the static and the shared lib
91 from waflib import Options
92 Options.commands = [bld.cmd + '-shared', bld.cmd + '-static'] + Options.commands
94 def build_library(bld, lib_str):
97 sources = directory.ant_glob('src/*.c')
99 # Compile platform-dependant code
102 sources = sources + directory.ant_glob('src/%s/*.c' % bld.env.PLATFORM)
104 # SHA1 methods source
105 if bld.env.sha1 == "ppc":
106 sources.append('src/ppc/sha1.c')
108 sources.append('src/block-sha1/sha1.c')
110 features = ['c', lib_str]
112 #------------------------------
113 # Build the main library
114 #------------------------------
116 # either as static or shared;
117 bld(features=features,
121 install_path='${LIBDIR}',
125 # On Unix systems, build the Pkg-config entry file
126 if bld.env.PLATFORM == 'unix':
127 bld(rule="""sed -e 's#@prefix@#$(prefix)#' -e 's#@libdir@#$(libdir)#' < ${SRC} > ${TGT}""",
128 source='libgit2.pc.in',
130 install_path='${LIBDIR}/pkgconfig',
134 bld.install_files('${PREFIX}/include', directory.find_node('src/git2.h'))
135 bld.install_files('${PREFIX}/include/git2', directory.ant_glob('src/git2/*.h'))
137 def grep_test_header(text, test_file):
138 return '\n'.join(l for l in test_file.read().splitlines() if text in l)
140 def build_tests(bld):
147 resources_path = directory.find_node('tests/resources/').abspath()
149 # Common object with the Test library methods
150 bld.objects(source=['tests/test_helpers.c', 'tests/test_lib.c'], includes=['src', 'tests'], target='test_helper')
152 # Build all tests in the tests/ folder
153 for test_file in directory.ant_glob('tests/t????-*.c'):
154 test_name, _ = os.path.splitext(os.path.basename(test_file.abspath()))
156 # Preprocess table of contents for each test
157 test_toc_file = directory.make_node('tests/%s.toc' % test_name)
158 if bld.cmd == 'clean-tests': # cleanup; delete the generated TOC file
159 test_toc_file.delete()
160 elif bld.cmd == 'build-tests': # build; create TOC
161 test_toc_file.write(grep_test_header('BEGIN_TEST', test_file))
163 # Build individual test (don't run)
165 source=[test_file, 'tests/test_main.c'],
167 includes=['src', 'tests'],
168 defines=['TEST_TOC="%s.toc"' % test_name, 'TEST_RESOURCES="%s"' % resources_path],
170 stlib=['git2'], # link with the git2 static lib we've just compiled'
171 stlibpath=[directory.find_node('build/tests/').abspath(), directory.abspath()],
172 use=['test_helper', 'git2'] + ALL_LIBS # link with all the libs we know
173 # libraries which are not enabled won't link
177 class _test(BuildContext):
182 from waflib import Options
183 Options.commands = ['build-tests', 'run-tests'] + Options.commands
185 class _build_doc(Context):
190 ctx.exec_command("doxygen api.doxygen")
191 ctx.exec_command("git stash")
192 ctx.exec_command("git checkout gh-pages")
193 ctx.exec_command("cp -Rf apidocs/html/* .")
194 ctx.exec_command("git add .")
195 ctx.exec_command("git commit -am 'generated docs'")
196 ctx.exec_command("git push origin gh-pages")
197 ctx.exec_command("git checkout master")
199 class _run_tests(Context):
204 import shutil, tempfile, sys
207 test_folder = tempfile.mkdtemp()
208 test_glob = 'build/tests/t????-*'
210 if sys.platform == 'win32':
213 for test in ctx.path.ant_glob(test_glob):
214 if ctx.exec_command(test.abspath(), cwd=test_folder) != 0:
218 shutil.rmtree(test_folder)
221 ctx.fatal('Test run failed')
225 'build' : BuildContext,
226 'clean' : CleanContext,
227 'install' : InstallContext,
228 'uninstall' : UninstallContext
231 def build_command(command):
232 ctx, var = command.split('-')
233 class _gen_command(CONTEXTS[ctx]):
237 build_command('build-static')
238 build_command('build-shared')
239 build_command('build-tests')
241 build_command('clean-static')
242 build_command('clean-shared')
243 build_command('clean-tests')
245 build_command('install-static')
246 build_command('install-shared')
248 build_command('uninstall-static')
249 build_command('uninstall-shared')