# Travis CI configuration file
# https://travis-ci.org/dfandrich/gpscorrelate

sudo: false

language: cpp

git:
  # Set to 10 instead of 1 to avoid problems when the most recent commits have [skip ci]
  depth: 10

# Install needed packages on Ubuntu and OS X
addons:
  apt:
    packages:
      - desktop-file-utils
      - docbook-xsl
      - exiv2
      - libexiv2-dev
      - libgtk2.0-dev
      - xsltproc
  homebrew:
    packages:
      - desktop-file-utils
      - docbook-xsl
      - exiv2
      - gtk+
      - libxml2
    # The default OS X image is now (August 2019) too old to work without this
    update: true

install:
  - if [ "$CONFIG" = "coverage" ] ; then pip install --user cpp-coveralls; fi
  - |
    if [ "$CONFIG" = "musl" ] ; then
      echo > Dockerfile "
        FROM alpine
        RUN apk add --no-cache build-base exiv2-dev libxml2-dev gettext-dev gtk+2.0-dev desktop-file-utils
        # Use the same user id as the one building this container
        RUN adduser -D -u $(id -u) build
      "
      docker build -t alpine-build .
    fi

env:
  # More configurations are configured in the matrix section
  matrix:
    - CONFIG=normal
    - CONFIG=no-nls
  global:
    - MAKEFLAGS='-j 2'

compiler:
  - clang
  - gcc

os:
  - linux
  - osx

matrix:
  include:
  # Only run the coverage build in one configuration
  - env: CONFIG=coverage DIST=bionic
    os: linux
    dist: bionic
    compiler: gcc
  - env: CONFIG=debuglog DIST=bionic
    os: linux
    dist: bionic
    compiler: gcc
  - env: CONFIG=no-werror GTK=3
    os: linux
    compiler: gcc
    addons:
      apt:
        packages:
          - desktop-file-utils
          - docbook-xsl
          - exiv2
          - libexiv2-dev
          - libgtk-3-dev
          - xsltproc
  - env: CONFIG=no-werror DIST=bionic GTK=3
    os: linux
    dist: bionic
    compiler: gcc
    addons:
      apt:
        packages:
          - desktop-file-utils
          - docbook-xsl
          - exiv2
          - libexiv2-dev
          - libgtk-3-dev
          - xsltproc
  - env: CONFIG=normal DIST=bionic
    os: linux
    dist: bionic
    compiler: gcc
  - env: CONFIG=no-werror DIST=bionic
    os: linux
    dist: bionic
    compiler: clang
  - env: CONFIG=normal DIST=xenial
    os: linux
    dist: xenial
    compiler: gcc
  - env: CONFIG=no-werror  DIST=xenial
    os: linux
    dist: xenial
    compiler: clang
  - env: CONFIG=clang6 DIST=xenial
    os: linux
    dist: xenial
    compiler: clang
    addons:
      apt:
        packages:
          - desktop-file-utils
          - docbook-xsl
          - exiv2
          - libexiv2-dev
          - libgtk2.0-dev
          - clang-6.0
          - xsltproc
  - env: CONFIG=clang7
    os: linux
    compiler: clang
    addons:
      apt:
        sources:
          - llvm-toolchain-trusty-7
          - ubuntu-toolchain-r-test
        packages:
          - desktop-file-utils
          - docbook-xsl
          - exiv2
          - libexiv2-dev
          - libgtk2.0-dev
          - clang-7
          - xsltproc
  - env: CONFIG=clang8
    os: linux
    compiler: clang
    addons:
      apt:
        sources:
          - llvm-toolchain-trusty-8
          - ubuntu-toolchain-r-test
        packages:
          - desktop-file-utils
          - docbook-xsl
          - exiv2
          - libexiv2-dev
          - libgtk2.0-dev
          - clang-8
          - xsltproc
  - env: CONFIG=gcc8
    os: linux
    compiler: gcc
    addons:
      apt:
        sources:
          - ubuntu-toolchain-r-test
        packages:
          - desktop-file-utils
          - docbook-xsl
          - exiv2
          - libexiv2-dev
          - libgtk2.0-dev
          - g++-8
          - xsltproc
  - env: CONFIG=sanitize
    os: linux
    compiler: clang
    addons:
      apt:
        sources:
          - llvm-toolchain-trusty-8
          - ubuntu-toolchain-r-test
        packages:
          - desktop-file-utils
          - docbook-xsl
          - exiv2
          - libexiv2-dev
          - libgtk2.0-dev
          - clang-8
          - xsltproc
  - env: CONFIG=musl DIST=alpine
    os: linux
    sudo: required
    services: docker
  - env: CONFIG=no-werror GTK=3
    os: osx
    compiler: gcc
    addons:
      homebrew:
        packages:
          - desktop-file-utils
          - docbook-xsl
          - exiv2
          - gtk+3
          - libxml2
        # The default OS X image is now (August 2019) too old to work without this
        update: true
  - env: CONFIG=normal
    os: osx
    osx_image: xcode11
    compiler: clang
    addons:
      homebrew:
        packages:
          - desktop-file-utils
          - docbook-xsl
          - exiv2
          - gtk+
          - libxml2
  - env: CONFIG=normal DIST=bionic
    arch: arm64
    os: linux
    dist: bionic
    compiler: gcc

