Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
usFunctionCheckCompilerFlags.cmake
Go to the documentation of this file.
1 #
2 # Helper macro allowing to check if the given flags are supported
3 # by the underlying build tool
4 #
5 # If the flag(s) is/are supported, they will be appended to the string identified by RESULT_VAR
6 #
7 # Usage:
8 # usFunctionCheckCompilerFlags(FLAGS_TO_CHECK VALID_FLAGS_VAR)
9 #
10 # Example:
11 #
12 # set(myflags)
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})
19 #
20 # The output will be:
21 # 1-myflags: -fprofile-arcs
22 # 2-myflags: -fprofile-arcs
23 # 3-myflags: -fprofile-arcs -Wall
24 
25 include(CheckCXXCompilerFlag)
26 
27 function(usFunctionCheckCompilerFlags FLAG_TO_TEST RESULT_VAR)
28 
29  if(FLAG_TO_TEST STREQUAL "")
30  message(FATAL_ERROR "FLAG_TO_TEST shouldn't be empty")
31  endif()
32 
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}})
36 
37  # Clear all flags. If not, existing flags triggering warnings might lead to
38  # false-negatives when checking for certain compiler flags.
39  set(CMAKE_C_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 )
44  set(CMAKE_CXX_FLAGS )
45  set(CMAKE_CXX_FLAGS_DEBUG )
46  set(CMAKE_CXX_FLAGS_MINSIZEREL )
47  set(CMAKE_CXX_FLAGS_RELEASE )
48  set(CMAKE_CXX_FLAGS_RELWITHDEBINFO )
49 
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})
63 
64  if(HAS_CXX_FLAG_${suffix})
65  set(${RESULT_VAR} "${_saved_result_var} ${FLAG_TO_TEST}" PARENT_SCOPE)
66  endif()
67 
68 endfunction()
usFunctionCheckCompilerFlags(FLAG_TO_TEST, RESULT_VAR)