GenerateExportHeader.cmake 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 macro
  25. # adds -fvisibility=hidden to CMAKE_CXX_FLAGS if supported, and is a no-op on Windows
  26. # which does not need extra compiler flags for exporting support.
  27. #
  28. # This means that in the simplest case, users of these functions will be equivalent to:
  29. #
  30. # add_compiler_export_flags()
  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_BUILD_DIR} called
  47. # 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 COMPILE_FLAGS -DLIBSHARED_AND_STATIC_STATIC_DEFINE)
  90. #
  91. # This will cause the export macros to expand to nothing when building the static library.
  92. #
  93. # If DEFINE_NO_DEPRECATED is specified, then a macro ${BASE_NAME}_NO_DEPRECATED will be defined
  94. # This macro can be used to remove deprecated code from preprocessor output.
  95. #
  96. # option(EXCLUDE_DEPRECATED "Exclude deprecated parts of the library" FALSE)
  97. # if (EXCLUDE_DEPRECATED)
  98. # set(NO_BUILD_DEPRECATED DEFINE_NO_DEPRECATED)
  99. # endif()
  100. # generate_export_header(somelib ${NO_BUILD_DEPRECATED})
  101. #
  102. # And then in somelib:
  103. #
  104. # class SOMELIB_EXPORT SomeClass
  105. # {
  106. # public:
  107. # #ifndef SOMELIB_NO_DEPRECATED
  108. # SOMELIB_DEPRECATED void oldMethod();
  109. # #endif
  110. # };
  111. #
  112. # #ifndef SOMELIB_NO_DEPRECATED
  113. # void SomeClass::oldMethod() { }
  114. # #endif
  115. #
  116. # If PREFIX_NAME is specified, the argument will be used as a prefix to all
  117. # generated macros.
  118. #
  119. # For example:
  120. #
  121. # generate_export_header(somelib PREFIX_NAME VTK_)
  122. #
  123. # Generates the macros VTK_SOMELIB_EXPORT etc.
  124. #=============================================================================
  125. # Copyright 2011 Stephen Kelly <[email protected]>
  126. #
  127. # Distributed under the OSI-approved BSD License (the "License");
  128. # see accompanying file Copyright.txt for details.
  129. #
  130. # This software is distributed WITHOUT ANY WARRANTY; without even the
  131. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  132. # See the License for more information.
  133. #=============================================================================
  134. # (To distribute this file outside of CMake, substitute the full
  135. # License text for the above reference.)
  136. include(CMakeParseArguments)
  137. include(CheckCXXCompilerFlag)
  138. # TODO: Install this macro separately?
  139. macro(_check_cxx_compiler_attribute _ATTRIBUTE _RESULT)
  140. check_cxx_source_compiles("${_ATTRIBUTE} int somefunc() { return 0; } int main() { return somefunc();}" ${_RESULT}
  141. # Some compilers do not fail with a bad flag
  142. FAIL_REGEX "unrecognized .*option" # GNU
  143. FAIL_REGEX "ignoring unknown option" # MSVC
  144. FAIL_REGEX "warning D9002" # MSVC, any lang
  145. FAIL_REGEX "[Uu]nknown option" # HP
  146. FAIL_REGEX "[Ww]arning: [Oo]ption" # SunPro
  147. FAIL_REGEX "command option .* is not recognized" # XL
  148. )
  149. endmacro()
  150. macro(_test_compiler_hidden_visibility)
  151. if (CMAKE_COMPILER_IS_GNUCXX)
  152. exec_program(${CMAKE_C_COMPILER} ARGS --version OUTPUT_VARIABLE _gcc_version_info)
  153. string (REGEX MATCH "[345]\\.[0-9]\\.[0-9]" _gcc_version "${_gcc_version_info}")
  154. # gcc on mac just reports: "gcc (GCC) 3.3 20030304 ..." without the
  155. # patch level, handle this here:
  156. if(NOT _gcc_version)
  157. string (REGEX REPLACE ".*\\(GCC\\).* ([34]\\.[0-9]) .*" "\\1.0" _gcc_version "${_gcc_version_info}")
  158. endif()
  159. if(${_gcc_version} VERSION_LESS "4.2")
  160. set(GCC_TOO_OLD TRUE)
  161. message(WARNING "GCC version older than 4.2")
  162. endif()
  163. endif()
  164. if(CMAKE_CXX_COMPILER_ID MATCHES Intel)
  165. exec_program(${CMAKE_CXX_COMPILER} ARGS -V OUTPUT_VARIABLE _intel_version_info)
  166. string (REGEX REPLACE ".*Version ([0-9]+(\\.[0-9]+)+).*" "\\1" _intel_version "${_intel_version_info}")
  167. if(${_intel_version} VERSION_LESS "12.0")
  168. set(_INTEL_TOO_OLD TRUE)
  169. message(WARNING "Intel compiler older than 12.0")
  170. endif()
  171. endif()
  172. # Exclude XL here because it misinterprets -fvisibility=hidden even though
  173. # the check_cxx_compiler_flag passes
  174. # http://www.cdash.org/CDash/testDetails.php?test=109109951&build=1419259
  175. if (NOT GCC_TOO_OLD
  176. AND NOT _INTEL_TOO_OLD
  177. AND NOT WIN32
  178. AND NOT CYGWIN
  179. AND NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES XL
  180. AND NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES PGI
  181. AND NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES Watcom)
  182. check_cxx_compiler_flag(-fvisibility=hidden COMPILER_HAS_HIDDEN_VISIBILITY)
  183. check_cxx_compiler_flag(-fvisibility-inlines-hidden COMPILER_HAS_HIDDEN_INLINE_VISIBILITY)
  184. option(USE_COMPILER_HIDDEN_VISIBILITY "Use HIDDEN visibility support if available." ON)
  185. mark_as_advanced(USE_COMPILER_HIDDEN_VISIBILITY)
  186. endif()
  187. endmacro()
  188. macro(_test_compiler_has_deprecated)
  189. if("${CMAKE_CXX_COMPILER_ID}" MATCHES Borland
  190. OR "${CMAKE_CXX_COMPILER_ID}" MATCHES HP
  191. OR GCC_TOO_OLD
  192. OR "${CMAKE_CXX_COMPILER_ID}" MATCHES PGI
  193. OR "${CMAKE_CXX_COMPILER_ID}" MATCHES Watcom)
  194. set(COMPILER_HAS_DEPRECATED "" CACHE INTERNAL "Compiler support for a deprecated attribute")
  195. else()
  196. _check_cxx_compiler_attribute("__attribute__((__deprecated__))" COMPILER_HAS_DEPRECATED_ATTR)
  197. if(COMPILER_HAS_DEPRECATED_ATTR)
  198. set(COMPILER_HAS_DEPRECATED "${COMPILER_HAS_DEPRECATED_ATTR}" CACHE INTERNAL "Compiler support for a deprecated attribute")
  199. else()
  200. _check_cxx_compiler_attribute("__declspec(deprecated)" COMPILER_HAS_DEPRECATED)
  201. endif()
  202. endif()
  203. endmacro()
  204. set(myDir "${CMAKE_CURRENT_LIST_DIR}")
  205. macro(_DO_SET_MACRO_VALUES TARGET_LIBRARY)
  206. set(DEFINE_DEPRECATED)
  207. set(DEFINE_EXPORT)
  208. set(DEFINE_IMPORT)
  209. set(DEFINE_NO_EXPORT)
  210. if (COMPILER_HAS_DEPRECATED_ATTR)
  211. set(DEFINE_DEPRECATED "__attribute__ ((__deprecated__))")
  212. elseif(COMPILER_HAS_DEPRECATED)
  213. set(DEFINE_DEPRECATED "__declspec(deprecated)")
  214. endif()
  215. get_property(type TARGET ${TARGET_LIBRARY} PROPERTY TYPE)
  216. if(NOT ${type} STREQUAL "STATIC_LIBRARY")
  217. if(WIN32)
  218. set(DEFINE_EXPORT "__declspec(dllexport)")
  219. set(DEFINE_IMPORT "__declspec(dllimport)")
  220. elseif(COMPILER_HAS_HIDDEN_VISIBILITY AND USE_COMPILER_HIDDEN_VISIBILITY)
  221. set(DEFINE_EXPORT "__attribute__((visibility(\"default\")))")
  222. set(DEFINE_IMPORT "__attribute__((visibility(\"default\")))")
  223. set(DEFINE_NO_EXPORT "__attribute__((visibility(\"hidden\")))")
  224. endif()
  225. endif()
  226. endmacro()
  227. macro(_DO_GENERATE_EXPORT_HEADER TARGET_LIBRARY)
  228. # Option overrides
  229. set(options DEFINE_NO_DEPRECATED)
  230. set(oneValueArgs PREFIX_NAME BASE_NAME EXPORT_MACRO_NAME EXPORT_FILE_NAME DEPRECATED_MACRO_NAME NO_EXPORT_MACRO_NAME STATIC_DEFINE NO_DEPRECATED_MACRO_NAME)
  231. set(multiValueArgs)
  232. cmake_parse_arguments(_GEH "${options}" "${oneValueArgs}" "${multiValueArgs}" ${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 "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_NO_DEPRECATED")
  246. if(_GEH_UNPARSED_ARGUMENTS)
  247. message(FATAL_ERROR "Unknown keywords given to GENERATE_EXPORT_HEADER(): \"${_GEH_UNPARSED_ARGUMENTS}\"")
  248. endif()
  249. if(_GEH_EXPORT_MACRO_NAME)
  250. set(EXPORT_MACRO_NAME ${_GEH_PREFIX_NAME}${_GEH_EXPORT_MACRO_NAME})
  251. endif()
  252. if(_GEH_EXPORT_FILE_NAME)
  253. if(IS_ABSOLUTE _GEH_EXPORT_FILE_NAME)
  254. set(EXPORT_FILE_NAME ${_GEH_EXPORT_FILE_NAME})
  255. else()
  256. set(EXPORT_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/${_GEH_EXPORT_FILE_NAME}")
  257. endif()
  258. endif()
  259. if(_GEH_DEPRECATED_MACRO_NAME)
  260. set(DEPRECATED_MACRO_NAME ${_GEH_PREFIX_NAME}${_GEH_DEPRECATED_MACRO_NAME})
  261. endif()
  262. if(_GEH_NO_EXPORT_MACRO_NAME)
  263. set(NO_EXPORT_MACRO_NAME ${_GEH_PREFIX_NAME}${_GEH_NO_EXPORT_MACRO_NAME})
  264. endif()
  265. if(_GEH_STATIC_DEFINE)
  266. set(STATIC_DEFINE ${_GEH_PREFIX_NAME}${_GEH_STATIC_DEFINE})
  267. endif()
  268. if (_GEH_DEFINE_NO_DEPRECATED)
  269. set(DEFINE_NO_DEPRECATED TRUE)
  270. endif()
  271. if (_GEH_NO_DEPRECATED_MACRO_NAME)
  272. set(NO_DEPRECATED_MACRO_NAME ${_GEH_PREFIX_NAME}${_GEH_NO_DEPRECATED_MACRO_NAME})
  273. endif()
  274. set(INCLUDE_GUARD_NAME "${EXPORT_MACRO_NAME}_H")
  275. get_target_property(EXPORT_IMPORT_CONDITION ${TARGET_LIBRARY} DEFINE_SYMBOL)
  276. if (NOT EXPORT_IMPORT_CONDITION)
  277. set(EXPORT_IMPORT_CONDITION ${TARGET_LIBRARY}_EXPORTS)
  278. endif()
  279. configure_file("${myDir}/exportheader.cmake.in" "${EXPORT_FILE_NAME}" @ONLY)
  280. endmacro()
  281. function(GENERATE_EXPORT_HEADER TARGET_LIBRARY)
  282. get_property(type TARGET ${TARGET_LIBRARY} PROPERTY TYPE)
  283. if(${type} STREQUAL "MODULE")
  284. message(WARNING "This macro should not be used with libraries of type MODULE")
  285. return()
  286. endif()
  287. if(NOT ${type} STREQUAL "STATIC_LIBRARY" AND NOT ${type} STREQUAL "SHARED_LIBRARY")
  288. message(WARNING "This macro can only be used with libraries")
  289. return()
  290. endif()
  291. _test_compiler_hidden_visibility()
  292. _test_compiler_has_deprecated()
  293. _do_set_macro_values(${TARGET_LIBRARY})
  294. _do_generate_export_header(${TARGET_LIBRARY} ${ARGN})
  295. endfunction()
  296. function(add_compiler_export_flags)
  297. _test_compiler_hidden_visibility()
  298. _test_compiler_has_deprecated()
  299. if(NOT (USE_COMPILER_HIDDEN_VISIBILITY AND COMPILER_HAS_HIDDEN_VISIBILITY))
  300. message(WARNING "Compiler doesn't have hidden visibility")
  301. return()
  302. endif()
  303. set (EXTRA_FLAGS "-fvisibility=hidden")
  304. if(COMPILER_HAS_HIDDEN_INLINE_VISIBILITY)
  305. set (EXTRA_FLAGS "${EXTRA_FLAGS} -fvisibility-inlines-hidden")
  306. endif()
  307. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_FLAGS}" PARENT_SCOPE)
  308. endfunction()