# Copyright 2016-2020 the Tectonic Project
# Licensed under the MIT License.
#
# Azure Pipelines template for a standard build-and-test job getting
# dependencies through pkg-config. 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: primaryBuild
  type: boolean
  default: false
- name: installAllDeps
  type: boolean
  default: false
- name: defaultFeatures
  type: boolean
  default: true
- name: explicitFeatures
  type: string
  default: ''

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

- bash: |
    set -xeuo pipefail

    pkgs="
      libgraphite2-dev
      libfontconfig1-dev
      libicu-dev
      libssl-dev
      openssl
      zlib1g-dev"
    if [[ $INSTALL_ALL_DEPS == True ]] ; then
      pkgs="$pkgs libharfbuzz-dev"
    fi

    sudo apt-get update
    sudo env DEBIAN_FRONTEND=noninteractive apt-get install -y $pkgs
  displayName: "Install pkg-config dependencies (Ubuntu)"
  condition: and(succeeded(), eq(variables['Agent.OS'], 'Linux'))
  env:
    INSTALL_ALL_DEPS: ${{ parameters.installAllDeps }}  # maps to "True" or "False"

- bash: |
    set -xeuo pipefail

    pkgs="
      freetype
      graphite2
      icu4c
      libpng
      openssl"
    if [[ $INSTALL_ALL_DEPS == True ]] ; then
      pkgs="$pkgs harfbuzz"
    fi

    brew install $pkgs
    icupcdir="$(echo /usr/local/Cellar/icu4c/*/lib/pkgconfig)"
    export PKG_CONFIG_PATH="$icupcdir"
    set +x  # `set -x` messes up setvariable
    echo "##vso[task.setvariable variable=PKG_CONFIG_PATH;]$PKG_CONFIG_PATH"
  displayName: "Install pkg-config dependencies (macOS)"
  condition: and(succeeded(), eq(variables['Agent.OS'], 'Darwin'))
  env:
    INSTALL_ALL_DEPS: ${{ parameters.installAllDeps }}

- bash: |
    echo "##vso[task.prependpath]C:\msys64\usr\bin"
    echo "##vso[task.prependpath]C:\msys64\mingw64\bin"
  displayName: "Set MSYS64 paths (Windows)"
  condition: and(succeeded(), eq(variables['Agent.OS'], 'Windows_NT'))

- bash: |
    set -xeuo pipefail
    pacman -S --noconfirm \
      mingw-w64-x86_64-fontconfig \
      mingw-w64-x86_64-freetype \
      mingw-w64-x86_64-icu
    # Format file locking issue workaround:
    set +x  # `set -x` messes up setvariable
    echo "##vso[task.setvariable variable=RUST_TEST_THREADS;]1"
  displayName: "Install pkg-config dependencies (Windows)"
  condition: and(succeeded(), eq(variables['Agent.OS'], 'Windows_NT'))

# Note: setvariable + `set -x` adds spurious single quotes at ends of variable values
- bash: |
    echo "##vso[task.setvariable variable=TECTONIC_DEP_BACKEND;]pkg-config"
  displayName: Setup build variables

- template: azure-generic-build.yml
  parameters:
    canaryBuild: ${{ parameters.canaryBuild }}
    primaryBuild: ${{ parameters.primaryBuild }}
    defaultFeatures: ${{ parameters.defaultFeatures }}
    explicitFeatures: ${{ parameters.explicitFeatures }}
