From 0b26ca0c6ec05984c34f445b23a9cfac68490e8c Mon Sep 17 00:00:00 2001
From: Nirbheek Chauhan <nirbheek@centricular.com>
Date: Tue, 27 Jun 2023 23:58:51 +0530
Subject: [PATCH] ci: Update to VS 2022 Build Tools

We need the Windows 11 SDK for Windows Graphics Capture API support,
which will be enabled at runtime based on feature availability on
Windows, so should work correctly on Windows 8, 8.1, 10, and 11.

However, if we enable it in the VS 2019 installer, it will install
both Windows 10 SDK (required) and Windows 11 SDK (optional), which
will bloat the image by 3GB or more.

So just move to VS 2022 for the Windows images, which requires only
the Windows 11 SDK.

Had to remove the UWP build tools because they were causing the
installation to fail, likely due to an installer bug. We don't need
UWP anymore anyway. We just need the ARM64 build tools for the
cross-arm64 monorepo build.

Also stop installing into C:\BuildTools and let Meson pick the install
up with --vsenv.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/4939>
---
 .gitlab-ci.yml                                | 75 ++++++++++---------
 .gitlab-image-tags.yml                        |  2 +-
 ci/docker/windows/Dockerfile                  | 46 ++++++------
 ci/docker/windows/prepare_cerbero_env.sh      |  2 +-
 ...-cross-file.txt => vs2022-arm64-cross.ini} | 10 ---
 ci/meson/vs2022-paths.ini                     | 12 +++
 ...-native-file.txt => vs2022-x64-native.ini} | 12 ---
 7 files changed, 73 insertions(+), 86 deletions(-)
 rename ci/meson/{vs2019-arm64-cross-file.txt => vs2022-arm64-cross.ini} (54%)
 create mode 100644 ci/meson/vs2022-paths.ini
 rename ci/meson/{vs2019-x64-native-file.txt => vs2022-x64-native.ini} (69%)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index c6821ced6d..4c000b7d8d 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -449,38 +449,36 @@ build fedora clang:
     # Renormalize line endings
     - git add --renormalize .
     - ci/scripts/handle-subprojects-cache.py subprojects/
-    # For some reason, options are separated by newline instead of space, so we
-    # have to replace them first.
-    - $env:MESON_ARGS = $env:MESON_ARGS.replace("`n"," ")
-    - $env:MESON_CROSS_ARGS = $env:MESON_CROSS_ARGS.replace("`n"," ")
-    - cmd.exe /C "C:\BuildTools\Common7\Tools\VsDevCmd.bat -host_arch=amd64 -arch=$env:ARCH &&
-        meson build $env:MESON_ARGS $env:MESON_CROSS_ARGS &&
-        meson compile -C build"
+    - echo $env:MESON_ARGS
   artifacts:
    expire_in: "7 days"
    when: "always"
    paths:
      - 'build/meson-logs/'
 
-build vs2019 amd64:
+.build windows native:
   extends: '.build windows'
+  script:
+    - !reference [".build windows", "script"]
+    - meson setup --vsenv build $env:MESON_ARGS
+    - meson compile -C build
+    - .\gst-env.py gst-inspect-1.0.exe --version
+    - .\gst-env.py gst-inspect-1.0.exe
+    - mkdir destdir
+    - meson install -C build --destdir installdir
+    - Remove-Item -Recurse -Force build\installdir
+
+build vs2022 amd64:
+  extends: '.build windows native'
   variables:
     ARCH: 'amd64'
-  script:
-    - !reference [".build windows", "script",]
-    - cmd.exe /C "C:\BuildTools\Common7\Tools\VsDevCmd.bat -host_arch=amd64 -arch=$env:ARCH &&
-        .\gst-env.py gst-inspect-1.0.exe --version &&
-        .\gst-env.py gst-inspect-1.0.exe &&
-        mkdir .\destdir &&
-        meson install --destdir=$env:CI_PROJECT_DIR\destdir -C build &&
-        rmdir /s /q  $env:CI_PROJECT_DIR\destdir"
 
-build vs2019 x86:
-  extends: '.build windows'
+build vs2022 x86:
+  extends: '.build windows native'
   variables:
     ARCH: 'x86'
 
-build vs2019 arm64 uwp:
+build vs2022 arm64:
   extends: '.build windows'
   variables:
     ARCH: 'arm64'
