wscript 8.79 KB
Newer Older
1
from __future__ import with_statement
2 3 4 5
from waflib.Context import Context
from waflib.Build import BuildContext, CleanContext, \
        InstallContext, UninstallContext

6
# Unix flags
7
CFLAGS_UNIX = ["-O2", "-Wall", "-Wextra", "-fPIC"]
8
CFLAGS_UNIX_DBG = ['-g', '-O0']
9 10

# Windows MSVC flags
11
CFLAGS_WIN32_COMMON = ['/TC', '/W4', '/WX', '/nologo', '/Zi']
12 13 14 15 16 17
CFLAGS_WIN32_RELEASE = ['/O2', '/MD']

# Note: /RTC* cannot be used with optimization on.
CFLAGS_WIN32_DBG = ['/Od', '/RTC1', '/RTCc', '/DEBUG', '/MDd']
CFLAGS_WIN32_L = ['/RELEASE']  # used for /both/ debug and release builds.
                               # sets the module's checksum in the header.
18
CFLAGS_WIN32_L_DBG = ['/DEBUG']
Vicent Marti committed
19

Dmitry Kovega committed
20
ALL_LIBS = ['crypto', 'pthread', 'sqlite3', 'hiredis']
21 22

def options(opt):
23 24 25
    opt.load('compiler_c')
    opt.add_option('--sha1', action='store', default='builtin',
        help="Use the builtin SHA1 routines (builtin), the \
26
PPC optimized version (ppc) or the SHA1 functions from OpenSSL (openssl)")
27 28 29 30 31 32
    opt.add_option('--debug', action='store_true', default=False,
        help='Compile with debug symbols')
    opt.add_option('--msvc', action='store', default=None,
        help='Force a specific MSVC++ version (7.1, 8.0, 9.0, 10.0), if more than one is installed')
    opt.add_option('--arch', action='store', default='x86',
        help='Select target architecture (ia64, x64, x86, x86_amd64, x86_ia64)')
Vicent Marti committed
33
    opt.add_option('--with-sqlite', action='store_true', default=False,
34
        dest='use_sqlite', help='Enable sqlite support')
Dmitry Kovega committed
35 36
    opt.add_option('--with-hiredis', action='store_true', default=False,
        dest='use_hiredis', help='Enable redis support using hiredis')
Vicent Marti committed
37 38
    opt.add_option('--threadsafe', action='store_true', default=False,
        help='Make libgit2 thread-safe (requires pthreads)')
39 40

def configure(conf):
41

42 43 44
    # load the MSVC configuration flags
    if conf.options.msvc:
        conf.env['MSVC_VERSIONS'] = ['msvc ' + conf.options.msvc]
45

46
    conf.env['MSVC_TARGETS'] = [conf.options.arch]
47

48 49
    # default configuration for C programs
    conf.load('compiler_c')
50

51 52
    dbg = conf.options.debug

53
    conf.env.CFLAGS += CFLAGS_UNIX + (CFLAGS_UNIX_DBG if dbg else [])
54

55 56
    if conf.env.DEST_OS == 'win32':
        conf.env.PLATFORM = 'win32'
Vicent Marti committed
57

58 59 60 61 62 63 64
        if conf.env.CC_NAME == 'msvc':
            conf.env.CFLAGS = CFLAGS_WIN32_COMMON + \
              (CFLAGS_WIN32_DBG if dbg else CFLAGS_WIN32_RELEASE)
            conf.env.LINKFLAGS += CFLAGS_WIN32_L + \
              (CFLAGS_WIN32_L_DBG if dbg else [])
            conf.env.DEFINES += ['WIN32', '_DEBUG', '_LIB']

65 66
    else:
        conf.env.PLATFORM = 'unix'
Vicent Marti committed
67

Trent Mick committed
68 69 70
    if conf.env.DEST_OS == 'sunos':
        conf.env.DEFINES += ['NO_VIZ']

Vicent Marti committed
71 72 73 74
    if conf.options.threadsafe:
        if conf.env.PLATFORM == 'unix':
            conf.check_cc(lib='pthread', uselib_store='pthread')
        conf.env.DEFINES += ['GIT_THREADS']
Vicent Marti committed
75

76
    # check for sqlite3
77 78
    if conf.options.use_sqlite and conf.check_cc(
        lib='sqlite3', uselib_store='sqlite3', install_path=None, mandatory=False):
79
        conf.env.DEFINES += ['GIT2_SQLITE_BACKEND']
80

Dmitry Kovega committed
81 82 83 84 85 86
    # check for hiredis
    if conf.options.use_hiredis and conf.check_cc(
        lib='hiredis', uselib_store='hiredis', install_path=None, mandatory=False):
        conf.env.DEFINES += ['GIT2_HIREDIS_BACKEND']


87
    if conf.options.sha1 not in ['openssl', 'ppc', 'builtin']:
88
        conf.fatal('Invalid SHA1 option')
89

90 91 92 93
    # check for libcrypto (openssl) if we are using its SHA1 functions
    if conf.options.sha1 == 'openssl':
        conf.check_cfg(package='libcrypto', args=['--cflags', '--libs'], uselib_store='crypto')
        conf.env.DEFINES += ['OPENSSL_SHA1']
Vicent Marti committed
94

95 96
    elif conf.options.sha1 == 'ppc':
        conf.env.DEFINES += ['PPC_SHA1']
97

98
    conf.env.sha1 = conf.options.sha1
99 100

def build(bld):
101

102 103
    # command '[build|clean|install|uninstall]-static'
    if bld.variant == 'static':
104
        build_library(bld, 'static')
105

106 107
    # command '[build|clean|install|uninstall]-shared'
    elif bld.variant == 'shared':
108
        build_library(bld, 'shared')
109

110
    # command '[build|clean]-tests'
111
    elif bld.variant == 'test':
112
        build_library(bld, 'objects')
113
        build_test(bld)
114

115 116 117 118 119
    # command 'build|clean|install|uninstall': by default, run
    # the same command for both the static and the shared lib
    else:
        from waflib import Options
        Options.commands = [bld.cmd + '-shared', bld.cmd + '-static'] + Options.commands
120

121 122 123 124 125
def get_libgit2_version(git2_h):
    import re
    line = None

    with open(git2_h) as f:
126
        line = re.search(r'^#define LIBGIT2_VERSION "(\d+\.\d+\.\d+)"$', f.read(), re.MULTILINE)
127 128

    if line is None:
129
        raise Exception("Failed to detect libgit2 version")
130 131 132 133

    return line.group(1)


134 135 136 137 138 139 140
def build_library(bld, build_type):

    BUILD = {
        'shared' : bld.shlib,
        'static' : bld.stlib,
        'objects' : bld.objects
    }
141

142
    directory = bld.path
143 144
    sources = directory.ant_glob('src/*.c')

145
    # Find the version of the library, from our header file
146
    version = get_libgit2_version(directory.find_node("include/git2.h").abspath())
147

148 149 150 151
    # Compile platform-dependant code
    # E.g.  src/unix/*.c
    #       src/win32/*.c
    sources = sources + directory.ant_glob('src/%s/*.c' % bld.env.PLATFORM)
152
    sources = sources + directory.ant_glob('src/backends/*.c')
153
    sources = sources + directory.ant_glob('deps/zlib/*.c')
154 155 156 157 158 159 160 161 162 163 164

    # SHA1 methods source
    if bld.env.sha1 == "ppc":
        sources.append('src/ppc/sha1.c')
    else:
        sources.append('src/block-sha1/sha1.c')
    #------------------------------
    # Build the main library
    #------------------------------

    # either as static or shared;
165
    BUILD[build_type](
166 167
        source=sources,
        target='git2',
168
        includes=['src', 'include', 'deps/zlib'],
169
        install_path='${LIBDIR}',
170 171
        use=ALL_LIBS,
        vnum=version,
172 173 174
    )

    # On Unix systems, build the Pkg-config entry file
175
    if bld.env.PLATFORM == 'unix' and bld.is_install:
176
        bld(rule="""sed -e 's#@prefix@#${PREFIX}#' -e 's#@libdir@#${LIBDIR}#' -e 's#@version@#%s#' < ${SRC} > ${TGT}""" % version,
177 178 179 180 181 182
            source='libgit2.pc.in',
            target='libgit2.pc',
            install_path='${LIBDIR}/pkgconfig',
        )

    # Install headers
183 184
    bld.install_files('${PREFIX}/include', directory.find_node('include/git2.h'))
    bld.install_files('${PREFIX}/include/git2', directory.ant_glob('include/git2/*.h'))
185

186
    # On Unix systems, let them know about installation
187
    if bld.env.PLATFORM == 'unix' and bld.cmd == 'install-shared':
188 189 190
        bld.add_post_fun(call_ldconfig)

def call_ldconfig(bld):
191 192 193 194
    import distutils.spawn as s
    ldconf = s.find_executable('ldconfig')
    if ldconf:
        bld.exec_command(ldconf)
195

196
def build_test(bld):
197
    directory = bld.path
Vicent Marti committed
198
    resources_path = directory.find_node('tests/resources/').abspath().replace('\\', '/')
199

200 201
    sources = ['tests/test_lib.c', 'tests/test_helpers.c', 'tests/test_main.c']
    sources = sources + directory.ant_glob('tests/t??-*.c')
202

203 204 205
    bld.program(
        source=sources,
        target='libgit2_test',
206
        includes=['src', 'tests', 'include'],
207 208 209
        defines=['TEST_RESOURCES="%s"' % resources_path],
        use=['git2'] + ALL_LIBS
    )
210 211

class _test(BuildContext):
212 213
    cmd = 'test'
    fun = 'test'
214 215

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

219 220 221 222 223 224 225 226 227 228 229 230 231
class _build_doc(Context):
    cmd = 'doxygen'
    fun = 'build_docs'

def build_docs(ctx):
    ctx.exec_command("doxygen api.doxygen")
    ctx.exec_command("git stash")
    ctx.exec_command("git checkout gh-pages")
    ctx.exec_command("cp -Rf apidocs/html/* .")
    ctx.exec_command("git add .")
    ctx.exec_command("git commit -am 'generated docs'")
    ctx.exec_command("git push origin gh-pages")
    ctx.exec_command("git checkout master")
232

233 234 235
class _run_test(Context):
    cmd = 'run-test'
    fun = 'run_test'
236

237
def run_test(ctx):
238
    import shutil, tempfile, sys
239

240
    failed = False
241

242
    test_path = 'build/test/libgit2_test'
243
    if sys.platform == 'win32':
244 245 246 247
        test_path += '.exe'

    test_folder = tempfile.mkdtemp()
    test = ctx.path.find_node(test_path)
248

249 250
    if not test or ctx.exec_command(test.abspath(), cwd=test_folder) != 0:
        failed = True
251

252
    shutil.rmtree(test_folder)
Vicent Marti committed
253

254 255
    if failed:
        ctx.fatal('Test run failed')
256

257 258

CONTEXTS = {
259 260 261 262
    'build'     : BuildContext,
    'clean'     : CleanContext,
    'install'   : InstallContext,
    'uninstall' : UninstallContext
263 264 265
}

def build_command(command):
266 267 268 269
    ctx, var = command.split('-')
    class _gen_command(CONTEXTS[ctx]):
        cmd = command
        variant = var
270 271 272

build_command('build-static')
build_command('build-shared')
273
build_command('build-test')
274 275 276

build_command('clean-static')
build_command('clean-shared')
277
build_command('clean-test')
278 279 280 281 282 283

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

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