Support building with clang on Windows

This commit is contained in:
Jonathan White
2025-11-15 10:06:11 -05:00
parent 4e59c1c579
commit a2e7132ead
4 changed files with 32 additions and 18 deletions

1
.gitignore vendored
View File

@@ -27,6 +27,7 @@ CMakePresets.json
CMakeUserPresets.json CMakeUserPresets.json
.vs/ .vs/
out/ out/
\.clangd
# vcpkg # vcpkg
vcpkg_installed*/ vcpkg_installed*/

View File

@@ -229,17 +229,23 @@ if("${CMAKE_SIZEOF_VOID_P}" EQUAL "4")
set(IS_32BIT TRUE) set(IS_32BIT TRUE)
endif() endif()
set(CLANG_COMPILER_ID_REGEX "^(Apple)?[Cc]lang$") if("${CMAKE_CXX_COMPILER}" MATCHES "clang-cl(.exe)?$")
if("${CMAKE_C_COMPILER}" MATCHES "clang$" # clang-cl uses MSVC compiler flags
set(MSVC 1)
set(CMAKE_COMPILER_IS_CLANG_MSVC 1)
else()
set(CLANG_COMPILER_ID_REGEX "^(Apple)?[Cc]lang$")
if("${CMAKE_C_COMPILER}" MATCHES "clang$"
OR "${CMAKE_EXTRA_GENERATOR_C_SYSTEM_DEFINED_MACROS}" MATCHES "__clang__" OR "${CMAKE_EXTRA_GENERATOR_C_SYSTEM_DEFINED_MACROS}" MATCHES "__clang__"
OR "${CMAKE_C_COMPILER_ID}" MATCHES ${CLANG_COMPILER_ID_REGEX}) OR "${CMAKE_C_COMPILER_ID}" MATCHES ${CLANG_COMPILER_ID_REGEX})
set(CMAKE_COMPILER_IS_CLANG 1) set(CMAKE_COMPILER_IS_CLANG 1)
endif() endif()
if("${CMAKE_CXX_COMPILER}" MATCHES "clang(\\+\\+)?$" if("${CMAKE_CXX_COMPILER}" MATCHES "clang(\\+\\+)?$"
OR "${CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_DEFINED_MACROS}" MATCHES "__clang__" OR "${CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_DEFINED_MACROS}" MATCHES "__clang__"
OR "${CMAKE_CXX_COMPILER_ID}" MATCHES ${CLANG_COMPILER_ID_REGEX}) OR "${CMAKE_CXX_COMPILER_ID}" MATCHES ${CLANG_COMPILER_ID_REGEX})
set(CMAKE_COMPILER_IS_CLANGXX 1) set(CMAKE_COMPILER_IS_CLANGXX 1)
endif()
endif() endif()
macro(add_gcc_compiler_cxxflags FLAGS) macro(add_gcc_compiler_cxxflags FLAGS)
@@ -402,13 +408,17 @@ if (MSVC)
if(MSVC_TOOLSET_VERSION LESS 141) if(MSVC_TOOLSET_VERSION LESS 141)
message(FATAL_ERROR "Only Microsoft Visual Studio 17 and newer are supported!") message(FATAL_ERROR "Only Microsoft Visual Studio 17 and newer are supported!")
endif() endif()
add_compile_options(/permissive- /utf-8 /MP) add_compile_options(/permissive- /utf-8)
# Clang-cl does not support /MP, /Zf, or /fsanitize=address
if (NOT CMAKE_COMPILER_IS_CLANG_MSVC)
add_compile_options(/MP)
if(IS_DEBUG_BUILD) if(IS_DEBUG_BUILD)
add_compile_options(/Zf) add_compile_options(/Zf)
if(MSVC_TOOLSET_VERSION GREATER 141) if(MSVC_TOOLSET_VERSION GREATER 141)
add_compile_definitions(/fsanitize=address) add_compile_definitions(/fsanitize=address)
endif() endif()
endif() endif()
endif()
endif() endif()
if(WIN32) if(WIN32)
@@ -422,7 +432,7 @@ if(WIN32)
# By default MSVC enables NXCOMPAT # By default MSVC enables NXCOMPAT
add_compile_options(/guard:cf) add_compile_options(/guard:cf)
add_link_options(/DYNAMICBASE /HIGHENTROPYVA /GUARD:CF) add_link_options(/DYNAMICBASE /HIGHENTROPYVA /GUARD:CF)
else(MINGW) else()
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--nxcompat -Wl,--dynamicbase") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--nxcompat -Wl,--dynamicbase")
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,--nxcompat -Wl,--dynamicbase") set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,--nxcompat -Wl,--dynamicbase")
# Enable high entropy ASLR for 64-bit builds # Enable high entropy ASLR for 64-bit builds
@@ -432,6 +442,8 @@ if(WIN32)
endif() endif()
endif() endif()
endif() endif()
# Determine if we can link against the Windows SDK, used for Windows Hello support
find_library(WINSDK WindowsApp.lib)
endif() endif()
if(APPLE AND WITH_APP_BUNDLE OR WIN32) if(APPLE AND WITH_APP_BUNDLE OR WIN32)

View File

@@ -271,7 +271,7 @@ if(WIN32)
list(APPEND gui_SOURCES list(APPEND gui_SOURCES
gui/osutils/winutils/ScreenLockListenerWin.cpp gui/osutils/winutils/ScreenLockListenerWin.cpp
gui/osutils/winutils/WinUtils.cpp) gui/osutils/winutils/WinUtils.cpp)
if (MSVC) if (WINSDK)
list(APPEND gui_SOURCES quickunlock/WindowsHello.cpp) list(APPEND gui_SOURCES quickunlock/WindowsHello.cpp)
endif() endif()
endif() endif()
@@ -415,7 +415,7 @@ if(UNIX AND NOT APPLE)
endif() endif()
if(WIN32) if(WIN32)
target_link_libraries(keepassxc_gui Wtsapi32.lib Ws2_32.lib) target_link_libraries(keepassxc_gui Wtsapi32.lib Ws2_32.lib)
if (MSVC) if (WINSDK)
target_link_libraries(keepassxc_gui WindowsApp.lib) target_link_libraries(keepassxc_gui WindowsApp.lib)
endif() endif()
endif() endif()

View File

@@ -175,6 +175,7 @@ namespace Bootstrap
if (!CreateWellKnownSid(WinCreatorOwnerRightsSid, nullptr, pOwnerRightsSid, &pOwnerRightsSidSize)) { if (!CreateWellKnownSid(WinCreatorOwnerRightsSid, nullptr, pOwnerRightsSid, &pOwnerRightsSidSize)) {
auto error = GetLastError(); auto error = GetLastError();
Q_UNUSED(error)
goto Cleanup; goto Cleanup;
} }