GenerateExportHeader.cmake 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. GenerateExportHeader
  5. --------------------
  6. Function for generation of export macros for libraries
  7. This module provides the function ``GENERATE_EXPORT_HEADER()``.
  8. The ``GENERATE_EXPORT_HEADER`` function can be used to generate a file
  9. suitable for preprocessor inclusion which contains EXPORT macros to be
  10. used in library classes::
  11. GENERATE_EXPORT_HEADER( LIBRARY_TARGET
  12. [BASE_NAME <base_name>]
  13. [EXPORT_MACRO_NAME <export_macro_name>]
  14. [EXPORT_FILE_NAME <export_file_name>]
  15. [DEPRECATED_MACRO_NAME <deprecated_macro_name>]
  16. [NO_EXPORT_MACRO_NAME <no_export_macro_name>]
  17. [INCLUDE_GUARD_NAME <include_guard_name>]
  18. [STATIC_DEFINE <static_define>]
  19. [NO_DEPRECATED_MACRO_NAME <no_deprecated_macro_name>]
  20. [DEFINE_NO_DEPRECATED]
  21. [PREFIX_NAME <prefix_name>]
  22. [CUSTOM_CONTENT_FROM_VARIABLE <variable>]
  23. )
  24. The target properties :prop_tgt:`CXX_VISIBILITY_PRESET <<LANG>_VISIBILITY_PRESET>`
  25. and :prop_tgt:`VISIBILITY_INLINES_HIDDEN` can be used to add the appropriate
  26. compile flags for targets. See the documentation of those target properties,
  27. and the convenience variables
  28. :variable:`CMAKE_CXX_VISIBILITY_PRESET <CMAKE_<LANG>_VISIBILITY_PRESET>` and
  29. :variable:`CMAKE_VISIBILITY_INLINES_HIDDEN`.
  30. By default ``GENERATE_EXPORT_HEADER()`` generates macro names in a file
  31. name determined by the name of the library. This means that in the
  32. simplest case, users of ``GenerateExportHeader`` will be equivalent to:
  33. .. code-block:: cmake
  34. set(CMAKE_CXX_VISIBILITY_PRESET hidden)
  35. set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)
  36. add_library(somelib someclass.cpp)
  37. generate_export_header(somelib)
  38. install(TARGETS somelib DESTINATION ${LIBRARY_INSTALL_DIR})
  39. install(FILES
  40. someclass.h
  41. ${PROJECT_BINARY_DIR}/somelib_export.h DESTINATION ${INCLUDE_INSTALL_DIR}
  42. )
  43. And in the ABI header files:
  44. .. code-block:: c++
  45. #include "somelib_export.h"
  46. class SOMELIB_EXPORT SomeClass {
  47. ...
  48. };
  49. The CMake fragment will generate a file in the
  50. ``${CMAKE_CURRENT_BINARY_DIR}`` called ``somelib_export.h`` containing the
  51. macros ``SOMELIB_EXPORT``, ``SOMELIB_NO_EXPORT``, ``SOMELIB_DEPRECATED``,
  52. ``SOMELIB_DEPRECATED_EXPORT`` and ``SOMELIB_DEPRECATED_NO_EXPORT``.
  53. They will be followed by content taken from the variable specified by
  54. the ``CUSTOM_CONTENT_FROM_VARIABLE`` option, if any.
  55. The resulting file should be installed with other headers in the library.
  56. The ``BASE_NAME`` argument can be used to override the file name and the
  57. names used for the macros:
  58. .. code-block:: cmake
  59. add_library(somelib someclass.cpp)
  60. generate_export_header(somelib
  61. BASE_NAME other_name
  62. )
  63. Generates a file called ``other_name_export.h`` containing the macros
  64. ``OTHER_NAME_EXPORT``, ``OTHER_NAME_NO_EXPORT`` and ``OTHER_NAME_DEPRECATED``
  65. etc.
  66. The ``BASE_NAME`` may be overridden by specifying other options in the
  67. function. For example:
  68. .. code-block:: cmake
  69. add_library(somelib someclass.cpp)
  70. generate_export_header(somelib
  71. EXPORT_MACRO_NAME OTHER_NAME_EXPORT
  72. )
  73. creates the macro ``OTHER_NAME_EXPORT`` instead of ``SOMELIB_EXPORT``, but
  74. other macros and the generated file name is as default:
  75. .. code-block:: cmake
  76. add_library(somelib someclass.cpp)
  77. generate_export_header(somelib
  78. DEPRECATED_MACRO_NAME KDE_DEPRECATED
  79. )
  80. creates the macro ``KDE_DEPRECATED`` instead of ``SOMELIB_DEPRECATED``.
  81. If ``LIBRARY_TARGET`` is a static library, macros are defined without
  82. values.
  83. If the same sources are used to create both a shared and a static
  84. library, the uppercased symbol ``${BASE_NAME}_STATIC_DEFINE`` should be
  85. used when building the static library:
  86. .. code-block:: cmake
  87. add_library(shared_variant SHARED ${lib_SRCS})
  88. add_library(static_variant ${lib_SRCS})
  89. generate_export_header(shared_variant BASE_NAME libshared_and_static)
  90. set_target_properties(static_variant PROPERTIES
  91. COMPILE_FLAGS -DLIBSHARED_AND_STATIC_STATIC_DEFINE)
  92. This will cause the export macros to expand to nothing when building
  93. the static library.
  94. If ``DEFINE_NO_DEPRECATED`` is specified, then a macro
  95. ``${BASE_NAME}_NO_DEPRECATED`` will be defined This macro can be used to
  96. remove deprecated code from preprocessor output:
  97. .. code-block:: cmake
  98. option(EXCLUDE_DEPRECATED "Exclude deprecated parts of the library" FALSE)
  99. if (EXCLUDE_DEPRECATED)
  100. set(NO_BUILD_DEPRECATED DEFINE_NO_DEPRECATED)
  101. endif()
  102. generate_export_header(somelib ${NO_BUILD_DEPRECATED})
  103. And then in somelib:
  104. .. code-block:: c++
  105. class SOMELIB_EXPORT SomeClass
  106. {
  107. public:
  108. #ifndef SOMELIB_NO_DEPRECATED
  109. SOMELIB_DEPRECATED void oldMethod();
  110. #endif
  111. };
  112. .. code-block:: c++
  113. #ifndef SOMELIB_NO_DEPRECATED
  114. void SomeClass::oldMethod() { }
  115. #endif
  116. If ``PREFIX_NAME`` is specified, the argument will be used as a prefix to
  117. all generated macros.
  118. For example:
  119. .. code-block:: cmake
  120. generate_export_header(somelib PREFIX_NAME VTK_)
  121. Generates the macros ``VTK_SOMELIB_EXPORT`` etc.
  122. ::
  123. ADD_COMPILER_EXPORT_FLAGS( [<output_variable>] )
  124. The ``ADD_COMPILER_EXPORT_FLAGS`` function adds ``-fvisibility=hidden`` to
  125. :variable:`CMAKE_CXX_FLAGS <CMAKE_<LANG>_FLAGS>` if supported, and is a no-op
  126. on Windows which does not need extra compiler flags for exporting support.
  127. You may optionally pass a single argument to ``ADD_COMPILER_EXPORT_FLAGS``
  128. that will be populated with the ``CXX_FLAGS`` required to enable visibility
  129. support for the compiler/architecture in use.
  130. This function is deprecated. Set the target properties
  131. :prop_tgt:`CXX_VISIBILITY_PRESET <<LANG>_VISIBILITY_PRESET>` and
  132. :prop_tgt:`VISIBILITY_INLINES_HIDDEN` instead.
  133. #]=======================================================================]
  134. include(CheckCCompilerFlag)
  135. include(CheckCXXCompilerFlag)
  136. # TODO: Install this macro separately?
  137. macro(_check_cxx_compiler_attribute _ATTRIBUTE _RESULT)
  138. check_cxx_source_compiles("${_ATTRIBUTE} int somefunc() { return 0; }
  139. int main() { return somefunc();}" ${_RESULT}
  140. )
  141. endmacro()
  142. # TODO: Install this macro separately?
  143. macro(_check_c_compiler_attribute _ATTRIBUTE _RESULT)
  144. check_c_source_compiles("${_ATTRIBUTE} int somefunc() { return 0; }
  145. int main() { return somefunc();}" ${_RESULT}
  146. )
  147. endmacro()
  148. macro(_test_compiler_hidden_visibility)
  149. if(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.2")
  150. set(GCC_TOO_OLD TRUE)
  151. elseif(CMAKE_COMPILER_IS_GNUCC AND CMAKE_C_COMPILER_VERSION VERSION_LESS "4.2")
  152. set(GCC_TOO_OLD TRUE)
  153. elseif(CMAKE_CXX_COMPILER_ID MATCHES Intel AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "12.0")
  154. set(_INTEL_TOO_OLD TRUE)
  155. endif()
  156. # Exclude XL here because it misinterprets -fvisibility=hidden even though
  157. # the check_cxx_compiler_flag passes
  158. if(NOT GCC_TOO_OLD
  159. AND NOT _INTEL_TOO_OLD
  160. AND NOT WIN32
  161. AND NOT CYGWIN
  162. AND NOT CMAKE_CXX_COMPILER_ID MATCHES XL
  163. AND NOT CMAKE_CXX_COMPILER_ID MATCHES PGI
  164. AND NOT CMAKE_CXX_COMPILER_ID MATCHES Watcom)
  165. if (CMAKE_CXX_COMPILER_LOADED)
  166. check_cxx_compiler_flag(-fvisibility=hidden COMPILER_HAS_HIDDEN_VISIBILITY)
  167. check_cxx_compiler_flag(-fvisibility-inlines-hidden
  168. COMPILER_HAS_HIDDEN_INLINE_VISIBILITY)
  169. else()
  170. check_c_compiler_flag(-fvisibility=hidden COMPILER_HAS_HIDDEN_VISIBILITY)
  171. check_c_compiler_flag(-fvisibility-inlines-hidden
  172. COMPILER_HAS_HIDDEN_INLINE_VISIBILITY)
  173. endif()
  174. endif()
  175. endmacro()
  176. macro(_test_compiler_has_deprecated)
  177. # NOTE: Some Embarcadero compilers silently compile __declspec(deprecated)
  178. # without error, but this is not a documented feature and the attribute does
  179. # not actually generate any warnings.
  180. if(CMAKE_CXX_COMPILER_ID MATCHES Borland
  181. OR CMAKE_CXX_COMPILER_ID MATCHES Embarcadero
  182. OR CMAKE_CXX_COMPILER_ID MATCHES HP
  183. OR GCC_TOO_OLD
  184. OR CMAKE_CXX_COMPILER_ID MATCHES PGI
  185. OR CMAKE_CXX_COMPILER_ID MATCHES Watcom)
  186. set(COMPILER_HAS_DEPRECATED "" CACHE INTERNAL
  187. "Compiler support for a deprecated attribute")
  188. else()
  189. if (CMAKE_CXX_COMPILER_LOADED)
  190. _check_cxx_compiler_attribute("__attribute__((__deprecated__))"
  191. COMPILER_HAS_DEPRECATED_ATTR)
  192. if(COMPILER_HAS_DEPRECATED_ATTR)
  193. set(COMPILER_HAS_DEPRECATED "${COMPILER_HAS_DEPRECATED_ATTR}"
  194. CACHE INTERNAL "Compiler support for a deprecated attribute")
  195. else()
  196. _check_cxx_compiler_attribute("__declspec(deprecated)"
  197. COMPILER_HAS_DEPRECATED)
  198. endif()
  199. else()
  200. _check_c_compiler_attribute("__attribute__((__deprecated__))"
  201. COMPILER_HAS_DEPRECATED_ATTR)
  202. if(COMPILER_HAS_DEPRECATED_ATTR)
  203. set(COMPILER_HAS_DEPRECATED "${COMPILER_HAS_DEPRECATED_ATTR}"
  204. CACHE INTERNAL "Compiler support for a deprecated attribute")
  205. else()
  206. _check_c_compiler_attribute("__declspec(deprecated)"
  207. COMPILER_HAS_DEPRECATED)
  208. endif()
  209. endif()
  210. endif()
  211. endmacro()
  212. get_filename_component(_GENERATE_EXPORT_HEADER_MODULE_DIR
  213. "${CMAKE_CURRENT_LIST_FILE}" PATH)
  214. macro(_DO_SET_MACRO_VALUES TARGET_LIBRARY)
  215. set(DEFINE_DEPRECATED)
  216. set(DEFINE_EXPORT)
  217. set(DEFINE_IMPORT)
  218. set(DEFINE_NO_EXPORT)
  219. if (COMPILER_HAS_DEPRECATED_ATTR)
  220. set(DEFINE_DEPRECATED "__attribute__ ((__deprecated__))")
  221. elseif(COMPILER_HAS_DEPRECATED)
  222. set(DEFINE_DEPRECATED "__declspec(deprecated)")
  223. endif()
  224. get_property(type TARGET ${TARGET_LIBRARY} PROPERTY TYPE)
  225. if(NOT ${type} STREQUAL "STATIC_LIBRARY")
  226. if(WIN32 OR CYGWIN)
  227. set(DEFINE_EXPORT "__declspec(dllexport)")
  228. set(DEFINE_IMPORT "__declspec(dllimport)")
  229. elseif(COMPILER_HAS_HIDDEN_VISIBILITY)
  230. set(DEFINE_EXPORT "__attribute__((visibility(\"default\")))")
  231. set(DEFINE_IMPORT "__attribute__((visibility(\"default\")))")
  232. set(DEFINE_NO_EXPORT "__attribute__((visibility(\"hidden\")))")
  233. endif()
  234. endif()
  235. endmacro()
  236. macro(_DO_GENERATE_EXPORT_HEADER TARGET_LIBRARY)
  237. # Option overrides
  238. set(options DEFINE_NO_DEPRECATED)
  239. set(oneValueArgs PREFIX_NAME BASE_NAME EXPORT_MACRO_NAME EXPORT_FILE_NAME
  240. DEPRECATED_MACRO_NAME NO_EXPORT_MACRO_NAME STATIC_DEFINE
  241. NO_DEPRECATED_MACRO_NAME CUSTOM_CONTENT_FROM_VARIABLE INCLUDE_GUARD_NAME)
  242. set(multiValueArgs)
  243. cmake_parse_arguments(_GEH "${options}" "${oneValueArgs}" "${multiValueArgs}"
  244. ${ARGN})
  245. set(BASE_NAME "${TARGET_LIBRARY}")
  246. if(_GEH_BASE_NAME)
  247. set(BASE_NAME ${_GEH_BASE_NAME})
  248. endif()
  249. string(TOUPPER ${BASE_NAME} BASE_NAME_UPPER)
  250. string(TOLOWER ${BASE_NAME} BASE_NAME_LOWER)
  251. # Default options
  252. set(EXPORT_MACRO_NAME "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_EXPORT")
  253. set(NO_EXPORT_MACRO_NAME "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_NO_EXPORT")
  254. set(EXPORT_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/${BASE_NAME_LOWER}_export.h")
  255. set(DEPRECATED_MACRO_NAME "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_DEPRECATED")
  256. set(STATIC_DEFINE "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_STATIC_DEFINE")
  257. set(NO_DEPRECATED_MACRO_NAME
  258. "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_NO_DEPRECATED")
  259. if(_GEH_UNPARSED_ARGUMENTS)
  260. message(FATAL_ERROR "Unknown keywords given to GENERATE_EXPORT_HEADER(): \"${_GEH_UNPARSED_ARGUMENTS}\"")
  261. endif()
  262. if(_GEH_EXPORT_MACRO_NAME)
  263. set(EXPORT_MACRO_NAME ${_GEH_PREFIX_NAME}${_GEH_EXPORT_MACRO_NAME})
  264. endif()
  265. string(MAKE_C_IDENTIFIER ${EXPORT_MACRO_NAME} EXPORT_MACRO_NAME)
  266. if(_GEH_EXPORT_FILE_NAME)
  267. if(IS_ABSOLUTE ${_GEH_EXPORT_FILE_NAME})
  268. set(EXPORT_FILE_NAME ${_GEH_EXPORT_FILE_NAME})
  269. else()
  270. set(EXPORT_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/${_GEH_EXPORT_FILE_NAME}")
  271. endif()
  272. endif()
  273. if(_GEH_DEPRECATED_MACRO_NAME)
  274. set(DEPRECATED_MACRO_NAME ${_GEH_PREFIX_NAME}${_GEH_DEPRECATED_MACRO_NAME})
  275. endif()
  276. string(MAKE_C_IDENTIFIER ${DEPRECATED_MACRO_NAME} DEPRECATED_MACRO_NAME)
  277. if(_GEH_NO_EXPORT_MACRO_NAME)
  278. set(NO_EXPORT_MACRO_NAME ${_GEH_PREFIX_NAME}${_GEH_NO_EXPORT_MACRO_NAME})
  279. endif()
  280. string(MAKE_C_IDENTIFIER ${NO_EXPORT_MACRO_NAME} NO_EXPORT_MACRO_NAME)
  281. if(_GEH_STATIC_DEFINE)
  282. set(STATIC_DEFINE ${_GEH_PREFIX_NAME}${_GEH_STATIC_DEFINE})
  283. endif()
  284. string(MAKE_C_IDENTIFIER ${STATIC_DEFINE} STATIC_DEFINE)
  285. if(_GEH_DEFINE_NO_DEPRECATED)
  286. set(DEFINE_NO_DEPRECATED 1)
  287. else()
  288. set(DEFINE_NO_DEPRECATED 0)
  289. endif()
  290. if(_GEH_NO_DEPRECATED_MACRO_NAME)
  291. set(NO_DEPRECATED_MACRO_NAME
  292. ${_GEH_PREFIX_NAME}${_GEH_NO_DEPRECATED_MACRO_NAME})
  293. endif()
  294. string(MAKE_C_IDENTIFIER ${NO_DEPRECATED_MACRO_NAME} NO_DEPRECATED_MACRO_NAME)
  295. if(_GEH_INCLUDE_GUARD_NAME)
  296. set(INCLUDE_GUARD_NAME ${_GEH_INCLUDE_GUARD_NAME})
  297. else()
  298. set(INCLUDE_GUARD_NAME "${EXPORT_MACRO_NAME}_H")
  299. endif()
  300. get_target_property(EXPORT_IMPORT_CONDITION ${TARGET_LIBRARY} DEFINE_SYMBOL)
  301. if(NOT EXPORT_IMPORT_CONDITION)
  302. set(EXPORT_IMPORT_CONDITION ${TARGET_LIBRARY}_EXPORTS)
  303. endif()
  304. string(MAKE_C_IDENTIFIER ${EXPORT_IMPORT_CONDITION} EXPORT_IMPORT_CONDITION)
  305. if(_GEH_CUSTOM_CONTENT_FROM_VARIABLE)
  306. if(DEFINED "${_GEH_CUSTOM_CONTENT_FROM_VARIABLE}")
  307. set(CUSTOM_CONTENT "${${_GEH_CUSTOM_CONTENT_FROM_VARIABLE}}")
  308. else()
  309. set(CUSTOM_CONTENT "")
  310. endif()
  311. endif()
  312. configure_file("${_GENERATE_EXPORT_HEADER_MODULE_DIR}/exportheader.cmake.in"
  313. "${EXPORT_FILE_NAME}" @ONLY)
  314. endmacro()
  315. function(GENERATE_EXPORT_HEADER TARGET_LIBRARY)
  316. get_property(type TARGET ${TARGET_LIBRARY} PROPERTY TYPE)
  317. if(NOT ${type} STREQUAL "STATIC_LIBRARY"
  318. AND NOT ${type} STREQUAL "SHARED_LIBRARY"
  319. AND NOT ${type} STREQUAL "OBJECT_LIBRARY"
  320. AND NOT ${type} STREQUAL "MODULE_LIBRARY")
  321. message(WARNING "This macro can only be used with libraries")
  322. return()
  323. endif()
  324. _test_compiler_hidden_visibility()
  325. _test_compiler_has_deprecated()
  326. _do_set_macro_values(${TARGET_LIBRARY})
  327. _do_generate_export_header(${TARGET_LIBRARY} ${ARGN})
  328. endfunction()
  329. function(add_compiler_export_flags)
  330. if(NOT CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS 2.8.12)
  331. message(DEPRECATION "The add_compiler_export_flags function is obsolete. Use the CXX_VISIBILITY_PRESET and VISIBILITY_INLINES_HIDDEN target properties instead.")
  332. endif()
  333. _test_compiler_hidden_visibility()
  334. _test_compiler_has_deprecated()
  335. option(USE_COMPILER_HIDDEN_VISIBILITY
  336. "Use HIDDEN visibility support if available." ON)
  337. mark_as_advanced(USE_COMPILER_HIDDEN_VISIBILITY)
  338. if(NOT (USE_COMPILER_HIDDEN_VISIBILITY AND COMPILER_HAS_HIDDEN_VISIBILITY))
  339. # Just return if there are no flags to add.
  340. return()
  341. endif()
  342. set (EXTRA_FLAGS "-fvisibility=hidden")
  343. if(COMPILER_HAS_HIDDEN_INLINE_VISIBILITY)
  344. set (EXTRA_FLAGS "${EXTRA_FLAGS} -fvisibility-inlines-hidden")
  345. endif()
  346. # Either return the extra flags needed in the supplied argument, or to the
  347. # CMAKE_CXX_FLAGS if no argument is supplied.
  348. if(ARGC GREATER 0)
  349. set(${ARGV0} "${EXTRA_FLAGS}" PARENT_SCOPE)
  350. else()
  351. string(APPEND CMAKE_CXX_FLAGS " ${EXTRA_FLAGS}")
  352. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" PARENT_SCOPE)
  353. endif()
  354. endfunction()