GenerateExportHeader.cmake 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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(CheckCXXCompilerFlag)
  187. # TODO: Install this macro separately?
  188. macro(_check_cxx_compiler_attribute _ATTRIBUTE _RESULT)
  189. check_cxx_source_compiles("${_ATTRIBUTE} int somefunc() { return 0; }
  190. int main() { return somefunc();}" ${_RESULT}
  191. )
  192. endmacro()
  193. macro(_test_compiler_hidden_visibility)
  194. if(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.2")
  195. set(GCC_TOO_OLD TRUE)
  196. elseif(CMAKE_COMPILER_IS_GNUCC AND CMAKE_C_COMPILER_VERSION VERSION_LESS "4.2")
  197. set(GCC_TOO_OLD TRUE)
  198. elseif(CMAKE_CXX_COMPILER_ID MATCHES Intel AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "12.0")
  199. set(_INTEL_TOO_OLD TRUE)
  200. endif()
  201. # Exclude XL here because it misinterprets -fvisibility=hidden even though
  202. # the check_cxx_compiler_flag passes
  203. if(NOT GCC_TOO_OLD
  204. AND NOT _INTEL_TOO_OLD
  205. AND NOT WIN32
  206. AND NOT CYGWIN
  207. AND NOT CMAKE_CXX_COMPILER_ID MATCHES XL
  208. AND NOT CMAKE_CXX_COMPILER_ID MATCHES PGI
  209. AND NOT CMAKE_CXX_COMPILER_ID MATCHES Watcom)
  210. check_cxx_compiler_flag(-fvisibility=hidden COMPILER_HAS_HIDDEN_VISIBILITY)
  211. check_cxx_compiler_flag(-fvisibility-inlines-hidden
  212. COMPILER_HAS_HIDDEN_INLINE_VISIBILITY)
  213. endif()
  214. endmacro()
  215. macro(_test_compiler_has_deprecated)
  216. # NOTE: Some Embarcadero compilers silently compile __declspec(deprecated)
  217. # without error, but this is not a documented feature and the attribute does
  218. # not actually generate any warnings.
  219. if(CMAKE_CXX_COMPILER_ID MATCHES Borland
  220. OR CMAKE_CXX_COMPILER_ID MATCHES Embarcadero
  221. OR CMAKE_CXX_COMPILER_ID MATCHES HP
  222. OR GCC_TOO_OLD
  223. OR CMAKE_CXX_COMPILER_ID MATCHES PGI
  224. OR CMAKE_CXX_COMPILER_ID MATCHES Watcom)
  225. set(COMPILER_HAS_DEPRECATED "" CACHE INTERNAL
  226. "Compiler support for a deprecated attribute")
  227. else()
  228. _check_cxx_compiler_attribute("__attribute__((__deprecated__))"
  229. COMPILER_HAS_DEPRECATED_ATTR)
  230. if(COMPILER_HAS_DEPRECATED_ATTR)
  231. set(COMPILER_HAS_DEPRECATED "${COMPILER_HAS_DEPRECATED_ATTR}"
  232. CACHE INTERNAL "Compiler support for a deprecated attribute")
  233. else()
  234. _check_cxx_compiler_attribute("__declspec(deprecated)"
  235. COMPILER_HAS_DEPRECATED)
  236. endif()
  237. endif()
  238. endmacro()
  239. get_filename_component(_GENERATE_EXPORT_HEADER_MODULE_DIR
  240. "${CMAKE_CURRENT_LIST_FILE}" PATH)
  241. macro(_DO_SET_MACRO_VALUES TARGET_LIBRARY)
  242. set(DEFINE_DEPRECATED)
  243. set(DEFINE_EXPORT)
  244. set(DEFINE_IMPORT)
  245. set(DEFINE_NO_EXPORT)
  246. if (COMPILER_HAS_DEPRECATED_ATTR)
  247. set(DEFINE_DEPRECATED "__attribute__ ((__deprecated__))")
  248. elseif(COMPILER_HAS_DEPRECATED)
  249. set(DEFINE_DEPRECATED "__declspec(deprecated)")
  250. endif()
  251. get_property(type TARGET ${TARGET_LIBRARY} PROPERTY TYPE)
  252. if(NOT ${type} STREQUAL "STATIC_LIBRARY")
  253. if(WIN32 OR CYGWIN)
  254. set(DEFINE_EXPORT "__declspec(dllexport)")
  255. set(DEFINE_IMPORT "__declspec(dllimport)")
  256. elseif(COMPILER_HAS_HIDDEN_VISIBILITY)
  257. set(DEFINE_EXPORT "__attribute__((visibility(\"default\")))")
  258. set(DEFINE_IMPORT "__attribute__((visibility(\"default\")))")
  259. set(DEFINE_NO_EXPORT "__attribute__((visibility(\"hidden\")))")
  260. endif()
  261. endif()
  262. endmacro()
  263. macro(_DO_GENERATE_EXPORT_HEADER TARGET_LIBRARY)
  264. # Option overrides
  265. set(options DEFINE_NO_DEPRECATED)
  266. set(oneValueArgs PREFIX_NAME BASE_NAME EXPORT_MACRO_NAME EXPORT_FILE_NAME
  267. DEPRECATED_MACRO_NAME NO_EXPORT_MACRO_NAME STATIC_DEFINE
  268. NO_DEPRECATED_MACRO_NAME CUSTOM_CONTENT_FROM_VARIABLE INCLUDE_GUARD_NAME)
  269. set(multiValueArgs)
  270. cmake_parse_arguments(_GEH "${options}" "${oneValueArgs}" "${multiValueArgs}"
  271. ${ARGN})
  272. set(BASE_NAME "${TARGET_LIBRARY}")
  273. if(_GEH_BASE_NAME)
  274. set(BASE_NAME ${_GEH_BASE_NAME})
  275. endif()
  276. string(TOUPPER ${BASE_NAME} BASE_NAME_UPPER)
  277. string(TOLOWER ${BASE_NAME} BASE_NAME_LOWER)
  278. # Default options
  279. set(EXPORT_MACRO_NAME "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_EXPORT")
  280. set(NO_EXPORT_MACRO_NAME "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_NO_EXPORT")
  281. set(EXPORT_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/${BASE_NAME_LOWER}_export.h")
  282. set(DEPRECATED_MACRO_NAME "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_DEPRECATED")
  283. set(STATIC_DEFINE "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_STATIC_DEFINE")
  284. set(NO_DEPRECATED_MACRO_NAME
  285. "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_NO_DEPRECATED")
  286. if(_GEH_UNPARSED_ARGUMENTS)
  287. message(FATAL_ERROR "Unknown keywords given to GENERATE_EXPORT_HEADER(): \"${_GEH_UNPARSED_ARGUMENTS}\"")
  288. endif()
  289. if(_GEH_EXPORT_MACRO_NAME)
  290. set(EXPORT_MACRO_NAME ${_GEH_PREFIX_NAME}${_GEH_EXPORT_MACRO_NAME})
  291. endif()
  292. string(MAKE_C_IDENTIFIER ${EXPORT_MACRO_NAME} EXPORT_MACRO_NAME)
  293. if(_GEH_EXPORT_FILE_NAME)
  294. if(IS_ABSOLUTE ${_GEH_EXPORT_FILE_NAME})
  295. set(EXPORT_FILE_NAME ${_GEH_EXPORT_FILE_NAME})
  296. else()
  297. set(EXPORT_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/${_GEH_EXPORT_FILE_NAME}")
  298. endif()
  299. endif()
  300. if(_GEH_DEPRECATED_MACRO_NAME)
  301. set(DEPRECATED_MACRO_NAME ${_GEH_PREFIX_NAME}${_GEH_DEPRECATED_MACRO_NAME})
  302. endif()
  303. string(MAKE_C_IDENTIFIER ${DEPRECATED_MACRO_NAME} DEPRECATED_MACRO_NAME)
  304. if(_GEH_NO_EXPORT_MACRO_NAME)
  305. set(NO_EXPORT_MACRO_NAME ${_GEH_PREFIX_NAME}${_GEH_NO_EXPORT_MACRO_NAME})
  306. endif()
  307. string(MAKE_C_IDENTIFIER ${NO_EXPORT_MACRO_NAME} NO_EXPORT_MACRO_NAME)
  308. if(_GEH_STATIC_DEFINE)
  309. set(STATIC_DEFINE ${_GEH_PREFIX_NAME}${_GEH_STATIC_DEFINE})
  310. endif()
  311. string(MAKE_C_IDENTIFIER ${STATIC_DEFINE} STATIC_DEFINE)
  312. if(_GEH_DEFINE_NO_DEPRECATED)
  313. set(DEFINE_NO_DEPRECATED 1)
  314. else()
  315. set(DEFINE_NO_DEPRECATED 0)
  316. endif()
  317. if(_GEH_NO_DEPRECATED_MACRO_NAME)
  318. set(NO_DEPRECATED_MACRO_NAME
  319. ${_GEH_PREFIX_NAME}${_GEH_NO_DEPRECATED_MACRO_NAME})
  320. endif()
  321. string(MAKE_C_IDENTIFIER ${NO_DEPRECATED_MACRO_NAME} NO_DEPRECATED_MACRO_NAME)
  322. if(_GEH_INCLUDE_GUARD_NAME)
  323. set(INCLUDE_GUARD_NAME ${_GEH_INCLUDE_GUARD_NAME})
  324. else()
  325. set(INCLUDE_GUARD_NAME "${EXPORT_MACRO_NAME}_H")
  326. endif()
  327. get_target_property(EXPORT_IMPORT_CONDITION ${TARGET_LIBRARY} DEFINE_SYMBOL)
  328. if(NOT EXPORT_IMPORT_CONDITION)
  329. set(EXPORT_IMPORT_CONDITION ${TARGET_LIBRARY}_EXPORTS)
  330. endif()
  331. string(MAKE_C_IDENTIFIER ${EXPORT_IMPORT_CONDITION} EXPORT_IMPORT_CONDITION)
  332. if(_GEH_CUSTOM_CONTENT_FROM_VARIABLE)
  333. if(DEFINED "${_GEH_CUSTOM_CONTENT_FROM_VARIABLE}")
  334. set(CUSTOM_CONTENT "${${_GEH_CUSTOM_CONTENT_FROM_VARIABLE}}")
  335. else()
  336. set(CUSTOM_CONTENT "")
  337. endif()
  338. endif()
  339. configure_file("${_GENERATE_EXPORT_HEADER_MODULE_DIR}/exportheader.cmake.in"
  340. "${EXPORT_FILE_NAME}" @ONLY)
  341. endmacro()
  342. function(GENERATE_EXPORT_HEADER TARGET_LIBRARY)
  343. get_property(type TARGET ${TARGET_LIBRARY} PROPERTY TYPE)
  344. if(NOT ${type} STREQUAL "STATIC_LIBRARY"
  345. AND NOT ${type} STREQUAL "SHARED_LIBRARY"
  346. AND NOT ${type} STREQUAL "OBJECT_LIBRARY"
  347. AND NOT ${type} STREQUAL "MODULE_LIBRARY")
  348. message(WARNING "This macro can only be used with libraries")
  349. return()
  350. endif()
  351. _test_compiler_hidden_visibility()
  352. _test_compiler_has_deprecated()
  353. _do_set_macro_values(${TARGET_LIBRARY})
  354. _do_generate_export_header(${TARGET_LIBRARY} ${ARGN})
  355. endfunction()
  356. function(add_compiler_export_flags)
  357. if(NOT CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS 2.8.12)
  358. message(DEPRECATION "The add_compiler_export_flags function is obsolete. Use the CXX_VISIBILITY_PRESET and VISIBILITY_INLINES_HIDDEN target properties instead.")
  359. endif()
  360. _test_compiler_hidden_visibility()
  361. _test_compiler_has_deprecated()
  362. option(USE_COMPILER_HIDDEN_VISIBILITY
  363. "Use HIDDEN visibility support if available." ON)
  364. mark_as_advanced(USE_COMPILER_HIDDEN_VISIBILITY)
  365. if(NOT (USE_COMPILER_HIDDEN_VISIBILITY AND COMPILER_HAS_HIDDEN_VISIBILITY))
  366. # Just return if there are no flags to add.
  367. return()
  368. endif()
  369. set (EXTRA_FLAGS "-fvisibility=hidden")
  370. if(COMPILER_HAS_HIDDEN_INLINE_VISIBILITY)
  371. set (EXTRA_FLAGS "${EXTRA_FLAGS} -fvisibility-inlines-hidden")
  372. endif()
  373. # Either return the extra flags needed in the supplied argument, or to the
  374. # CMAKE_CXX_FLAGS if no argument is supplied.
  375. if(ARGC GREATER 0)
  376. set(${ARGV0} "${EXTRA_FLAGS}" PARENT_SCOPE)
  377. else()
  378. string(APPEND CMAKE_CXX_FLAGS " ${EXTRA_FLAGS}")
  379. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" PARENT_SCOPE)
  380. endif()
  381. endfunction()