GenerateExportHeader.cmake 14 KB

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