GenerateExportHeader.cmake 12 KB

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