From 78b6a31e7d6d1a1637ca490174b07df4c0d46861 Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Fri, 2 Oct 2026 20:07:38 +1000 Subject: [PATCH] [client/common] windows: fix native clang-cl build --- client/CMakeLists.txt | 7 +++++ client/CMakePresets.json | 3 +- client/cmake/toolchains/windows-clangcl.cmake | 28 +++++++++++++++++++ common/src/platform/windows/filesystem.c | 3 +- doc/build_client_windows.rst | 20 +++++++++---- 5 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 client/cmake/toolchains/windows-clangcl.cmake diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index 08ee5251..c08cc354 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -42,6 +42,13 @@ if(LG_CLIENT_PLATFORM_WINDOWS AND MSVC AND "C++; use the windows-clangcl configure preset") endif() +if(LG_CLIENT_PLATFORM_WINDOWS AND MSVC AND + NOT CMAKE_SIZEOF_VOID_P EQUAL 8) + message(FATAL_ERROR + "The native Windows client requires an x64 clang-cl target; remove the " + "existing build directory and use the windows-clangcl configure preset") +endif() + set(OPTIMIZE_FOR_NATIVE_DEFAULT ON) include(OptimizeForNative) # option(OPTIMIZE_FOR_NATIVE) include(UninstallTarget) diff --git a/client/CMakePresets.json b/client/CMakePresets.json index d0e558a0..e586cd04 100644 --- a/client/CMakePresets.json +++ b/client/CMakePresets.json @@ -19,8 +19,6 @@ "rhs": "Windows" }, "cacheVariables": { - "CMAKE_C_COMPILER": "clang-cl", - "CMAKE_CXX_COMPILER": "clang-cl", "CMAKE_BUILD_TYPE": "RelWithDebInfo", "CMAKE_MSVC_RUNTIME_LIBRARY": "MultiThreadedDLL", "ENABLE_AUDIO": true, @@ -28,6 +26,7 @@ "ENABLE_NETWORK": true, "ENABLE_WASAPI": true, "OPTIMIZE_FOR_NATIVE": "OFF", + "VCPKG_CHAINLOAD_TOOLCHAIN_FILE": "${sourceDir}/cmake/toolchains/windows-clangcl.cmake", "VCPKG_OVERLAY_TRIPLETS": "${sourceDir}/cmake/vcpkg-triplets", "VCPKG_TARGET_TRIPLET": "x64-windows-lg" } diff --git a/client/cmake/toolchains/windows-clangcl.cmake b/client/cmake/toolchains/windows-clangcl.cmake new file mode 100644 index 00000000..d7fc77da --- /dev/null +++ b/client/cmake/toolchains/windows-clangcl.cmake @@ -0,0 +1,28 @@ +if(NOT DEFINED ENV{VSCMD_VER} OR + NOT "$ENV{VSCMD_ARG_TGT_ARCH}" MATCHES "^(x64|amd64)$" OR + NOT "$ENV{VSCMD_ARG_HOST_ARCH}" MATCHES "^(x64|amd64)$") + message(FATAL_ERROR + "The windows-clangcl preset requires the x64 Native Tools environment; " + "open an x64 Native Tools command prompt and configure again") +endif() + +find_program(LG_WINDOWS_CLANG_CL + NAMES clang-cl + PATHS "$ENV{VCINSTALLDIR}/Tools/Llvm/x64/bin" + NO_DEFAULT_PATH + REQUIRED) +execute_process( + COMMAND "${LG_WINDOWS_CLANG_CL}" -print-target-triple + RESULT_VARIABLE LG_WINDOWS_CLANG_RESULT + OUTPUT_VARIABLE LG_WINDOWS_CLANG_TARGET + ERROR_VARIABLE LG_WINDOWS_CLANG_ERROR + OUTPUT_STRIP_TRAILING_WHITESPACE) +if(NOT LG_WINDOWS_CLANG_RESULT EQUAL 0 OR + NOT LG_WINDOWS_CLANG_TARGET MATCHES "^x86_64-") + message(FATAL_ERROR + "The x64 Native Tools environment selected an invalid clang-cl: " + "${LG_WINDOWS_CLANG_ERROR}${LG_WINDOWS_CLANG_TARGET}") +endif() + +set(CMAKE_C_COMPILER "${LG_WINDOWS_CLANG_CL}" CACHE FILEPATH "" FORCE) +set(CMAKE_CXX_COMPILER "${LG_WINDOWS_CLANG_CL}" CACHE FILEPATH "" FORCE) diff --git a/common/src/platform/windows/filesystem.c b/common/src/platform/windows/filesystem.c index 6ef30e77..db93a840 100644 --- a/common/src/platform/windows/filesystem.c +++ b/common/src/platform/windows/filesystem.c @@ -22,6 +22,7 @@ #include "common/win32.h" +#include #include #include #include @@ -144,7 +145,7 @@ FILE * lgOpenFile(const char * path, const char * mode) return NULL; } - FILE * result = _wfopen(widePathValue, wideMode); + FILE * result = _wfsopen(widePathValue, wideMode, _SH_DENYNO); free(widePathValue); free(wideMode); return result; diff --git a/doc/build_client_windows.rst b/doc/build_client_windows.rst index 7b3c1fb4..e5cba88b 100644 --- a/doc/build_client_windows.rst +++ b/doc/build_client_windows.rst @@ -4,8 +4,8 @@ Build the Windows client ######################## The Windows client uses ``clang-cl`` and the MSVC ABI. ANGLE continues to -provide EGL and OpenGL ES, while MsQuic and its bundled OpenSSL are linked -statically. +provide EGL and OpenGL ES, while MsQuic and the vcpkg-provided OpenSSL are +linked statically. Requirements ------------ @@ -14,7 +14,7 @@ Install the following tools: - Visual Studio 2022 Build Tools with the Desktop development with C++ workload, Clang tools for Windows, and a Windows SDK -- CMake 3.21 or newer +- CMake 3.24 or newer - Ninja, with ``ninja.exe`` available on ``PATH`` - Git - vcpkg @@ -30,13 +30,23 @@ works before configuring the client. Building -------- -Open an x64 Native Tools command prompt or Developer PowerShell, then run: +Open an x64 Native Tools command prompt. Alternatively, initialize a regular +PowerShell session for an x64 host and target first (replace ``Community`` +with the installed Visual Studio edition when necessary): + +.. code:: powershell + + $vsTools = 'C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools' + & "$vsTools\Launch-VsDevShell.ps1" ` + -Arch amd64 -HostArch amd64 -SkipAutomaticLocation + +Then run: .. code:: powershell git submodule update --init --recursive cd client - cmake --preset windows-clangcl + cmake --fresh --preset windows-clangcl cmake --build --preset windows-clangcl-release The executable is written beneath ``client/build/windows-clangcl``. The