# This template installs sccache (Shared Compilation Cache)
# More info: https://github.com/mozilla/sccache

# Template originally from wasm-bindgen
# https://github.com/rustwasm/wasm-bindgen/blob/master/ci/azure-install-sccache.yml
steps:
  - bash: |
      set -ex
      curl -L https://github.com/mozilla/sccache/releases/download/0.2.10/sccache-0.2.10-x86_64-unknown-linux-musl.tar.gz | tar xzf -
      sccache=`pwd`/sccache-0.2.10-x86_64-unknown-linux-musl/sccache
      echo "##vso[task.setvariable variable=RUSTC_WRAPPER;]$sccache"
    displayName: Install sccache - Linux X64
    condition: and(eq(variables['Agent.OS'], 'Linux'), eq(variables['Agent.OSArchitecture'], 'X64'))
  - bash: |
      command -v sccache > /dev/null || cargo install sccache
      echo "##vso[task.setvariable variable=RUSTC_WRAPPER;]sccache"
    displayName: Install sccache - Linux ARM
    condition: and(eq(variables['Agent.OS'], 'Linux'), eq(variables['Agent.OSArchitecture'], 'ARM64'))
  - bash: |
      set -ex
      brew install openssl@1.1
      curl -L https://github.com/mozilla/sccache/releases/download/0.2.10/sccache-0.2.10-x86_64-apple-darwin.tar.gz | tar xzf -
      sccache=`pwd`/sccache-0.2.10-x86_64-apple-darwin/sccache
      echo "##vso[task.setvariable variable=RUSTC_WRAPPER;]$sccache"
    displayName: Install sccache - Darwin
    condition: eq( variables['Agent.OS'], 'Darwin' )
  - powershell: |
      Invoke-WebRequest https://github.com/mozilla/sccache/releases/download/0.2.10/sccache-0.2.10-x86_64-pc-windows-msvc.tar.gz -OutFile sccache.tar.gz
      tar xzf sccache.tar.gz
      Write-Host "##vso[task.setvariable variable=RUSTC_WRAPPER;]$pwd/sccache-0.2.10-x86_64-pc-windows-msvc/sccache.exe"
    displayName: Install sccache - Windows
    condition: eq( variables['Agent.OS'], 'Windows_NT' )
  - bash: |
      set -ex
      env
      SCCACHE_ERROR_LOG=`pwd`/sccache.log RUST_LOG=debug $RUSTC_WRAPPER --start-server
      $RUSTC_WRAPPER -s
      cat sccache.log
    displayName: "start sccache"
    condition: not(eq( variables['Agent.OS'], 'Windows_NT' ))
    env:
      SCCACHE_AZURE_CONNECTION_STRING: $(SCCACHE_AZURE_CONNECTION_STRING)
      SCCACHE_AZURE_BLOB_CONTAINER: $(SCCACHE_AZURE_BLOB_CONTAINER)

  # Only use Azure pipelines cache in Windows
  - bash: |
      set -ex
      env
      mkdir -p $SCCACHE_DIR
      SCCACHE_ERROR_LOG=`pwd`/sccache.log RUST_LOG=debug $RUSTC_WRAPPER --start-server
      $RUSTC_WRAPPER -s
      cat sccache.log
    displayName: "start sccache"
    condition: eq( variables['Agent.OS'], 'Windows_NT' )
    env:
      SCCACHE_DIR: $(Pipeline.Workspace)/.sccache
  - task: CacheBeta@0
    inputs:
      key: sccache | $(Agent.OS) | Cargo.lock
      path: $(Pipeline.Workspace)/.sccache
    displayName: Cache Cargo Target
    condition: eq( variables['Agent.OS'], 'Windows_NT' )
