CTK  0.1.0
The Common Toolkit is a community effort to provide support code for medical image analysis, surgical navigation, and related projects.
ctkFunctionGetCompilerVisibilityFlags.cmake
Go to the documentation of this file.
1 
2 
3 #!
4 #! \brief Helper macro which appends gcc compatible visibility flags to the
5 #! variable given by RESULT_VAR.
6 #!
7 #! If supported, the flags -fvisibility=hidden and -fvisibility-inlines-hidden
8 #! will be added. This applies to gcc >= 4.5 and Clang.
9 #!
10 #! Usage:
11 #! ctkFunctionGetCompilerVisibilityFlags(RESULT_VAR)
12 #!
13 #! Example:
14 #!
15 #! \code
16 #! set(myflags "-Werror")
17 #! ctkFunctionGetCompilerVisibilityFlags(myflags)
18 #! \endcode
19 #!
20 #! The variable \emph myflags will contain the string "-Werror -fvisibility -fvisibility-inlines-hidden"
21 #! if for example gcc 4.6 is used.
22 #!
23 #! \ingroup CMakeUtilities
24 function(ctkFunctionGetCompilerVisibilityFlags RESULT_VAR)
25 
26  # We only support hidden visibility for gcc for now. Clang 3.0 still has troubles with
27  # correctly marking template declarations and explicit template instantiations as exported.
28  # See http://comments.gmane.org/gmane.comp.compilers.clang.scm/50028
29  # and http://llvm.org/bugs/show_bug.cgi?id=10113
30  set(use_visibility_flags 0)
31 
32  if(CMAKE_COMPILER_IS_GNUCXX)
33 
34  set(use_visibility_flags 1)
35  ctkFunctionGetGccVersion(${CMAKE_CXX_COMPILER} GCC_VERSION)
36 
37  # MinGW does not export all symbols automatically, so no need to set flags.
38  #
39  # With gcc < 4.5, RTTI symbols from classes declared in third-party libraries
40  # which are not "gcc visibility aware" are marked with hidden visibility in
41  # DSOs which include the class declaration and which are compiled with
42  # hidden visibility. This leads to dynamic_cast and exception handling problems.
43  # While this problem could be worked around by sandwiching the include
44  # directives for the third-party headers between "#pragma visibility push/pop"
45  # statements, it is generally safer to just use default visibility with
46  # gcc < 4.5.
47 
48  if(${GCC_VERSION} VERSION_LESS "4.5" OR MINGW)
49  set(use_visibility_flags 0)
50  endif()
51 
52  endif()
53 
54  if(use_visibility_flags)
55  set(visibility_flags "")
56  ctkFunctionCheckCompilerFlags("-fvisibility=hidden" visibility_flags)
57  ctkFunctionCheckCompilerFlags("-fvisibility-inlines-hidden" visibility_flags)
58  set(${RESULT_VAR} "${${RESULT_VAR}} ${visibility_flags}" PARENT_SCOPE)
59  endif()
60 
61 endfunction()
62