# Use new container infrastructure to enable caching
sudo: false

# Do not choose a language; we provide our own build tools.
language: generic

# Caching so the next build will be fast too.
cache:
  directories:
  - $HOME/.cabsnap
  - $HOME/.cabal/packages
  - $HOME/.stack
  - $HOME/.cabal/store
  # alternatively:
  #- $HOME/.stack/bin
  #- $HOME/.stack/precompiled
  #- $HOME/.stack/programs
  #- $HOME/.stack/setup-exe-cache
  #- $HOME/.stack/snapshots

before_cache:
  - rm -fv $HOME/.cabal/packages/hackage.haskell.org/build-reports.log
  # remove files that are regenerated by 'cabal update'
  - rm -fv $HOME/.cabal/packages/hackage.haskell.org/00-index.*
  - rm -fv $HOME/.cabal/packages/hackage.haskell.org/*.json
  - rm -fv $HOME/.cabal/packages/hackage.haskell.org/01-index.cache
  - rm -fv $HOME/.cabal/packages/hackage.haskell.org/01-index.tar
  - rm -fv $HOME/.cabal/packages/hackage.haskell.org/01-index.tar.idx

# The different configurations we want to test. We have
# - BUILD=cabal which uses cabal-install(<2.0)
# - BUILD=canew which uses cabal-install 2.0 "new-build"
# - BUILD=stack which uses Stack.
#
# We set the compiler values here to tell Travis to use a different
# cache file per set of arguments.
#
# If you need to have different apt packages for each combination in the
# matrix, you can use a line such as:
#     addons: {apt: {packages: [libfcgi-dev,libgmp-dev]}}
matrix:
  include:
  
  ##### OSX test via stack #####

  # Build on macOS in addition to Linux
  - env: BUILD=stack ARGS="--stack-yaml stack-8.2.2.yaml"
    compiler: ": #stack 8.2.2 osx"
    os: osx

  ##### CABAL #####
  
  - env: BUILD=cabal GHCVER=8.0.2 CABALVER=1.24 HAPPYVER=1.19.5 ALEXVER=3.1.7
    compiler: ": #cabal 8.0.2"
    addons: {apt: {packages: [cabal-install-1.24,ghc-8.0.2,happy-1.19.5,alex-3.1.7], sources: [hvr-ghc]}}
  - env: BUILD=cabal GHCVER=8.2.2 CABALVER=1.24 HAPPYVER=1.19.5 ALEXVER=3.1.7
    compiler: ": #cabal 8.2.2"
    addons: {apt: {packages: [cabal-install-1.24,ghc-8.2.2,happy-1.19.5,alex-3.1.7], sources: [hvr-ghc]}}
  - env: BUILD=cabal GHCVER=8.4.4 CABALVER=1.24 HAPPYVER=1.19.5 ALEXVER=3.1.7
    compiler: ": #cabal 8.4.4"
    addons: {apt: {packages: [cabal-install-1.24,ghc-8.4.4,happy-1.19.5,alex-3.1.7], sources: [hvr-ghc]}}
  - env: BUILD=cabal GHCVER=8.6.5 CABALVER=1.24 HAPPYVER=1.19.5 ALEXVER=3.1.7
    compiler: ": #cabal 8.6.5"
    addons: {apt: {packages: [cabal-install-1.24,ghc-8.6.5,happy-1.19.5,alex-3.1.7], sources: [hvr-ghc]}}

  # Build with the newest GHC and cabal-install. This is an accepted failure,
  # see below.
  #- env: BUILD=cabal GHCVER=head  CABALVER=head HAPPYVER=1.19.5 ALEXVER=3.1.7
  #  compiler: ": #GHC HEAD"
  #  addons: {apt: {packages: [cabal-install-head,ghc-head,happy-1.19.5,alex-3.1.7], sources: [hvr-ghc]}}

  ##### CABAL DIST CHECK

  - env: BUILD=cabaldist GHCVER=8.2.2 CABALVER=1.24 HAPPYVER=1.19.5 ALEXVER=3.1.7
    compiler: ": #cabal 8.2.2 dist"
    addons: {apt: {packages: [cabal-install-1.24,ghc-8.2.2,happy-1.19.5,alex-3.1.7], sources: [hvr-ghc]}}

  ##### CANEW #####

  - env: BUILD=canew GHCVER=8.2.2 CABALVER=2.0 HAPPYVER=1.19.5 ALEXVER=3.1.7
    compiler: ": #cabal new 8.2.2"
    addons: {apt: {packages: [cabal-install-2.0,ghc-8.2.2,happy-1.19.5,alex-3.1.7], sources: [hvr-ghc]}}

  ##### STACK #####

  # The Stack builds. We can pass in arbitrary Stack arguments via the ARGS
  # variable, such as using --stack-yaml to point to a different file.
  - env: BUILD=stack ARGS=""
    compiler: ": #stack default"
    addons: {apt: {packages: [libgmp-dev]}}

  - env: BUILD=stack ARGS="--stack-yaml stack-8.0.2.yaml"
    compiler: ": #stack 8.0.2"
    addons: {apt: {packages: [libgmp-dev]}}
  - env: BUILD=stack ARGS="--stack-yaml stack-8.2.2.yaml"
    compiler: ": #stack 8.2.2"
    addons: {apt: {packages: [libgmp-dev]}}
  - env: BUILD=stack ARGS="--stack-yaml stack-8.4.3.yaml"
    compiler: ": #stack 8.4.3"
    addons: {apt: {packages: [libgmp-dev]}}
  - env: BUILD=stack ARGS="--stack-yaml stack-8.6.5.yaml"
    compiler: ": #stack 8.6.5"
    addons: {apt: {packages: [libgmp-dev]}}

  # Nightly builds are allowed to fail
  - env: BUILD=stack ARGS="--resolver nightly"
    compiler: ": #stack nightly"
    addons: {apt: {packages: [libgmp-dev]}}

  allow_failures:
  #- env: BUILD=cabal GHCVER=head  CABALVER=head HAPPYVER=1.19.5 ALEXVER=3.1.7
  - env: BUILD=stack ARGS="--resolver nightly"
  - env: BUILD=stack ARGS=""

before_install:
# Using compiler above sets CC to an invalid value, so unset it
- unset CC

# We want to always allow newer versions of packages when building on GHC HEAD
- CABALARGS=""
- if [ "x$GHCVER" = "xhead" ]; then CABALARGS=--allow-newer; fi

# Download and unpack the stack executable
- export PATH=/opt/ghc/$GHCVER/bin:/opt/cabal/$CABALVER/bin:$HOME/.local/bin:/opt/alex/$ALEXVER/bin:/opt/happy/$HAPPYVER/bin:$HOME/.cabal/bin:$PATH
- mkdir -p ~/.local/bin
- |
  if [ `uname` = "Darwin" ]
  then
    travis_retry curl --insecure -L https://www.stackage.org/stack/osx-x86_64 | tar xz --strip-components=1 --include '*/stack' -C ~/.local/bin
  else
    travis_retry curl -L https://www.stackage.org/stack/linux-x86_64 | tar xz --wildcards --strip-components=1 -C ~/.local/bin '*/stack'
  fi

  # Use the more reliable S3 mirror of Hackage
  #mkdir -p $HOME/.cabal
  #echo 'remote-repo: hackage.haskell.org:http://hackage.fpcomplete.com/' > $HOME/.cabal/config
  #echo 'remote-repo-cache: $HOME/.cabal/packages' >> $HOME/.cabal/config

  #if [ "$CABALVER" != "1.16" ]
  #then
  #  echo 'jobs: $ncpus' >> $HOME/.cabal/config
  #fi
