2 # Helper macro allowing to check if the given flags are supported
3 # by the underlying build tool
5 # If the flag(s) is/are supported, they will be appended to the string identified by RESULT_VAR
8 # usFunctionCheckCompilerFlags(FLAGS_TO_CHECK VALID_FLAGS_VAR)
13 # usFunctionCheckCompilerFlags("-fprofile-arcs" myflags)
14 # message(1-myflags:${myflags})
15 # usFunctionCheckCompilerFlags("-fauto-bugfix" myflags)
16 # message(2-myflags:${myflags})
17 # usFunctionCheckCompilerFlags("-Wall" myflags)
18 # message(1-myflags:${myflags})
21 # 1-myflags: -fprofile-arcs
22 # 2-myflags: -fprofile-arcs
23 # 3-myflags: -fprofile-arcs -Wall
25 include(CheckCXXCompilerFlag)
29 if(FLAG_TO_TEST STREQUAL "")
30 message(FATAL_ERROR "FLAG_TO_TEST shouldn't be empty")
33 # Save the contents of RESULT_VAR temporarily.
34 # This is needed in case ${RESULT_VAR} is one of the CMAKE_<LANG>_FLAGS_* variables.
35 set(_saved_result_var ${${RESULT_VAR}})
37 # Clear all flags. If not, existing flags triggering warnings might lead to
38 #
false-negatives when checking
for certain compiler flags.
40 set(CMAKE_C_FLAGS_DEBUG )
41 set(CMAKE_C_FLAGS_MINSIZEREL )
42 set(CMAKE_C_FLAGS_RELEASE )
43 set(CMAKE_C_FLAGS_RELWITHDEBINFO )
45 set(CMAKE_CXX_FLAGS_DEBUG )
46 set(CMAKE_CXX_FLAGS_MINSIZEREL )
47 set(CMAKE_CXX_FLAGS_RELEASE )
48 set(CMAKE_CXX_FLAGS_RELWITHDEBINFO )
50 # Internally, the macro CMAKE_CXX_COMPILER_FLAG calls TRY_COMPILE. To avoid
51 # the cost of compiling the test each time the project is configured, the variable set by
52 # the macro is added to the cache so that following invocation of the macro with
53 # the same variable name skip the compilation step.
54 # For that same reason, the usFunctionCheckCompilerFlags function appends a unique suffix to
55 # the HAS_CXX_FLAG variable. This suffix is created using a 'clean version' of the
56 # flag to test. The value of HAS_CXX_FLAG_${suffix} additonally needs to be a valid
57 # pre-processor token because CHECK_CXX_COMPILER_FLAG adds it as a definition to the compiler
58 # arguments. An invalid token triggers compiler warnings, which in case of the "-Werror" flag
59 # leads to false-negative checks.
60 string(REGEX REPLACE
"[/-]" "_" suffix ${FLAG_TO_TEST})
61 string(REGEX REPLACE
"[, \\$\\+\\*\\{\\}\\(\\)\\#]" "" suffix ${suffix})
62 CHECK_CXX_COMPILER_FLAG(${FLAG_TO_TEST} HAS_CXX_FLAG_${suffix})
64 if(HAS_CXX_FLAG_${suffix})
65 set(${RESULT_VAR}
"${_saved_result_var} ${FLAG_TO_TEST}" PARENT_SCOPE)
usFunctionCheckCompilerFlags(FLAG_TO_TEST, RESULT_VAR)