GenerateExportHeader.cmake 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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( [FATAL_WARNINGS] )
  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. # Some compilers do not fail with a bad flag
  150. FAIL_REGEX "unrecognized .*option" # GNU
  151. FAIL_REGEX "ignoring unknown option" # MSVC
  152. FAIL_REGEX "warning D9002" # MSVC, any lang
  153. FAIL_REGEX "[Uu]nknown option" # HP
  154. FAIL_REGEX "[Ww]arning: [Oo]ption" # SunPro
  155. FAIL_REGEX "command option .* is not recognized" # XL
  156. )
  157. endmacro()
  158. macro(_test_compiler_hidden_visibility)
  159. if(CMAKE_COMPILER_IS_GNUCXX)
  160. exec_program(${CMAKE_C_COMPILER} ARGS --version
  161. OUTPUT_VARIABLE _gcc_version_info)
  162. string(REGEX MATCH "[345]\\.[0-9]\\.[0-9]"
  163. _gcc_version "${_gcc_version_info}")
  164. # gcc on mac just reports: "gcc (GCC) 3.3 20030304 ..." without the
  165. # patch level, handle this here:
  166. if(NOT _gcc_version)
  167. string(REGEX REPLACE ".*\\(GCC\\).* ([34]\\.[0-9]) .*" "\\1.0"
  168. _gcc_version "${_gcc_version_info}")
  169. endif()
  170. if("${_gcc_version}" VERSION_LESS "4.2")
  171. set(GCC_TOO_OLD TRUE)
  172. message(WARNING "GCC version older than 4.2")
  173. endif()
  174. endif()
  175. if(CMAKE_CXX_COMPILER_ID MATCHES Intel)
  176. exec_program(${CMAKE_CXX_COMPILER} ARGS -V
  177. OUTPUT_VARIABLE _intel_version_info)
  178. string(REGEX REPLACE ".*Version ([0-9]+(\\.[0-9]+)+).*" "\\1"
  179. _intel_version "${_intel_version_info}")
  180. if(${_intel_version} VERSION_LESS "12.0")
  181. set(_INTEL_TOO_OLD TRUE)
  182. message(WARNING "Intel compiler older than 12.0")
  183. endif()
  184. endif()
  185. # Exclude XL here because it misinterprets -fvisibility=hidden even though
  186. # the check_cxx_compiler_flag passes
  187. # http://www.cdash.org/CDash/testDetails.php?test=109109951&build=1419259
  188. if(NOT GCC_TOO_OLD
  189. AND NOT _INTEL_TOO_OLD
  190. AND NOT WIN32
  191. AND NOT CYGWIN
  192. AND NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES XL
  193. AND NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES PGI
  194. AND NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES Watcom)
  195. check_cxx_compiler_flag(-fvisibility=hidden COMPILER_HAS_HIDDEN_VISIBILITY)
  196. check_cxx_compiler_flag(-fvisibility-inlines-hidden
  197. COMPILER_HAS_HIDDEN_INLINE_VISIBILITY)
  198. option(USE_COMPILER_HIDDEN_VISIBILITY
  199. "Use HIDDEN visibility support if available." ON)
  200. mark_as_advanced(USE_COMPILER_HIDDEN_VISIBILITY)
  201. endif()
  202. endmacro()
  203. macro(_test_compiler_has_deprecated)
  204. if("${CMAKE_CXX_COMPILER_ID}" MATCHES Borland
  205. OR "${CMAKE_CXX_COMPILER_ID}" MATCHES HP
  206. OR GCC_TOO_OLD
  207. OR "${CMAKE_CXX_COMPILER_ID}" MATCHES PGI
  208. OR "${CMAKE_CXX_COMPILER_ID}" MATCHES Watcom)
  209. set(COMPILER_HAS_DEPRECATED "" CACHE INTERNAL
  210. "Compiler support for a deprecated attribute")
  211. else()
  212. _check_cxx_compiler_attribute("__attribute__((__deprecated__))"
  213. COMPILER_HAS_DEPRECATED_ATTR)
  214. if(COMPILER_HAS_DEPRECATED_ATTR)
  215. set(COMPILER_HAS_DEPRECATED "${COMPILER_HAS_DEPRECATED_ATTR}"
  216. CACHE INTERNAL "Compiler support for a deprecated attribute")
  217. else()
  218. _check_cxx_compiler_attribute("__declspec(deprecated)"
  219. COMPILER_HAS_DEPRECATED)
  220. endif()
  221. endif()
  222. endmacro()
  223. get_filename_component(_GENERATE_EXPORT_HEADER_MODULE_DIR
  224. "${CMAKE_CURRENT_LIST_FILE}" PATH)
  225. macro(_DO_SET_MACRO_VALUES TARGET_LIBRARY)
  226. set(DEFINE_DEPRECATED)
  227. set(DEFINE_EXPORT)
  228. set(DEFINE_IMPORT)
  229. set(DEFINE_NO_EXPORT)
  230. if (COMPILER_HAS_DEPRECATED_ATTR)
  231. set(DEFINE_DEPRECATED "__attribute__ ((__deprecated__))")
  232. elseif(COMPILER_HAS_DEPRECATED)
  233. set(DEFINE_DEPRECATED "__declspec(deprecated)")
  234. endif()
  235. get_property(type TARGET ${TARGET_LIBRARY} PROPERTY TYPE)
  236. if(NOT ${type} STREQUAL "STATIC_LIBRARY")
  237. if(WIN32)
  238. set(DEFINE_EXPORT "__declspec(dllexport)")
  239. set(DEFINE_IMPORT "__declspec(dllimport)")
  240. elseif(COMPILER_HAS_HIDDEN_VISIBILITY AND USE_COMPILER_HIDDEN_VISIBILITY)
  241. set(DEFINE_EXPORT "__attribute__((visibility(\"default\")))")
  242. set(DEFINE_IMPORT "__attribute__((visibility(\"default\")))")
  243. set(DEFINE_NO_EXPORT "__attribute__((visibility(\"hidden\")))")
  244. endif()
  245. endif()
  246. endmacro()
  247. macro(_DO_GENERATE_EXPORT_HEADER TARGET_LIBRARY)
  248. # Option overrides
  249. set(options DEFINE_NO_DEPRECATED)
  250. set(oneValueArgs PREFIX_NAME BASE_NAME EXPORT_MACRO_NAME EXPORT_FILE_NAME
  251. DEPRECATED_MACRO_NAME NO_EXPORT_MACRO_NAME STATIC_DEFINE
  252. NO_DEPRECATED_MACRO_NAME)
  253. set(multiValueArgs)
  254. cmake_parse_arguments(_GEH "${options}" "${oneValueArgs}" "${multiValueArgs}"
  255. ${ARGN})
  256. set(BASE_NAME "${TARGET_LIBRARY}")
  257. if(_GEH_BASE_NAME)
  258. set(BASE_NAME ${_GEH_BASE_NAME})
  259. endif()
  260. string(TOUPPER ${BASE_NAME} BASE_NAME_UPPER)
  261. string(TOLOWER ${BASE_NAME} BASE_NAME_LOWER)
  262. # Default options
  263. set(EXPORT_MACRO_NAME "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_EXPORT")
  264. set(NO_EXPORT_MACRO_NAME "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_NO_EXPORT")
  265. set(EXPORT_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/${BASE_NAME_LOWER}_export.h")
  266. set(DEPRECATED_MACRO_NAME "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_DEPRECATED")
  267. set(STATIC_DEFINE "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_STATIC_DEFINE")
  268. set(NO_DEPRECATED_MACRO_NAME
  269. "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_NO_DEPRECATED")
  270. if(_GEH_UNPARSED_ARGUMENTS)
  271. message(FATAL_ERROR "Unknown keywords given to GENERATE_EXPORT_HEADER(): \"${_GEH_UNPARSED_ARGUMENTS}\"")
  272. endif()
  273. if(_GEH_EXPORT_MACRO_NAME)
  274. set(EXPORT_MACRO_NAME ${_GEH_PREFIX_NAME}${_GEH_EXPORT_MACRO_NAME})
  275. endif()
  276. if(_GEH_EXPORT_FILE_NAME)
  277. if(IS_ABSOLUTE ${_GEH_EXPORT_FILE_NAME})
  278. set(EXPORT_FILE_NAME ${_GEH_EXPORT_FILE_NAME})
  279. else()
  280. set(EXPORT_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/${_GEH_EXPORT_FILE_NAME}")
  281. endif()
  282. endif()
  283. if(_GEH_DEPRECATED_MACRO_NAME)
  284. set(DEPRECATED_MACRO_NAME ${_GEH_PREFIX_NAME}${_GEH_DEPRECATED_MACRO_NAME})
  285. endif()
  286. if(_GEH_NO_EXPORT_MACRO_NAME)
  287. set(NO_EXPORT_MACRO_NAME ${_GEH_PREFIX_NAME}${_GEH_NO_EXPORT_MACRO_NAME})
  288. endif()
  289. if(_GEH_STATIC_DEFINE)
  290. set(STATIC_DEFINE ${_GEH_PREFIX_NAME}${_GEH_STATIC_DEFINE})
  291. endif()
  292. if(_GEH_DEFINE_NO_DEPRECATED)
  293. set(DEFINE_NO_DEPRECATED TRUE)
  294. endif()
  295. if(_GEH_NO_DEPRECATED_MACRO_NAME)
  296. set(NO_DEPRECATED_MACRO_NAME
  297. ${_GEH_PREFIX_NAME}${_GEH_NO_DEPRECATED_MACRO_NAME})
  298. endif()
  299. set(INCLUDE_GUARD_NAME "${EXPORT_MACRO_NAME}_H")
  300. get_target_property(EXPORT_IMPORT_CONDITION ${TARGET_LIBRARY} DEFINE_SYMBOL)
  301. if(NOT EXPORT_IMPORT_CONDITION)
  302. set(EXPORT_IMPORT_CONDITION ${TARGET_LIBRARY}_EXPORTS)
  303. endif()
  304. configure_file("${_GENERATE_EXPORT_HEADER_MODULE_DIR}/exportheader.cmake.in"
  305. "${EXPORT_FILE_NAME}" @ONLY)
  306. endmacro()
  307. function(GENERATE_EXPORT_HEADER TARGET_LIBRARY)
  308. get_property(type TARGET ${TARGET_LIBRARY} PROPERTY TYPE)
  309. if(${type} STREQUAL "MODULE")
  310. message(WARNING "This macro should not be used with libraries of type MODULE")
  311. return()
  312. endif()
  313. if(NOT ${type} STREQUAL "STATIC_LIBRARY" AND NOT ${type} STREQUAL "SHARED_LIBRARY")
  314. message(WARNING "This macro can only be used with libraries")
  315. return()
  316. endif()
  317. _test_compiler_hidden_visibility()
  318. _test_compiler_has_deprecated()
  319. _do_set_macro_values(${TARGET_LIBRARY})
  320. _do_generate_export_header(${TARGET_LIBRARY} ${ARGN})
  321. endfunction()
  322. function(add_compiler_export_flags)
  323. _test_compiler_hidden_visibility()
  324. _test_compiler_has_deprecated()
  325. if(NOT (USE_COMPILER_HIDDEN_VISIBILITY AND COMPILER_HAS_HIDDEN_VISIBILITY))
  326. # Just return if there are no flags to add.
  327. return()
  328. endif()
  329. set (EXTRA_FLAGS "-fvisibility=hidden")
  330. if(COMPILER_HAS_HIDDEN_INLINE_VISIBILITY)
  331. set (EXTRA_FLAGS "${EXTRA_FLAGS} -fvisibility-inlines-hidden")
  332. endif()
  333. # Either return the extra flags needed in the supplied argument, or to the
  334. # CMAKE_CXX_FLAGS if no argument is supplied.
  335. if(ARGV0)
  336. set(${ARGV0} "${EXTRA_FLAGS}" PARENT_SCOPE)
  337. else()
  338. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_FLAGS}" PARENT_SCOPE)
  339. endif()
  340. endfunction()