resources:
- repo: self

stages:
- stage: Build
  jobs:
  - job: Windows
    timeoutInMinutes: 90

    pool:
      vmImage: windows-2022

    variables:
      HLSL_SRC_DIR: '$(Build.SourcesDirectory)'
      HLSL_BLD_DIR: '$(Agent.BuildDirectory)'
      platform: x64

    strategy:
      matrix:
        VS2022_Release:
         configuration: Release
        VS2022_Debug:
         configuration: Debug

    steps:
    - checkout: self
      clean: true
      submodules: true
    - script: |
        call utils\hct\hctstart.cmd %HLSL_SRC_DIR% %HLSL_BLD_DIR%
        call utils\hct\hctbuild.cmd -$(platform) -$(configuration) -show-cmake-log -spirvtest -enable-lit
      displayName: 'Building'
    - script: |
        call utils\hct\hctstart.cmd %HLSL_SRC_DIR% %HLSL_BLD_DIR%
        call utils\hct\hcttest.cmd -$(configuration) noexec -enable-lit
      displayName: 'DXIL Tests'
    - script: |
        call utils\hct\hctstart.cmd %HLSL_SRC_DIR% %HLSL_BLD_DIR%
        call utils\hct\hcttest.cmd -$(configuration) spirv_only -enable-lit
      displayName: 'SPIRV Tests'

  - job: Nix
    timeoutInMinutes: 90

    variables:
      macOS: macOS-latest
      linux: Ubuntu-latest

    strategy:
      matrix:
        Linux_Clang_Release:
          image: ${{ variables.linux }}
          configuration: Release
          CC: clang
          CXX: clang++
          CXX_FLAGS: -Werror
          OS: Linux
        Linux_Clang_Debug:
          image: ${{ variables.linux }}
          configuration: Debug
          CC: clang
          CXX: clang++
          CXX_FLAGS: -Werror
        Linux_Gcc_Release:
          image: ${{ variables.linux }}
          configuration: Release
          CC: gcc-9
          CXX: g++-9
          CXX_FLAGS:
        Linux_Gcc_Debug:
          image: ${{ variables.linux }}
          configuration: Debug
          CC: gcc-9
          CXX: g++-9
          CXX_FLAGS:
        MacOS_Clang_Release:
          image: ${{ variables.macOS }}
          configuration: Release
          CC: clang
          CXX: clang++
          CXX_FLAGS: -Werror
          OS: MacOS
        MacOS_Clang_Debug:
          image: ${{ variables.macOS }}
          configuration: Debug
          CC: clang
          CXX: clang++
          CXX_FLAGS: -Werror

    pool:
      vmImage: $(image)

    steps:
    - checkout: self
      clean: true
      submodules: true

    - task: UsePythonVersion@0
      inputs:
        versionSpec: '3.x'

    - bash: sudo apt-get install ninja-build
      displayName: 'Installing dependencies'
      condition: eq(variables['image'], variables['linux'])

    - bash: |
        brew update
        brew install ninja
        ulimit -Sn 1024
      displayName: 'Installing dependencies'
      condition: eq(variables['image'], variables['macOS'])
    - bash: |
        cmake -B build -G Ninja $BUILD_SOURCESDIRECTORY -DLLVM_LIT_ARGS=--xunit-xml-output=testresults.xunit.xml -DDXC_USE_LIT=On -C $BUILD_SOURCESDIRECTORY/cmake/caches/PredefinedParams.cmake -DSPIRV_BUILD_TESTS=ON -DCMAKE_BUILD_TYPE=$(configuration) -DCMAKE_C_COMPILER=$(CC) -DCMAKE_CXX_COMPILER=$(CXX) -DCMAKE_CXX_FLAGS=$(CXX_FLAGS)
      displayName: 'Running Cmake'
    - bash: |
        ninja -C build test-depends
      displayName: 'Building'
    - bash: |
        ./build/bin/dxc -T ps_6_0 tools/clang/test/CodeGenSPIRV/passthru-ps.hlsl2spv
        ./build/bin/dxc -T ps_6_0 -Fo passthru-ps.dxil tools/clang/test/CodeGenSPIRV/passthru-ps.hlsl2spv
        ./build/bin/dxc -T ps_6_0 -Fo passthru-ps.spv tools/clang/test/CodeGenSPIRV/passthru-ps.hlsl2spv -spirv
        ls -ld $AGENT_BUILDDIRECTORY
        ls -ld $BUILD_SOURCESDIRECTORY
      displayName: 'Smoke tests'
    - bash: ninja -C build check-all
      displayName: 'DXC tests'
    - task: PublishTestResults@2
      inputs:
        testResultsFormat: 'JUnit'
        testResultsFiles: '**/testresults.xunit.xml'
      condition: succeededOrFailed()

