]> git.proxmox.com Git - libgit2.git/blob - wscript
Merge branch 'parse-tag-buffer' of https://github.com/carlosmn/libgit2 into development
[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', '-O0']
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 = ['crypto', 'pthread', 'sqlite3']
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 opt.add_option('--with-sqlite', action='store_true', default=False,
33 dest='use_sqlite', help='Enable sqlite support')
34 opt.add_option('--threadsafe', action='store_true', default=False,
35 help='Make libgit2 thread-safe (requires pthreads)')
36
37 def configure(conf):
38
39 # load the MSVC configuration flags
40 if conf.options.msvc:
41 conf.env['MSVC_VERSIONS'] = ['msvc ' + conf.options.msvc]
42
43 conf.env['MSVC_TARGETS'] = [conf.options.arch]
44
45 # default configuration for C programs
46 conf.load('compiler_c')
47
48 dbg = conf.options.debug
49
50 conf.env.CFLAGS = CFLAGS_UNIX + (CFLAGS_UNIX_DBG if dbg else [])
51
52 if conf.env.DEST_OS == 'win32':
53 conf.env.PLATFORM = 'win32'
54
55 if conf.env.CC_NAME == 'msvc':
56 conf.env.CFLAGS = CFLAGS_WIN32_COMMON + \
57 (CFLAGS_WIN32_DBG if dbg else CFLAGS_WIN32_RELEASE)
58 conf.env.LINKFLAGS += CFLAGS_WIN32_L + \
59 (CFLAGS_WIN32_L_DBG if dbg else [])
60 conf.env.DEFINES += ['WIN32', '_DEBUG', '_LIB']
61
62 else:
63 conf.env.PLATFORM = 'unix'
64
65 if conf.options.threadsafe:
66 if conf.env.PLATFORM == 'unix':
67 conf.check_cc(lib='pthread', uselib_store='pthread')
68 conf.env.DEFINES += ['GIT_THREADS']
69
70 # check for sqlite3
71 if conf.options.use_sqlite and conf.check_cc(
72 lib='sqlite3', uselib_store='sqlite3', install_path=None, mandatory=False):
73 conf.env.DEFINES += ['GIT2_SQLITE_BACKEND']
74
75 if conf.options.sha1 not in ['openssl', 'ppc', 'builtin']:
76 conf.fatal('Invalid SHA1 option')
77
78 # check for libcrypto (openssl) if we are using its SHA1 functions
79 if conf.options.sha1 == 'openssl':
80 conf.check_cfg(package='libcrypto', args=['--cflags', '--libs'], uselib_store='crypto')
81 conf.env.DEFINES += ['OPENSSL_SHA1']
82
83 elif conf.options.sha1 == 'ppc':
84 conf.env.DEFINES += ['PPC_SHA1']
85
86 conf.env.sha1 = conf.options.sha1
87
88 def build(bld):
89
90 # command '[build|clean|install|uninstall]-static'
91 if bld.variant == 'static':
92 build_library(bld, 'static')
93
94 # command '[build|clean|install|uninstall]-shared'
95 elif bld.variant == 'shared':
96 build_library(bld, 'shared')
97
98 # command '[build|clean]-tests'
99 elif bld.variant == 'test':
100 build_library(bld, 'objects')
101 build_test(bld)
102
103 # command 'build|clean|install|uninstall': by default, run
104 # the same command for both the static and the shared lib
105 else:
106 from waflib import Options
107 Options.commands = [bld.cmd + '-shared', bld.cmd + '-static'] + Options.commands
108
109 def get_libgit2_version(git2_h):
110 import re
111 line = None
112
113 with open(git2_h) as f:
114 line = re.search(r'^#define LIBGIT2_VERSION "(\d+\.\d+\.\d+)"$', f.read(), re.MULTILINE)
115
116 if line is None:
117 raise Exception("Failed to detect libgit2 version")
118
119 return line.group(1)
120
121
122 def build_library(bld, build_type):
123
124 BUILD = {
125 'shared' : bld.shlib,
126 'static' : bld.stlib,
127 'objects' : bld.objects
128 }
129
130 directory = bld.path
131 sources = directory.ant_glob('src/*.c')
132
133 # Find the version of the library, from our header file
134 version = get_libgit2_version(directory.find_node("include/git2.h").abspath())
135
136 # Compile platform-dependant code
137 # E.g. src/unix/*.c
138 # src/win32/*.c
139 sources = sources + directory.ant_glob('src/%s/*.c' % bld.env.PLATFORM)
140 sources = sources + directory.ant_glob('src/backends/*.c')
141 sources = sources + directory.ant_glob('deps/zlib/*.c')
142
143 # SHA1 methods source
144 if bld.env.sha1 == "ppc":
145 sources.append('src/ppc/sha1.c')
146 else:
147 sources.append('src/block-sha1/sha1.c')
148 #------------------------------
149 # Build the main library
150 #------------------------------
151
152 # either as static or shared;
153 BUILD[build_type](
154 source=sources,
155 target='git2',
156 includes=['src', 'include', 'deps/zlib'],
157 install_path='${LIBDIR}',
158 use=ALL_LIBS,
159 vnum=version,
160 )
161
162 # On Unix systems, build the Pkg-config entry file
163 if bld.env.PLATFORM == 'unix' and bld.is_install:
164 bld(rule="""sed -e 's#@prefix@#${PREFIX}#' -e 's#@libdir@#${LIBDIR}#' -e 's#@version@#%s#' < ${SRC} > ${TGT}""" % version,
165 source='libgit2.pc.in',
166 target='libgit2.pc',
167 install_path='${LIBDIR}/pkgconfig',
168 )
169
170 # Install headers
171 bld.install_files('${PREFIX}/include', directory.find_node('include/git2.h'))
172 bld.install_files('${PREFIX}/include/git2', directory.ant_glob('include/git2/*.h'))
173
174 # On Unix systems, let them know about installation
175 if bld.env.PLATFORM == 'unix' and bld.cmd == 'install-shared':
176 bld.add_post_fun(call_ldconfig)
177
178 def call_ldconfig(bld):
179 import distutils.spawn as s
180 ldconf = s.find_executable('ldconfig')
181 if ldconf:
182 bld.exec_command(ldconf)
183
184 def build_test(bld):
185 directory = bld.path
186 resources_path = directory.find_node('tests/resources/').abspath().replace('\\', '/')
187
188 sources = ['tests/test_lib.c', 'tests/test_helpers.c', 'tests/test_main.c']
189 sources = sources + directory.ant_glob('tests/t??-*.c')
190
191 bld.program(
192 source=sources,
193 target='libgit2_test',
194 includes=['src', 'tests', 'include'],
195 defines=['TEST_RESOURCES="%s"' % resources_path],
196 use=['git2'] + ALL_LIBS
197 )
198
199 class _test(BuildContext):
200 cmd = 'test'
201 fun = 'test'
202
203 def test(bld):
204 from waflib import Options
205 Options.commands = ['build-test', 'run-test'] + Options.commands
206
207 class _build_doc(Context):
208 cmd = 'doxygen'
209 fun = 'build_docs'
210
211 def build_docs(ctx):
212 ctx.exec_command("doxygen api.doxygen")
213 ctx.exec_command("git stash")
214 ctx.exec_command("git checkout gh-pages")
215 ctx.exec_command("cp -Rf apidocs/html/* .")
216 ctx.exec_command("git add .")
217 ctx.exec_command("git commit -am 'generated docs'")
218 ctx.exec_command("git push origin gh-pages")
219 ctx.exec_command("git checkout master")
220
221 class _run_test(Context):
222 cmd = 'run-test'
223 fun = 'run_test'
224
225 def run_test(ctx):
226 import shutil, tempfile, sys
227
228 failed = False
229
230 test_path = 'build/test/libgit2_test'
231 if sys.platform == 'win32':
232 test_path += '.exe'
233
234 test_folder = tempfile.mkdtemp()
235 test = ctx.path.find_node(test_path)
236
237 if not test or ctx.exec_command(test.abspath(), cwd=test_folder) != 0:
238 failed = True
239
240 shutil.rmtree(test_folder)
241
242 if failed:
243 ctx.fatal('Test run failed')
244
245
246 CONTEXTS = {
247 'build' : BuildContext,
248 'clean' : CleanContext,
249 'install' : InstallContext,
250 'uninstall' : UninstallContext
251 }
252
253 def build_command(command):
254 ctx, var = command.split('-')
255 class _gen_command(CONTEXTS[ctx]):
256 cmd = command
257 variant = var
258
259 build_command('build-static')
260 build_command('build-shared')
261 build_command('build-test')
262
263 build_command('clean-static')
264 build_command('clean-shared')
265 build_command('clean-test')
266
267 build_command('install-static')
268 build_command('install-shared')
269
270 build_command('uninstall-static')
271 build_command('uninstall-shared')
272