1#!/usr/bin/env python 2# Build the project on Travis CI. 3 4from __future__ import print_function 5import errno, os, re, shutil, sys, tempfile, urllib 6from subprocess import call, check_call, check_output, 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(['npm', 'install', '-g', 'less', '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 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) 88common_cmake_flags = [ 89 '-DCMAKE_INSTALL_PREFIX=' + install_dir, '-DCMAKE_BUILD_TYPE=' + build 90] 91extra_cmake_flags = [] 92if standard != '0x': 93 extra_cmake_flags = [ 94 '-DCMAKE_CXX_FLAGS=-std=c++' + standard, '-DFMT_USE_CPP11=OFF' 95 ] 96check_call(['cmake', '-DFMT_DOC=OFF', '-DFMT_PEDANTIC=ON', fmt_dir] + 97 common_cmake_flags + extra_cmake_flags, cwd=build_dir) 98 99# Build library. 100check_call(['make', '-j4'], cwd=build_dir) 101 102# Test library. 103env = os.environ.copy() 104env['CTEST_OUTPUT_ON_FAILURE'] = '1' 105if call(['make', 'test'], env=env, cwd=build_dir): 106 with open('Testing/Temporary/LastTest.log', 'r') as f: 107 print(f.read()) 108 sys.exit(-1) 109 110# Install library. 111check_call(['make', 'install'], cwd=build_dir) 112 113# Test installation. 114makedirs_if_not_exist(test_build_dir) 115check_call(['cmake', '-DCMAKE_CXX_FLAGS=-std=c++' + standard, 116 os.path.join(fmt_dir, "test", "find-package-test")] + 117 common_cmake_flags, cwd=test_build_dir) 118check_call(['make', '-j4'], cwd=test_build_dir) 119