# This will download kythe version ${kythe_version} (must be higher than
# v0.0.39 and build a ${hash}.kzip file for Kythe to enable cross referencing.
#
# To manually trigger a build on GCB, run:
# gcloud builds submit --config cloudbuild-kythe.yaml --substitutions \
#   _KYTHE_VERSION=[kythe_version] ..
#
# To trigger a build automatically, follow the instructions below and add a trigger:
# https://cloud.google.com/cloud-build/docs/running-builds/automate-builds
steps:
# Download Kythe
- name: 'gcr.io/${PROJECT_ID}/builder:latest'
  entrypoint: /bin/bash
  args:
  - -c
  - |
    wget -q \
      https://github.com/kythe/kythe/releases/download/${_KYTHE_VERSION}/kythe-${_KYTHE_VERSION}.tar.gz
    tar xvf kythe-${_KYTHE_VERSION}.tar.gz
    rm kythe-${_KYTHE_VERSION}.tar.gz
    mv kythe-${_KYTHE_VERSION} kythe
# Build Nomulus with the Kythe wrapper
- name: 'gcr.io/${PROJECT_ID}/builder:latest'
  entrypoint: /bin/bash
  args:
  - -c
  - |
    export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which javac))))
    mv $${JAVA_HOME}/bin/javac $${JAVA_HOME}/bin/javac.real
    cp ./kythe/extractors/javac-wrapper.sh $${JAVA_HOME}/bin/javac
    export JAVAC_EXTRACTOR_JAR="$${PWD}/kythe/extractors/javac_extractor.jar"
    export KYTHE_VNAMES="$${PWD}/vnames.json"
    export KYTHE_ROOT_DIRECTORY="$${PWD}"
    export KYTHE_OUTPUT_DIRECTORY="$${PWD}/kythe_output"
    mkdir -p $${KYTHE_OUTPUT_DIRECTORY}
    mkdir -p $${KYTHE_OUTPUT_DIRECTORY}/merged
    ./gradlew clean testClasses -x compileProdJS \
      -Dno_werror=true -PenableCrossReferencing=true
# Merge kzip files
- name: 'gcr.io/${PROJECT_ID}/builder:latest'
  entrypoint: /bin/bash
  args:
  - -c
  - |
    export KYTHE_OUTPUT_DIRECTORY="$${PWD}/kythe_output"
    ./kythe/tools/kzip merge \
      --output $${KYTHE_OUTPUT_DIRECTORY}/merged/${COMMIT_SHA}.kzip \
      $${KYTHE_OUTPUT_DIRECTORY}/*.kzip

artifacts:
  objects:
    location: 'gs://${PROJECT_ID}-codesearch'
    paths:
    - 'kythe_output/merged/${COMMIT_SHA}.kzip'

timeout: 3600s
options:
  machineType: 'E2_HIGHCPU_32'
