I'm looking for the right way to set Compiler flags, per build type, with a multi platform build. In this case targeting Windows, MSVC is the part that's failing.
I currently include this flags.cmake into every subproject
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)
And I have a lot of sub projects, 250+
The issue is that the if(CMAKE_BUILD_TYPE STREQUAL "Debug") line is effectively ignored and I get /fsanitize=address in non Debug builds.
This is apparently a known issue for which CMake blames Ninja and Visual Studio. i.e. they aren't going to fix it.
AI assisted search tells me I can use a Generator Expression to do the same thing and it will work. However it can't provide me with a working example or get the Generator Expression syntax right, even once. There clearly aren't a lot of examples out there.
CMake's documentation for Generator Expressions is massive and seems to show how to do everything except actually use them to set compiler flags.
The other alternative is apparently a CMakePresets.json which is yet another separate DSL that looks like I'd need to change all 250 sub projects. Is this the case?
My question is what is the right route to go?
I need to be able to set specific Debug, RelWithDebugInfo and Release flags for GCC & Clang on Linux and MSVC & Clang on Windows.
I need them forcibly consistent across all 250+ sub projects, so set in one place.
P.S. If your answer is use Basel instead then you're welcome to raise the working Basel build scripts as a PR on (QOR Github)[https://github.com/mfaithfull/linuxQOR]. Same with Jam or Scons or whatever. The CMake question stands.
Top comments (0)