# Copyright 2016-2020 the Tectonic Project
# Licensed under the MIT License.
#
# Azure Pipelines template for a standard build-and-test job getting
# dependencies through vcpkg. Besides the parameters, the following variables
# are expected:
#
# - TARGET: the build target triple (e.g. "x86_86-unknown-linux-gnu")
# - TOOLCHAIN: the rust toolchain type (e.g., "stable",
#   "beta-x86_64-pc-windows-msvc")

parameters:
- name: canaryBuild
  type: boolean
  default: false
- name: windowsVcpkgWorkaround
  type: boolean
  default: false

steps:
- template: azure-generic-build-setup.yml

- bash: |
    set -xeuo pipefail
    brew install pkg-config
  displayName: "Install vcpkg dependencies (macOS)"
  condition: and(succeeded(), eq(variables['Agent.OS'], 'Darwin'))

- ${{ if parameters.windowsVcpkgWorkaround }}:
  - download: current

  - bash: |
      set -xeuo pipefail
      BASH_WORKSPACE="$(Pipeline.Workspace)"

      # work around https://github.com/microsoft/azure-pipelines-tasks/issues/10653
      # (in an `if` statement for future-proofiness)
      if [[ $AGENT_OS == Windows_NT ]] ; then
        BASH_WORKSPACE=$(echo "$BASH_WORKSPACE" | sed -e 's|\\|\/|g' -e 's|^\([A-Za-z]\)\:/\(.*\)|/\L\1\E/\2|')
      fi

      mkdir -p target
      mv $BASH_WORKSPACE/vcpkg-deps-windows target/vcpkg
    displayName: Recover vcpkg deps from artifacts

- ${{ if not(parameters.windowsVcpkgWorkaround) }}:
  - bash: |
      set -xeuo pipefail
      cargo install cargo-vcpkg
    displayName: Install cargo-vcpkg

  - bash: |
      set -xeuo pipefail
      cargo vcpkg -v build
      ls target/vcpkg
      echo target/vcpkg/installed/*
      ls target/vcpkg/installed/*/lib
    displayName: Build vcpkg deps

# Note: setvariable + `set -x` adds spurious single quotes at ends of variable values
- bash: |
    echo "##vso[task.setvariable variable=VCPKG_ROOT;]$(pwd)/target/vcpkg"
    echo "##vso[task.setvariable variable=TECTONIC_DEP_BACKEND;]vcpkg"
  displayName: Setup build variables

# Without RUST_TEST_THREAD=1, on Windows the doctests fail with a
# PermissionDenied issue that seems to be due to creating multiple tempfiles in
# the same directory (tests/) at once.
- bash: |
    echo "##vso[task.setvariable variable=RUSTFLAGS;]-Ctarget-feature=+crt-static"
    echo "##vso[task.setvariable variable=RUST_TEST_THREADS;]1"
  displayName: Setup build variables (Windows)
  condition: and(succeeded(), eq(variables['Agent.OS'], 'Windows_NT'))

- template: azure-generic-build.yml
  parameters:
    canaryBuild: ${{ parameters.canaryBuild }}
    explicitFeatures: "external-harfbuzz"
