diff --git a/cmake/modules/AddCompilerFlags.cmake b/cmake/modules/AddCompilerFlags.cmake --- a/cmake/modules/AddCompilerFlags.cmake +++ b/cmake/modules/AddCompilerFlags.cmake @@ -74,6 +74,24 @@ remove_cxx_compiler_flags(${ARGN}) 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. + remove_cxx_compiler_flags(${FALLBACK}) + + set(FLAG_CANDIDATE ${FLAG}) + check_compiler_flag(FLAG_IS_SUPPORTED CXX ${FLAG_CANDIDATE}) + if(NOT ${FLAG_IS_SUPPORTED}) + set(FLAG_CANDIDATE ${FALLBACK}) + check_compiler_flag(FLAG_IS_SUPPORTED CXX ${FLAG_CANDIDATE}) + endif() + + if(${FLAG_IS_SUPPORTED}) + string(APPEND ${TARGET_VAR} " ${FLAG_CANDIDATE}") + 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. # However since CMake 3.2 introduced the CMP0056 policy, the diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -25,6 +25,18 @@ include(AddCompilerFlags) remove_compiler_flags(-DNDEBUG) +# Overrides the flags for the Debug build type +# This mimics the autotools behavior by setting the CFLAGS to '-g -O2`, which +# are not well suited for debugging. +# FIXME: update CFLAGS with better debug oriented optimization flags +set(CMAKE_C_FLAGS_DEBUG "-g -O2") + +# Prefer -g3, defaults to -g if unavailable +add_cxx_compiler_flag_with_fallback(CMAKE_CXX_FLAGS_DEBUG -g3 -g) + +# Prefer -Og, defaults to -O0 if unavailable +add_cxx_compiler_flag_with_fallback(CMAKE_CXX_FLAGS_DEBUG -Og -O0) + # Ensure that WINDRES_PREPROC is enabled when using windres. if(${CMAKE_SYSTEM_NAME} MATCHES "Windows") list(APPEND CMAKE_RC_FLAGS "-DWINDRES_PREPROC")