language: "perl"
os:
  - linux
  - osx
perl:
  - "5.10"
before_install:
  |-
    echo "Begin ‘before_install’ section of .travis.yml"
    # If we aren't on MoarVM/MoarVM or it's a pull request we can't decode the deploy key
    if [ ! "$TRAVIS_REPO_SLUG" = 'MoarVM/MoarVM' ] || [ ! "$TRAVIS_PULL_REQUEST" = "false" ]; then
      export COVERAGE=''; export MOCK_COVERAGE=1;
    fi
    if [ "$COVERAGE" ]; then
      # Put github's into known_hosts /* may not need this */
      printf "github.com,192.30.255.112 ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==\n" >> ~/.ssh/known_hosts
      # Decrypt deploy_key using Travis *magic* variables
      eval 'openssl aes-256-cbc -K $encrypted_b77ce3a1cc5c_key -iv $encrypted_b77ce3a1cc5c_iv -in ./tools/gh-pages-deploy_key.enc -out deploy_key -d'
      # Now that it's decrypted, clear the keys so it can't leak
      export encrypted_b77ce3a1cc5c_key=""
      export encrypted_b77ce3a1cc5c_iv=""
      chmod 600 deploy_key # Change perms or ssh-agent will get mad at us
      eval $(ssh-agent -s) # Run the code that ssh-agent -s gives back to setup ENV
      # Decrypted key has no password, but it will still ask for one. Use expect program to interact with it non-interactively
      expect -c 'spawn ssh-add ./deploy_key; expect "enter passphrase for"; send "\n"; expect "Identity added:"; interact'
    fi
install:
  - echo "Begin ‘install’ section of .travis.yml"
addons:
  apt:
    sources:
      - sourceline: 'ppa:ubuntu-toolchain-r/test'
      - sourceline: 'deb http://apt.llvm.org/precise/ llvm-toolchain-precise main'
    packages:
      - clang-5.0
      - llvm-5.0
      - expect
      - libffi-dev
      - gdb

before_script:
  - echo "Begin ‘before_script’ section of .travis.yml"
  - uname -a
  - gcc --version
  - clang --version
  # Fun function that finds files in /usr/bin naming as foo-4.2 for example
  # and returns the highest version. Travis CI installs newer versions under
  # their own verison numbers.
  - find_latest_version () { ls /usr/bin | grep -Ee "^$1(\-[0-9.]+)?$" | sort -r | head -n 1; }
  # MacOS's readlink doesn't support -f, so use our own solution
  - resolve_folder      () { O="$(pwd -P)"; D="$(dirname $1)"; T="$(basename $1)"; cd -- "$D" && cd -- "$T" && pwd -P; cd -- "$O" > /dev/null; }
  - find_latest_version llvm-profdata
  - find_latest_version llvm-cov
  # Find the latest clang that is on the system
  - CLANG_LATEST=$(find_latest_version clang)
  - if [ ! "$CLANG_LATEST" = 'clang' ]; then eval "$CLANG_LATEST --version"; fi
  - if [ "$USE_CLANG_LATEST" ]; then echo "Using $CLANG_LATEST due to build setting"; CC="$CLANG_LATEST"; fi
  # Generic debug and optimization settings that are used for all builds except
  # the coverage builds
  - MVM_debug="--debug"; MVM_optimize="--optimize"
  - if [ "$COVERAGE" ] || [ "$MOCK_COVERAGE" ]; then MVM_debug="--debug=3"; MVM_optimize="--optimize=0"; fi
  - if [ "$TRAVIS_OS_NAME" == "linux" ] ; then sudo apt-get update -qq || sudo apt-get update -qq ; fi
  - git fetch --unshallow
  - git clone --depth 1 git://github.com/perl6/nqp

