language: c
compiler: gcc
sudo: false
dist: xenial
osx_image: xcode9.2

branches:
  only:
    - master
    - travis
    - coverity_scan

env:
  global:
    - MAKEFLAGS="-j4"
    # For Coverity
    - secure: "MeTS1Pqa5gzx1nn/peW/9a5kq84bba3XYUljOfkCUqzuyGiERk/nmok+RW7skrgzboBlKxnNG8+ykKqHMwK9s9M83ezFxvEWXBcKEpmEQKkqXPI5hpMs6jGLTgpeuheSIzqHA3danV8iircp1GOiTLWA0pt/AOsNLZiaYBh0OiE="

matrix:
  include:
    - os: linux
      env: LINUX_NOX=true
    - 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
    # Temporary measure, expect this to be gone by the August 1 release
    - os: osx
      env:
        - OSX_ONLY=true
        - BINTRAY_DEPLOY=true
        - PYTHON=python

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, libxml2-dev, libpango1.0-dev, libcairo2-dev, python3.7-dev,
      libbrotli-dev, ninja-build, cmake, lcov]
  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 && ./bootstrap --force && ./configure $FFCONFIG
    build_command: make -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 /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="--prefix=$PREFIX --enable-fontforge-extras --with-libspiro --with-libuninameslist"
    # Required for finding libuninameslist/libspiro properly...
    export LDFLAGS="$LDFLAGS -L$DEPSPREFIX/lib"

    if [ ! -z "$LINUX_FULL" ] ; then
      export FFCONFIG="$FFCONFIG --enable-code-coverage --enable-debug --enable-woff2"
    elif [ ! -z "$LINUX_NOX" ]; then
      export FFCONFIG="$FFCONFIG --without-x --disable-python-extension --disable-python-scripting"
    elif [ ! -z "$LINUX_RELEASE" ];then
      export FFCONFIG="$FFCONFIG --without-libzmq --enable-woff2"
    fi

    # For some inane reason Travis defines this to '-g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security'
    # This prevents pkg-config from setting the CFLAGS properly.
    unset PYTHON_CFLAGS

    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 --with-freetype-source=$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"

        pushd libspiro && autoreconf -fiv && ./configure --prefix=$DEPSPREFIX && make && make install && popd
        pushd libuninameslist && autoreconf -fiv && ./configure --enable-pscript --prefix=$DEPSPREFIX && make && 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
        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 FFCONFIG="$FFCONFIG --enable-gdk=gdk3 --enable-woff2"
      export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig:/usr/local/opt/libffi/lib/pkgconfig
      export PATH="/usr/local/opt/ruby/bin:$PATH"
      export HOMEBREW_NO_AUTO_UPDATE=1
      # 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
      brew install cairo coreutils fontconfig gettext giflib gtk+3 jpeg libpng libspiro libtiff libtool libuninameslist python@2 python@3 wget woff2
      # Pin pango to this version, because of issues, see #3343
      brew unlink pango || true
      brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/ad91e4df4a843764901811a965a17f650f34e2c1/Formula/pango.rb
      # This *must* be linked or else gettext/intl will be disabled
      brew link --force gettext
      # Until Homebrew fixes their gtk
      export CFLAGS="$CFLAGS -DGDK_WINDOWING_QUARTZ=1"
    fi

    export PYVER=$($PYTHON --version 2>&1 | cut -d' ' -f2 | cut -d. -f1-2)
    export PYTHONPATH=$PYTHONPATH:$PREFIX/lib/python$PYVER/site-packages

    # This is so terrible...
    # DO NOT SET LIBS OR FEATURE DETECTION WILL BE BROKEN
    export LIBSPIRO_CFLAGS=`pkg-config --cflags libspiro`
    export LIBUNINAMESLIST_CFLAGS=`pkg-config --cflags libuninameslist`

    set +e

script:
  - ./bootstrap
  - ./configure $FFCONFIG
  - make
  - make install
  - make check
  - if [ ! -z "$LINUX_FULL" ]; then make distcheck; fi
  - fontforge -version
  - if [ -z "$LINUX_NOX" ]; then $PYTHON travis-scripts/pyhook_smoketest.py; fi
  - if [ "$TRAVIS_OS_NAME" == "osx" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ]; then travis-scripts/ffosxbuild.sh $PREFIX $TRAVIS_COMMIT; fi
  - if [ ! -z "$LINUX_RELEASE" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ]; then travis-scripts/ffappimagebuild.sh; fi

after_success:
  - if [ ! -z "$LINUX_FULL" ]; then coveralls --gcov-options '\-bulp' --gcov $(which $GCOV); fi

after_failure:
  - which python3
  - env
  - cat tests/testsuite.log || 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
