From 00c15d84d63dc69468fe2a4810da55669fd9d7b7 Mon Sep 17 00:00:00 2001 From: Florent Castelli Date: Wed, 25 Jan 2017 15:04:00 +0100 Subject: [PATCH] cmake: check_and_add_flag supports adding to Debug or Release only --- CMakeTests/CheckAndAddFlag.cmake | 45 +++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/CMakeTests/CheckAndAddFlag.cmake b/CMakeTests/CheckAndAddFlag.cmake index 34ab8e60b1..9494664e0a 100644 --- a/CMakeTests/CheckAndAddFlag.cmake +++ b/CMakeTests/CheckAndAddFlag.cmake @@ -1,14 +1,39 @@ include(CheckCCompilerFlag) include(CheckCXXCompilerFlag) -macro(check_and_add_flag var flag) - check_c_compiler_flag(${flag} FLAG_C_${var}) - if(FLAG_C_${var}) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}") - endif() +# check_add_add_flag( [DEBUG_ONLY | RELEASE_ONLY]) +# +# Add a C or C++ compilation flag to the current scope +# +# Can optionally add the flag to Debug or Release configurations only, use this when +# targeting multi-configuration generators like Visual Studio or Xcode. +# Release configurations means NOT Debug, so it will work for RelWithDebInfo or MinSizeRel too. +# +# If the flag is added successfully, the variables FLAG_C_ and FLAG_CXX_ +# may be set to ON. +# +# Examples: +# check_and_add_flag(FOO -foo) +# check_and_add_flag(ONLYDEBUG -onlydebug DEBUG_ONLY) +# check_and_add_flag(OPTMAX -O9001 RELEASE_ONLY) - check_cxx_compiler_flag(${flag} FLAG_CXX_${var}) - if(FLAG_CXX_${var}) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}") - endif() -endmacro() +function(check_and_add_flag var flag) + set(genexp_config_test "1") + if(ARGV2 STREQUAL "DEBUG_ONLY") + set(genexp_config_test "$") + elseif(ARGV2 STREQUAL "RELEASE_ONLY") + set(genexp_config_test "$>") + elseif(ARGV2) + message(FATAL_ERROR "check_and_add_flag called with incorrect arguments: ${ARGN}") + endif() + + check_c_compiler_flag(${flag} FLAG_C_${var}) + if(FLAG_C_${var}) + add_compile_options("$<$,${genexp_config_test}>:${flag}>") + endif() + + check_cxx_compiler_flag(${flag} FLAG_CXX_${var}) + if(FLAG_CXX_${var}) + add_compile_options("$<$,${genexp_config_test}>:${flag}>") + endif() +endfunction()