1#!/usr/local/bin/python
2# Build the project on Travis CI.
3
4from __future__ import print_function
5import errno, os, shutil, subprocess, sys, urllib
6from subprocess import call, check_call, Popen, PIPE, STDOUT
7
8def rmtree_if_exists(dir):
9    try:
10        shutil.rmtree(dir)
11    except OSError as e:
12        if e.errno == errno.ENOENT:
13            pass
14
15def makedirs_if_not_exist(dir):
16    try:
17        os.makedirs(dir)
18    except OSError as e:
19        if e.errno != errno.EEXIST:
20            raise
21
22def install_dependencies():
23    branch = os.environ['TRAVIS_BRANCH']
24    if branch != 'master':
25        print('Branch: ' + branch)
26        exit(0) # Ignore non-master branches
27    check_call('curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key ' +
28               '| sudo apt-key add -', shell=True)
29    check_call('echo "deb https://deb.nodesource.com/node_0.10 precise main" ' +
30               '| sudo tee /etc/apt/sources.list.d/nodesource.list', shell=True)
31    check_call(['sudo', 'apt-get', 'update'])
32    check_call(['sudo', 'apt-get', 'install', 'python-virtualenv', 'nodejs'])
33    check_call(['sudo', 'npm', 'install', '-g', 'less@2.6.1', 'less-plugin-clean-css'])
34    deb_file = 'doxygen_1.8.6-2_amd64.deb'
35    urllib.urlretrieve('http://mirrors.kernel.org/ubuntu/pool/main/d/doxygen/' +
36                       deb_file, deb_file)
37    check_call(['sudo', 'dpkg', '-i', deb_file])
38
39fmt_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
40
41build = os.environ['BUILD']
42if build == 'Doc':
43    travis = 'TRAVIS' in os.environ
44    if travis:
45        install_dependencies()
46    sys.path.insert(0, os.path.join(fmt_dir, 'doc'))
47    import build
48    build.create_build_env()
49    html_dir = build.build_docs()
50    repo = 'fmtlib.github.io'
51    if travis and 'KEY' not in os.environ:
52        # Don't update the repo if building on Travis from an account that
53        # doesn't have push access.
54        print('Skipping update of ' + repo)
55        exit(0)
56    # Clone the fmtlib.github.io repo.
57    rmtree_if_exists(repo)
58    git_url = 'https://github.com/' if travis else 'git@github.com:'
59    check_call(['git', 'clone', git_url + 'fmtlib/{}.git'.format(repo)])
60    # Copy docs to the repo.
61    target_dir = os.path.join(repo, 'dev')
62    rmtree_if_exists(target_dir)
63    shutil.copytree(html_dir, target_dir, ignore=shutil.ignore_patterns('.*'))
64    if travis:
65        check_call(['git', 'config', '--global', 'user.name', 'amplbot'])
66        check_call(['git', 'config', '--global', 'user.email', 'viz@ampl.com'])
67    # Push docs to GitHub pages.
68    check_call(['git', 'add', '--all'], cwd=repo)
69    if call(['git', 'diff-index', '--quiet', 'HEAD'], cwd=repo):
70        check_call(['git', 'commit', '-m', 'Update documentation'], cwd=repo)
71        cmd = 'git push'
72        if travis:
73            cmd += ' https://$KEY@github.com/fmtlib/fmtlib.github.io.git master'
74        p = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT, cwd=repo)
75        # Print the output without the key.
76        print(p.communicate()[0].replace(os.environ['KEY'], '$KEY'))
77        if p.returncode != 0:
78            raise subprocess.CalledProcessError(p.returncode, cmd)
79    exit(0)
80
81standard = os.environ['STANDARD']
82install_dir    = os.path.join(fmt_dir, "_install")
83build_dir      = os.path.join(fmt_dir, "_build")
84test_build_dir = os.path.join(fmt_dir, "_build_test")
85
86# Configure library.
87makedirs_if_not_exist(build_dir)
88cmake_flags = [
89    '-DCMAKE_INSTALL_PREFIX=' + install_dir, '-DCMAKE_BUILD_TYPE=' + build,
90    '-DCMAKE_CXX_STANDARD=' + standard
91]
92check_call(['cmake', '-DFMT_DOC=OFF', '-DFMT_PEDANTIC=ON', '-DFMT_WERROR=ON', fmt_dir] +
93           cmake_flags, cwd=build_dir)
94
95# Build library.
96check_call(['make', '-j4'], cwd=build_dir)
97
98# Test library.
99env = os.environ.copy()
100env['CTEST_OUTPUT_ON_FAILURE'] = '1'
101if call(['make', 'test'], env=env, cwd=build_dir):
102    with open(os.path.join(build_dir, 'Testing', 'Temporary', 'LastTest.log'), 'r') as f:
103        print(f.read())
104    sys.exit(-1)
105
106# Install library.
107check_call(['make', 'install'], cwd=build_dir)
108
109# Test installation.
110makedirs_if_not_exist(test_build_dir)
111check_call(['cmake', os.path.join(fmt_dir, "test", "find-package-test")] +
112            cmake_flags, cwd=test_build_dir)
113check_call(['make', '-j4'], cwd=test_build_dir)
114