language: cpp
matrix:
  include:
  # This config does a few things:
  # - Checks C++ and Python code styles (check-style.sh and flake8).
  # - Makes sure sphinx can build the docs without any errors or warnings.
  # - Tests setup.py sdist and install (all header files should be present).
  # - Makes sure that everything still works without optional deps (numpy/scipy/eigen) and
  #   also tests the automatic discovery functions in CMake (Python version, C++ standard).
  - os: linux
    dist: xenial # Necessary to run doxygen 1.8.15
    name: Style, docs, and pip
    cache: false
    before_install:
    - pyenv global $(pyenv whence 2to3)  # activate all python versions
    - PY_CMD=python3
    - $PY_CMD -m pip install --user --upgrade pip wheel setuptools
    install:
    - $PY_CMD -m pip install --user --upgrade sphinx sphinx_rtd_theme breathe flake8 pep8-naming pytest
    - curl -fsSL https://sourceforge.net/projects/doxygen/files/rel-1.8.15/doxygen-1.8.15.linux.bin.tar.gz/download | tar xz
    - export PATH="$PWD/doxygen-1.8.15/bin:$PATH"
    script:
    - tools/check-style.sh
    - flake8
    - $PY_CMD -m sphinx -W -b html docs docs/.build
    - |
      # Make sure setup.py distributes and installs all the headers
      $PY_CMD setup.py sdist
      $PY_CMD -m pip install --user -U ./dist/*
      installed=$($PY_CMD -c "import pybind11; print(pybind11.get_include(True) + '/pybind11')")
      diff -rq $installed ./include/pybind11
    - |
      # Barebones build
      cmake -DCMAKE_BUILD_TYPE=Debug -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON -DPYTHON_EXECUTABLE=$(which $PY_CMD) .
      make pytest -j 2
      make cpptest -j 2
  # The following are regular test configurations, including optional dependencies.
  # With regard to each other they differ in Python version, C++ standard and compiler.
  - os: linux
    dist: trusty
    name: Python 2.7, c++11, gcc 4.8
    env: PYTHON=2.7 CPP=11 GCC=4.8
    addons:
      apt:
        packages:
          - cmake=2.\*
          - cmake-data=2.\*
  - os: linux
    dist: trusty
    name: Python 3.6, c++11, gcc 4.8
    env: PYTHON=3.6 CPP=11 GCC=4.8
    addons:
      apt:
        sources:
          - deadsnakes
        packages:
          - python3.6-dev
          - python3.6-venv
          - cmake=2.\*
          - cmake-data=2.\*
  - os: linux
    dist: trusty
    env: PYTHON=2.7 CPP=14 GCC=6 CMAKE=1
    name: Python 2.7, c++14, gcc 4.8, CMake test
    addons:
      apt:
        sources:
          - ubuntu-toolchain-r-test
        packages:
          - g++-6
  - os: linux
    dist: trusty
    name: Python 3.5, c++14, gcc 6, Debug build
    # N.B. `ensurepip` could be installed transitively by `python3.5-venv`, but
    # seems to have apt conflicts (at least for Trusty). Use Docker instead.
    services: docker
    env: DOCKER=debian:stretch PYTHON=3.5 CPP=14 GCC=6 DEBUG=1
  - os: linux
    dist: xenial
    env: PYTHON=3.6 CPP=17 GCC=7
    name: Python 3.6, c++17, gcc 7
    addons:
      apt:
        sources:
          - deadsnakes
          - ubuntu-toolchain-r-test
        packages:
          - g++-7
          - python3.6-dev
          - python3.6-venv
  - os: linux
    dist: xenial
    env: PYTHON=3.6 CPP=17 CLANG=7
    name: Python 3.6, c++17, Clang 7
    addons:
      apt:
        sources:
          - deadsnakes
          - llvm-toolchain-xenial-7
        packages:
          - python3.6-dev
          - python3.6-venv
          - clang-7
          - libclang-7-dev
          - llvm-7-dev
          - lld-7
          - libc++-7-dev
          - libc++abi-7-dev  # Why is this necessary???
  - os: osx
    name: Python 2.7, c++14, AppleClang 7.3, CMake test
    osx_image: xcode7.3
    env: PYTHON=2.7 CPP=14 CLANG CMAKE=1
  - os: osx
    name: Python 3.7, c++14, AppleClang 9, Debug build
    osx_image: xcode9
    env: PYTHON=3.7 CPP=14 CLANG DEBUG=1
  # Test a PyPy 2.7 build
  - os: linux
    dist: trusty
    env: PYPY=5.8 PYTHON=2.7 CPP=11 GCC=4.8
    name: PyPy 5.8, Python 2.7, c++11, gcc 4.8
    addons:
      apt:
        packages:
          - libblas-dev
          - liblapack-dev
          - gfortran
  # Build in 32-bit mode and tests against the CMake-installed version
  - os: linux
    dist: trusty
    services: docker
    env: DOCKER=i386/debian:stretch PYTHON=3.5 CPP=14 GCC=6 INSTALL=1
    name: Python 3.4, c++14, gcc 6, 32-bit
    script:
      - |
        # Consolidated 32-bit Docker Build + Install
        set -ex
        $SCRIPT_RUN_PREFIX sh -c "
          set -ex
          cmake ${CMAKE_EXTRA_ARGS} -DPYBIND11_INSTALL=1 -DPYBIND11_TEST=0 .
          make install
          cp -a tests /pybind11-tests
          mkdir /build-tests && cd /build-tests
          cmake ../pybind11-tests ${CMAKE_EXTRA_ARGS} -DPYBIND11_WERROR=ON
          make pytest -j 2"
        set +ex
cache:
  directories:
  - $HOME/.local/bin
  - $HOME/.local/lib
  - $HOME/.local/include
  - $HOME/Library/Python
before_install:
- |
  # Configure build variables
  set -ex
  if [ "$TRAVIS_OS_NAME" = "linux" ]; then
    if [ -n "$CLANG" ]; then
      export CXX=clang++-$CLANG CC=clang-$CLANG
      EXTRA_PACKAGES+=" clang-$CLANG llvm-$CLANG-dev"
    else
      if [ -z "$GCC" ]; then GCC=4.8
      else EXTRA_PACKAGES+=" g++-$GCC"
      fi
      export CXX=g++-$GCC CC=gcc-$GCC
    fi
  elif [ "$TRAVIS_OS_NAME" = "osx" ]; then
    export CXX=clang++ CC=clang;
  fi
  if [ -n "$CPP" ]; then CPP=-std=c++$CPP; fi
  if [ "${PYTHON:0:1}" = "3" ]; then PY=3; fi
  if [ -n "$DEBUG" ]; then CMAKE_EXTRA_ARGS+=" -DCMAKE_BUILD_TYPE=Debug"; fi
  set +ex
- |
  # Initialize environment
  set -ex
  if [ -n "$DOCKER" ]; then
    docker pull $DOCKER

    containerid=$(docker run --detach --tty \
      --volume="$PWD":/pybind11 --workdir=/pybind11 \
      --env="CC=$CC" --env="CXX=$CXX" --env="DEBIAN_FRONTEND=$DEBIAN_FRONTEND" \
      --env=GCC_COLORS=\  \
      $DOCKER)
    SCRIPT_RUN_PREFIX="docker exec --tty $containerid"
    $SCRIPT_RUN_PREFIX sh -c 'for s in 0 15; do sleep $s; apt-get update && apt-get -qy dist-upgrade && break; done'
  else
    if [ "$PYPY" = "5.8" ]; then
      curl -fSL https://bitbucket.org/pypy/pypy/downloads/pypy2-v5.8.0-linux64.tar.bz2 | tar xj
      PY_CMD=$(echo `pwd`/pypy2-v5.8.0-linux64/bin/pypy)
      CMAKE_EXTRA_ARGS+=" -DPYTHON_EXECUTABLE:FILEPATH=$PY_CMD"
    else
      PY_CMD=python$PYTHON
      if [ "$TRAVIS_OS_NAME" = "osx" ]; then
        if [ "$PY" = "3" ]; then
          brew update && brew upgrade python
        else
          curl -fsSL https://bootstrap.pypa.io/get-pip.py | $PY_CMD - --user
        fi
      fi
    fi
    if [ "$PY" = 3 ] || [ -n "$PYPY" ]; then
      $PY_CMD -m ensurepip --user
    fi
    $PY_CMD --version
    $PY_CMD -m pip install --user --upgrade pip wheel
  fi
  set +ex
install:
- |
  # Install dependencies
  set -ex
  cmake --version
  if [ -n "$DOCKER" ]; then
    if [ -n "$DEBUG" ]; then
      PY_DEBUG="python$PYTHON-dbg python$PY-scipy-dbg"
      CMAKE_EXTRA_ARGS+=" -DPYTHON_EXECUTABLE=/usr/bin/python${PYTHON}dm"
    fi
    $SCRIPT_RUN_PREFIX sh -c "for s in 0 15; do sleep \$s; \
      apt-get -qy --no-install-recommends install \
        $PY_DEBUG python$PYTHON-dev python$PY-pytest python$PY-scipy \
        libeigen3-dev libboost-dev cmake make ${EXTRA_PACKAGES} && break; done"
  else

    if [ "$CLANG" = "7" ]; then
      export CXXFLAGS="-stdlib=libc++"
    fi

    export NPY_NUM_BUILD_JOBS=2
    echo "Installing pytest, numpy, scipy..."
    local PIP_CMD=""
    if [ -n $PYPY ]; then
      # For expediency, install only versions that are available on the extra index.
      travis_wait 30 \
        $PY_CMD -m pip install --user --upgrade --extra-index-url https://imaginary.ca/trusty-pypi \
          pytest numpy==1.15.4 scipy==1.2.0
    else
      $PY_CMD -m pip install --user --upgrade pytest numpy scipy
    fi
    echo "done."

    mkdir eigen
    curl -fsSL https://bitbucket.org/eigen/eigen/get/3.3.4.tar.bz2 | \
        tar --extract -j --directory=eigen --strip-components=1
    export CMAKE_INCLUDE_PATH="${CMAKE_INCLUDE_PATH:+$CMAKE_INCLUDE_PATH:}$PWD/eigen"
  fi
  set +ex
script:
- |
  # CMake Configuration
  set -ex
  $SCRIPT_RUN_PREFIX cmake ${CMAKE_EXTRA_ARGS} \
    -DPYBIND11_PYTHON_VERSION=$PYTHON \
    -DPYBIND11_CPP_STANDARD=$CPP \
    -DPYBIND11_WERROR=${WERROR:-ON} \
    -DDOWNLOAD_CATCH=${DOWNLOAD_CATCH:-ON} \
    .
  set +ex
- |
  # pytest
  set -ex
  $SCRIPT_RUN_PREFIX make pytest -j 2 VERBOSE=1
  set +ex
- |
  # cpptest
  set -ex
  $SCRIPT_RUN_PREFIX make cpptest -j 2
  set +ex
- |
  # CMake Build Interface
  set -ex
  if [ -n "$CMAKE" ]; then $SCRIPT_RUN_PREFIX make test_cmake_build; fi
  set +ex
after_failure: cat tests/test_cmake_build/*.log*
after_script:
- |
  # Cleanup (Docker)
  set -ex
  if [ -n "$DOCKER" ]; then docker stop "$containerid"; docker rm "$containerid"; fi
  set +ex