My CMAKE_BUILD_TYPE checks are failing on Windows with MSVC
flags.cmake file included in every single one to keep things consistent.The logic seems straightforward: check the compiler ID, check the build type, and append the flags. Here is the snippet that's causing me grief:
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++2a -fcoroutines -fvisibility=hidden -fconcepts-diagnostics-depth=3 -Wno-attributes -Wall -Wextra -Wpedantic")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++2a -fcoroutines -fvisibility=hidden")
endif()
endif()
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS OFF)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc /GR /utf-8 /fsanitize=address")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc /GR /utf-8")
endif()
endif()
set(global_defines -Dqor_pp_unicode)The problem is that if(CMAKE_BUILD_TYPE STREQUAL "Debug") is essentially being ignored on Windows. I'm seeing /fsanitize=address leaking into my non-Debug builds, which is a nightmare for performance and stability. From what I can gather, this is a known friction point between CMake, Ninja, and Visual Studio, and apparently, no one is rushing to fix it.
I've tried using LLMs to help me migrate to Generator Expressions since they are supposed to handle multi-config generators properly, but the AI keeps hallucinating the syntax. Every example it gives me is slightly off or fails to compile. The official CMake documentation for Generator Expressions is a mountain of text, but it's surprisingly hard to find a concrete, real-world example of using them specifically to toggle compiler flags based on the configuration.
I've also heard about CMakePresets.json, but that feels like a massive migration task. If I move to presets, do I have to touch all 250+ sub-projects, or can I handle this globally?
I need a robust AI workflow for my build system that ensures flags for Debug, RelWithDebugInfo, and Release are strictly enforced across GCC, Clang, and MSVC without having to manually edit hundreds of files. I'm looking for the "correct" modern CMake way to do this.
If you're thinking of suggesting a total switch to something like SCons or Jam, I'm specifically looking for a CMake-based solution right now to avoid a total infrastructure rewrite.