GenerateExportHeader.cmake 14 KB

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