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

5
# Unix flags
6 7
CFLAGS_UNIX = ["-O2", "-Wall", "-Wextra"]
CFLAGS_UNIX_DBG = ['-g']
8 9 10 11 12 13 14 15 16

# Windows MSVC flags
CFLAGS_WIN32_COMMON = ['/TC', '/W4', '/WX', '/nologo', '/Zi']
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.
17
CFLAGS_WIN32_L_DBG = ['/DEBUG']
Vicent Marti committed
18

19
ALL_LIBS = ['z', 'crypto', 'pthread', 'sqlite3']
20 21

def options(opt):
22 23 24
    opt.load('compiler_c')
    opt.add_option('--sha1', action='store', default='builtin',
        help="Use the builtin SHA1 routines (builtin), the \
25
PPC optimized version (ppc) or the SHA1 functions from OpenSSL (openssl)")
26 27 28 29 30 31
    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)')
32 33
    opt.add_option('--without-sqlite', action='store_false', default=True,
        dest='use_sqlite', help='Disable sqlite support')
34 35

def configure(conf):
36

37 38 39
    # load the MSVC configuration flags
    if conf.options.msvc:
        conf.env['MSVC_VERSIONS'] = ['msvc ' + conf.options.msvc]
40

41
    conf.env['MSVC_TARGETS'] = [conf.options.arch]
42

43 44
    # default configuration for C programs
    conf.load('compiler_c')
45

46 47
    dbg = conf.options.debug
    zlib_name = 'z'
Vicent Marti committed
48

49
    conf.env.CFLAGS = CFLAGS_UNIX + (CFLAGS_UNIX_DBG if dbg else [])
Vicent Marti committed
50

51 52
    if conf.env.DEST_OS == 'win32':
        conf.env.PLATFORM = 'win32'
Vicent Marti committed
53

54
        if conf.env.CC_NAME == 'msvc':
55 56 57 58
            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 [])
59 60
            conf.env.DEFINES += ['WIN32', '_DEBUG', '_LIB', 'ZLIB_WINAPI']
            zlib_name = 'zlibwapi'
Vicent Marti committed
61

62
        elif conf.env.CC_NAME == 'gcc':
63
            conf.check_cc(lib='pthread', uselib_store='pthread')
Vicent Marti committed
64

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

68
    # check for Z lib
69 70 71
    conf.check_cc(lib=zlib_name, uselib_store='z', install_path=None)

    # check for sqlite3
72 73
    if conf.options.use_sqlite and conf.check_cc(
        lib='sqlite3', uselib_store='sqlite3', install_path=None, mandatory=False):
74
        conf.env.DEFINES += ['GIT2_SQLITE_BACKEND']
75

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

79 80 81 82
    # 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
83

84 85
    elif conf.options.sha1 == 'ppc':
        conf.env.DEFINES += ['PPC_SHA1']
86

87
    conf.env.sha1 = conf.options.sha1
88 89

def build(bld):
90

91 92
    # command '[build|clean|install|uninstall]-static'
    if bld.variant == 'static':
93
        build_library(bld, 'static')
94

95 96
    # command '[build|clean|install|uninstall]-shared'
    elif bld.variant == 'shared':
97
        build_library(bld, 'shared')
98

99
    # command '[build|clean]-tests'
100
    elif bld.variant == 'test':
101
        build_library(bld, 'objects')
102
        build_test(bld)
103

104 105 106 107 108
    # 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
109

110 111 112 113 114 115 116 117 118 119 120 121 122
def get_libgit2_version(git2_h):
    import re
    line = None

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

    if line is None:
        raise "Failed to detect libgit2 version"

    return line.group(1)


123 124 125 126 127 128 129
def build_library(bld, build_type):

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

131
    directory = bld.path
132 133
    sources = directory.ant_glob('src/*.c')

134 135 136
    # Find the version of the library, from our header file
    version = get_libgit2_version(directory.find_node("src/git2.h").abspath())

137 138 139 140
    # Compile platform-dependant code
    # E.g.  src/unix/*.c
    #       src/win32/*.c
    sources = sources + directory.ant_glob('src/%s/*.c' % bld.env.PLATFORM)
141
    sources = sources + directory.ant_glob('src/backends/*.c')
142 143 144 145 146 147 148 149 150 151 152

    # 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;
153
    BUILD[build_type](
154 155 156 157
        source=sources,
        target='git2',
        includes='src',
        install_path='${LIBDIR}',
158 159
        use=ALL_LIBS,
        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 166 167 168 169 170 171 172
            source='libgit2.pc.in',
            target='libgit2.pc',
            install_path='${LIBDIR}/pkgconfig',
        )

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

174
    # On Unix systems, let them know about installation
175
    if bld.env.PLATFORM == 'unix' and bld.cmd == 'install-shared':
176 177 178
        bld.add_post_fun(call_ldconfig)

def call_ldconfig(bld):
179 180 181 182
    import distutils.spawn as s
    ldconf = s.find_executable('ldconfig')
    if ldconf:
        bld.exec_command(ldconf)
183

184
def build_test(bld):
185
    directory = bld.path
Vicent Marti committed
186
    resources_path = directory.find_node('tests/resources/').abspath().replace('\\', '/')
187

188 189
    sources = ['tests/test_lib.c', 'tests/test_helpers.c', 'tests/test_main.c']
    sources = sources + directory.ant_glob('tests/t??-*.c')
190

191 192 193 194 195 196 197
    bld.program(
        source=sources,
        target='libgit2_test',
        includes=['src', 'tests'],
        defines=['TEST_RESOURCES="%s"' % resources_path],
        use=['git2'] + ALL_LIBS
    )
198 199

class _test(BuildContext):
200 201
    cmd = 'test'
    fun = 'test'
202 203

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

207 208 209 210 211 212 213 214 215 216 217 218 219
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")
220

221 222 223
class _run_test(Context):
    cmd = 'run-test'
    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 233 234 235
        test_path += '.exe'

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

237 238
    if not test or ctx.exec_command(test.abspath(), cwd=test_folder) != 0:
        failed = True
239

240
    shutil.rmtree(test_folder)
Vicent Marti committed
241

242 243
    if failed:
        ctx.fatal('Test run failed')
244

245 246

CONTEXTS = {
247 248 249 250
    'build'     : BuildContext,
    'clean'     : CleanContext,
    'install'   : InstallContext,
    'uninstall' : UninstallContext
251 252 253
}

def build_command(command):
254 255 256 257
    ctx, var = command.split('-')
    class _gen_command(CONTEXTS[ctx]):
        cmd = command
        variant = var
258 259 260

build_command('build-static')
build_command('build-shared')
261
build_command('build-test')
262 263 264

build_command('clean-static')
build_command('clean-shared')
265
build_command('clean-test')
266 267 268 269 270 271

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

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