]> git.proxmox.com Git - libgit2.git/blob - wscript
Tests now run with the resources folder as a hardcoded path
[libgit2.git] / wscript
1 from waflib.Context import Context
2 from waflib.Build import BuildContext, CleanContext, \
3 InstallContext, UninstallContext
4
5 CFLAGS_UNIX = ["-O2", "-Wall", "-Wextra"]
6 CFLAGS_WIN32 = ['/TC', '/W4', '/RTC1', '/nologo']
7
8 CFLAGS_UNIX_DBG = ['-g']
9 CFLAGS_WIN32_DBG = ['/Zi', '/DEBUG']
10 CFLAGS_WIN32_L_DBG = ['/DEBUG']
11
12 ALL_LIBS = ['z', 'crypto', 'pthread']
13
14 def options(opt):
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)')
25
26 def configure(conf):
27
28 # load the MSVC configuration flags
29 if conf.options.msvc:
30 conf.env['MSVC_VERSIONS'] = ['msvc ' + conf.options.msvc]
31
32 conf.env['MSVC_TARGETS'] = [conf.options.arch]
33
34 # default configuration for C programs
35 conf.load('compiler_c')
36
37 dbg = conf.options.debug
38 zlib_name = 'z'
39
40 conf.env.CFLAGS = CFLAGS_UNIX + (CFLAGS_UNIX_DBG if dbg else [])
41
42 if conf.env.DEST_OS == 'win32':
43 conf.env.PLATFORM = 'win32'
44
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'
50
51 elif conf.env.CC_NAME == 'gcc':
52 conf.check(features='c cprogram', lib='pthread', uselib_store='pthread')
53
54 else:
55 conf.env.PLATFORM = 'unix'
56
57 # check for Z lib
58 conf.check(features='c cprogram', lib=zlib_name, uselib_store='z', install_path=None)
59
60 if conf.options.sha1 not in ['openssl', 'ppc', 'builtin']:
61 ctx.fatal('Invalid SHA1 option')
62
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']
67
68 elif conf.options.sha1 == 'ppc':
69 conf.env.DEFINES += ['PPC_SHA1']
70
71 conf.env.sha1 = conf.options.sha1
72
73 def build(bld):
74
75 # command '[build|clean|install|uninstall]-static'
76 if bld.variant == 'static':
77 build_library(bld, 'cstlib')
78
79 # command '[build|clean|install|uninstall]-shared'
80 elif bld.variant == 'shared':
81 build_library(bld, 'cshlib')
82
83 # command '[build|clean]-tests'
84 elif bld.variant == 'tests':
85 build_library(bld, 'cstlib')
86 build_tests(bld)
87
88 # command 'build|clean|install|uninstall': by default, run
89 # the same command for both the static and the shared lib
90 else:
91 from waflib import Options
92 Options.commands = [bld.cmd + '-shared', bld.cmd + '-static'] + Options.commands
93
94 def build_library(bld, lib_str):
95 directory = bld.path
96
97 sources = directory.ant_glob('src/*.c')
98
99 # Compile platform-dependant code
100 # E.g. src/unix/*.c
101 # src/win32/*.c
102 sources = sources + directory.ant_glob('src/%s/*.c' % bld.env.PLATFORM)
103
104 # SHA1 methods source
105 if bld.env.sha1 == "ppc":
106 sources.append('src/ppc/sha1.c')
107 else:
108 sources.append('src/block-sha1/sha1.c')
109
110 features = ['c', lib_str]
111
112 #------------------------------
113 # Build the main library
114 #------------------------------
115
116 # either as static or shared;
117 bld(features=features,
118 source=sources,
119 target='git2',
120 includes='src',
121 install_path='${LIBDIR}',
122 use=ALL_LIBS
123 )
124
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',
129 target='libgit2.pc',
130 install_path='${LIBDIR}/pkgconfig',
131 )
132
133 # Install headers
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'))
136
137 def grep_test_header(text, test_file):
138 return '\n'.join(l for l in test_file.read().splitlines() if text in l)
139
140 def build_tests(bld):
141 import os
142
143 if bld.is_install:
144 return
145
146 directory = bld.path
147 resources_path = directory.find_node('tests/resources/').abspath()
148
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')
151
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()))
155
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))
162
163 # Build individual test (don't run)
164 bld.program(
165 source=[test_file, 'tests/test_main.c'],
166 target=test_name,
167 includes=['src', 'tests'],
168 defines=['TEST_TOC="%s.toc"' % test_name, 'TEST_RESOURCES="%s"' % resources_path],
169 install_path=None,
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
174 )
175
176
177 class _test(BuildContext):
178 cmd = 'test'
179 fun = 'test'
180
181 def test(bld):
182 from waflib import Options
183 Options.commands = ['build-tests', 'run-tests'] + Options.commands
184
185 class _build_doc(Context):
186 cmd = 'doxygen'
187 fun = 'build_docs'
188
189 def build_docs(ctx):
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")
198
199 class _run_tests(Context):
200 cmd = 'run-tests'
201 fun = 'run_tests'
202
203 def run_tests(ctx):
204 import shutil, tempfile, sys
205
206 failed = False
207 test_folder = tempfile.mkdtemp()
208 test_glob = 'build/tests/t????-*'
209
210 if sys.platform == 'win32':
211 test_glob += '.exe'
212
213 for test in ctx.path.ant_glob(test_glob):
214 if ctx.exec_command(test.abspath(), cwd=test_folder) != 0:
215 failed = True
216 break
217
218 shutil.rmtree(test_folder)
219
220 if failed:
221 ctx.fatal('Test run failed')
222
223
224 CONTEXTS = {
225 'build' : BuildContext,
226 'clean' : CleanContext,
227 'install' : InstallContext,
228 'uninstall' : UninstallContext
229 }
230
231 def build_command(command):
232 ctx, var = command.split('-')
233 class _gen_command(CONTEXTS[ctx]):
234 cmd = command
235 variant = var
236
237 build_command('build-static')
238 build_command('build-shared')
239 build_command('build-tests')
240
241 build_command('clean-static')
242 build_command('clean-shared')
243 build_command('clean-tests')
244
245 build_command('install-static')
246 build_command('install-shared')
247
248 build_command('uninstall-static')
249 build_command('uninstall-shared')
250