- PKGNAME='brittany'
- JOBS='1'
- |
  function better_wait() {
    date
    time "$@" & # send the long living command to background!

    set +x
    MINUTES=0
    LIMIT=30
    while kill -0 $! >/dev/null 2>&1; do
      echo -n -e " \b" # never leave evidences!

      if [ $MINUTES == $LIMIT ]; then
        break;
      fi

      MINUTES=$((MINUTES+1))

      sleep 60
    done
    wait $!
    set -x
  }

install:
- echo "$(ghc --version) [$(ghc --print-project-git-commit-id 2> /dev/null || echo '?')]"
- if [ -f configure.ac ]; then autoreconf -i; fi
- |
  set -ex
  case "$BUILD" in
    stack)
      stack -j$JOBS --no-terminal --install-ghc $ARGS test --bench --only-dependencies
      ;;
    cabal*)
      cabal --version
      echo "$(ghc --version) [$(ghc --print-project-git-commit-id 2> /dev/null || echo '?')]"
      if [ -f $HOME/.cabal/packages/hackage.haskell.org/00-index.tar.gz ];
      then
        zcat $HOME/.cabal/packages/hackage.haskell.org/00-index.tar.gz >$HOME/.cabal/packages/hackage.haskell.org/00-index.tar;
      fi
      travis_retry cabal update -v
      sed -i 's/^jobs:/-- jobs:/' ${HOME}/.cabal/config
      cabal install --only-dependencies --enable-tests --enable-benchmarks --dry -v > installplan.txt
      sed -i -e '1,/^Resolving /d' installplan.txt; cat installplan.txt

      # check whether current requested install-plan matches cached package-db snapshot
      if diff -u $HOME/.cabsnap/installplan.txt installplan.txt;
      then
        echo "cabal build-cache HIT";
        rm -rfv .ghc;
        cp -a $HOME/.cabsnap/ghc $HOME/.ghc;
        cp -a $HOME/.cabsnap/lib $HOME/.cabsnap/share $HOME/.cabsnap/bin $HOME/.cabal/;
      else
        echo "cabal build-cache MISS";
        rm -rf $HOME/.cabsnap;
        mkdir -p $HOME/.ghc $HOME/.cabal/lib $HOME/.cabal/share $HOME/.cabal/bin;
        cabal install -j$JOBS --only-dependencies --enable-tests --enable-benchmarks --ghc-options="-j1 +RTS -M500M -RTS";
      fi
      
      # snapshot package-db on cache miss
      if [ ! -d $HOME/.cabsnap ];
      then
         echo "snapshotting package-db to build-cache";
         mkdir $HOME/.cabsnap;
         cp -a $HOME/.ghc $HOME/.cabsnap/ghc;
         cp -a $HOME/.cabal/lib $HOME/.cabal/share $HOME/.cabal/bin installplan.txt $HOME/.cabsnap/;
      fi
      ;;
    canew)
      cabal --version
      travis_retry cabal update -v
      echo 'packages: .' > cabal.project
      echo 'package brittany'                          > cabal.project.local
      echo '  ghc-options: -Werror -with-rtsopts=-N1' >> cabal.project.local
      rm -f cabal.project.freeze
      cabal new-build -j$JOBS --enable-test --enable-benchmarks --dep
      cabal new-build -j$JOBS --disable-tests --disable-benchmarks --dep
      ;;
  esac
  set +ex

