# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
#
# http://go.microsoft.com/fwlink/?LinkID=615560

# Builds the library and test suite using the MinGW compiler.

schedules:
- cron: "0 0 * * *"
  displayName: 'Nightly build'
  branches:
    include:
    - main

trigger:
  branches:
    include:
    - main
  paths:
    exclude:
    - README.md
    - HISTORY.md
    - SECURITY.md
    - LICENSE
pr:
  branches:
    include:
    - main
  paths:
    exclude:
    - README.md
    - HISTORY.md
    - SECURITY.md
    - LICENSE
  drafts: false

resources:
  repositories:
  - repository: self
    type: git
    ref: refs/heads/main

name: $(Year:yyyy).$(Month).$(DayOfMonth)$(Rev:.r)

pool:
  vmImage: windows-2022

variables:
  GITHUB_PAT: $(GITHUBPUBLICTOKEN)
  URL_MINGW32: https://github.com/brechtsanders/winlibs_mingw/releases/download/12.2.0-14.0.6-10.0.0-ucrt-r2/winlibs-i686-posix-dwarf-gcc-12.2.0-llvm-14.0.6-mingw-w64ucrt-10.0.0-r2.zip
  HASH_MINGW32: 'fcd1e11b896190da01c83d5b5fb0d37b7c61585e53446c2dab0009debc3915e757213882c35e35396329338de6f0222ba012e23a5af86932db45186a225d1272'

jobs:
- job: MINGW32_BUILD
  displayName: 'Minimalist GNU for Windows (MinGW32)'
  steps:
  - checkout: self
    clean: true
    fetchTags: false
  - task: CmdLine@2
    displayName: Fetch Tests
    inputs:
      script: git clone --quiet --no-tags https://%GITHUB_PAT%@github.com/walbourn/directxmathtest.git Tests
      workingDirectory: $(Build.SourcesDirectory)
  - task: PowerShell@2
    displayName: Install MinGW32
    inputs:
      targetType: inline
      script: |
        $ProgressPreference = 'SilentlyContinue'
        Write-Host "Downloading winlibs..."
        Invoke-WebRequest -Uri "$(URL_MINGW32)" -OutFile "gw32.zip"
        Write-Host "Downloaded."
        $fileHash = Get-FileHash -Algorithm SHA512 gw32.zip | ForEach { $_.Hash} | Out-String
        $filehash = $fileHash.Trim()
        Write-Host "##[debug]SHA512: " $fileHash
        if ($fileHash -ne '$(HASH_MINGW32)') {
            Write-Error -Message "##[error]Computed hash does not match!" -ErrorAction Stop
        }
        Write-Host "Extracting winlibs..."
        Expand-Archive -LiteralPath 'gw32.zip'
        Write-Host "Extracted."
        Write-Host "Added to path: $env:BUILD_SOURCESDIRECTORY\gw32\mingw32\bin"
        Write-Host "##vso[task.prependpath]$env:BUILD_SOURCESDIRECTORY\gw32\mingw32\bin"

      workingDirectory: $(Build.SourcesDirectory)
  - task: CmdLine@2
    displayName: GCC version
    inputs:
      script: g++ --version
  - task: CMake@1
    displayName: CMake (MinGW32) Dbg
    inputs:
      cwd: Tests
      cmakeArgs: -B out -DCMAKE_BUILD_TYPE="Debug" -DDXMATH_ARCHITECTURE=x86 -DCMAKE_CXX_COMPILER="g++.exe" -G "MinGW Makefiles"
  - task: CMake@1
    displayName: CMake (MinGW32) Build Dbg
    inputs:
      cwd: Tests
      cmakeArgs: --build out
  - task: CMake@1
    displayName: CMake (MinGW32) Rel
    inputs:
      cwd: Tests
      cmakeArgs: -B out2 -DCMAKE_BUILD_TYPE="RelWithDebInfo" -DDXMATH_ARCHITECTURE=x86 -DCMAKE_CXX_COMPILER="g++.exe" -G "MinGW Makefiles"
  - task: CMake@1
    displayName: CMake (MinGW32) Build Rel
    inputs:
      cwd: Tests
      cmakeArgs: --build out2
  - task: CMake@1
    displayName: CMake (MinGW32) Dbg NI
    inputs:
      cwd: Tests
      cmakeArgs: -B out3 -DCMAKE_BUILD_TYPE="Debug" -DBUILD_NO_INTRINSICS=ON -DDXMATH_ARCHITECTURE=x86 -DCMAKE_CXX_COMPILER="g++.exe" -G "MinGW Makefiles"
  - task: CMake@1
    displayName: CMake (MinGW32) Build Dbg NI
    inputs:
      cwd: Tests
      cmakeArgs: --build out3

- job: MINGW64_BUILD
  displayName: 'Minimalist GNU for Windows (MinGW-W64) BUILD_TESTING=ON'
  steps:
  - checkout: self
    clean: true
    fetchTags: false
  - task: CmdLine@2
    displayName: Fetch Tests
    inputs:
      script: git clone --quiet --no-tags https://%GITHUB_PAT%@github.com/walbourn/directxmathtest.git Tests
      workingDirectory: $(Build.SourcesDirectory)
  - task: CmdLine@2
    displayName: GCC version
    inputs:
      script: g++ --version
  - task: CMake@1
    displayName: CMake (MinGW-W64) Dbg
    inputs:
      cwd: Tests
      cmakeArgs: -B out -DCMAKE_BUILD_TYPE="Debug" -DDXMATH_ARCHITECTURE=x64 -DCMAKE_CXX_COMPILER="g++.exe" -G "MinGW Makefiles"
  - task: CMake@1
    displayName: CMake (MinGW-W64) Build Dbg
    inputs:
      cwd: Tests
      cmakeArgs: --build out
  - task: CMake@1
    displayName: CMake (MinGW-W64) Rel
    inputs:
      cwd: Tests
      cmakeArgs: -B out2 -DCMAKE_BUILD_TYPE="RelWithDebInfo" -DDXMATH_ARCHITECTURE=x64 -DCMAKE_CXX_COMPILER="g++.exe" -G "MinGW Makefiles"
  - task: CMake@1
    displayName: CMake (MinGW-W64) Build Rel
    inputs:
      cwd: Tests
      cmakeArgs: --build out2
  - task: CMake@1
    displayName: CMake (MinGW-W64) Dbg NI
    inputs:
      cwd: Tests
      cmakeArgs: -B out3 -DCMAKE_BUILD_TYPE="Debug" -DBUILD_NO_INTRINSICS=ON -DDXMATH_ARCHITECTURE=x64 -DCMAKE_CXX_COMPILER="g++.exe" -G "MinGW Makefiles"
  - task: CMake@1
    displayName: CMake (MinGW-W64) Build Dbg NI
    inputs:
      cwd: Tests
      cmakeArgs: --build out3
