primehack/CMake/CheckAndAddFlag.cmake
Michael Maltese 45d1f88ea7 CMake: move modules from CMakeTests/ -> CMake/
The name "CMakeTests" is inaccurate and irregular.
2017-02-07 22:55:27 -08:00

40 lines
1.4 KiB
CMake

include(CheckCCompilerFlag)
include(CheckCXXCompilerFlag)
# check_add_add_flag(<variable> <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_<variable> and FLAG_CXX_<variable>
# 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)
function(check_and_add_flag var flag)
set(genexp_config_test "1")
if(ARGV2 STREQUAL "DEBUG_ONLY")
set(genexp_config_test "$<CONFIG:Debug>")
elseif(ARGV2 STREQUAL "RELEASE_ONLY")
set(genexp_config_test "$<NOT:$<CONFIG:Debug>>")
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("$<$<AND:$<COMPILE_LANGUAGE:C>,${genexp_config_test}>:${flag}>")
endif()
check_cxx_compiler_flag(${flag} FLAG_CXX_${var})
if(FLAG_CXX_${var})
add_compile_options("$<$<AND:$<COMPILE_LANGUAGE:CXX>,${genexp_config_test}>:${flag}>")
endif()
endfunction()