| # Sets up the MSVC toolchain on a Linux runner, running under Wine. | |
| # SPDX-FileCopyrightText: The Eigen Authors | |
| # SPDX-License-Identifier: MPL-2.0 | |
| # | |
| # Source (don't execute) this: it puts the MSVC wrappers (cl, link, ...) on PATH. | |
| # | |
| # The toolchain is fetched from Microsoft's own installer manifests via | |
| # msvc-wine (https://github.com/mstorsjo/msvc-wine), per job (~800 MB, ~3 GB | |
| # unpacked), rather than baked into the CI image. | |
| # | |
| # The jobs resolve a 17.x release, so the Visual Studio Community 2022 license | |
| # applies: https://visualstudio.microsoft.com/license-terms/vs2022-ga-community/ | |
| # It permits using MSVC to develop and test "applications released under Open | |
| # Source Initiative (OSI) approved open source software licenses" -- Eigen's | |
| # MPL-2.0 -- and installing Build Tools "onto your build devices, including | |
| # physical devices and virtual machines or containers on those machines" that | |
| # are "dedicated solely to your use". It also says you may not "share, publish, | |
| # rent or lease the software", which a registry image would be. Hence per job, | |
| # and out of the job artifacts and the GitLab cache too. A runner of our own may | |
| # keep it on its own disk instead; see EIGEN_CI_MSVC_CACHE_DIR below. | |
| # | |
| # Nothing is licensed or activated at run time -- --accept-license is a local | |
| # flag, and the installed compiler builds with no network access. | |
| # The msvc-wine scripts (ISC licensed) ship in the CI image; fall back to a | |
| # clone so this also works outside it, e.g. when reproducing a failure locally. | |
| msvc_wine_dir="${EIGEN_CI_MSVC_WINE_DIR:-/opt/msvc-wine}" | |
| # The image records its commit beside the scripts. A clone checks out the one | |
| # the image is built from, read from the file both sides use so they cannot | |
| # drift; the Dockerfile has it in its build context. | |
| if [ -f "${msvc_wine_dir}/vsdownload.py" ]; then | |
| msvc_wine_commit=$(cat "${msvc_wine_dir%/}-commit.txt" 2>/dev/null) | |
| else | |
| msvc_wine_commit="${EIGEN_CI_MSVC_WINE_COMMIT}" | |
| if [ -z "${msvc_wine_commit}" ]; then | |
| commit_file="$(dirname "${BASH_SOURCE[0]}")/../docker" | |
| commit_file="${commit_file}/ubuntu-24.04-amd64-msvc-wine-build" | |
| msvc_wine_commit=$(cat "${commit_file}/msvc-wine-commit.txt") || return 1 | |
| fi | |
| fi | |
| # One Wine prefix per job, created by install.sh's wineboot when it installs the | |
| # toolchain and otherwise by the first compile; never the user's ~/.wine. | |
| export WINEDEBUG=-all | |
| export WINEPREFIX="${WINEPREFIX:-${PWD}/.wine}" | |
| # Resolve from the manifest pinned in the CI image when there is one, so the | |
| # serviced compiler build only changes when that image is rebuilt. Outside | |
| # the image, fall back to whatever the release channel currently offers. | |
| msvc_manifest="${EIGEN_CI_MSVC_MANIFEST:-/opt/msvc-manifest/vs.manifest}" | |
| if [ -f "${msvc_manifest}" ]; then | |
| # The version is written next to the manifest, so follow it if the manifest | |
| # was overridden rather than reporting the image's version for someone | |
| # else's file. | |
| msvc_manifest_version=$(cat "$(dirname "${msvc_manifest}")/version.txt" \ | |
| 2>/dev/null) | |
| printf 'Using pinned installer manifest: %s\n' \ | |
| "${msvc_manifest_version:-unknown}" | |
| channel_args="--manifest ${msvc_manifest}" | |
| else | |
| channel_args="--major ${EIGEN_CI_MSVC_VS_MAJOR:-17}" | |
| fi | |
| # A runner can keep the toolchain on its own disk across jobs: set | |
| # EIGEN_CI_MSVC_CACHE_DIR in its config.toml to a directory on a volume that | |
| # outlives the job container. Only a pinned manifest fixes the toolchain, so | |
| # only then is an install reused, keyed by the manifest, the Visual Studio | |
| # version and the msvc-wine commit that generates the wrappers. The install | |
| # goes to <key>.tmp and is renamed when complete, under a lock, so a directory | |
| # named <key> is always whole and concurrent jobs install it once. Old keys | |
| # are not removed; that is the runner's to prune. | |
| msvc_dir="${EIGEN_CI_MSVC_DIR:-${PWD}/.msvc}" | |
| msvc_install_dir="${msvc_dir}" | |
| msvc_lock="" | |
| if [ -z "${EIGEN_CI_MSVC_DIR}" ] && [ -n "${EIGEN_CI_MSVC_CACHE_DIR}" ] && \ | |
| [ -n "${msvc_manifest_version}" ] && [ -n "${msvc_wine_commit}" ]; then | |
| msvc_dir="${EIGEN_CI_MSVC_CACHE_DIR}/${msvc_manifest_version}" | |
| msvc_dir="${msvc_dir}-vs${EIGEN_CI_MSVC_VS_VERSION:-17.14}-${msvc_wine_commit:0:12}" | |
| msvc_install_dir="${msvc_dir}.tmp" | |
| mkdir -p "${EIGEN_CI_MSVC_CACHE_DIR}" | |
| exec {msvc_lock}>"${msvc_dir}.lock" | |
| flock "${msvc_lock}" | |
| printf 'MSVC toolchain cache: %s\n' "${msvc_dir}" | |
| fi | |
| if [ ! -x "${msvc_dir}/bin/x64/cl" ]; then | |
| # Under the lock, a <key>.tmp left behind is a job that was killed mid-install. | |
| if [ -n "${msvc_lock}" ]; then | |
| rm -rf "${msvc_install_dir}" | |
| fi | |
| if [ ! -f "${msvc_wine_dir}/vsdownload.py" ]; then | |
| msvc_wine_dir="${PWD}/.msvc-wine" | |
| rm -rf "${msvc_wine_dir}" | |
| git clone -q https://github.com/mstorsjo/msvc-wine.git "${msvc_wine_dir}" | |
| git -C "${msvc_wine_dir}" checkout -q "${msvc_wine_commit}" | |
| fi | |
| # Installing MSVC is the step that cannot move into the CI image; see the | |
| # license note at the top of this file. | |
| # | |
| # Note: --msvc-version takes the Visual Studio product version, not the | |
| # toolset version. 17.14 selects the v143 toolset (MSVC 14.44), matching the | |
| # hosted Windows runners the cross-compiled tests run on. | |
| "${msvc_wine_dir}/vsdownload.py" --accept-license \ | |
| ${channel_args} \ | |
| --msvc-version "${EIGEN_CI_MSVC_VS_VERSION:-17.14}" \ | |
| --architecture x64 --with-atl no --with-asan no \ | |
| --dest "${msvc_install_dir}" && | |
| # Creating the Wine prefix logs | |
| # wine: failed to open L"C:\windows\syswow64\rundll32.exe": c0000135 | |
| # because the image installs 64-bit Wine only, so wineboot cannot register the | |
| # 32-bit subsystem. Nothing here needs it -- the compiler, the linker and the | |
| # tests they produce are all x64 -- and WINEARCH=win64 does not suppress it. | |
| # It runs without the lock descriptor, which the wineserver its wineboot | |
| # starts would otherwise inherit and hold past the install. | |
| ( if [ -n "${msvc_lock}" ]; then exec {msvc_lock}>&-; fi | |
| "${msvc_wine_dir}/install.sh" "${msvc_install_dir}" ) && | |
| # The wrappers find the toolchain relative to themselves, so it can move. | |
| if [ -n "${msvc_lock}" ]; then | |
| mv "${msvc_install_dir}" "${msvc_dir}" | |
| fi | |
| fi | |
| if [ -n "${msvc_lock}" ]; then | |
| exec {msvc_lock}>&- | |
| fi | |
| # The generated wrappers record the toolset they point at. Surface it so the | |
| # Windows test job can compare it against the runtime it has. | |
| export EIGEN_CI_MSVC_TOOLSET=$(sed -n 's/^MSVCVER=//p' "${msvc_dir}/bin/x64/msvcenv.sh") | |
| export PATH="${msvc_dir}/bin/x64:${PATH}" |