ctkFunctionGetCompilerVisibilityFlags.cmake 2.2 KB

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