From 85849fea41cd58888042a1347b97af678eb12ff0 Mon Sep 17 00:00:00 2001 From: Seth Flynn Date: Mon, 22 Dec 2025 02:23:48 -0500 Subject: [PATCH] fix(build): handle CMAKE_BUILD_TYPE with generator expressions Since we've started using ninja's multi-config generator, evaluating CMAKE_BUILD_TYPE at configure-time is no longer reliable. Thankfully, CMake offers "generator expressions" that are evaluated during build system generation, which allows us to continue using these conditional flags without much headache Signed-off-by: Seth Flynn --- CMakeLists.txt | 32 ++++++++++++-------------------- launcher/CMakeLists.txt | 12 ++++++------ 2 files changed, 18 insertions(+), 26 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d0bea2da3..7e3959645 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -80,17 +80,13 @@ else() set(CMAKE_EXE_LINKER_FLAGS "-Wl,--stack,8388608 ${CMAKE_EXE_LINKER_FLAGS}") # Emit PDBs for WinDbg, etc. - if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo") - set(CMAKE_EXE_LINKER_FLAGS "-Wl,--pdb= ${CMAKE_EXE_LINKER_FLAGS}") - - foreach(lang C CXX) - set("CMAKE_${lang}_FLAGS" "-gcodeview ${CMAKE_${lang}_FLAGS}") - - # Force-enabling this to use generator expressions like TARGET_PDB_FILE - # (and because we can actually emit PDBs) - set("CMAKE_${lang}_LINKER_SUPPORTS_PDB" ON) - endforeach() - endif() + add_compile_options($<$,$>:-gcodeview>) + add_link_options($<$,$>:-Wl,--pdb=>) + foreach(lang C CXX) + # Force-enabling this to use generator expressions like TARGET_PDB_FILE + # (and because we can actually emit PDBs) + set("CMAKE_${lang}_LINKER_SUPPORTS_PDB" ON) + endforeach() # -ffunction-sections and -fdata-sections help reduce binary size # -mguard=cf enables Control Flow Guard @@ -116,30 +112,26 @@ endif() option(DEBUG_ADDRESS_SANITIZER "Enable Address Sanitizer in Debug builds" OFF) # If this is a Debug build turn on address sanitiser -if ((CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo") AND DEBUG_ADDRESS_SANITIZER) +if (DEBUG_ADDRESS_SANITIZER) message(STATUS "Address Sanitizer enabled for Debug builds, Turn it off with -DDEBUG_ADDRESS_SANITIZER=off") if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") if (CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC") # using clang with clang-cl front end message(STATUS "Address Sanitizer available on Clang MSVC frontend") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /fsanitize=address /Oy-") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /fsanitize=address /Oy-") + add_compile_options($<$,$>:/fsanitize=address /Oy->) else() # AppleClang and Clang message(STATUS "Address Sanitizer available on Clang") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer -fsanitize=undefined -fno-sanitize-recover=null") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer -fsanitize=undefined -fno-sanitize-recover=null") + add_compile_options($<$,$>:-fsanitize=address -fno-omit-frame-pointer -fsanitize=undefined -fno-sanitize-recover=null>) endif() elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") # GCC message(STATUS "Address Sanitizer available on GCC") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer -fsanitize=undefined -fno-sanitize-recover") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer -fsanitize=undefined -fno-sanitize-recover") + add_compile_options($<$,$>:-fsanitize=address -fno-omit-frame-pointer -fsanitize=undefined -fno-sanitize-recover=null>) link_libraries("asan") elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") message(STATUS "Address Sanitizer available on MSVC") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /fsanitize=address /Oy-") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /fsanitize=address /Oy-") + add_compile_options($<$,$>:/fsanitize=address /Oy->) else() message(STATUS "Address Sanitizer not available on compiler ${CMAKE_CXX_COMPILER_ID}") endif() diff --git a/launcher/CMakeLists.txt b/launcher/CMakeLists.txt index 02fc1321a..2b933be38 100644 --- a/launcher/CMakeLists.txt +++ b/launcher/CMakeLists.txt @@ -1412,8 +1412,8 @@ install(TARGETS ${Launcher_Name} ) # Deploy PDBs -if(WIN32 AND (CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")) - install(FILES $ DESTINATION ${BINARY_DEST_DIR}) +if(CMAKE_CXX_LINKER_SUPPORTS_PDB) + install(FILES $ DESTINATION ${BINARY_DEST_DIR} OPTIONAL) endif() if(Launcher_BUILD_UPDATER) @@ -1455,8 +1455,8 @@ if(Launcher_BUILD_UPDATER) ) # Deploy PDBs - if(WIN32 AND (CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")) - install(FILES $ DESTINATION ${BINARY_DEST_DIR}) + if(CMAKE_CXX_LINKER_SUPPORTS_PDB) + install(FILES $ DESTINATION ${BINARY_DEST_DIR} OPTIONAL) endif() endif() @@ -1506,8 +1506,8 @@ if(WIN32 OR (DEFINED Launcher_BUILD_FILELINKER AND Launcher_BUILD_FILELINKER)) ) # Deploy PDBs - if(WIN32 AND (CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")) - install(FILES $ DESTINATION ${BINARY_DEST_DIR}) + if(CMAKE_CXX_LINKER_SUPPORTS_PDB) + install(FILES $ DESTINATION ${BINARY_DEST_DIR} OPTIONAL) endif() endif()