@@ -490,8 +488,23 @@ build vs2019 arm64 uwp:
       -Dgst-plugins-base:pango=disabled
       -Dgst-plugins-good:cairo=disabled
       -Dgst-devtools:cairo=disabled
-      --cross-file ci/meson/vs2019-arm64-cross-file.txt
-      --native-file ci/meson/vs2019-x64-native-file.txt
+      --cross-file ci/meson/vs2022-paths.ini
+      --cross-file ci/meson/vs2022-arm64-cross.ini
+      --native-file ci/meson/vs2022-paths.ini
+      --native-file ci/meson/vs2022-x64-native.ini
+  script:
+    - !reference [".build windows", "script"]
+    # For some reason, options are separated by newline instead of space, so we
+    # have to replace them first.
+    - $env:MESON_ARGS = $env:MESON_ARGS.replace("`n"," ")
+    - $env:MESON_CROSS_ARGS = $env:MESON_CROSS_ARGS.replace("`n"," ")
+      # Setting up a cross build with MSVC is still non-trivial because
+      # the --vsenv argument cannot be used to set it up
+    - echo $env:MESON_ARGS
+    - echo $env:MESON_CROSS_ARGS
+    - cmd.exe /C "C:\BuildTools\Common7\Tools\VsDevCmd.bat -host_arch=amd64 -arch=$env:ARCH &&
+        meson setup build $env:MESON_ARGS $env:MESON_CROSS_ARGS &&
+        meson compile -C build "
 
 build msys2 :
   extends: '.build windows'
@@ -502,24 +515,12 @@ build msys2 :
       allow_failure: true
       when: 'manual'
   script:
