CTK 0.1.0
The Common Toolkit is a community effort to provide support code for medical image analysis, surgical navigation, and related projects.
ctkFunctionCheckCompilerFlags.cmake
Go to the documentation of this file.
1
2
3#
4# Helper macro allowing to check if the given flags are supported
5# by the underlying build tool
6#
7# If the flag(s) is/are supported, they will be appended to the string identified by RESULT_VAR
8#
9# Usage:
10# ctkFunctionCheckCompilerFlags(FLAGS_TO_CHECK VALID_FLAGS_VAR)
11#
12# Example:
13#
14# set(myflags)
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})
21#
22# The output will be:
23# 1-myflags: -fprofile-arcs
24# 2-myflags: -fprofile-arcs
25# 3-myflags: -fprofile-arcs -Wall
26
27include(TestCXXAcceptsFlag)
28
29#! \ingroup CMakeUtilities
30function(ctkFunctionCheckCompilerFlags CXX_FLAG_TO_TEST RESULT_VAR)
31
32 if(CXX_FLAG_TO_TEST STREQUAL "")
33 message(FATAL_ERROR "CXX_FLAG_TO_TEST shouldn't be empty")
34 endif()
35
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})
44
45 if(HAS_FLAG_${suffix})
46 set(${RESULT_VAR} "${${RESULT_VAR}} ${CXX_FLAG_TO_TEST}" PARENT_SCOPE)
47 endif()
48
49endfunction()
50