GenerateExportHeader.cmake 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. # [STATIC_DEFINE <static_define>]
  22. # [NO_DEPRECATED_MACRO_NAME <no_deprecated_macro_name>]
  23. # [DEFINE_NO_DEPRECATED]
  24. # [PREFIX_NAME <prefix_name>]
  25. # [CUSTOM_CONTENT_FROM_VARIABLE <variable>]
  26. # )
  27. #
  28. #
  29. # The target properties :prop_tgt:`CXX_VISIBILITY_PRESET <<LANG>_VISIBILITY_PRESET>`
  30. # and :prop_tgt:`VISIBILITY_INLINES_HIDDEN` can be used to add the appropriate
  31. # compile flags for targets. See the documentation of those target properties,
  32. # and the convenience variables
  33. # :variable:`CMAKE_CXX_VISIBILITY_PRESET <CMAKE_<LANG>_VISIBILITY_PRESET>` and
  34. # :variable:`CMAKE_VISIBILITY_INLINES_HIDDEN`.
  35. #
  36. # By default ``GENERATE_EXPORT_HEADER()`` generates macro names in a file
  37. # name determined by the name of the library. This means that in the
  38. # simplest case, users of ``GenerateExportHeader`` will be equivalent to:
  39. #
  40. # .. code-block:: cmake
  41. #
  42. # set(CMAKE_CXX_VISIBILITY_PRESET hidden)
  43. # set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)
  44. # add_library(somelib someclass.cpp)
  45. # generate_export_header(somelib)
  46. # install(TARGETS somelib DESTINATION ${LIBRARY_INSTALL_DIR})
  47. # install(FILES
  48. # someclass.h
  49. # ${PROJECT_BINARY_DIR}/somelib_export.h DESTINATION ${INCLUDE_INSTALL_DIR}
  50. # )
  51. #
  52. #
  53. # And in the ABI header files:
  54. #
  55. # .. code-block:: c++
  56. #
  57. # #include "somelib_export.h"
  58. # class SOMELIB_EXPORT SomeClass {
  59. # ...
  60. # };
  61. #
  62. #
  63. # The CMake fragment will generate a file in the
  64. # ``${CMAKE_CURRENT_BINARY_DIR}`` called ``somelib_export.h`` containing the
  65. # macros ``SOMELIB_EXPORT``, ``SOMELIB_NO_EXPORT``, ``SOMELIB_DEPRECATED``,
  66. # ``SOMELIB_DEPRECATED_EXPORT`` and ``SOMELIB_DEPRECATED_NO_EXPORT``.
  67. # They will be followed by content taken from the variable specified by
  68. # the ``CUSTOM_CONTENT_FROM_VARIABLE`` option, if any.
  69. # The resulting file should be installed with other headers in the library.
  70. #
  71. # The ``BASE_NAME`` argument can be used to override the file name and the
  72. # names used for the macros:
  73. #
  74. # .. code-block:: cmake
  75. #
  76. # add_library(somelib someclass.cpp)
  77. # generate_export_header(somelib
  78. # BASE_NAME other_name
  79. # )
  80. #
  81. #
  82. # Generates a file called ``other_name_export.h`` containing the macros
  83. # ``OTHER_NAME_EXPORT``, ``OTHER_NAME_NO_EXPORT`` and ``OTHER_NAME_DEPRECATED``
  84. # etc.
  85. #
  86. # The ``BASE_NAME`` may be overridden by specifying other options in the
  87. # function. For example:
  88. #
  89. # .. code-block:: cmake
  90. #
  91. # add_library(somelib someclass.cpp)
  92. # generate_export_header(somelib
  93. # EXPORT_MACRO_NAME OTHER_NAME_EXPORT
  94. # )
  95. #
  96. #
  97. # creates the macro ``OTHER_NAME_EXPORT`` instead of ``SOMELIB_EXPORT``, but
  98. # other macros and the generated file name is as default:
  99. #
  100. # .. code-block:: cmake
  101. #
  102. # add_library(somelib someclass.cpp)
  103. # generate_export_header(somelib
  104. # DEPRECATED_MACRO_NAME KDE_DEPRECATED
  105. # )
  106. #
  107. #
  108. # creates the macro ``KDE_DEPRECATED`` instead of ``SOMELIB_DEPRECATED``.
  109. #
  110. # If ``LIBRARY_TARGET`` is a static library, macros are defined without
  111. # values.
  112. #
  113. # If the same sources are used to create both a shared and a static
  114. # library, the uppercased symbol ``${BASE_NAME}_STATIC_DEFINE`` should be
  115. # used when building the static library:
  116. #
  117. # .. code-block:: cmake
  118. #
  119. # add_library(shared_variant SHARED ${lib_SRCS})
  120. # add_library(static_variant ${lib_SRCS})
  121. # generate_export_header(shared_variant BASE_NAME libshared_and_static)
  122. # set_target_properties(static_variant PROPERTIES
  123. # COMPILE_FLAGS -DLIBSHARED_AND_STATIC_STATIC_DEFINE)
  124. #
  125. # This will cause the export macros to expand to nothing when building
  126. # the static library.
  127. #
  128. # If ``DEFINE_NO_DEPRECATED`` is specified, then a macro
  129. # ``${BASE_NAME}_NO_DEPRECATED`` will be defined This macro can be used to
  130. # remove deprecated code from preprocessor output:
  131. #
  132. # .. code-block:: cmake
  133. #
  134. # option(EXCLUDE_DEPRECATED "Exclude deprecated parts of the library" FALSE)
  135. # if (EXCLUDE_DEPRECATED)
  136. # set(NO_BUILD_DEPRECATED DEFINE_NO_DEPRECATED)
  137. # endif()
  138. # generate_export_header(somelib ${NO_BUILD_DEPRECATED})
  139. #
  140. #
  141. # And then in somelib:
  142. #
  143. # .. code-block:: c++
  144. #
  145. # class SOMELIB_EXPORT SomeClass
  146. # {
  147. # public:
  148. # #ifndef SOMELIB_NO_DEPRECATED
  149. # SOMELIB_DEPRECATED void oldMethod();
  150. # #endif
  151. # };
  152. #
  153. # .. code-block:: c++
  154. #
  155. # #ifndef SOMELIB_NO_DEPRECATED
  156. # void SomeClass::oldMethod() { }
  157. # #endif
  158. #
  159. #
  160. # If ``PREFIX_NAME`` is specified, the argument will be used as a prefix to
  161. # all generated macros.
  162. #
  163. # For example:
  164. #
  165. # .. code-block:: cmake
  166. #
  167. # generate_export_header(somelib PREFIX_NAME VTK_)
  168. #
  169. # Generates the macros ``VTK_SOMELIB_EXPORT`` etc.
  170. #
  171. # ::
  172. #
  173. # ADD_COMPILER_EXPORT_FLAGS( [<output_variable>] )
  174. #
  175. # The ``ADD_COMPILER_EXPORT_FLAGS`` function adds ``-fvisibility=hidden`` to
  176. # :variable:`CMAKE_CXX_FLAGS <CMAKE_<LANG>_FLAGS>` if supported, and is a no-op
  177. # on Windows which does not need extra compiler flags for exporting support.
  178. # You may optionally pass a single argument to ``ADD_COMPILER_EXPORT_FLAGS``
  179. # that will be populated with the ``CXX_FLAGS`` required to enable visibility
  180. # support for the compiler/architecture in use.
  181. #
  182. # This function is deprecated. Set the target properties
  183. # :prop_tgt:`CXX_VISIBILITY_PRESET <<LANG>_VISIBILITY_PRESET>` and
  184. # :prop_tgt:`VISIBILITY_INLINES_HIDDEN` instead.
  185. # Modified by Yao Wei Tjong for Urho3D
  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. # Urho3D - set the flags in a variable before testing
  211. set (COMPILER_HIDDEN_VISIBILITY_FLAGS -fvisibility=hidden)
  212. set (COMPILER_HIDDEN_INLINE_VISIBILITY_FLAGS -fvisibility-inlines-hidden)
  213. check_cxx_compiler_flag(${COMPILER_HIDDEN_VISIBILITY_FLAGS} COMPILER_HAS_HIDDEN_VISIBILITY)
  214. check_cxx_compiler_flag(${COMPILER_HIDDEN_INLINE_VISIBILITY_FLAGS} COMPILER_HAS_HIDDEN_INLINE_VISIBILITY)
  215. endif()
  216. endmacro()
  217. macro(_test_compiler_has_deprecated)
  218. # NOTE: Some Embarcadero compilers silently compile __declspec(deprecated)
  219. # without error, but this is not a documented feature and the attribute does
  220. # not actually generate any warnings.
  221. if(CMAKE_CXX_COMPILER_ID MATCHES Borland
  222. OR CMAKE_CXX_COMPILER_ID MATCHES Embarcadero
  223. OR CMAKE_CXX_COMPILER_ID MATCHES HP
  224. OR GCC_TOO_OLD
  225. OR CMAKE_CXX_COMPILER_ID MATCHES PGI
  226. OR CMAKE_CXX_COMPILER_ID MATCHES Watcom)
  227. set(COMPILER_HAS_DEPRECATED "" CACHE INTERNAL
  228. "Compiler support for a deprecated attribute")
  229. else()
  230. _check_cxx_compiler_attribute("__attribute__((__deprecated__))"
  231. COMPILER_HAS_DEPRECATED_ATTR)
  232. if(COMPILER_HAS_DEPRECATED_ATTR)
  233. set(COMPILER_HAS_DEPRECATED "${COMPILER_HAS_DEPRECATED_ATTR}"
  234. CACHE INTERNAL "Compiler support for a deprecated attribute")
  235. else()
  236. _check_cxx_compiler_attribute("__declspec(deprecated)"
  237. COMPILER_HAS_DEPRECATED)
  238. endif()
  239. endif()
  240. endmacro()
  241. get_filename_component(_GENERATE_EXPORT_HEADER_MODULE_DIR
  242. "${CMAKE_CURRENT_LIST_FILE}" PATH)
  243. macro(_DO_SET_MACRO_VALUES TARGET_LIBRARY)
  244. set(DEFINE_DEPRECATED)
  245. set(DEFINE_EXPORT)
  246. set(DEFINE_IMPORT)
  247. set(DEFINE_NO_EXPORT)
  248. if (COMPILER_HAS_DEPRECATED_ATTR)
  249. set(DEFINE_DEPRECATED "__attribute__ ((__deprecated__))")
  250. elseif(COMPILER_HAS_DEPRECATED)
  251. set(DEFINE_DEPRECATED "__declspec(deprecated)")
  252. endif()
  253. # Urho3D: always generate header file regardless of target type
  254. if(WIN32 OR CYGWIN)
  255. set(DEFINE_EXPORT "__declspec(dllexport)")
  256. set(DEFINE_IMPORT "__declspec(dllimport)")
  257. elseif(COMPILER_HAS_HIDDEN_VISIBILITY)
  258. set(DEFINE_EXPORT "__attribute__((visibility(\"default\")))")
  259. set(DEFINE_IMPORT "__attribute__((visibility(\"default\")))")
  260. set(DEFINE_NO_EXPORT "__attribute__((visibility(\"hidden\")))")
  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)
  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. set(INCLUDE_GUARD_NAME "${EXPORT_MACRO_NAME}_H")
  323. # Urho3D: Our revised version does not depend on the target to be added first, so always derive the variable value from target name instead
  324. set(EXPORT_IMPORT_CONDITION ${TARGET_LIBRARY}_EXPORTS)
  325. string(MAKE_C_IDENTIFIER ${EXPORT_IMPORT_CONDITION} EXPORT_IMPORT_CONDITION)
  326. if(_GEH_CUSTOM_CONTENT_FROM_VARIABLE)
  327. if(DEFINED "${_GEH_CUSTOM_CONTENT_FROM_VARIABLE}")
  328. set(CUSTOM_CONTENT "${${_GEH_CUSTOM_CONTENT_FROM_VARIABLE}}")
  329. else()
  330. set(CUSTOM_CONTENT "")
  331. endif()
  332. endif()
  333. configure_file("${_GENERATE_EXPORT_HEADER_MODULE_DIR}/exportheader.cmake.in"
  334. "${EXPORT_FILE_NAME}" @ONLY)
  335. endmacro()
  336. # Urho3D: revise to pass the library type by argument so that it does not depend on the target to be added first
  337. function(GENERATE_EXPORT_HEADER TARGET_LIBRARY type)
  338. if(NOT ${type} MATCHES STATIC|SHARED|OBJECT|MODULE)
  339. message(WARNING "This macro can only be used with libraries")
  340. return()
  341. endif()
  342. _test_compiler_hidden_visibility()
  343. _test_compiler_has_deprecated()
  344. _do_set_macro_values(${TARGET_LIBRARY} ${type})
  345. _do_generate_export_header(${TARGET_LIBRARY} ${ARGN})
  346. endfunction()