1
0

GenerateExportHeader.cmake 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. # - Function for generation of export macros for libraries
  2. #
  3. # This module provides the function GENERATE_EXPORT_HEADER() and the
  4. # accompanying ADD_COMPILER_EXPORT_FLAGS() function.
  5. #
  6. # The GENERATE_EXPORT_HEADER function can be used to generate a file suitable
  7. # for preprocessor inclusion which contains EXPORT macros to be used in
  8. # library classes.
  9. #
  10. # GENERATE_EXPORT_HEADER( LIBRARY_TARGET
  11. # [BASE_NAME <base_name>]
  12. # [EXPORT_MACRO_NAME <export_macro_name>]
  13. # [EXPORT_FILE_NAME <export_file_name>]
  14. # [DEPRECATED_MACRO_NAME <deprecated_macro_name>]
  15. # [NO_EXPORT_MACRO_NAME <no_export_macro_name>]
  16. # [STATIC_DEFINE <static_define>]
  17. # )
  18. #
  19. # ADD_COMPILER_EXPORT_FLAGS( [FATAL_WARNINGS] )
  20. #
  21. # By default GENERATE_EXPORT_HEADER() generates macro names in a file name
  22. # determined by the name of the library. The ADD_COMPILER_EXPORT_FLAGS macro adds
  23. # -fvisibility=hidden to CMAKE_CXX_FLAGS if supported, and is a no-op on Windows
  24. # which does not need extra compiler flags for exporting support.
  25. #
  26. # This means that in the simplest case, users of these functions will be equivalent to:
  27. #
  28. # add_compiler_export_flags()
  29. #
  30. # add_library(somelib someclass.cpp)
  31. #
  32. # generate_export_header(somelib)
  33. #
  34. # install(TARGETS somelib DESTINATION ${LIBRARY_INSTALL_DIR})
  35. #
  36. # install(FILES
  37. # someclass.h
  38. # ${PROJECT_BINARY_DIR}/somelib_export.h DESTINATION ${INCLUDE_INSTALL_DIR}
  39. # )
  40. #
  41. # And in the ABI header files:
  42. #
  43. # \code
  44. # #include "somelib_export.h"
  45. #
  46. # class SOMELIB_EXPORT SomeClass {
  47. #
  48. # };
  49. # \endcode
  50. #
  51. # The CMake fragment will generate a file in the ${CMAKE_CURRENT_BUILD_DIR} called
  52. # somelib_export.h containing the macros SOMELIB_EXPORT, SOMELIB_NO_EXPORT,
  53. # SOMELIB_DEPRECATED, SOMELIB_DEPRECATED_EXPORT and SOMELIB_DEPRECATED_NO_EXPORT.
  54. # The resulting file should be installed with other headers in the library.
  55. #
  56. # The BASE_NAME argument can be used to override the file name and the names
  57. # used for the macros
  58. #
  59. # add_library(somelib someclass.cpp)
  60. # generate_export_header(somelib
  61. # BASE_NAME other_name
  62. # )
  63. #
  64. # Generates a file called other_name_export.h containing the macros
  65. # OTHER_NAME_EXPORT, OTHER_NAME_NO_EXPORT and OTHER_NAME_DEPRECATED etc.
  66. #
  67. # The BASE_NAME may be overridden by specifiying other options in the function.
  68. # For example:
  69. #
  70. # add_library(somelib someclass.cpp)
  71. # generate_export_header(somelib
  72. # EXPORT_MACRO_NAME OTHER_NAME_EXPORT
  73. # )
  74. #
  75. # creates the macro OTHER_NAME_EXPORT instead of SOMELIB_EXPORT, but other macros
  76. # and the generated file name is as default.
  77. #
  78. # add_library(somelib someclass.cpp)
  79. # generate_export_header(somelib
  80. # DEPRECATED_MACRO_NAME KDE_DEPRECATED
  81. # )
  82. #
  83. # creates the macro KDE_DEPRECATED instead of SOMELIB_DEPRECATED.
  84. #
  85. # If LIBRARY_TARGET is a static library, macros are defined without values.
  86. #
  87. # If the same sources are used to create both a shared and a static library, the
  88. # uppercased symbol ${BASE_NAME}_STATIC_DEFINE should be used when building the
  89. # static library
  90. #
  91. # add_library(shared_variant SHARED ${lib_SRCS})
  92. # add_library(static_variant ${lib_SRCS})
  93. #
  94. # generate_export_header(shared_variant BASE_NAME libshared_and_static)
  95. #
  96. # set_target_properties(static_variant PROPERTIES COMPILE_FLAGS -DLIBSHARED_AND_STATIC_STATIC_DEFINE)
  97. #
  98. # This will cause the export macros to expand to nothing when building the static library.
  99. #=============================================================================
  100. # Copyright 2011 Stephen Kelly <[email protected]>
  101. #
  102. # Distributed under the OSI-approved BSD License (the "License");
  103. # see accompanying file Copyright.txt for details.
  104. #
  105. # This software is distributed WITHOUT ANY WARRANTY; without even the
  106. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  107. # See the License for more information.
  108. #=============================================================================
  109. # (To distribute this file outside of CMake, substitute the full
  110. # License text for the above reference.)
  111. include(CMakeParseArguments)
  112. include(CheckCXXCompilerFlag)
  113. # TODO: Install this macro separately?
  114. macro(_check_cxx_compiler_attribute _ATTRIBUTE _RESULT)
  115. check_cxx_source_compiles("${_ATTRIBUTE} int somefunc() { return 0; } int main() { return somefunc();}" ${_RESULT}
  116. # Some compilers do not fail with a bad flag
  117. FAIL_REGEX "unrecognized .*option" # GNU
  118. FAIL_REGEX "ignoring unknown option" # MSVC
  119. FAIL_REGEX "warning D9002" # MSVC, any lang
  120. FAIL_REGEX "[Uu]nknown option" # HP
  121. FAIL_REGEX "[Ww]arning: [Oo]ption" # SunPro
  122. FAIL_REGEX "command option .* is not recognized" # XL
  123. )
  124. endmacro()
  125. macro(_test_compiler_hidden_visibility)
  126. if (CMAKE_COMPILER_IS_GNUCXX)
  127. exec_program(${CMAKE_C_COMPILER} ARGS --version OUTPUT_VARIABLE _gcc_version_info)
  128. string (REGEX MATCH "[345]\\.[0-9]\\.[0-9]" _gcc_version "${_gcc_version_info}")
  129. # gcc on mac just reports: "gcc (GCC) 3.3 20030304 ..." without the
  130. # patch level, handle this here:
  131. if(NOT _gcc_version)
  132. string (REGEX REPLACE ".*\\(GCC\\).* ([34]\\.[0-9]) .*" "\\1.0" _gcc_version "${_gcc_version_info}")
  133. endif()
  134. if(${_gcc_version} VERSION_LESS "4.2")
  135. set(GCC_TOO_OLD TRUE)
  136. message(WARNING "GCC version older than 4.2")
  137. endif()
  138. endif()
  139. if(CMAKE_CXX_COMPILER_ID MATCHES Intel)
  140. exec_program(${CMAKE_CXX_COMPILER} ARGS -V OUTPUT_VARIABLE _intel_version_info)
  141. string (REGEX REPLACE ".*Version ([0-9]+(\\.[0-9]+)+).*" "\\1" _intel_version "${_intel_version_info}")
  142. if(${_intel_version} VERSION_LESS "12.0")
  143. set(_INTEL_TOO_OLD TRUE)
  144. message(WARNING "Intel compiler older than 12.0")
  145. endif()
  146. endif()
  147. # Exclude XL here because it misinterprets -fvisibility=hidden even though
  148. # the check_cxx_compiler_flag passes
  149. # http://www.cdash.org/CDash/testDetails.php?test=109109951&build=1419259
  150. if (NOT GCC_TOO_OLD
  151. AND NOT _INTEL_TOO_OLD
  152. AND NOT WIN32
  153. AND NOT CYGWIN
  154. AND NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES XL
  155. AND NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES PGI
  156. AND NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES Watcom)
  157. check_cxx_compiler_flag(-fvisibility=hidden COMPILER_HAS_HIDDEN_VISIBILITY)
  158. check_cxx_compiler_flag(-fvisibility-inlines-hidden COMPILER_HAS_HIDDEN_INLINE_VISIBILITY)
  159. option(USE_COMPILER_HIDDEN_VISIBILITY "Use HIDDEN visibility support if available." ON)
  160. mark_as_advanced(USE_COMPILER_HIDDEN_VISIBILITY)
  161. endif()
  162. endmacro()
  163. macro(_test_compiler_has_deprecated)
  164. if("${CMAKE_CXX_COMPILER_ID}" MATCHES Borland
  165. OR "${CMAKE_CXX_COMPILER_ID}" MATCHES HP
  166. OR GCC_TOO_OLD
  167. OR "${CMAKE_CXX_COMPILER_ID}" MATCHES PGI
  168. OR "${CMAKE_CXX_COMPILER_ID}" MATCHES Watcom)
  169. set(COMPILER_HAS_DEPRECATED "" CACHE INTERNAL "Compiler support for a deprecated attribute")
  170. else()
  171. _check_cxx_compiler_attribute("__attribute__((__deprecated__))" COMPILER_HAS_DEPRECATED_ATTR)
  172. if(COMPILER_HAS_DEPRECATED_ATTR)
  173. set(COMPILER_HAS_DEPRECATED "${COMPILER_HAS_DEPRECATED_ATTR}" CACHE INTERNAL "Compiler support for a deprecated attribute")
  174. else()
  175. _check_cxx_compiler_attribute("__declspec(deprecated)" COMPILER_HAS_DEPRECATED)
  176. endif()
  177. endif()
  178. endmacro()
  179. set(myDir "${CMAKE_CURRENT_LIST_DIR}")
  180. macro(_DO_SET_MACRO_VALUES TARGET_LIBRARY)
  181. set(DEFINE_DEPRECATED)
  182. set(DEFINE_EXPORT)
  183. set(DEFINE_IMPORT)
  184. set(DEFINE_NO_EXPORT)
  185. if (COMPILER_HAS_DEPRECATED_ATTR)
  186. set(DEFINE_DEPRECATED "__attribute__ ((__deprecated__))")
  187. elseif(COMPILER_HAS_DEPRECATED)
  188. set(DEFINE_DEPRECATED "__declspec(deprecated)")
  189. endif()
  190. get_property(type TARGET ${TARGET_LIBRARY} PROPERTY TYPE)
  191. if(NOT ${type} STREQUAL "STATIC_LIBRARY")
  192. if(WIN32)
  193. set(DEFINE_EXPORT "__declspec(dllexport)")
  194. set(DEFINE_IMPORT "__declspec(dllimport)")
  195. elseif(COMPILER_HAS_HIDDEN_VISIBILITY AND USE_COMPILER_HIDDEN_VISIBILITY)
  196. set(DEFINE_EXPORT "__attribute__((visibility(\"default\")))")
  197. set(DEFINE_IMPORT "__attribute__((visibility(\"default\")))")
  198. set(DEFINE_NO_EXPORT "__attribute__((visibility(\"hidden\")))")
  199. endif()
  200. endif()
  201. endmacro()
  202. macro(_DO_GENERATE_EXPORT_HEADER TARGET_LIBRARY)
  203. # Option overrides
  204. set(options DEFINE_NO_DEPRECATED)
  205. set(oneValueArgs PREFIX_NAME BASE_NAME EXPORT_MACRO_NAME EXPORT_FILE_NAME DEPRECATED_MACRO_NAME NO_EXPORT_MACRO_NAME STATIC_DEFINE NO_DEPRECATED_MACRO_NAME)
  206. set(multiValueArgs)
  207. cmake_parse_arguments(_GEH "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  208. set(BASE_NAME "${TARGET_LIBRARY}")
  209. if(_GEH_BASE_NAME)
  210. set(BASE_NAME ${_GEH_BASE_NAME})
  211. endif()
  212. string(TOUPPER ${BASE_NAME} BASE_NAME_UPPER)
  213. string(TOLOWER ${BASE_NAME} BASE_NAME_LOWER)
  214. # Default options
  215. set(EXPORT_MACRO_NAME "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_EXPORT")
  216. set(NO_EXPORT_MACRO_NAME "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_NO_EXPORT")
  217. set(EXPORT_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/${BASE_NAME_LOWER}_export.h")
  218. set(DEPRECATED_MACRO_NAME "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_DEPRECATED")
  219. set(STATIC_DEFINE "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_STATIC_DEFINE")
  220. set(NO_DEPRECATED_MACRO_NAME "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_NO_DEPRECATED")
  221. if(_GEH_UNPARSED_ARGUMENTS)
  222. message(FATAL_ERROR "Unknown keywords given to GENERATE_EXPORT_HEADER(): \"${_GEH_UNPARSED_ARGUMENTS}\"")
  223. endif()
  224. if(_GEH_EXPORT_MACRO_NAME)
  225. set(EXPORT_MACRO_NAME ${_GEH_PREFIX_NAME}${_GEH_EXPORT_MACRO_NAME})
  226. endif()
  227. if(_GEH_EXPORT_FILE_NAME)
  228. if(IS_ABSOLUTE _GEH_EXPORT_FILE_NAME)
  229. set(EXPORT_FILE_NAME ${_GEH_EXPORT_FILE_NAME})
  230. else()
  231. set(EXPORT_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/${_GEH_EXPORT_FILE_NAME}")
  232. endif()
  233. endif()
  234. if(_GEH_DEPRECATED_MACRO_NAME)
  235. set(DEPRECATED_MACRO_NAME ${_GEH_PREFIX_NAME}${_GEH_DEPRECATED_MACRO_NAME})
  236. endif()
  237. if(_GEH_NO_EXPORT_MACRO_NAME)
  238. set(NO_EXPORT_MACRO_NAME ${_GEH_PREFIX_NAME}${_GEH_NO_EXPORT_MACRO_NAME})
  239. endif()
  240. if(_GEH_STATIC_DEFINE)
  241. set(STATIC_DEFINE ${_GEH_PREFIX_NAME}${_GEH_STATIC_DEFINE})
  242. endif()
  243. if (_GEH_DEFINE_NO_DEPRECATED)
  244. set(DEFINE_NO_DEPRECATED TRUE)
  245. endif()
  246. if (_GEH_NO_DEPRECATED_MACRO_NAME)
  247. set(NO_DEPRECATED_MACRO_NAME ${_GEH_PREFIX_NAME}${_GEH_NO_DEPRECATED_MACRO_NAME})
  248. endif()
  249. set(INCLUDE_GUARD_NAME "${EXPORT_MACRO_NAME}_H")
  250. get_target_property(EXPORT_IMPORT_CONDITION ${TARGET_LIBRARY} DEFINE_SYMBOL)
  251. if (NOT EXPORT_IMPORT_CONDITION)
  252. set(EXPORT_IMPORT_CONDITION ${TARGET_LIBRARY}_EXPORTS)
  253. endif()
  254. configure_file("${myDir}/exportheader.cmake.in" "${EXPORT_FILE_NAME}" @ONLY)
  255. endmacro()
  256. function(GENERATE_EXPORT_HEADER TARGET_LIBRARY)
  257. get_property(type TARGET ${TARGET_LIBRARY} PROPERTY TYPE)
  258. if(${type} STREQUAL "MODULE")
  259. message(WARNING "This macro should not be used with libraries of type MODULE")
  260. return()
  261. endif()
  262. if(NOT ${type} STREQUAL "STATIC_LIBRARY" AND NOT ${type} STREQUAL "SHARED_LIBRARY")
  263. message(WARNING "This macro can only be used with libraries")
  264. return()
  265. endif()
  266. _test_compiler_hidden_visibility()
  267. _test_compiler_has_deprecated()
  268. _do_set_macro_values(${TARGET_LIBRARY})
  269. _do_generate_export_header(${TARGET_LIBRARY} ${ARGN})
  270. endfunction()
  271. function(add_compiler_export_flags)
  272. _test_compiler_hidden_visibility()
  273. _test_compiler_has_deprecated()
  274. if(NOT (USE_COMPILER_HIDDEN_VISIBILITY AND COMPILER_HAS_HIDDEN_VISIBILITY))
  275. message(WARNING "Compiler doesn't have hidden visibility")
  276. return()
  277. endif()
  278. set (EXTRA_FLAGS "-fvisibility=hidden")
  279. if(COMPILER_HAS_HIDDEN_INLINE_VISIBILITY)
  280. set (EXTRA_FLAGS "${EXTRA_FLAGS} -fvisibility-inlines-hidden")
  281. endif()
  282. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_FLAGS}" PARENT_SCOPE)
  283. endfunction()