2014-07-31 22:20:20 +08:00
|
|
|
# - Adds a compiler flag if it is supported by the compiler
|
2014-08-01 21:24:56 +08:00
|
|
|
#
|
2014-07-31 22:20:20 +08:00
|
|
|
# This function checks that the supplied compiler flag is supported and then
|
|
|
|
# adds it to the corresponding compiler flags
|
2014-08-01 21:24:56 +08:00
|
|
|
#
|
2014-07-31 22:20:20 +08:00
|
|
|
# add_cxx_compiler_flag(<FLAG> [<VARIANT>])
|
2014-08-01 21:24:56 +08:00
|
|
|
#
|
|
|
|
# - Example
|
|
|
|
#
|
|
|
|
# include(AddCXXCompilerFlag)
|
2014-07-31 22:20:20 +08:00
|
|
|
# add_cxx_compiler_flag(-Wall)
|
|
|
|
# add_cxx_compiler_flag(-no-strict-aliasing RELEASE)
|
2014-08-01 21:24:56 +08:00
|
|
|
# Requires CMake 2.6+
|
|
|
|
|
2014-07-31 22:20:20 +08:00
|
|
|
if(__add_cxx_compiler_flag)
|
2014-08-01 21:24:56 +08:00
|
|
|
return()
|
|
|
|
endif()
|
2014-07-31 22:20:20 +08:00
|
|
|
set(__add_cxx_compiler_flag INCLUDED)
|
2014-08-01 21:24:56 +08:00
|
|
|
|
|
|
|
include(CheckCXXCompilerFlag)
|
|
|
|
|
2017-03-28 15:43:42 +08:00
|
|
|
function(mangle_compiler_flag FLAG OUTPUT)
|
2015-04-30 21:04:28 +08:00
|
|
|
string(TOUPPER "HAVE_CXX_FLAG_${FLAG}" SANITIZED_FLAG)
|
|
|
|
string(REPLACE "+" "X" SANITIZED_FLAG ${SANITIZED_FLAG})
|
2014-08-22 21:36:19 +08:00
|
|
|
string(REGEX REPLACE "[^A-Za-z_0-9]" "_" SANITIZED_FLAG ${SANITIZED_FLAG})
|
|
|
|
string(REGEX REPLACE "_+" "_" SANITIZED_FLAG ${SANITIZED_FLAG})
|
2017-03-28 15:43:42 +08:00
|
|
|
set(${OUTPUT} "${SANITIZED_FLAG}" PARENT_SCOPE)
|
|
|
|
endfunction(mangle_compiler_flag)
|
|
|
|
|
|
|
|
function(add_cxx_compiler_flag FLAG)
|
|
|
|
mangle_compiler_flag("${FLAG}" MANGLED_FLAG)
|
|
|
|
set(OLD_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
|
|
|
|
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${FLAG}")
|
|
|
|
check_cxx_compiler_flag("${FLAG}" ${MANGLED_FLAG})
|
|
|
|
set(CMAKE_REQUIRED_FLAGS "${OLD_CMAKE_REQUIRED_FLAGS}")
|
|
|
|
if(${MANGLED_FLAG})
|
2021-04-26 16:15:07 +08:00
|
|
|
if(ARGC GREATER 1)
|
|
|
|
set(VARIANT ${ARGV1})
|
2015-04-30 21:04:28 +08:00
|
|
|
string(TOUPPER "_${VARIANT}" VARIANT)
|
2021-04-26 16:15:07 +08:00
|
|
|
else()
|
|
|
|
set(VARIANT "")
|
2015-04-30 21:04:28 +08:00
|
|
|
endif()
|
2017-08-29 07:10:19 +08:00
|
|
|
set(CMAKE_CXX_FLAGS${VARIANT} "${CMAKE_CXX_FLAGS${VARIANT}} ${BENCHMARK_CXX_FLAGS${VARIANT}} ${FLAG}" PARENT_SCOPE)
|
2014-08-01 21:24:56 +08:00
|
|
|
endif()
|
|
|
|
endfunction()
|
|
|
|
|
2017-03-28 15:43:42 +08:00
|
|
|
function(add_required_cxx_compiler_flag FLAG)
|
|
|
|
mangle_compiler_flag("${FLAG}" MANGLED_FLAG)
|
|
|
|
set(OLD_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
|
|
|
|
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${FLAG}")
|
|
|
|
check_cxx_compiler_flag("${FLAG}" ${MANGLED_FLAG})
|
|
|
|
set(CMAKE_REQUIRED_FLAGS "${OLD_CMAKE_REQUIRED_FLAGS}")
|
|
|
|
if(${MANGLED_FLAG})
|
2021-04-26 16:15:07 +08:00
|
|
|
if(ARGC GREATER 1)
|
|
|
|
set(VARIANT ${ARGV1})
|
2017-03-28 15:43:42 +08:00
|
|
|
string(TOUPPER "_${VARIANT}" VARIANT)
|
2021-04-26 16:15:07 +08:00
|
|
|
else()
|
|
|
|
set(VARIANT "")
|
2017-03-28 15:43:42 +08:00
|
|
|
endif()
|
|
|
|
set(CMAKE_CXX_FLAGS${VARIANT} "${CMAKE_CXX_FLAGS${VARIANT}} ${FLAG}" PARENT_SCOPE)
|
|
|
|
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${FLAG}" PARENT_SCOPE)
|
|
|
|
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${FLAG}" PARENT_SCOPE)
|
|
|
|
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${FLAG}" PARENT_SCOPE)
|
|
|
|
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${FLAG}" PARENT_SCOPE)
|
|
|
|
else()
|
|
|
|
message(FATAL_ERROR "Required flag '${FLAG}' is not supported by the compiler")
|
|
|
|
endif()
|
|
|
|
endfunction()
|
2018-02-15 04:44:41 +08:00
|
|
|
|
|
|
|
function(check_cxx_warning_flag FLAG)
|
|
|
|
mangle_compiler_flag("${FLAG}" MANGLED_FLAG)
|
|
|
|
set(OLD_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
|
|
|
|
# Add -Werror to ensure the compiler generates an error if the warning flag
|
|
|
|
# doesn't exist.
|
|
|
|
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror ${FLAG}")
|
|
|
|
check_cxx_compiler_flag("${FLAG}" ${MANGLED_FLAG})
|
|
|
|
set(CMAKE_REQUIRED_FLAGS "${OLD_CMAKE_REQUIRED_FLAGS}")
|
|
|
|
endfunction()
|