script:
  - echo "Begin ‘script’ section of .travis.yml"
  # Enable coredumps
  - if [ "$TRAVIS_OS_NAME" == "linux" ]; then ulimit -c unlimited; fi
  - export MOAR_PREFIX="/tmp/moar"; export MOAR_FOLDER="$TRAVIS_BUILD_DIR"
  - export NQP_FOLDER="$(resolve_folder ./nqp)"
  # Set location for core dumps
  - if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo bash -c "echo \"${MOAR_PREFIX}/core.%p.%E\" > /proc/sys/kernel/core_pattern"; sudo cat /proc/sys/kernel/core_pattern; fi
  - echo "perl Configure.pl --prefix=$MOAR_PREFIX $MVM_OPTIONS --cc=\"$CC\" $MVM_debug $MVM_optimize;"
  - perl Configure.pl --prefix=$MOAR_PREFIX $MVM_OPTIONS --cc="$CC" $MVM_debug $MVM_optimize;
  - make -j2 install;
  - cd -- "$NQP_FOLDER";
  - echo "perl Configure.pl --prefix=$MOAR_PREFIX --backends=moar;"
  - perl Configure.pl --prefix=$MOAR_PREFIX --backends=moar;
  - make
  - make test;
  - cd -- "$MOAR_FOLDER"
  - if [ "$COVERAGE" ]; then git clone --depth 1 'https://github.com/samcv/MoarVM-cover.git' && cp -v MoarVM-cover/html-cover.sh . && cp -v MoarVM-cover/nqp-profile ./nqp/ && cp -v MoarVM-cover/merge-profraw.sh ./nqp/ && ./html-cover.sh 2; fi

after_success:
  - echo "Begin ‘after_success’ section of .travis.yml"
  - cd "$TRAVIS_BUILD_DIR"
  - if [ "$COVERAGE" ]; then ./tools/update-gh-pages.sh; fi

after_failure:
# On failure, dump all ENV vars, in case we need to look at them (doesn't print if our secrets are defined)
  - if [ ! "$encrypted_b77ce3a1cc5c_key" ] && [ ! "$encrypted_b77ce3a1cc5c_iv" ]; then printenv; fi
  # If we have any coredumps, print out the traces with gdb
  - for i in $(find "${MOAR_PREFIX}" -maxdepth 1 -name 'core*' -print); do gdb "${MOAR_PREFIX}/bin/moar" "${i}" -ex "thread apply all bt" -ex "set pagination 0" -batch; done;

branches:
   only:
    - master
    - coverage
    - /smoke-me/

notifications:
  irc:
    branches:
      except:
        - gh-pages
    channels:
      - "irc.freenode.net#moarvm"
    on_success: change
    on_failure: always

    template:
      - "MoarVM build %{result}. %{author} '%{commit_message}'"
      - "%{build_url} %{compare_url}"

env:
  matrix:
    - MVM_OPTIONS="--no-jit"               CC='gcc'
    - MVM_OPTIONS=""                       CC='gcc'
    - MVM_OPTIONS="--no-jit"               CC='clang'
    - MVM_OPTIONS=""                       CC='clang'
    - MVM_OPTIONS="--has-libffi --no-jit"  CC='gcc'
    - MVM_OPTIONS="--has-libffi"           CC='gcc'
    - MVM_OPTIONS="--has-libffi --no-jit"  CC='clang'
    - MVM_OPTIONS="--has-libffi"           CC='clang'
    - MVM_OPTIONS="--compiler=clang --coverage --optimize=0 --debug=3" COVERAGE=1 USE_CLANG_LATEST=1 CC='clang'

matrix:
  exclude:
  - os: osx
    env: MVM_OPTIONS="--has-libffi --no-jit"  CC='gcc'
  - os: osx
    env: MVM_OPTIONS="--has-libffi"           CC='gcc'
#  - os: osx
#    env: MVM_OPTIONS="--no-jit"               CC='gcc'
#  - os: osx
#    env: MVM_OPTIONS=""                       CC='gcc'
  - os: osx
    env: MVM_OPTIONS="--has-libffi --no-jit"  CC='clang'
  - os: osx
    env: MVM_OPTIONS="--has-libffi"           CC='clang'
  - os: osx
    env: MVM_OPTIONS="--compiler=clang --coverage --optimize=0 --debug=3" COVERAGE=1 USE_CLANG_LATEST=1 CC='clang'
  allow_failures:
  - os: linux
    env: MVM_OPTIONS="--compiler=clang --coverage --optimize=0 --debug=3" COVERAGE=1 USE_CLANG_LATEST=1 CC='clang'
