language: c
compiler: gcc
sudo: false
dist: xenial
osx_image: xcode10.1

branches:
  only:
    - master
    - travis
    - coverity_scan

env:
  global:
    - CMAKE=cmake
    - CTEST_PARALLEL_LEVEL=100
    # For Coverity
    - secure: "MeTS1Pqa5gzx1nn/peW/9a5kq84bba3XYUljOfkCUqzuyGiERk/nmok+RW7skrgzboBlKxnNG8+ykKqHMwK9s9M83ezFxvEWXBcKEpmEQKkqXPI5hpMs6jGLTgpeuheSIzqHA3danV8iircp1GOiTLWA0pt/AOsNLZiaYBh0OiE="
    # For Crowdin
    - secure: "bvU1qkor+TQPr6LXxlQ6w2hwdLIXkEh+QWhipjmKLU0Aj59sGF4+U9uVLP/QbGNulDiwUanVCSV7QZhi1HKKB4rVDVlyhzOQvHU+67dQDB4CKDlezp4TKhVoHcpP7MerSdQ02wr45VbwOITAKpvicdBKwb5x/p1v8HsO25UbJlE="


matrix:
  include:
    - os: linux
      env:
        - LINUX_NOX=true
        - CMAKE=/usr/bin/cmake # Make sure at least one build uses CMake 3.5
    - os: linux
      env: LINUX_FULL=true
    - os: linux
      env:
        - LINUX_RELEASE=true
        - BINTRAY_DEPLOY=true
    - os: osx
      env:
        - OSX_ONLY=true
        - BINTRAY_DEPLOY=true
        - PYTHON=python3

addons:
  apt:
    sources:
      - ubuntu-toolchain-r-test
      - deadsnakes
    packages: [autoconf, automake, autotools-dev, bzip2, gcc-8, g++-8, libtool,
      libjpeg-dev, libtiff5-dev, libpng12-dev, libfreetype6-dev, libgif-dev,
      libx11-dev, libgtk-3-dev, libxml2-dev, libpango1.0-dev, libcairo2-dev, python3.7-dev,
      libbrotli-dev, ninja-build, cmake, lcov, python3.7-venv]
  coverity_scan:
    project:
      name: "fontforge/fontforge"
      description: "Free (libre) font editor for Windows, Mac OS X and GNU+Linux"
    build_command_prepend: cov-configure --comptype gcc --compiler $CC --template && mkdir build && pushd build && cmake $FFCONFIG .. && popd
    build_command: make -C build -j4
    branch_pattern: coverity_scan
    notification_email: jtanx_blackhole@outlook.com

cache:
  directories:
    - $TRAVIS_BUILD_DIR/travisdeps

before_install:
  - if [ ! -z "$LINUX_FULL" ]; then pip --quiet install --user cpp-coveralls -U ; fi
  - if [ "$TRAVIS_OS_NAME" == "osx" ]; then ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)" -- --force; sudo rm -rf ~/.rvm; fi
  - if [ "$TRAVIS_OS_NAME" == "osx" ]; then git config --global protocol.version 1 && /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"; fi

