dist: trusty
sudo: false

language: c

env:
  global:
    - PKGNAME=idris

matrix:
  include:
    - env: CABALVER="2.2" GHCVER="8.4.1" STACKVER="7.14" STYLISH=YES
      addons: {apt: {packages: [cabal-install-2.2,ghc-8.4.1,hscolour], sources: [hvr-ghc]}}
    - env: CABALVER="2.2" GHCVER="7.10.3" TESTS="test_c"
      compiler: ": #GHC 7.10.3"
      addons: {apt: {packages: [cabal-install-2.2,ghc-7.10.3,cppcheck,hscolour], sources: [hvr-ghc]}}
# Disabled because it's always timing out
#    - env: CABALVER="1.24" GHCVER="8.0.1" TESTS="test_c"
#      compiler: ": #GHC 8.0.1"
#      addons: {apt: {packages: [cabal-install-1.24,ghc-8.0.1,cppcheck,hscolour], sources: [hvr-ghc]}}
    - env: CABALVER="2.0" GHCVER="8.2.2" TESTS="lib_doc doc"
      compiler: ": #GHC 8.2.2"
      addons: {apt: {packages: [cabal-install-2.0,ghc-8.2.2,cppcheck,hscolour], sources: [hvr-ghc]}}
    - env: CABALVER="2.0" GHCVER="8.2.2" TESTS="test_js"
      compiler: ": #GHC 8.2.2"
      addons: {apt: {packages: [cabal-install-2.0,ghc-8.2.2,cppcheck,hscolour], sources: [hvr-ghc]}}
    - env: CABALVER="2.0" GHCVER="8.2.2" TESTS="test_c"
      compiler: ": #GHC 8.2.2"
      addons: {apt: {packages: [cabal-install-2.0,ghc-8.2.2,cppcheck,hscolour], sources: [hvr-ghc]}}
#TODO: The idris built with those fails due to libffi dynamic linking version mismatch.
#      Caused by the GHC backport to Trusty?
#    - env: CABALVER="2.2" GHCVER="8.4.1" TESTS="lib_doc doc"
#      compiler: ": #GHC 8.4.1"
#      addons: {apt: {packages: [cabal-install-2.2,ghc-8.4.1,cppcheck,hscolour], sources: [hvr-ghc]}}
#    - env: CABALVER="2.2" GHCVER="8.4.1" TESTS="test_js"
#      compiler: ": #GHC 8.4.1"
#      addons: {apt: {packages: [cabal-install-2.2,ghc-8.4.1,cppcheck,hscolour], sources: [hvr-ghc]}}
#    - env: CABALVER="2.2" GHCVER="8.4.1" TESTS="test_c"
#      compiler: ": #GHC 8.4.1"
#      addons: {apt: {packages: [cabal-install-2.2,ghc-8.4.1,cppcheck,hscolour], sources: [hvr-ghc]}}
  fast-finish: true

cache:
  directories:
    - $HOME/.cabsnap
    - $HOME/.cabal/packages
    - $HOME/.stack

before_cache:
  - rm -fv $HOME/.cabal/packages/hackage.haskell.org/build-reports.log
  - rm -fv $HOME/.cabal/packages/hackage.haskell.org/00-index.tar

before_install:
  - nvm install 6
  - nvm use 6
  - unset CC
  - if [[ $TRAVIS_OS_NAME == 'linux' ]];
    then
        export PATH=/opt/ghc/$GHCVER/bin:/opt/cabal/$CABALVER/bin:$HOME/.cabal/bin:$PATH;
        export SED=sed;
        export ZCAT=zcat;
    fi
  - env
  - if [[ $TRAVIS_OS_NAME == 'osx' ]];
    then
        brew outdated pkgconfig || brew install pkgconfig;
        brew install ghc cabal-install libffi cppcheck gnu-sed;
        export PATH=$HOME/.cabal/bin:$PATH;
        export SED=gsed;
        export ZCAT=gzcat;
        export PKG_CONFIG_PATH=/usr/local/opt/libffi/lib/pkgconfig:$PKG_CONFIG_PATH;
    fi
  - if [ -n "$STYLISH" ]; then ./stylize.sh; exit $?; fi

install:
  - which cabal
  - which ghc
  - 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
  # Run build with 2 parallel jobs
  # The container environment reports 16 cores,
  # causing cabal's default configuration (jobs: $ncpus)
  # to run into the GHC #9221 bug which can result in longer build-times.
  - $SED -i -r 's/(^jobs:).*/\1 2/' $HOME/.cabal/config
  - cabal install -f FFI --only-dependencies --enable-tests --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 installplan.txt $HOME/.cabsnap/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 -f FFI --only-dependencies --enable-tests;
    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

before_script:
  - ORIGINAL_DIR=$(pwd)
  - cabal sdist
  - cd ..
  - tar -xf ${ORIGINAL_DIR}/dist/${PKGNAME}*.tar.gz
  - cd ${PKGNAME}*
script:
###
  - echo 'Configure...' && echo -en 'travis_fold:start:script.configure\\r'
  - cabal configure -f FFI -f CI --enable-tests
  - echo -en 'travis_fold:end:script.configure\\r'
###
  - echo 'Build...' && echo -en 'travis_fold:start:script.build\\r'
  - cabal build
  - echo -en 'travis_fold:end:script.build\\r'
###
  - echo 'Copy...' && echo -en 'travis_fold:start:script.copy\\r'
  - cabal copy
  - echo -en 'travis_fold:end:script.copy\\r'
###
  - echo 'Register...' && echo -en 'travis_fold:start:script.register\\r'
  - cabal register
  - echo -en 'travis_fold:end:script.register\\r'
###
  - echo 'Cppcheck...' && echo -en 'travis_fold:start:script.cppcheck\\r'
  - if [[ "$TESTS" == "test_c" ]]; then
      cppcheck -i 'mini-gmp.c' rts;
    fi
  - echo -en 'travis_fold:end:script.cppcheck\\r'
###
  - echo 'Tests...' && echo -en 'travis_fold:start:script.tests\\r'
  - for test in $TESTS; do
      echo "make TEST-JOBS=2 $test";
      travis_wait make TEST-JOBS=2 $test;
    done
  - echo -en 'travis_fold:end:script.tests\\r'
###
  - echo 'Benchmarks...' && echo -en 'travis_fold:start:script.benchmarks\\r'
  - if [[ "$TESTS" == "test_c" ]]; then
      cd benchmarks && ./build.pl && ./run.pl && cd ..;
    fi
  - echo -en 'travis_fold:end:script.benchmarks\\r'
###
# EOF
