GenerateExportHeader.cmake 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 specifying 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 <[email protected]>
  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. # Modified by Yao Wei Tjong for Urho3D
  193. include(CMakeParseArguments)
  194. include(CheckCXXCompilerFlag)
  195. # TODO: Install this macro separately?
  196. macro(_check_cxx_compiler_attribute _ATTRIBUTE _RESULT)
  197. check_cxx_source_compiles("${_ATTRIBUTE} int somefunc() { return 0; }
  198. int main() { return somefunc();}" ${_RESULT}
  199. )
  200. endmacro()
  201. macro(_test_compiler_hidden_visibility)
  202. if(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.2")
  203. set(GCC_TOO_OLD TRUE)
  204. elseif(CMAKE_COMPILER_IS_GNUC AND CMAKE_C_COMPILER_VERSION VERSION_LESS "4.2")
  205. set(GCC_TOO_OLD TRUE)
  206. elseif(CMAKE_CXX_COMPILER_ID MATCHES Intel AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "12.0")
  207. set(_INTEL_TOO_OLD TRUE)
  208. endif()
  209. # Exclude XL here because it misinterprets -fvisibility=hidden even though
  210. # the check_cxx_compiler_flag passes
  211. if(NOT GCC_TOO_OLD
  212. AND NOT _INTEL_TOO_OLD
  213. AND NOT WIN32
  214. AND NOT CYGWIN
  215. AND NOT CMAKE_CXX_COMPILER_ID MATCHES XL
  216. AND NOT CMAKE_CXX_COMPILER_ID MATCHES PGI
  217. AND NOT CMAKE_CXX_COMPILER_ID MATCHES Watcom)
  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. option(USE_COMPILER_HIDDEN_VISIBILITY
  222. "Use HIDDEN visibility support if available." ON)
  223. mark_as_advanced(USE_COMPILER_HIDDEN_VISIBILITY)
  224. endif()
  225. endmacro()
  226. macro(_test_compiler_has_deprecated)
  227. if(CMAKE_CXX_COMPILER_ID MATCHES Borland
  228. OR CMAKE_CXX_COMPILER_ID MATCHES HP
  229. OR GCC_TOO_OLD
  230. OR CMAKE_CXX_COMPILER_ID MATCHES PGI
  231. OR CMAKE_CXX_COMPILER_ID MATCHES Watcom)
  232. set(COMPILER_HAS_DEPRECATED "" CACHE INTERNAL
  233. "Compiler support for a deprecated attribute")
  234. else()
  235. _check_cxx_compiler_attribute("__attribute__((__deprecated__))"
  236. COMPILER_HAS_DEPRECATED_ATTR)
  237. if(COMPILER_HAS_DEPRECATED_ATTR)
  238. set(COMPILER_HAS_DEPRECATED "${COMPILER_HAS_DEPRECATED_ATTR}"
  239. CACHE INTERNAL "Compiler support for a deprecated attribute")
  240. else()
  241. _check_cxx_compiler_attribute("__declspec(deprecated)"
  242. COMPILER_HAS_DEPRECATED)
  243. endif()
  244. endif()
  245. endmacro()
  246. get_filename_component(_GENERATE_EXPORT_HEADER_MODULE_DIR
  247. "${CMAKE_CURRENT_LIST_FILE}" PATH)
  248. macro(_DO_SET_MACRO_VALUES TARGET_LIBRARY)
  249. set(DEFINE_DEPRECATED)
  250. set(DEFINE_EXPORT)
  251. set(DEFINE_IMPORT)
  252. set(DEFINE_NO_EXPORT)
  253. if (COMPILER_HAS_DEPRECATED_ATTR)
  254. set(DEFINE_DEPRECATED "__attribute__ ((__deprecated__))")
  255. elseif(COMPILER_HAS_DEPRECATED)
  256. set(DEFINE_DEPRECATED "__declspec(deprecated)")
  257. endif()
  258. # Urho3D: always generate header file regardless of target type
  259. if(WIN32)
  260. set(DEFINE_EXPORT "__declspec(dllexport)")
  261. set(DEFINE_IMPORT "__declspec(dllimport)")
  262. elseif(COMPILER_HAS_HIDDEN_VISIBILITY AND USE_COMPILER_HIDDEN_VISIBILITY)
  263. set(DEFINE_EXPORT "__attribute__((visibility(\"default\")))")
  264. set(DEFINE_IMPORT "__attribute__((visibility(\"default\")))")
  265. set(DEFINE_NO_EXPORT "__attribute__((visibility(\"hidden\")))")
  266. endif()
  267. endmacro()
  268. macro(_DO_GENERATE_EXPORT_HEADER TARGET_LIBRARY)
  269. # Option overrides
  270. set(options DEFINE_NO_DEPRECATED)
  271. set(oneValueArgs PREFIX_NAME BASE_NAME EXPORT_MACRO_NAME EXPORT_FILE_NAME
  272. DEPRECATED_MACRO_NAME NO_EXPORT_MACRO_NAME STATIC_DEFINE
  273. NO_DEPRECATED_MACRO_NAME)
  274. set(multiValueArgs)
  275. cmake_parse_arguments(_GEH "${options}" "${oneValueArgs}" "${multiValueArgs}"
  276. ${ARGN})
  277. set(BASE_NAME "${TARGET_LIBRARY}")
  278. if(_GEH_BASE_NAME)
  279. set(BASE_NAME ${_GEH_BASE_NAME})
  280. endif()
  281. string(TOUPPER ${BASE_NAME} BASE_NAME_UPPER)
  282. string(TOLOWER ${BASE_NAME} BASE_NAME_LOWER)
  283. # Default options
  284. set(EXPORT_MACRO_NAME "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_EXPORT")
  285. set(NO_EXPORT_MACRO_NAME "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_NO_EXPORT")
  286. set(EXPORT_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/${BASE_NAME_LOWER}_export.h")
  287. set(DEPRECATED_MACRO_NAME "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_DEPRECATED")
  288. set(STATIC_DEFINE "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_STATIC_DEFINE")
  289. set(NO_DEPRECATED_MACRO_NAME
  290. "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_NO_DEPRECATED")
  291. if(_GEH_UNPARSED_ARGUMENTS)
  292. message(FATAL_ERROR "Unknown keywords given to GENERATE_EXPORT_HEADER(): \"${_GEH_UNPARSED_ARGUMENTS}\"")
  293. endif()
  294. if(_GEH_EXPORT_MACRO_NAME)
  295. set(EXPORT_MACRO_NAME ${_GEH_PREFIX_NAME}${_GEH_EXPORT_MACRO_NAME})
  296. endif()
  297. # Urho3D: CMake version less than 2.8.12 does not have this sub-command yet, so comment it out until the cmake minimum required is 2.8.12
  298. #string(MAKE_C_IDENTIFIER ${EXPORT_MACRO_NAME} EXPORT_MACRO_NAME)
  299. if(_GEH_EXPORT_FILE_NAME)
  300. if(IS_ABSOLUTE ${_GEH_EXPORT_FILE_NAME})
  301. set(EXPORT_FILE_NAME ${_GEH_EXPORT_FILE_NAME})
  302. else()
  303. set(EXPORT_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/${_GEH_EXPORT_FILE_NAME}")
  304. endif()
  305. endif()
  306. if(_GEH_DEPRECATED_MACRO_NAME)
  307. set(DEPRECATED_MACRO_NAME ${_GEH_PREFIX_NAME}${_GEH_DEPRECATED_MACRO_NAME})
  308. endif()
  309. #string(MAKE_C_IDENTIFIER ${DEPRECATED_MACRO_NAME} DEPRECATED_MACRO_NAME)
  310. if(_GEH_NO_EXPORT_MACRO_NAME)
  311. set(NO_EXPORT_MACRO_NAME ${_GEH_PREFIX_NAME}${_GEH_NO_EXPORT_MACRO_NAME})
  312. endif()
  313. #string(MAKE_C_IDENTIFIER ${NO_EXPORT_MACRO_NAME} NO_EXPORT_MACRO_NAME)
  314. if(_GEH_STATIC_DEFINE)
  315. set(STATIC_DEFINE ${_GEH_PREFIX_NAME}${_GEH_STATIC_DEFINE})
  316. endif()
  317. #string(MAKE_C_IDENTIFIER ${STATIC_DEFINE} STATIC_DEFINE)
  318. if(_GEH_DEFINE_NO_DEPRECATED)
  319. set(DEFINE_NO_DEPRECATED TRUE)
  320. endif()
  321. if(_GEH_NO_DEPRECATED_MACRO_NAME)
  322. set(NO_DEPRECATED_MACRO_NAME
  323. ${_GEH_PREFIX_NAME}${_GEH_NO_DEPRECATED_MACRO_NAME})
  324. endif()
  325. #string(MAKE_C_IDENTIFIER ${NO_DEPRECATED_MACRO_NAME} NO_DEPRECATED_MACRO_NAME)
  326. set(INCLUDE_GUARD_NAME "${EXPORT_MACRO_NAME}_H")
  327. # Urho3D: Our revised version does not depend on the target to be added first, so always derive the variable value from target name instead
  328. set(EXPORT_IMPORT_CONDITION ${TARGET_LIBRARY}_EXPORTS)
  329. #string(MAKE_C_IDENTIFIER ${EXPORT_IMPORT_CONDITION} EXPORT_IMPORT_CONDITION)
  330. configure_file("${_GENERATE_EXPORT_HEADER_MODULE_DIR}/exportheader.cmake.in"
  331. "${EXPORT_FILE_NAME}" @ONLY)
  332. endmacro()
  333. # Urho3D: revise to pass the library type by argument so that it does not depend on the target to be added first
  334. function(GENERATE_EXPORT_HEADER TARGET_LIBRARY type)
  335. if(NOT ${type} MATCHES STATIC|SHARED|OBJECT|MODULE)
  336. message(WARNING "This macro can only be used with libraries")
  337. return()
  338. endif()
  339. _test_compiler_hidden_visibility()
  340. _test_compiler_has_deprecated()
  341. _do_set_macro_values(${TARGET_LIBRARY} ${type})
  342. _do_generate_export_header(${TARGET_LIBRARY} ${ARGN})
  343. endfunction()
  344. function(add_compiler_export_flags)
  345. if(NOT CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS 2.8.12)
  346. message(DEPRECATION "The add_compiler_export_flags function is obsolete. Use the CXX_VISIBILITY_PRESET and VISIBILITY_INLINES_HIDDEN target properties instead.")
  347. endif()
  348. _test_compiler_hidden_visibility()
  349. _test_compiler_has_deprecated()
  350. if(NOT (USE_COMPILER_HIDDEN_VISIBILITY AND COMPILER_HAS_HIDDEN_VISIBILITY))
  351. # Just return if there are no flags to add.
  352. return()
  353. endif()
  354. set (EXTRA_FLAGS "-fvisibility=hidden")
  355. if(COMPILER_HAS_HIDDEN_INLINE_VISIBILITY)
  356. set (EXTRA_FLAGS "${EXTRA_FLAGS} -fvisibility-inlines-hidden")
  357. endif()
  358. # Either return the extra flags needed in the supplied argument, or to the
  359. # CMAKE_CXX_FLAGS if no argument is supplied.
  360. if(ARGV0)
  361. set(${ARGV0} "${EXTRA_FLAGS}" PARENT_SCOPE)
  362. else()
  363. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_FLAGS}" PARENT_SCOPE)
  364. endif()
  365. endfunction()