diff --git a/cmake/modules/AddCompilerFlags.cmake b/cmake/modules/AddCompilerFlags.cmake --- a/cmake/modules/AddCompilerFlags.cmake +++ b/cmake/modules/AddCompilerFlags.cmake @@ -44,6 +44,29 @@ endforeach() endmacro() +function(add_cxx_compiler_flag_with_fallback TARGET_VAR FLAG FALLBACK) + # Remove the fallback flag if it exists, so that the main flag will override + # it if it was previously added. + string(REGEX REPLACE "${FALLBACK}( |$)" "" ${TARGET_VAR} "${${TARGET_VAR}}") + + set(TMP_FLAG ${FLAG}) + unset(FLAG_IS_SUPPORTED CACHE) + CHECK_CXX_COMPILER_FLAG(${TMP_FLAG} FLAG_IS_SUPPORTED) + if(NOT FLAG_IS_SUPPORTED) + set(TMP_FLAG ${FALLBACK}) + unset(FLAG_IS_SUPPORTED CACHE) + CHECK_CXX_COMPILER_FLAG(${TMP_FLAG} FLAG_IS_SUPPORTED) + endif() + + if(FLAG_IS_SUPPORTED) + if(NOT "${${TARGET_VAR}}" STREQUAL "") + string(APPEND ${TARGET_VAR} " ") + endif() + string(APPEND ${TARGET_VAR} "${TMP_FLAG}") + set(${TARGET_VAR} ${${TARGET_VAR}} PARENT_SCOPE) + endif() +endfunction() + # Note that CMake does not provide any facility to check that a linker flag is # supported by the compiler, but most linker will just drop any unsupported flag # (eventually with a warning). diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -24,6 +24,18 @@ include(AddCompilerFlags) remove_compiler_flags(-DNDEBUG) +# Overrides the flags for the Debug build type +# Prefer -g3, defaults to -g if unavailable +add_cxx_compiler_flag_with_fallback(DEBUG_G_CXX_FLAG -g3 -g) + +# Prefer -Og, defaults to -O0 if unavailable +add_cxx_compiler_flag_with_fallback(DEBUG_O_CXX_FLAG -Og -O0) + +set(CMAKE_C_FLAGS_DEBUG "${DEFAULT_G_C_FLAG} ${DEFAULT_O_C_FLAG}") +set(CMAKE_CXX_FLAGS_DEBUG "${DEBUG_G_CXX_FLAG} ${DEBUG_O_CXX_FLAG}") +# Define the debugging symbols DEBUG and DEBUG_LOCKORDER +set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUG -DDEBUG_LOCKORDER") + # Ensure that WINDRES_PREPROC is enabled when using windres. if(${CMAKE_SYSTEM_NAME} MATCHES "Windows") list(APPEND CMAKE_RC_FLAGS "-DWINDRES_PREPROC")