GenerateExportHeader.cmake 13 KB

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