install:
  - |
    set -e
    export PREFIX=$TRAVIS_BUILD_DIR/target
    export DEPSPREFIX=$TRAVIS_BUILD_DIR/travisdeps
    export PATH=$PATH:$DEPSPREFIX/bin:$PREFIX/bin
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$DEPSPREFIX/lib:$PREFIX/lib
    export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$DEPSPREFIX/lib/pkgconfig
    export FFCONFIG="-DCMAKE_INSTALL_PREFIX=$PREFIX -DENABLE_FONTFORGE_EXTRAS=ON"

    if [ ! -z "$LINUX_FULL" ] ; then
      export FFCONFIG="$FFCONFIG -DENABLE_X11=ON -DENABLE_CODE_COVERAGE=ON -DCMAKE_BUILD_TYPE=Debug -DENABLE_DOCS=ON -DSPHINX_USE_VENV=ON"
    elif [ ! -z "$LINUX_NOX" ]; then
      export FFCONFIG="$FFCONFIG -DENABLE_GUI=OFF -DENABLE_PYTHON_SCRIPTING=OFF"
    fi

    if [ "$TRAVIS_OS_NAME" == "linux" ]; then
      export FTVER=`dpkg -s libfreetype6-dev | perl -ne 'print $1 if /^Version: (\d+(?:\.\d+)+)/'`
      export SFFTVER=`echo $FTVER | perl -ne 'print $1 if /^(\d+(?:\.\d+){1,2})/'`
      export FFCONFIG="$FFCONFIG -DENABLE_FREETYPE_DEBUGGER=$DEPSPREFIX/freetype-$FTVER"
      export CC=gcc-8
      export CXX=g++-8
      export GCOV=gcov-8

      # This block only runs if the cache isn't present.
      if [ ! "$(ls -A $DEPSPREFIX)" ]; then
        mkdir -p work && pushd work
        git clone --depth 1 https://github.com/fontforge/libspiro
        git clone --depth 1 https://github.com/fontforge/libuninameslist
        git clone --depth 1 --branch v1.0.2 https://github.com/google/woff2
        wget --tries 1 "http://download.savannah.gnu.org/releases/freetype/freetype-$FTVER.tar.gz" || \
          wget "https://sourceforge.net/projects/freetype/files/freetype2/$SFFTVER/freetype-$FTVER.tar.gz"
        wget https://downloads.crowdin.com/cli/v2/crowdin-cli.zip

        pushd libspiro && autoreconf -fiv && ./configure --prefix=$DEPSPREFIX && make -j4 && make install && popd
        pushd libuninameslist && autoreconf -fiv && ./configure --enable-pscript --prefix=$DEPSPREFIX && make -j4 && make install && popd
        pushd woff2 && mkdir build && cd build && cmake -GNinja .. -DCMAKE_INSTALL_PREFIX=$DEPSPREFIX -DCMAKE_INSTALL_LIBDIR=lib && ninja install && popd
        tar -zxf freetype-$FTVER.tar.gz -C $DEPSPREFIX
        mkdir crowdin && pushd crowdin && unzip ../crowdin-cli.zip && mv */* . && mv crowdin-cli.jar $DEPSPREFIX && popd
        popd
      fi

      # These are FF-specific mods (set here so it runs also in Coverity)
      export CFLAGS="$CFLAGS -fdiagnostics-color=always -Wall -Wno-switch -Wduplicated-cond -Wduplicated-branches -Wlogical-op -Wrestrict -Wnull-dereference -Wjump-misses-init -Wdouble-promotion -Wshadow"
      export PYTHON=python3.7
    else
      export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig:/usr/local/opt/libffi/lib/pkgconfig
      export PATH="/usr/local/opt/ruby/bin:/usr/local/opt/gettext/bin:$PATH"
      export HOMEBREW_NO_AUTO_UPDATE=1
      export FFCONFIG="$FFCONFIG -DCMAKE_FIND_ROOT_PATH=/usr/local/opt/gettext"
      # Disable fc-cache on fontconfig install. Because it's slow.
      sed -i.bak '/fc-cache/d' "$(brew --prefix)/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/fontconfig.rb"
      # 10 billion years later...
      brew install autoconf automake libtool pkg-config ruby cmake ninja
      brew install cairo coreutils fontconfig gettext giflib gtk+3 jpeg libpng libspiro libtiff libtool libuninameslist python@3 wget woff2
    fi

    # Don't specify the prefix in the prefix='' argument else you get different results...
    export PYTHONPATH=$PREFIX/$($PYTHON -c "import distutils.sysconfig as sc; print(sc.get_python_lib(prefix='', plat_specific=True,standard_lib=False))")
    if [ "$TRAVIS_OS_NAME" == "osx" ]; then set +e; fi

script:
  - $CMAKE --version
  - mkdir build && pushd build && $CMAKE -GNinja $FFCONFIG .. && popd
  - ninja -C build install
  - ninja -C build check
  - if [ ! -z "$LINUX_FULL" ]; then ninja -C build dist; fi
  - if [ -z "$LINUX_NOX" ]; then $PYTHON travis-scripts/pyhook_smoketest.py; fi
  - if [ "$TRAVIS_OS_NAME" == "osx" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ]; then ninja -C build macbundle; fi
  - |
    if [ ! -z "$LINUX_RELEASE" ]; then
      ninja -C build potfiles
      DESTDIR=build/appdir $CMAKE -DCMAKE_INSTALL_PREFIX=/usr -P build/cmake_install.cmake
      travis-scripts/ffappimagebuild.sh build/appdir $TRAVIS_COMMIT
    fi

after_success:
  - if [ ! -z "$LINUX_FULL" ]; then coveralls -b build --gcov-options '\-bulp' --gcov $(which $GCOV); fi
  - if [ ! -z "$LINUX_RELEASE" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ] && [ "$TRAVIS_BRANCH" == "master" ]; then java -jar $DEPSPREFIX/crowdin-cli.jar -c .crowdin.yml upload; fi

after_failure:
  - which $PYTHON
  - env
  - cat build/CMakeCache.txt || true
  - cat build/CMakeFiles/CMakeOutput.log || true
  - cat build/Testing/Temporary/LastTest.log || true
  - cat build/build.ninja || true

deploy:
  provider: bintray
  file: "travis-scripts/bintray_descriptor.json"
  user: "fontforge-ci-bot"
  dry-run: false
  skip_cleanup: true
  key:
    secure: "YBvK5QLZ4LNE3WQPXjDYalLsTgmiGyRGYY87ep5knQ0YGyt3Q9OiBCtQ3GvhHNKOa2L2QlctJi2VWcUu+ftrS72mi5j2tvse9wRF3V8EVFIPb3bxjnN6E11FjXtLIqU8WR7HlAHJpOL2bhO+RleIhSVZm0MsiSjekFOoA5tfll4="
  on:
    condition: '"$BINTRAY_DEPLOY" = "true"'
    branch:
      - master
      #- travis
