4 # Helper macro allowing to check if the given flags are supported
5 # by the underlying build tool
7 # If the flag(s) is/are supported, they will be appended to the string identified by RESULT_VAR
10 # ctkFunctionCheckCompilerFlags(FLAGS_TO_CHECK VALID_FLAGS_VAR)
15 # ctkFunctionCheckCompilerFlags("-fprofile-arcs" myflags)
16 # message(1-myflags:${myflags})
17 # ctkFunctionCheckCompilerFlags("-fauto-bugfix" myflags)
18 # message(2-myflags:${myflags})
19 # ctkFunctionCheckCompilerFlags("-Wall" myflags)
20 # message(1-myflags:${myflags})
23 # 1-myflags: -fprofile-arcs
24 # 2-myflags: -fprofile-arcs
25 # 3-myflags: -fprofile-arcs -Wall
27 include(TestCXXAcceptsFlag)
29 #! \ingroup CMakeUtilities
30 function(ctkFunctionCheckCompilerFlags CXX_FLAG_TO_TEST RESULT_VAR)
32 if(CXX_FLAG_TO_TEST STREQUAL "")
33 message(FATAL_ERROR "CXX_FLAG_TO_TEST shouldn't be empty")
36 # Internally, the macro CMAKE_CXX_ACCEPTS_FLAG calls TRY_COMPILE. To avoid
37 # the cost of compiling the test each time the project is configured, the variable set by
38 # the macro is added to the cache so that following invocation of the macro with
39 # the same variable name skip the compilation step.
40 # For that same reason, ctkFunctionCheckCompilerFlags function appends a unique suffix to
41 # the HAS_FLAG variable. This suffix is created using a 'clean version' of the flag to test.
42 string(REGEX REPLACE "-\\s\\$\\+\\*\\{\\}\\(\\)\\#" "" suffix ${CXX_FLAG_TO_TEST})
43 CHECK_CXX_ACCEPTS_FLAG(${CXX_FLAG_TO_TEST} HAS_FLAG_${suffix})
45 if(HAS_FLAG_${suffix})
46 set(${RESULT_VAR} "${${RESULT_VAR}} ${CXX_FLAG_TO_TEST}" PARENT_SCOPE)