# 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"
        # Avoid conflicting with other projects running on the same machine.
        SCCACHE_SERVER_PORT: 4228
        # Do not let the sccache server shut down due to a slow machine.
        SCCACHE_IDLE_TIMEOUT: 0

### 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"
        # Force "desktop" OpenGL support. Qt seems to want to use EGL when run
        # from GitLab-CI by default (it runs as a Windows service).
        QT_OPENGL: desktop

.windows_vs2022:
    extends: .windows_build

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

.windows_vs2022_ninja:
    extends: .windows_vs2022

    variables:
        CMAKE_CONFIGURATION: windows_vs2022_ninja

## Tags

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

## Windows-specific scripts

.before_script_windows: &before_script_windows
    - $pwdpath = $pwd.Path
    - powershell -File ".gitlab/ci/cmake.ps1"
    - Set-Item -Force -Path "env:PATH" -Value "$pwdpath\.gitlab\cmake\bin;$env:PATH"
    - powershell -File ".gitlab/ci/ninja.ps1"
    - Set-Item -Force -Path "env:PATH" -Value "$pwdpath\.gitlab;$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"
    - 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
        - Invoke-Expression -Command "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
    interruptible: true
