diff --git a/CMakeLists.txt b/CMakeLists.txt index ebe62caf49..8b695c3d1e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -79,6 +79,7 @@ list(APPEND CMAKE_MODULE_PATH # Support functions include(CheckAndAddFlag) include(CheckCCompilerFlag) +include(DolphinCompileDefinitions) # Libraries to link set(LIBS) @@ -193,6 +194,7 @@ endif() if(CMAKE_C_COMPILER_ID MATCHES "MSVC") check_and_add_flag(EXCEPTIONS /EHsc) + dolphin_compile_definitions(-D_DEBUG DEBUG_ONLY) # Only MSBuild needs this, other generators will compile one file at a time if(CMAKE_GENERATOR MATCHES "Visual Studio") @@ -226,6 +228,8 @@ else() check_and_add_flag(VISIBILITY_HIDDEN -fvisibility=hidden) check_and_add_flag(FOMIT_FRAME_POINTER -fomit-frame-pointer RELEASE_ONLY) + + dolphin_compile_definitions(_DEBUG DEBUG_ONLY) check_and_add_flag(GGDB -ggdb DEBUG_ONLY) if(NOT ANDROID AND _M_X86_64) @@ -334,7 +338,6 @@ endif() if(CMAKE_BUILD_TYPE STREQUAL "Debug") - add_definitions(-D_DEBUG) option(ENABLE_GPROF "Enable gprof profiling (must be using Debug build)" OFF) if(ENABLE_GPROF) diff --git a/CMakeTests/DolphinCompileDefinitions.cmake b/CMakeTests/DolphinCompileDefinitions.cmake new file mode 100644 index 0000000000..7368f921f0 --- /dev/null +++ b/CMakeTests/DolphinCompileDefinitions.cmake @@ -0,0 +1,34 @@ +# Add a C or C++ compile definitions to the current scope +# +# dolphin_compile_definitions(def [def ...] [DEBUG_ONLY | RELEASE_ONLY]) +# +# Can optionally add the definitions to Debug or Release configurations only, use this so we can +# target multi-configuration generators like Visual Studio or Xcode. +# Release configurations means NOT Debug, so it will work for RelWithDebInfo or MinSizeRel too. +# The definitions are added to the COMPILE_DEFINITIONS folder property. +# Supports generator expressions, unlike add_definitions() +# +# Examples: +# dolphin_compile_definitions(FOO) -> -DFOO +# dolphin_compile_definitions(_DEBUG DEBUG_ONLY) -> -D_DEBUG +# dolphin_compile_definitions(NDEBUG RELEASE_ONLY) -> -DNDEBUG +# dolphin_compile_definitions($<$:THISISONLYFORC>) + +function(dolphin_compile_definitions) + set(defs ${ARGN}) + + list(GET defs -1 last_def) + list(REMOVE_AT defs -1) + + set(genexp_config_test "1") + if(last_def STREQUAL "DEBUG_ONLY") + set(genexp_config_test "$") + elseif(last_def STREQUAL "RELEASE_ONLY") + set(genexp_config_test "$>") + else() + list(APPEND defs ${last_def}) + endif() + + set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS + "$<${genexp_config_test}:${ARGN}>") +endfunction()