]> git.proxmox.com Git - libgit2.git/blob - wscript
Add proper version management
[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', '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
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_cc(lib='pthread', uselib_store='pthread')
62
63 else:
64 conf.env.PLATFORM = 'unix'
65
66 # check for Z lib
67 conf.check_cc(lib=zlib_name, uselib_store='z', install_path=None)
68
69 # check for sqlite3
70 if conf.check_cc(lib='sqlite3', uselib_store='sqlite3', install_path=None, mandatory=False):
71 conf.env.DEFINES += ['GIT2_SQLITE_BACKEND']
72
73 if conf.options.sha1 not in ['openssl', 'ppc', 'builtin']:
74 ctx.fatal('Invalid SHA1 option')
75
76 # check for libcrypto (openssl) if we are using its SHA1 functions
77 if conf.options.sha1 == 'openssl':
78 conf.check_cfg(package='libcrypto', args=['--cflags', '--libs'], uselib_store='crypto')
79 conf.env.DEFINES += ['OPENSSL_SHA1']
80
81 elif conf.options.sha1 == 'ppc':
82 conf.env.DEFINES += ['PPC_SHA1']
83
84 conf.env.sha1 = conf.options.sha1
85
86 def build(bld):
87
88 # command '[build|clean|install|uninstall]-static'
89 if bld.variant == 'static':
90 build_library(bld, 'static')
91
92 # command '[build|clean|install|uninstall]-shared'
93 elif bld.variant == 'shared':
94 build_library(bld, 'shared')
95
96 # command '[build|clean]-tests'
97 elif bld.variant == 'test':
98 build_library(bld, 'objects')
99 build_test(bld)
100
101 # command 'build|clean|install|uninstall': by default, run
102 # the same command for both the static and the shared lib
103 else:
104 from waflib import Options
105 Options.commands = [bld.cmd + '-shared', bld.cmd + '-static'] + Options.commands
106
107 def get_libgit2_version(git2_h):
108 import re
109 line = None
110
111 with open(git2_h) as f:
112 line = re.search(r'^#define LIBGIT2_VERSION "(\d\.\d\.\d)"$', f.read(), re.MULTILINE)
113
114 if line is None:
115 raise "Failed to detect libgit2 version"
116
117 return line.group(1)
118
119
120 def build_library(bld, build_type):
121
122 BUILD = {
123 'shared' : bld.shlib,
124 'static' : bld.stlib,
125 'objects' : bld.objects
126 }
127
128 directory = bld.path
129 sources = directory.ant_glob('src/*.c')
130
131 # Find the version of the library, from our header file
132 version = get_libgit2_version(directory.find_node("src/git2.h").abspath())
133
134 # Compile platform-dependant code
135 # E.g. src/unix/*.c
136 # src/win32/*.c
137 sources = sources + directory.ant_glob('src/%s/*.c' % bld.env.PLATFORM)
138 sources = sources + directory.ant_glob('src/backends/*.c')
139
140 # SHA1 methods source
141 if bld.env.sha1 == "ppc":
142 sources.append('src/ppc/sha1.c')
143 else:
144 sources.append('src/block-sha1/sha1.c')
145 #------------------------------
146 # Build the main library
147 #------------------------------
148
149 # either as static or shared;
150 BUILD[build_type](
151 source=sources,
152 target='git2',
153 includes='src',
154 install_path='${LIBDIR}',
155 use=ALL_LIBS,
156 vnum=version,
157 )
158
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',
163 target='libgit2.pc',
164 install_path='${LIBDIR}/pkgconfig',
165 )
166
167 # Install headers
168 bld.install_files('${PREFIX}/include', directory.find_node('src/git2.h'))
169 bld.install_files('${PREFIX}/include/git2', directory.ant_glob('src/git2/*.h'))
170
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)
174
175 def call_ldconfig(bld):
176 import distutils.spawn as s
177 ldconf = s.find_executable('ldconfig')
178 if ldconf:
179 bld.exec_command(ldconf)
180
181 def build_test(bld):
182 directory = bld.path
183 resources_path = directory.find_node('tests/resources/').abspath().replace('\\', '/')
184
185 sources = ['tests/test_lib.c', 'tests/test_helpers.c', 'tests/test_main.c']
186 sources = sources + directory.ant_glob('tests/t??-*.c')
187
188 bld.program(
189 source=sources,
190 target='libgit2_test',
191 includes=['src', 'tests'],
192 defines=['TEST_RESOURCES="%s"' % resources_path],
193 use=['git2'] + ALL_LIBS
194 )
195
196 class _test(BuildContext):
197 cmd = 'test'
198 fun = 'test'
199
200 def test(bld):
201 from waflib import Options
202 Options.commands = ['build-test', 'run-test'] + Options.commands
203
204 class _build_doc(Context):
205 cmd = 'doxygen'
206 fun = 'build_docs'
207
208 def build_docs(ctx):
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")
217
218 class _run_test(Context):
219 cmd = 'run-test'
220 fun = 'run_test'
221
222 def run_test(ctx):
223 import shutil, tempfile, sys
224
225 failed = False
226
227 test_path = 'build/test/libgit2_test'
228 if sys.platform == 'win32':
229 test_path += '.exe'
230
231 test_folder = tempfile.mkdtemp()
232 test = ctx.path.find_node(test_path)
233
234 if not test or ctx.exec_command(test.abspath(), cwd=test_folder) != 0:
235 failed = True
236
237 shutil.rmtree(test_folder)
238
239 if failed:
240 ctx.fatal('Test run failed')
241
242
243 CONTEXTS = {
244 'build' : BuildContext,
245 'clean' : CleanContext,
246 'install' : InstallContext,
247 'uninstall' : UninstallContext
248 }
249
250 def build_command(command):
251 ctx, var = command.split('-')
252 class _gen_command(CONTEXTS[ctx]):
253 cmd = command
254 variant = var
255
256 build_command('build-static')
257 build_command('build-shared')
258 build_command('build-test')
259
260 build_command('clean-static')
261 build_command('clean-shared')
262 build_command('clean-test')
263
264 build_command('install-static')
265 build_command('install-shared')
266
267 build_command('uninstall-static')
268 build_command('uninstall-shared')
269