# Windows-specific builder configurations and build commands

## Base configurations

.windows:
    variables:
        GIT_SUBMODULE_STRATEGY: none
        GIT_CLONE_PATH: "$CI_BUILDS_DIR\\paraview-ci-ext\\$CI_CONCURRENT_ID"
        # Turn off direct mode until we test it.
        BUILDCACHE_DIRECT_MODE: "false"

### 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\\paraview-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_kits_mpi_python_qt:
    extends: .windows_vs2022

    variables:
        CMAKE_CONFIGURATION: windows_vs2022_kits_mpi_python_qt

.windows_vs2022_qt:
    extends: .windows_vs2022

    variables:
        CMAKE_CONFIGURATION: windows_vs2022_qt

## Tags

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

.windows_test_tags:
    tags:
        - paraview # Since this is a bare runner, pin to a project.
        - msvc-19.35
        - nonconcurrent
        - shell
        - vs2022
        - windows-x86_64
        - test # Some machines have trouble running tests.

## 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
    # Support submodule references to the user's fork.
    - git submodule foreach --recursive cmake -P "$pwdpath/.gitlab/ci/fetch_submodule.cmake"
    - git submodule sync --recursive
    - git submodule update --init --recursive

.before_script_windows_deps: &before_script_windows_deps
    - cmake -P .gitlab/ci/download_qt.cmake
    - Set-Item -Force -Path "env:PATH" -Value "$pwdpath\.gitlab\qt\bin;$env:PATH"
    - cmake -P .gitlab/ci/download_index.cmake
    - Set-Item -Force -Path "env:PATH" -Value "$pwdpath\.gitlab\nvidia-index\lib;$env:PATH"
    - cmake -P .gitlab/ci/download_python.cmake
    - Set-Item -Force -Path "env:PATH" -Value "$pwdpath\.gitlab\python;$env:PATH"
    - Set-Item -Force -Path "env:PYTHONHOME" -Value "$pwdpath\.gitlab\python"

.cmake_build_windows:
    stage: build
    extends: .warning_policy

    script:
        - *before_script_windows
        - *before_script_windows_deps
        # Build and activate the venv
        - python -m venv $pwdpath\build\ci-venv
        # This may be necessary when running manually.
        # Set-ExecutionPolicy -Scope CurrentUser RemoteSigned
        - . .\build\ci-venv\Scripts\Activate.ps1
        - pip install -qq -r $pwdpath\.gitlab\ci\requirements.txt
        - Invoke-Expression -Command .gitlab/ci/buildcache.ps1
        - Set-Item -Force -Path "env:PATH" -Value "$env:PATH;$pwdpath\.gitlab\buildcache\bin"
        - Invoke-Expression -Command .gitlab/ci/vcvarsall.ps1
        - buildcache --show-stats
        - ctest -VV -S .gitlab/ci/ctest_configure.cmake
        - ctest -VV -S .gitlab/ci/ctest_build.cmake | Out-File -FilePath compile_output.log
        - buildcache --show-stats
        - .gitlab\ci\check_warnings.ps1 -BuildPath . # This will call exit in case of warnings
    interruptible: true

.cmake_test_windows:
    stage: test

    script:
        - *before_script_windows
        - *before_script_windows_deps
        # Reuse the venv from the build step (avoids version changes between
        # build and testing). This is set up manually because Activate.ps1
        # doesn't seem to work for our extracted Python tarball.
        - Set-Item -Force -Path "env:PATH" -Value "$pwdpath\build\ci-venv\Scripts;$env:PATH"
        - Set-Item -Force -Path "env:PYTHONPATH" -Value "$pwdpath\build\ci-venv\Lib\site-packages"
        # Some tests run CMake steps, so we need a compiler environment.
        - Invoke-Expression -Command .gitlab/ci/vcvarsall.ps1
        - ctest --output-on-failure -V -S .gitlab/ci/ctest_test.cmake
    interruptible: true