-    # Make sure powershell exits on errors
-    # https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variables?view=powershell-6
-    - $ErrorActionPreference = "Stop"
-
-    # Set the code page to UTF-8
-    - chcp 65001
-
+    - !reference [".build windows", "script"]
+    # Replace backslashes with forward so bash doesn't complain
+    - $env:CI_PROJECT_DIR = $env:CI_PROJECT_DIR.replace('\','/')
     # Configure MSYS2 to use the UCRT64 environment, start in the same directory
     # and inherit PATH
-    - $env:MSYSTEM = "UCRT64"
-    - $env:CHERE_INVOKING = "1"
-    - $env:MSYS2_PATH_TYPE = "inherit"
-    # For some reason, options are separated by newline instead of space, so we
-    # have to replace them first.
-    - $env:MESON_ARGS = $env:MESON_ARGS.replace("`n"," ")
-    # Replace forward slashes with backwards so bash doesn't complain
-    - $env:_PROJECT_DIR = $env:CI_PROJECT_DIR.replace('\','/')
-    - C:\msys64\usr\bin\bash -lc "meson build $env:MESON_ARGS && ninja -C build"
+    - C:\msys64\msys2_shell.cmd -ucrt64 -defterm -no-start -use-full-path -lc "meson setup build $env:MESON_ARGS && ninja -C build"
 
 # ---- Tests ----- #
 
diff --git a/.gitlab-image-tags.yml b/.gitlab-image-tags.yml
index 8aeb5400a1..631a787485 100644
--- a/.gitlab-image-tags.yml
+++ b/.gitlab-image-tags.yml
@@ -8,4 +8,4 @@ variables:
   FEDORA_TAG:  '2023-06-12.0'
   INDENT_TAG: '2023-06-14.1'
   LINT_TAG: '2023-02-16.5'
-  WINDOWS_TAG: '2023-06-12.0'
+  WINDOWS_TAG: '2023-07-07.0'
diff --git a/ci/docker/windows/Dockerfile b/ci/docker/windows/Dockerfile
index 5e48f4602a..cbb2021caa 100644
--- a/ci/docker/windows/Dockerfile
+++ b/ci/docker/windows/Dockerfile
@@ -8,10 +8,30 @@ SHELL ["powershell","-NoLogo", "-NonInteractive", "-Command"]
 
 RUN Install-WindowsFeature -Name Server-Media-Foundation
 
+# Visual Studio can't be installed with choco.
+# It depends on dotnetfx v4.8.0.20190930, which requires a reboot: dotnetfx (exit code 3010)
+# https://github.com/microsoft/vs-dockerfiles/blob/main/native-desktop/
+# Set up environment to collect install errors.
+COPY Install.cmd C:\TEMP\
+RUN Invoke-WebRequest -Uri https://aka.ms/vscollect.exe -OutFile C:\TEMP\collect.exe
+# Download channel for fixed install.
+RUN Invoke-WebRequest -Uri https://aka.ms/vs/17/release/channel -OutFile C:\TEMP\VisualStudio.chman
+# Download and install Build Tools for Visual Studio 2022 for native desktop workload.
+RUN Invoke-WebRequest -Uri https://aka.ms/vs/17/release/vs_buildtools.exe -OutFile C:\TEMP\vs_buildtools.exe
+RUN C:\TEMP\Install.cmd C:\TEMP\vs_buildtools.exe --quiet --wait --norestart --nocache `
+    --channelUri C:\TEMP\VisualStudio.chman `
+    --installChannelUri C:\TEMP\VisualStudio.chman `
+    --add Microsoft.VisualStudio.Workload.VCTools `
+    --add Microsoft.VisualStudio.Component.VC.Tools.ARM64 `
+    --includeRecommended `
+    --installPath C:\BuildTools
+
+RUN Get-ChildItem 'C:\BuildTools\VC\Tools\MSVC'
+RUN Get-ChildItem 'C:\Program Files (x86)\Windows Kits\10\lib'
+
 # Install Chocolatey
 RUN iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
 # Install required packages
-RUN choco install -y vcredist140
 RUN choco install -y cmake --installargs 'ADD_CMAKE_TO_PATH=System'
 RUN choco install -y git --params '/NoAutoCrlf /NoCredentialManager /NoShellHereIntegration /NoGuiHereIntegration /NoShellIntegration'
 RUN choco install -y git-lfs
@@ -27,30 +47,6 @@ RUN New-ItemProperty -Path 'HKLM:\\SYSTEM\\CurrentControlSet\\Control\\FileSyste
 RUN git config --system core.longpaths true
 
 RUN c:\msys64\usr\bin\bash -lc 'pacman -S --noconfirm mingw-w64-ucrt-x86_64-toolchain ninja'
-# Visual Studio can't be installed with choco.
-# It depends on dotnetfx v4.8.0.20190930, which requires a reboot: dotnetfx (exit code 3010)
-# https://github.com/microsoft/vs-dockerfiles/blob/main/native-desktop/
-# Set up environment to collect install errors.
-COPY Install.cmd C:\TEMP\
-RUN Invoke-WebRequest -Uri https://aka.ms/vscollect.exe -OutFile C:\TEMP\collect.exe
-# Download channel for fixed install.
-RUN Invoke-WebRequest -Uri https://aka.ms/vs/16/release/channel -OutFile C:\TEMP\VisualStudio.chman
-# Download and install Build Tools for Visual Studio 2017 for native desktop workload.
-RUN Invoke-WebRequest -Uri https://aka.ms/vs/16/release/vs_buildtools.exe -OutFile C:\TEMP\vs_buildtools.exe
-RUN C:\TEMP\Install.cmd C:\TEMP\vs_buildtools.exe --quiet --wait --norestart --nocache `
-    --channelUri C:\TEMP\VisualStudio.chman `
-    --installChannelUri C:\TEMP\VisualStudio.chman `
-    --add Microsoft.VisualStudio.Workload.VCTools `
-    --add Microsoft.VisualStudio.Workload.UniversalBuildTools `
-    --add Microsoft.VisualStudio.Component.VC.Tools.ARM64 `
-    --add Microsoft.VisualStudio.Component.VC.Tools.ARM `
-    --add Microsoft.VisualStudio.Component.UWP.VC.ARM64 `
-    --includeRecommended `
-    --installPath C:\BuildTools
-
-RUN Get-ChildItem C:\BuildTools
-RUN Get-ChildItem C:\BuildTools\VC\Tools\MSVC
-RUN Get-ChildItem 'C:\Program Files (x86)\Windows Kits\10\lib'
 
 RUN pip3 install meson==1.1.1
 
diff --git a/ci/docker/windows/prepare_cerbero_env.sh b/ci/docker/windows/prepare_cerbero_env.sh
index be9b4f688f..90fb80c765 100644
--- a/ci/docker/windows/prepare_cerbero_env.sh
+++ b/ci/docker/windows/prepare_cerbero_env.sh
@@ -9,7 +9,7 @@ cd C:/cerbero
 echo 'local_sources="C:/cerbero/cerbero-sources"' > localconf.cbc
 echo 'home_dir="C:/cerbero/cerbero-build"' >> localconf.cbc
 echo 'vs_install_path = "C:/BuildTools"' >> localconf.cbc
-echo 'vs_install_version = "vs16"' >> localconf.cbc
+echo 'vs_install_version = "vs17"' >> localconf.cbc
 
 # Fetch all bootstrap requirements
 ./cerbero-uninstalled -t  -v visualstudio -c localconf.cbc -c config/win64.cbc fetch-bootstrap --jobs=4
diff --git a/ci/meson/vs2019-arm64-cross-file.txt b/ci/meson/vs2022-arm64-cross.ini
similarity index 54%
rename from ci/meson/vs2019-arm64-cross-file.txt
rename to ci/meson/vs2022-arm64-cross.ini
index 21b05dbb4d..e5075c5156 100644
--- a/ci/meson/vs2019-arm64-cross-file.txt
+++ b/ci/meson/vs2022-arm64-cross.ini
@@ -1,19 +1,9 @@
-[constants]
-vs_path = 'C:\BuildTools'
-msvc_version = '14.29.30133'
-msvc_version_dir = vs_path / 'VC\Tools\MSVC' / msvc_version
-msvc_arm64_bindir = msvc_version_dir / 'bin\Hostx64\arm64'
-msvc_arm64_libdir = msvc_version_dir / 'lib\arm64'
-
 [host_machine]
 system = 'windows'
 cpu_family = 'aarch64'
 cpu = 'aarch64'
 endian = 'little'
 
-[properties]
-needs_exe_wrapper = true
-
 [binaries]
 lib       = msvc_arm64_bindir / 'lib.exe'
 c         = msvc_arm64_bindir / 'cl.exe'
diff --git a/ci/meson/vs2022-paths.ini b/ci/meson/vs2022-paths.ini
new file mode 100644
index 0000000000..f5d9cf75e2
--- /dev/null
+++ b/ci/meson/vs2022-paths.ini
@@ -0,0 +1,12 @@
+[constants]
+vs_path = 'C:\BuildTools'
+msvc_version = '14.36.32532'
+msvc_version_dir = vs_path / 'VC\Tools\MSVC' / msvc_version
+msvc_arm64_bindir = msvc_version_dir / 'bin\Hostx64\arm64'
+msvc_arm64_libdir = msvc_version_dir / 'lib\arm64'
+msvc_x64_bindir = msvc_version_dir / 'bin\Hostx64\x64'
+msvc_x64_libdir = msvc_version_dir / 'lib\x64'
+wk_path = 'C:\Program Files (x86)\Windows Kits'
+wk_version = '10.0.22621.0'
+wk_x64_libdir = wk_path / '10\lib' / wk_version / 'um\x64'
+wk_x64_crt_libdir = wk_path / '10\lib' / wk_version / 'ucrt\x64'
diff --git a/ci/meson/vs2019-x64-native-file.txt b/ci/meson/vs2022-x64-native.ini
similarity index 69%
rename from ci/meson/vs2019-x64-native-file.txt
rename to ci/meson/vs2022-x64-native.ini
index f4568d75a6..7a46688eb3 100644
--- a/ci/meson/vs2019-x64-native-file.txt
+++ b/ci/meson/vs2022-x64-native.ini
@@ -1,13 +1,4 @@
 [constants]
-vs_path = 'C:\BuildTools'
-msvc_version = '14.29.30133'
-msvc_version_dir = vs_path / 'VC\Tools\MSVC' / msvc_version
-msvc_x64_bindir = msvc_version_dir / 'bin\Hostx64\x64'
-msvc_x64_libdir = msvc_version_dir / 'lib\x64'
-wk_path = 'C:\Program Files (x86)\Windows Kits'
-wk_version = '10.0.19041.0'
-wk_x64_libdir = wk_path / '10\lib' / wk_version / 'um\x64'
-wk_x64_crt_libdir = wk_path / '10\lib' / wk_version / 'ucrt\x64'
 # Forcibly link to x64 libs when using native linker, otherwise the LIB
 # variable in the env will cause link.exe to pick up libs from the cross
 # msvc libdir. A better fix might be to use a wrapper script that calls
@@ -20,9 +11,6 @@ cpu_family = 'x86_64'
 cpu = 'x86_64'
 endian = 'little'
 
-[properties]
-needs_exe_wrapper = true
-
 [built-in options]
 # Ensure that x64 libs are used for linking even when we're inside, say, an
 # arm64 VS environment