summaryrefslogtreecommitdiff
path: root/wscript
blob: 22b55bfd46b7fb646f8661ce1e157d2e9b39e5c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from waflib.Context import Context
from waflib.Build import BuildContext, CleanContext, \
        InstallContext, UninstallContext

CFLAGS = ["-g", "-O2", "-Wall", "-Wextra"]

def options(opt):
	opt.load('compiler_c')
	opt.add_option('--sha1', action='store', default='builtin',
		help="Use the builtin SHA1 routines (builtin), the \
PPC optimized version (ppc) or the SHA1 functions from OpenSSH (openssh)")

def configure(conf):
	conf.load('compiler_c')

	if conf.options.sha1 not in ['openssh', 'ppc', 'builtin']:
		ctx.fatal('Invalid SHA1 option')

	conf.env.sha1 = conf.options.sha1

def build(bld):

	if bld.variant == 'static':
		build_library(bld, 'cstlib')

	elif bld.variant == 'shared':
		build_library(bld, 'cshlib')

	elif bld.variant == 'tests':
		build_tests(bld)

	else:
		from waflib import Options
		Options.commands = [bld.cmd + '-shared', bld.cmd + '-static'] + Options.commands

def build_library(bld, lib_str):
	import sys

	directory = bld.path

	sources = directory.ant_glob('src/*.c')
	flags = CFLAGS
	defines = []
	visibility = True
	os = 'unix'

	if sys.platform == 'win32':
		# windows configuration
		flags = flags + ['-TC', '-W4', '-RTC1', '-Zi']
		defines = defines = ['WIN32', '_DEBUG', '_LIB']
		visibility = False
		os = 'win32'

	elif sys.platform == 'cygwin':
		visibility = False

	elif sys.platform == 'mingw': # TODO
		pass

	if bld.env.sha1 == "openssh":
		defines.append('OPENSSL_SHA1')

	elif bld.env.sha1 == "ppc":
		defines.append('PPC_SHA1')
		sources.append('src/ppc/sha1.c')

	else:
		sources.append('src/block-sha1/sha1.c')

	if not visibility:
		flags.append('-fvisibility=hidden')

	sources = sources + directory.ant_glob('src/%s/*.c' % os)

	bld(features=['c', lib_str],
		source=sources,
		target='git2',
		includes='src',
		cflags=flags,
		defines=defines,
		stlib=['git2', 'z'],
		install_path='${LIBDIR}',
	)

	if os == 'unix':
		bld(rule="""sed -e 's#@prefix@#$(prefix)#' -e 's#@libdir@#$(libdir)#' < ${SRC} > ${TGT}""",
			source='libgit2.pc.in',
			target='libgit2.pc',
			install_path='${LIBDIR}/pkgconfig',
		)

	bld.install_files('${PREFIX}/include/git', directory.ant_glob('src/git/*.h'))

def build_tests(bld):
	import os

	if bld.is_install:
		return

	directory = bld.path
	bld.objects(source=['tests/test_helpers.c', 'tests/test_lib.c'], includes=['src', 'tests'], target='test_helper')

	for test_file in directory.ant_glob('tests/t????-*.c'):
		test_name, _ = os.path.splitext(os.path.basename(test_file.abspath()))

		test_toc_file = directory.make_node('tests/%s.toc' % test_name)
		if bld.cmd == 'clean-tests':
			test_toc_file.delete()
		elif bld.cmd == 'build-tests':
			test_toc = bld.cmd_and_log(['grep', 'BEGIN_TEST', test_file.abspath()], quiet=True)
			test_toc_file.write(test_toc)

		bld.program(
			source=[test_file, 'tests/test_main.c'],
			target=test_name,
			includes=['src', 'tests'],
			defines=['TEST_TOC="%s.toc"' % test_name],
			stlib=['git2', 'z'],
			stlibpath=directory.find_node('build/static/').abspath(),
			use='test_helper')


class _test(BuildContext):
	cmd = 'test'
	fun = 'test'

def test(bld):
	from waflib import Options
	Options.commands = ['build-static', 'build-tests', 'run-tests'] + Options.commands


class _run_tests(Context):
	cmd = 'run-tests'
	fun = 'run_tests'

def run_tests(ctx):
	test_folder = ctx.path.make_node('tests/tmp/')

	for test in ctx.path.ant_glob('build/tests/t????-*'):
		test_folder.delete()
		test_folder.mkdir()

		if ctx.exec_command(test.abspath(), cwd=test_folder.abspath()) != 0:
			ctx.fatal('Test run failed')
			break

	test_folder.delete()


CONTEXTS = {
	'build'		: BuildContext,
	'clean'		: CleanContext,
	'install'	: InstallContext,
	'uninstall' : UninstallContext
}

def build_command(command):
	ctx, var = command.split('-')
	class _gen_command(CONTEXTS[ctx]):
		cmd = command
		variant = var

build_command('build-static')
build_command('build-shared')
build_command('build-tests')

build_command('clean-static')
build_command('clean-shared')
build_command('clean-tests')

build_command('install-static')
build_command('install-shared')

build_command('uninstall-static')
build_command('uninstall-shared')