# Windows-specific builder configurations and build commands

## Base configurations

.windows:
    variables:
        GIT_SUBMODULE_STRATEGY: none
        GIT_CLONE_PATH: "$CI_BUILDS_DIR\\vtk-ci-ext\\$CI_CONCURRENT_ID"

.wheel_windows_x86_64:
    extends: .windows

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

.wheel_windows36_x86_64:
    extends: .wheel_windows_x86_64

    variables:
        CMAKE_CONFIGURATION: wheel_windows36_x86_64
        VTK_DISABLE_PYI_TESTING: 1

.wheel_windows37_x86_64:
    extends: .wheel_windows_x86_64

    variables:
        CMAKE_CONFIGURATION: wheel_windows37_x86_64
        VTK_DISABLE_PYI_TESTING: 1

.wheel_windows38_x86_64:
    extends: .wheel_windows_x86_64

    variables:
        CMAKE_CONFIGURATION: wheel_windows38_x86_64

.wheel_windows39_x86_64:
    extends: .wheel_windows_x86_64

    variables:
        CMAKE_CONFIGURATION: wheel_windows39_x86_64

.wheel_windows310_x86_64:
    extends: .wheel_windows_x86_64

    variables:
        CMAKE_CONFIGURATION: wheel_windows310_x86_64

.wheel_windows311_x86_64:
    extends: .wheel_windows_x86_64

    variables:
        CMAKE_CONFIGURATION: wheel_windows311_x86_64

### 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\\vtk-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_tbb_addin:
    variables:
        TBB_REDIST_DIR: "intel64\vc14"

.windows_vs2019:
    extends: .windows_build

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

.windows_vs2019_kits_mpi_python_qt:
    extends:
        - .windows_vs2019
        # - .windows_tbb_addin

    variables:
        CMAKE_CONFIGURATION: windows_vs2019_kits_mpi_python_qt

.windows_vs2019_kits_mpi_python_qt_stdthread:
    extends:
        - .windows_vs2019

    variables:
        CMAKE_CONFIGURATION: windows_vs2019_kits_mpi_python_qt_stdthread

.windows_vs2019_kits_mpi_python_qt_ext_vtk:
    extends:
        - .windows_vs2019
        # - .windows_tbb_addin

    variables:
        CMAKE_CONFIGURATION: windows_vs2019_kits_mpi_python_qt_ext_vtk

.windows_vs2019_mpi_python_qt:
    extends:
        - .windows_vs2019
        # - .windows_tbb_addin

    variables:
        CMAKE_CONFIGURATION: windows_vs2019_mpi_python_qt

.windows_vs2019_mpi_python_qt_static:
    extends:
        - .windows_vs2019
        # - .windows_tbb_addin

    variables:
        CMAKE_CONFIGURATION: windows_vs2019_mpi_python_qt_static

## Tags

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

.windows_wheel_builder_tags:
    tags:
        - vtk # Since this is a bare runner, pin to a project.
        - msvc-19.28
        - concurrent
        - shell
        - vs2019
        - windows-x86_64

.windows_tester_tags:
    tags:
        - vtk # Since this is a bare runner, pin to a project.
        - msvc-19.28
        - nonconcurrent
        - shell
        - vs2019
        - windows-x86_64
        # - test # Not needed yet?

.windows_ext_builder_tags:
    tags:
        - vtk # Since this is a bare runner, pin to a project.
        - msvc-19.28
        # - concurrent
        - nonconcurrent # FIXME: non-relocatability in VTK-m and `VTK_DLL_PATHS`
        - shell
        - vs2019
        - windows-x86_64
        # - test # Not needed yet?

## 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 update --init --recursive
    - 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_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 -P .gitlab/ci/download_tbb.cmake
    - Set-Item -Force -Path "env:CMAKE_PREFIX_PATH" -Value "$pwdpath\.gitlab\tbb;$env:CMAKE_PREFIX_PATH"
    - Set-Item -Force -Path "env:PATH" -Value "$pwdpath\.gitlab\tbb\redist\$env:TBB_REDIST_DIR;$env:PATH"

.cmake_build_windows:
    stage: build

    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
        - 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 | Out-File -FilePath compile_output.log
        - sccache --show-stats
        - sccache --stop-server
    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

.cmake_test_external_windows:
    stage: test-ext

    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"
        - Set-Item -Force -Path "env:PATH" -Value "$pwdpath\build\install\bin;$env:PATH"
        - Invoke-Expression -Command .gitlab/ci/vcvarsall.ps1
        - ctest -V --output-on-failure -S .gitlab/ci/ctest_test_external.cmake

    interruptible: true

.cmake_build_wheel_windows:
    stage: build

    script:
        - *before_script_windows
        - cmake -P .gitlab/ci/download_wheel_python.cmake
        - Set-Item -Force -Path "env:PYTHON_PREFIX" -Value "$pwdpath\.gitlab\python"
        - 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 | Out-File -FilePath compile_output.log
        - sccache --show-stats
        - sccache --stop-server
        - cd build
        - Set-Item -Force -Path "env:PATH" -Value "$env:PYTHON_PREFIX;$env:PATH"
        - python -m venv venv
        - . .\venv\Scripts\Activate.ps1
        - pip install wheel
        - python setup.py bdist_wheel
        - python "$env:GIT_CLONE_PATH\.gitlab\ci\create_wheel_sdk_archive.py"
        # XXX(delvewheel): It is finding 32bit Visual Studio libraries and
        # bailing instead of continuing to search for 64bit versions of the
        # DLL.
        # See https://github.com/adang1345/delvewheel/issues/12
        # - pip install delvewheel
        # - delvewheel show (Get-ChildItem dist\*.whl | % FullName) | Out-File -FilePath "$env:GIT_CLONE_PATH\wheel_output.log"
        # - delvewheel repair (Get-ChildItem dist\*.whl | % FullName) | Out-File -Append -FilePath "$env:GIT_CLONE_PATH\wheel_output.log"
    interruptible: true

.cmake_test_wheel_windows:
    stage: test

    script:
        - *before_script_windows
        - cmake -P .gitlab/ci/download_wheel_python.cmake
        - Set-Item -Force -Path "env:PYTHON_PREFIX" -Value "$pwdpath\.gitlab\python"
        - Set-Item -Force -Path "env:PATH" -Value "$env:PYTHON_PREFIX;$env:PATH"
        - python -m venv venv
        - . .\venv\Scripts\Activate.ps1
        - pip install twine
        - twine check (Get-ChildItem build\dist\*.whl | % FullName)
        - pip install (Get-ChildItem build\dist\*.whl | % FullName)
        - python -c "import vtk"
        - if ( "$env:VTK_DISABLE_PYI_TESTING" -ne "1" ) { python -m vtkmodules.generate_pyi --test -p vtkmodules } else { echo "pyi test is disabled." }
    interruptible: true
