]> git.proxmox.com Git - libgit2.git/blob - wscript
Revised build configuration for MSVC.
[libgit2.git] / wscript
1 from waflib.Context import Context
2 from waflib.Build import BuildContext, CleanContext, \
3 InstallContext, UninstallContext
4
5 # Unix flags
6 CFLAGS_UNIX = ["-O2", "-Wall", "-Wextra"]
7 CFLAGS_UNIX_DBG = ['-g']
8
9 # Windows MSVC flags
10 CFLAGS_WIN32_COMMON = ['/TC', '/W4', '/WX', '/nologo', '/Zi']
11 CFLAGS_WIN32_RELEASE = ['/O2', '/MD']
12
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']
18
19 ALL_LIBS = ['z', 'crypto', 'pthread']
20
21 def options(opt):
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
33 def configure(conf):
34
35 # load the MSVC configuration flags
36 if conf.options.msvc:
37 conf.env['MSVC_VERSIONS'] = ['msvc ' + conf.options.msvc]
38
39 conf.env['MSVC_TARGETS'] = [conf.options.arch]
40
41 # default configuration for C programs
42 conf.load('compiler_c')
43
44 dbg = conf.options.debug
45 zlib_name = 'z'
46
47 conf.env.CFLAGS = CFLAGS_UNIX + (CFLAGS_UNIX_DBG if dbg else [])
48
49 if conf.env.DEST_OS == 'win32':
50 conf.env.PLATFORM = 'win32'
51
52 if conf.env.CC_NAME == 'msvc':
53 conf.env.CFLAGS = CFLAGS_WIN32_COMMON + \
54 (CFLAGS_WIN32_DBG if dbg else CFLAGS_WIN32_RELEASE)
55 conf.env.LINKFLAGS += CFLAGS_WIN32_L + \
56 (CFLAGS_WIN32_L_DBG if dbg else [])
57 conf.env.DEFINES += ['WIN32', '_DEBUG', '_LIB', 'ZLIB_WINAPI']
58 zlib_name = 'zlibwapi'
59
60 elif conf.env.CC_NAME == 'gcc':
61 conf.check(features='c cprogram', lib='pthread', uselib_store='pthread')
62
63 else:
64 conf.env.PLATFORM = 'unix'
65
66 # check for Z lib
67 conf.check(features='c cprogram', lib=zlib_name, uselib_store='z', install_path=None)
68
69 if conf.options.sha1 not in ['openssl', 'ppc', 'builtin']:
70 ctx.fatal('Invalid SHA1 option')
71
72 # check for libcrypto (openssl) if we are using its SHA1 functions
73 if conf.options.sha1 == 'openssl':
74 conf.check_cfg(package='libcrypto', args=['--cflags', '--libs'], uselib_store='crypto')
75 conf.env.DEFINES += ['OPENSSL_SHA1']
76
77 elif conf.options.sha1 == 'ppc':
78 conf.env.DEFINES += ['PPC_SHA1']
79
80 conf.env.sha1 = conf.options.sha1
81
82 def build(bld):
83
84 # command '[build|clean|install|uninstall]-static'
85 if bld.variant == 'static':
86 build_library(bld, 'static')
87
88 # command '[build|clean|install|uninstall]-shared'
89 elif bld.variant == 'shared':
90 build_library(bld, 'shared')
91
92 # command '[build|clean]-tests'
93 elif bld.variant == 'tests':
94 build_library(bld, 'objects')
95 build_tests(bld)
96
97 # command 'build|clean|install|uninstall': by default, run
98 # the same command for both the static and the shared lib
99 else:
100 from waflib import Options
101 Options.commands = [bld.cmd + '-shared', bld.cmd + '-static'] + Options.commands
102
103 def build_library(bld, build_type):
104
105 BUILD = {
106 'shared' : bld.shlib,
107 'static' : bld.stlib,
108 'objects' : bld.objects
109 }
110
111 directory = bld.path
112 sources = directory.ant_glob('src/*.c')
113
114 # Compile platform-dependant code
115 # E.g. src/unix/*.c
116 # src/win32/*.c
117 sources = sources + directory.ant_glob('src/%s/*.c' % bld.env.PLATFORM)
118
119 # SHA1 methods source
120 if bld.env.sha1 == "ppc":
121 sources.append('src/ppc/sha1.c')
122 else:
123 sources.append('src/block-sha1/sha1.c')
124 #------------------------------
125 # Build the main library
126 #------------------------------
127
128 # either as static or shared;
129 BUILD[build_type](
130 source=sources,
131 target='git2',
132 includes='src',
133 install_path='${LIBDIR}',
134 use=ALL_LIBS
135 )
136
137 # On Unix systems, build the Pkg-config entry file
138 if bld.env.PLATFORM == 'unix' and bld.is_install:
139 bld(rule="""sed -e 's#@prefix@#${PREFIX}#' -e 's#@libdir@#${LIBDIR}#' < ${SRC} > ${TGT}""",
140 source='libgit2.pc.in',
141 target='libgit2.pc',
142 install_path='${LIBDIR}/pkgconfig',
143 )
144
145 # Install headers
146 bld.install_files('${PREFIX}/include', directory.find_node('src/git2.h'))
147 bld.install_files('${PREFIX}/include/git2', directory.ant_glob('src/git2/*.h'))
148
149 # On Unix systems, let them know about installation
150 if bld.env.PLATFORM == 'unix' and bld.cmd == 'install-shared':
151 bld.add_post_fun(call_ldconfig)
152
153 def call_ldconfig(bld):
154 import distutils.spawn as s
155 ldconf = s.find_executable('ldconfig')
156 if ldconf:
157 bld.exec_command(ldconf)
158
159 def grep_test_header(text, test_file):
160 return '\n'.join(l for l in test_file.read().splitlines() if text in l)
161
162 def build_tests(bld):
163 import os
164
165 if bld.is_install:
166 return
167
168 directory = bld.path
169 resources_path = directory.find_node('tests/resources/').abspath().replace('\\', '/')
170
171 # Common object with the Test library methods
172 bld.objects(source=['tests/test_helpers.c', 'tests/test_lib.c'], includes=['src', 'tests'], target='test_helper')
173
174 # Build all tests in the tests/ folder
175 for test_file in directory.ant_glob('tests/t????-*.c'):
176 test_name, _ = os.path.splitext(os.path.basename(test_file.abspath()))
177
178 # Preprocess table of contents for each test
179 test_toc_file = directory.make_node('tests/%s.toc' % test_name)
180 if bld.cmd == 'clean-tests': # cleanup; delete the generated TOC file
181 test_toc_file.delete()
182 elif bld.cmd == 'build-tests': # build; create TOC
183 test_toc_file.write(grep_test_header('BEGIN_TEST', test_file))
184
185 # Build individual test (don't run)
186 bld.program(
187 source=[test_file, 'tests/test_main.c'],
188 target=test_name,
189 includes=['src', 'tests'],
190 defines=['TEST_TOC="%s.toc"' % test_name, 'TEST_RESOURCES="%s"' % resources_path],
191 install_path=None,
192 use=['test_helper', 'git2'] + ALL_LIBS # link with all the libs we know
193 # libraries which are not enabled won't link
194 )
195
196
197 class _test(BuildContext):
198 cmd = 'test'
199 fun = 'test'
200
201 def test(bld):
202 from waflib import Options
203 Options.commands = ['build-tests', 'run-tests'] + Options.commands
204
205 class _build_doc(Context):
206 cmd = 'doxygen'
207 fun = 'build_docs'
208
209 def build_docs(ctx):
210 ctx.exec_command("doxygen api.doxygen")
211 ctx.exec_command("git stash")
212 ctx.exec_command("git checkout gh-pages")
213 ctx.exec_command("cp -Rf apidocs/html/* .")
214 ctx.exec_command("git add .")
215 ctx.exec_command("git commit -am 'generated docs'")
216 ctx.exec_command("git push origin gh-pages")
217 ctx.exec_command("git checkout master")
218
219 class _run_tests(Context):
220 cmd = 'run-tests'
221 fun = 'run_tests'
222
223 def run_tests(ctx):
224 import shutil, tempfile, sys
225
226 failed = False
227 test_folder = tempfile.mkdtemp()
228 test_glob = 'build/tests/t????-*'
229
230 if sys.platform == 'win32':
231 test_glob += '.exe'
232
233 for test in ctx.path.ant_glob(test_glob):
234 if ctx.exec_command(test.abspath(), cwd=test_folder) != 0:
235 failed = True
236 break
237
238 shutil.rmtree(test_folder)
239
240 if failed:
241 ctx.fatal('Test run failed')
242
243
244 CONTEXTS = {
245 'build' : BuildContext,
246 'clean' : CleanContext,
247 'install' : InstallContext,
248 'uninstall' : UninstallContext
249 }
250
251 def build_command(command):
252 ctx, var = command.split('-')
253 class _gen_command(CONTEXTS[ctx]):
254 cmd = command
255 variant = var
256
257 build_command('build-static')
258 build_command('build-shared')
259 build_command('build-tests')
260
261 build_command('clean-static')
262 build_command('clean-shared')
263 build_command('clean-tests')
264
265 build_command('install-static')
266 build_command('install-shared')
267
268 build_command('uninstall-static')
269 build_command('uninstall-shared')
270