language: android

env:
  global:
    - BORINGSSL_HOME="$HOME/boringssl"
    - CC=clang
    - CXX=clang++
    - CXXFLAGS="-std=c++11"
    - GOOGLE_JAVA_FORMAT_VERSION=1.1

cache:
  directories:
    - $HOME/.gradle/caches/
    - $HOME/.gradle/wrapper/dists/

matrix:
  include:
    ###
    ### Linux build is the only platform that builds Android here.
    ###
    - os: linux
      jdk: openjdk8
      sudo: false

      env:
        - ANDROID_TOOLS_URL="https://dl.google.com/android/repository/tools_r25.2.3-linux.zip"
        - ANDROID_HOME="$HOME/android-sdk-linux"
        - ANDROID_NDK_HOME="$ANDROID_HOME/ndk-bundle"
        - JAVA6_HOME=/usr/lib/jvm/java-6-openjdk-amd64

      before_install:
        - curl -L $ANDROID_TOOLS_URL -o $HOME/tools.zip
        - unzip -q $HOME/tools.zip -d $ANDROID_HOME
        - mkdir $ANDROID_HOME/licenses
        - echo -ne "\n8933bad161af4178b1185d1a37fbf41ea5269c55\n\nd56f5187479451eabf01fb78af6dfcb131a6481e" >> $ANDROID_HOME/licenses/android-sdk-license
        - echo -ne "\n84831b9409646a918e30573bab4c9c91346d8abd\n" >> $ANDROID_HOME/licenses/android-sdk-preview-license
        # sdkmanager 26.1.1 produces an enormous amount of progress info
        # Append tr '\r' '\n' | uniq to all the commands to suppress it
        - $ANDROID_HOME/tools/bin/sdkmanager tools | tr '\r' '\n' | uniq
        - $ANDROID_HOME/tools/bin/sdkmanager platform-tools | tr '\r' '\n' | uniq
        - $ANDROID_HOME/tools/bin/sdkmanager 'build-tools;25.0.0' | tr '\r' '\n' | uniq
        - $ANDROID_HOME/tools/bin/sdkmanager 'platforms;android-25' | tr '\r' '\n' | uniq
        - $ANDROID_HOME/tools/bin/sdkmanager 'extras;android;m2repository' | tr '\r' '\n' | uniq
        - $ANDROID_HOME/tools/bin/sdkmanager --channel=1 ndk-bundle | tr '\r' '\n' | uniq
        - $ANDROID_HOME/tools/bin/sdkmanager 'cmake;3.6.4111459' | tr '\r' '\n' | uniq

      addons:
        apt:
          sources:
            - kalakris-cmake
            - llvm-toolchain-precise-3.9  # for clang-format-3.9
            - ubuntu-toolchain-r-test
          packages:
            - clang-3.9
            - clang-format-3.9  # for style checks
            - cmake
            - g++-multilib
            - gcc-multilib
            - libc6-dev-i386
            - libc6-dev:i386
            - ninja-build
            - openjdk-6-jre # for running tests with Java 6

    ###
    ### MacOS build only does x86-64.
    ###
    - os: osx
      osx_image: xcode8
      before_install:
        - brew update
        - brew install ninja
        - export JAVA_HOME=`/usr/libexec/java_home -v 1.8`

before_cache:
  - find $HOME/.gradle -name "*.lock" -exec rm {} \;
  - rm -rf $HOME/.gradle/caches/[1-9]*

before_script:
  # Get Google Java Format
  - curl -L https://github.com/google/google-java-format/releases/download/google-java-format-1.1/google-java-format-${GOOGLE_JAVA_FORMAT_VERSION}-all-deps.jar -o $HOME/gjf.jar

  # get BoringSSL
  - mkdir $BORINGSSL_HOME
  - git clone --depth 1 https://boringssl.googlesource.com/boringssl $BORINGSSL_HOME

  # Build BoringSSL for 64-bit
  - mkdir $BORINGSSL_HOME/build64 && pushd $BORINGSSL_HOME/build64
  - cmake -DCMAKE_POSITION_INDEPENDENT_CODE=TRUE -DCMAKE_BUILD_TYPE=Release -DCMAKE_ASM_FLAGS=-Wa,--noexecstack -GNinja ..
  - ninja
  - popd

  # Get git-clang-format
  - if [ ! -d "$HOME/bin" ]; then mkdir $HOME/bin; fi
  - curl -L https://llvm.org/svn/llvm-project/cfe/trunk/tools/clang-format/git-clang-format -o $HOME/bin/git-clang-format
  - chmod 0755 $HOME/bin/git-clang-format
  - export PATH="$HOME/bin:$PATH"

  # We need this to find the merge-base
  - if [[ "$TRAVIS_OS_NAME" == "linux" && "$TRAVIS_PULL_REQUEST" != "false" ]];
    then
        git fetch origin +refs/heads/${TRAVIS_BRANCH}:refs/remotes/origin/${TRAVIS_BRANCH};
    fi

script:
  # MacOS (BSD) xargs is missing some nice features that make this easy, so skip it.
  - if [[ "$TRAVIS_OS_NAME" == "linux" && "$TRAVIS_PULL_REQUEST" != "false" ]];
    then
        git rev-list $(git merge-base HEAD origin/master)..HEAD | xargs -i git clang-format --binary=$(which clang-format-3.9) --style=file --diff {}^ {} | ( git apply; true ) && git diff --exit-code || { git reset --hard; false; }
    fi
  - if [[ "$TRAVIS_OS_NAME" == "linux" && "$TRAVIS_PULL_REQUEST" != "false" ]];
    then
        git rev-list $(git merge-base HEAD origin/master)..HEAD | xargs -i git diff-tree --no-commit-id --name-only -r {} | grep -E '\.java$' | xargs -r git ls-files | xargs -r java -jar $HOME/gjf.jar -a -i --fix-imports-only && git diff --exit-code || { git reset --hard; false; }
    fi

  - ./gradlew build -PcheckErrorQueue

  # Also test with Java 6 on linux
  - if [[ "$TRAVIS_OS_NAME" == "linux" && "$TRAVIS_PULL_REQUEST" != "false" ]];
      then
        ${JAVA6_HOME}/bin/java -version;
      fi
  - if [[ "$TRAVIS_OS_NAME" == "linux" && "$TRAVIS_PULL_REQUEST" != "false" ]];
    then
      ./gradlew check -DjavaExecutable64=${JAVA6_HOME}/bin/java -PcheckErrorQueue;
    fi

after_script:
  - "[ -f android/build/outputs/lint-results-debug.xml ] && cat android/build/outputs/lint-results-debug.xml"
