# Windows-specific builder configurations and build commands

## Base configurations

.windows:
    variables:
        GIT_SUBMODULE_STRATEGY: recursive
        GIT_CLONE_PATH: "$CI_BUILDS_DIR\\cmb-ci-ext\\$CI_CONCURRENT_ID"

### Build and test

.windows_build:
    extends: .windows

    variables:
        # Note that shell runners only support runners with a single
        # concurrency level. We can't use `$CI_CONCURRENCY_ID` because this may
        # change between the build and test stages which CMake doesn't support.
        # Even if we could, it could change if other runners on the machine
        # could run at the same time, so we drop it.
        GIT_CLONE_PATH: "$CI_BUILDS_DIR\\cmb-ci"

.windows_vs2019:
    extends: .windows_build

    variables:
        VCVARSALL: "${VS160COMNTOOLS}\\..\\..\\VC\\Auxiliary\\Build\\vcvarsall.bat"
        VCVARSPLATFORM: "x64"
        VCVARSVERSION: "14.26"

.windows_vs2019_ninja:
    extends: .windows_vs2019

    variables:
        CMAKE_CONFIGURATION: windows_vs2019_ninja

## Tags

.windows_builder_tags:
    tags:
        - cmb # Since this is a bare runner, pin to a project.
        - msvc-19.26
        - nonconcurrent
        - shell
        - vs2019
        - windows

## Windows-specific scripts

.before_script_windows: &before_script_windows
    - Invoke-Expression -Command .gitlab/ci/cmake.ps1
    - Invoke-Expression -Command .gitlab/ci/ninja.ps1
    - $pwdpath = $pwd.Path
    - Set-Item -Force -Path "env:PATH" -Value "$pwdpath\.gitlab;$pwdpath\.gitlab\cmake\bin;$env:PATH"
    - cmake --version
    - ninja --version
    - cmake -P .gitlab/ci/download_qt.cmake
    - Set-Item -Force -Path "env:PATH" -Value "$pwdpath\.gitlab\qt\bin;$env:PATH"
    # Add the superbuild to the PATH
    - cmake -P .gitlab/ci/download_superbuild.cmake
    - Set-Item -Force -Path "env:CMAKE_PREFIX_PATH" -Value "$pwdpath\build\install"
    - Set-Item -Force -Path "env:SUPERBUILD_PREFIX" -Value "$pwdpath\build"
    - Set-Item -Force -Path "env:PATH" -Value "$env:PATH;$pwdpath\build\install\bin;$pwdpath\build\install\Python"
    # Temporary - vtkmdiympi_nompi.dll is in the wrong place. Remove when
    # https://gitlab.kitware.com/vtk/vtk/-/merge_requests/7191 makes it into our paraview.
    - Set-Item -Force -Path "env:PATH" -Value "$env:PATH;$pwdpath\build\install\lib"
    - Set-Item -Force -Path "env:PYTHONHOME" -Value "$pwdpath\build\install\Python"

.cmake_build_windows:
    stage: build

    script:
        - *before_script_windows
        - Set-Item -Force -Path "env:PATH" -Value "$env:PATH;$env:SCCACHE_PATH"
        - Invoke-Expression -Command .gitlab/ci/vcvarsall.ps1
        - sccache --start-server
        - sccache --show-stats
        - ctest -VV -S .gitlab/ci/ctest_configure.cmake
        - ctest -VV -S .gitlab/ci/ctest_build.cmake
        - sccache --show-stats
        - sccache --stop-server
    interruptible: true

.cmake_test_windows:
    stage: test

    script:
        - *before_script_windows
        - Invoke-Expression -Command .gitlab/ci/vcvarsall.ps1
        - ctest --output-on-failure -V -S .gitlab/ci/ctest_test.cmake
    after_script:
        # Ensure that `git clean` can always remove the directory. But don't
        # remove so much that the build cannot be debugged.
        # https://github.com/git-for-windows/git/issues/2715
        - cmd /C "rmdir /S /Q build\superbuild\smtk\build\PluginTests"
    interruptible: true