script:
- |
  set -ex
  case "$BUILD" in
    stack)
      better_wait stack -j$JOBS --no-terminal $ARGS test --bench --no-run-benchmarks --haddock --no-haddock-deps --ghc-options="-j1 +RTS -M500M -RTS -Werror -with-rtsopts=-N1"
      ;;
    cabal)
      if [ -f configure.ac ]; then autoreconf -i; fi
      cabal configure --enable-tests --enable-benchmarks -v  # -v2 provides useful information for debugging
      better_wait cabal build -j$JOBS --ghc-options="-j1 +RTS -M500M -RTS -Werror" # this builds all libraries and executables (including tests/benchmarks)
      time cabal test --ghc-options="-with-rtsopts=-N1"
      ;;
    cabaldist)
      # cabal check
      cabal sdist   # tests that a source-distribution can be generated

      # Check that the resulting source distribution can be built & installed.
      # If there are no other `.tar.gz` files in `dist`, this can be even simpler:
      # `cabal install --force-reinstalls dist/*-*.tar.gz`
      SRC_TGZ=$(cabal info . | awk '{print $2;exit}').tar.gz &&
      (cd dist && better_wait cabal install -j$JOBS --force-reinstalls "$SRC_TGZ" --ghc-options="-j1 +RTS -M500M -RTS")
      ;;
    canew)
      better_wait cabal new-build -j$JOBS --ghc-options="-j1 +RTS -M500M -RTS" --disable-tests --disable-benchmarks
      better_wait cabal new-build -j$JOBS --ghc-options="-j1 +RTS -M500M -RTS" --enable-tests --enable-benchmarks
      time cabal new-test -j1 --ghc-options="-j1 +RTS -M500M -RTS"
      ;;
  esac
  set +ex