# This template installs LLVM (if doesn't exist in the systems)

steps:
  - bash: |
      set -ex
      if [ -x "`command -v llvm-config`" ]; then
        echo `command -v llvm-config` `llvm-config --version` installed
      else
        curl -O https://releases.llvm.org/8.0.0/clang+llvm-8.0.0-x86_64-apple-darwin.tar.xz
        tar xf clang+llvm-8.0.0-x86_64-apple-darwin.tar.xz
        export LLVM_PATH="`pwd`/clang+llvm-8.0.0-x86_64-apple-darwin/"
        echo "##vso[task.prependpath]$LLVM_PATH/bin"
        echo "##vso[task.setvariable variable=LLVM_SYS_80_PREFIX]$LLVM_PATH"
      fi
    displayName: "Install LLVM (macOS)"
    condition: eq(variables['Agent.OS'], 'Darwin')

  - bash: |
      set -ex
      if [ -x "`command -v llvm-config`" ]; then
        echo `command -v llvm-config` `llvm-config --version` installed
      else
        curl -O https://releases.llvm.org/8.0.0/clang+llvm-8.0.0-x86_64-linux-gnu-ubuntu-16.04.tar.xz
        tar xf clang+llvm-8.0.0-x86_64-linux-gnu-ubuntu-16.04.tar.xz
        export LLVM_PATH="`pwd`/clang+llvm-8.0.0-x86_64-linux-gnu-ubuntu-16.04/"
        echo "##vso[task.prependpath]$LLVM_PATH/bin"
        echo "##vso[task.setvariable variable=LLVM_SYS_80_PREFIX]$LLVM_PATH"
      fi
    displayName: "Install LLVM (Linux X64)"
    condition: and(eq(variables['Agent.OS'], 'Linux'), eq(variables['Agent.OSArchitecture'], 'X64'))

  - bash: |
      set -ex
      if [ -x "`command -v llvm-config`" ]; then
        echo `command -v llvm-config` `llvm-config --version` installed
      else
        curl -O https://releases.llvm.org/8.0.0/clang+llvm-8.0.0-aarch64-linux-gnu.tar.xz
        tar xf clang+llvm-8.0.0-aarch64-linux-gnu.tar.xz
        export LLVM_PATH="`pwd`/clang+llvm-8.0.0-aarch64-linux-gnu/"
        echo "##vso[task.prependpath]$LLVM_PATH/bin"
        echo "##vso[task.setvariable variable=LLVM_SYS_80_PREFIX]$LLVM_PATH"
      fi
    displayName: "Install LLVM (Linux ARM)"
    condition: and(eq(variables['Agent.OS'], 'Linux'), eq(variables['Agent.OSArchitecture'], 'ARM64'))

  - bash: |
      set -ex
      mkdir Win64_Release
      cd Win64_Release
      curl -OL https://github.com/wasmerio/llvm-build/releases/download/8.x/Win64_Release.zip
      7z x Win64_Release.zip
      llvm=`pwd`
      echo "##vso[task.prependpath]$llvm/bin"
      echo "##vso[task.setvariable variable=LLVM_SYS_80_PREFIX;]$llvm"
      cd ..
    displayName: "Install LLVM (Windows)"
    condition: eq(variables['Agent.OS'], 'Windows_NT')

  # Just to make sure the paths and vars are set properly
  - powershell: |
      Write-Host "##vso[task.prependpath]$pwd/Win64_Release/bin"
      Write-Host "##vso[task.setvariable variable=LLVM_SYS_80_PREFIX;]$pwd/Win64_Release/"
    displayName: Install LLVM (Windows)
    condition: eq(variables['Agent.OS'], 'Windows_NT')

  - bash: |
      set -ex
      env
      llvm-config --version
    displayName: LLVM version
