GenerateExportHeader.cmake 15 KB

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