before_install:
  - GTK=${GTK:-2}
  # The apt addons section is ignored on bionic for some reason
  - if [ "${DIST}" = "bionic" ] ; then sudo apt-get install -y desktop-file-utils docbook-xsl exiv2 gettext libexiv2-dev libgtk${GTK/3/-}${GTK/2/.0}-dev libxml2-dev xsltproc; fi

script:
  # Build both the command-line tool and GUI by default
  - TARGET='gpscorrelate gpscorrelate-gui'
  - INSTALLTARGET='install install-po install-desktop-file'
  - if [ "$CONFIG" = "normal" ] ; then CFLAGS='-Wall -Wextra -Werror -Wno-error=deprecated-declarations -O3 -DENABLE_NLS=1'; fi
  - if [ "$CONFIG" = "debuglog" ] ; then CFLAGS='-Wall -Wextra -Werror -O3 -DENABLE_NLS=1 -DDEBUG'; fi
  - if [ "$CONFIG" = "no-werror" ] ; then CFLAGS='-Wall -Wextra -O3 -DENABLE_NLS=1'; fi
  - if [ "$CONFIG" = "no-nls" ] ; then CFLAGS='-Wall -Wextra -Werror -Wno-error=deprecated-declarations -O3'; fi
  - if [ "$CONFIG" = "sanitize" ] ; then CFLAGS='-g -O0 -fsanitize=address -fsanitize=undefined -DENABLE_NLS=1'; LDFLAGS='$(CFLAGS)'; export CC=clang-8; export ASAN_OPTIONS=strict_string_checks=0:detect_stack_use_after_return=1:check_initialization_order=1:strict_init_order=1; fi
    # FIXME: There are no tests for the GUI, so don't bother building it.
  - if [ "$CONFIG" = "coverage" ] ; then CFLAGS='-g -O0 --coverage'; LDFLAGS='$(CFLAGS)'; TARGET='gpscorrelate'; INSTALLTARGET='install-po install-desktop-file'; fi
  - if [ "$CONFIG" = "clang6" ] ; then CFLAGS='-Wall -Wextra -Werror -Wno-error=deprecated-declarations -O3 -DENABLE_NLS=1'; CC=clang-6.0; CXX=clang++-6.0; fi
  - if [ "$CONFIG" = "clang7" ] ; then CFLAGS='-Wall -Wextra -Werror -Wno-error=deprecated-declarations -O3 -DENABLE_NLS=1'; CC=clang-7; CXX=clang++-7; fi
  - if [ "$CONFIG" = "clang8" ] ; then CFLAGS='-Wall -Wextra -Werror -Wno-error=deprecated-declarations -O3 -DENABLE_NLS=1'; CC=clang-8; CXX=clang++-8; fi
  - if [ "$CONFIG" = "gcc8" ] ; then CFLAGS='-Wall -Wextra -Werror -O3 -DENABLE_NLS=1'; CC=gcc-8; CXX=g++-8; fi

    # Explicitly point to Homebrew's gettext and pkg-config files on OS X
  - if [ "$TRAVIS_OS_NAME" = "osx" ] ; then CFLAGS="$CFLAGS -I/usr/local/opt/gettext/include"; LDFLAGS="$LDFLAGS -L/usr/local/opt/gettext/lib -lintl"; export PKG_CONFIG_PATH="$(brew --prefix libffi)/lib/pkgconfig/":"$(brew --prefix libxml2)/lib/pkgconfig/"; fi

  - |
    set -e
    if [ "$CONFIG" = "musl" ] ; then
      docker run --rm -v "$PWD":/tmp/src alpine-build sh -c '
        cat /etc/os-release
        # Some tests fail if run as root (the ones to do with read only files)
        su -l build -c "
          set -ex
          cd /tmp/src
          make CFLAGS=-DENABLE_NLS LDFLAGS=-lintl GTK=2 gpscorrelate gpscorrelate-gui
          ./gpscorrelate -V
          make check
          make install-po install-desktop-file prefix= DESTDIR="\${HOME}"
          "
        '
    else
      make CFLAGS="$CFLAGS" LDFLAGS="$LDFLAGS" CC="$CC" CXX="$CXX" GTK="$GTK" $TARGET
     ./gpscorrelate -V
      if [ "$CONFIG" = "debuglog" ] ; then
        # Debug logging enabled makes some tests fail due to differing output
        make check | tee /dev/stderr | grep -q '15 test(s) have FAILED'
      else
        make check
      fi
      # Ensure brew gettext is in the PATH so xgettext is found on OS X
      PATH="$PATH:/usr/local/opt/gettext/bin" make prefix= DESTDIR="${HOME}" $INSTALLTARGET
    fi
    set +e

after_success:
  - if [ "$CONFIG" = "coverage" ] ; then coveralls --gcov-options '\-lp'; fi
