GenerateExportHeader.cmake 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. # - Function for generation of export macros for libraries
  2. #
  3. # This module provides the function GENERATE_EXPORT_HEADER() and the
  4. # accompanying ADD_COMPILER_EXPORT_FLAGS() function.
  5. #
  6. # The GENERATE_EXPORT_HEADER function can be used to generate a file suitable
  7. # for preprocessor inclusion which contains EXPORT macros to be used in
  8. # library classes.
  9. #
  10. # GENERATE_EXPORT_HEADER( LIBRARY_TARGET
  11. # [BASE_NAME <base_name>]
  12. # [EXPORT_MACRO_NAME <export_macro_name>]
  13. # [EXPORT_FILE_NAME <export_file_name>]
  14. # [DEPRECATED_NAME <deprecated_name>]
  15. # [NO_EXPORT_MACRO_NAME <no_export_macro_name>]
  16. # [STATIC_DEFINE <static_define>]
  17. # )
  18. #
  19. # ADD_COMPILER_EXPORT_FLAGS( [FATAL_WARNINGS] )
  20. #
  21. # By default GENERATE_EXPORT_HEADER() generates macro names in a file name
  22. # determined by the name of the library. The ADD_COMPILER_EXPORT_FLAGS macro adds
  23. # -fvisibility=hidden to CMAKE_CXX_FLAGS if supported, and is a no-op on Windows
  24. # which does not need extra compiler flags for exporting support.
  25. #
  26. # This means that in the simplest case, users of these functions will be equivalent to:
  27. #
  28. # add_compiler_export_flags()
  29. #
  30. # add_library(somelib someclass.cpp)
  31. #
  32. # generate_export_header(somelib)
  33. #
  34. # install(TARGETS somelib DESTINATION ${LIBRARY_INSTALL_DIR})
  35. #
  36. # install(FILES
  37. # someclass.h
  38. # ${PROJECT_BINARY_DIR}/somelib_export.h DESTINATION ${INCLUDE_INSTALL_DIR}
  39. # )
  40. #
  41. # And in the ABI header files:
  42. #
  43. # \code
  44. # #include "somelib_export.h"
  45. #
  46. # class SOMELIB_EXPORT SomeClass {
  47. #
  48. # };
  49. # \endcode
  50. #
  51. # The CMake fragment will generate a file in the ${CMAKE_CURRENT_BUILD_DIR} called
  52. # somelib_export.h containing the macros SOMELIB_EXPORT, SOMELIB_NO_EXPORT,
  53. # SOMELIB_DEPRECATED, SOMELIB_DEPRECATED_EXPORT and SOMELIB_DEPRECATED_NO_EXPORT.
  54. # The resulting file should be installed with other headers in the library.
  55. #
  56. # The BASE_NAME argument can be used to override the file name and the names
  57. # used for the macros
  58. #
  59. # add_library(somelib someclass.cpp)
  60. # generate_export_header(somelib
  61. # BASE_NAME other_name
  62. # )
  63. #
  64. # Generates a file called other_name_export.h containing the macros
  65. # OTHER_NAME_EXPORT, OTHER_NAME_NO_EXPORT and OTHER_NAME_DEPRECATED etc.
  66. #
  67. # The BASE_NAME may be overridden by specifiying other options in the function.
  68. # For example:
  69. #
  70. # add_library(somelib someclass.cpp)
  71. # generate_export_header(somelib
  72. # EXPORT_MACRO_NAME OTHER_NAME_EXPORT
  73. # )
  74. #
  75. # creates the macro OTHER_NAME_EXPORT instead of SOMELIB_EXPORT, but other macros
  76. # and the generated file name is as default.
  77. #
  78. # add_library(somelib someclass.cpp)
  79. # generate_export_header(somelib
  80. # DEPRECATED_NAME KDE_DEPRECATED
  81. # )
  82. #
  83. # creates the macro KDE_DEPRECATED instead of SOMELIB_DEPRECATED.
  84. #
  85. # If LIBRARY_TARGET is a static library, macros are defined without values.
  86. #
  87. # If the same sources are used to create both a shared and a static library, the
  88. # uppercased symbol ${BASE_NAME}_STATIC_DEFINE should be used when building the
  89. # static library
  90. #
  91. # add_library(shared_variant SHARED ${lib_SRCS})
  92. # add_library(static_variant ${lib_SRCS})
  93. #
  94. # generate_export_header(shared_variant BASE_NAME libshared_and_static)
  95. #
  96. # set_target_properties(static_variant PROPERTIES COMPILE_FLAGS -DLIBSHARED_AND_STATIC_STATIC_DEFINE)
  97. #
  98. # This will cause the export macros to expand to nothing when building the static library.
  99. #=============================================================================
  100. # Copyright 2011 Stephen Kelly <[email protected]>
  101. #
  102. # Distributed under the OSI-approved BSD License (the "License");
  103. # see accompanying file Copyright.txt for details.
  104. #
  105. # This software is distributed WITHOUT ANY WARRANTY; without even the
  106. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  107. # See the License for more information.
  108. #=============================================================================
  109. # (To distribute this file outside of CMake, substitute the full
  110. # License text for the above reference.)
  111. include(CMakeParseArguments)
  112. include(CheckCXXCompilerFlag)
  113. # TODO: Install this macro separately?
  114. macro(_check_cxx_compiler_attribute _ATTRIBUTE _RESULT)
  115. check_cxx_source_compiles("${_ATTRIBUTE} int somefunc() { return 0; } int main() { return somefunc();}" ${_RESULT}
  116. # Some compilers do not fail with a bad flag
  117. FAIL_REGEX "unrecognized .*option" # GNU
  118. FAIL_REGEX "ignoring unknown option" # MSVC
  119. FAIL_REGEX "warning D9002" # MSVC, any lang
  120. FAIL_REGEX "[Uu]nknown option" # HP
  121. FAIL_REGEX "[Ww]arning: [Oo]ption" # SunPro
  122. FAIL_REGEX "command option .* is not recognized" # XL
  123. )
  124. endmacro()
  125. macro(_test_compiler_hidden_visibility)
  126. if (CMAKE_COMPILER_IS_GNUCXX)
  127. exec_program(${CMAKE_C_COMPILER} ARGS --version OUTPUT_VARIABLE _gcc_version_info)
  128. string (REGEX MATCH "[345]\\.[0-9]\\.[0-9]" _gcc_version "${_gcc_version_info}")
  129. # gcc on mac just reports: "gcc (GCC) 3.3 20030304 ..." without the
  130. # patch level, handle this here:
  131. if(NOT _gcc_version)
  132. string (REGEX REPLACE ".*\\(GCC\\).* ([34]\\.[0-9]) .*" "\\1.0" _gcc_version "${_gcc_version_info}")
  133. endif()
  134. if(${_gcc_version} VERSION_LESS "4.2")
  135. set(GCC_TOO_OLD TRUE)
  136. message(WARNING "GCC version older than 4.2")
  137. endif()
  138. endif()
  139. # Exclude XL here because it misinterprets -fvisibility=hidden even though
  140. # the check_cxx_compiler_flag passes
  141. # http://www.cdash.org/CDash/testDetails.php?test=109109951&build=1419259
  142. if (NOT GCC_TOO_OLD AND NOT WIN32 AND NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES XL)
  143. check_cxx_compiler_flag(-fvisibility=hidden COMPILER_HAS_HIDDEN_VISIBILITY)
  144. check_cxx_compiler_flag(-fvisibility-inlines-hidden COMPILER_HAS_HIDDEN_INLINE_VISIBILITY)
  145. option(USE_COMPILER_HIDDEN_VISIBILITY "Use HIDDEN visibility support if available." ON)
  146. mark_as_advanced(USE_COMPILER_HIDDEN_VISIBILITY)
  147. endif()
  148. endmacro()
  149. macro(_test_compiler_has_deprecated)
  150. if("${CMAKE_CXX_COMPILER_ID}" MATCHES Borland OR "${CMAKE_CXX_COMPILER_ID}" MATCHES HP)
  151. set(COMPILER_HAS_DEPRECATED "" CACHE INTERNAL "Compiler support for a deprecated attribute")
  152. else()
  153. _check_cxx_compiler_attribute("__attribute__((__deprecated__))" COMPILER_HAS_DEPRECATED_ATTR)
  154. if(COMPILER_HAS_DEPRECATED_ATTR)
  155. set(COMPILER_HAS_DEPRECATED "${COMPILER_HAS_DEPRECATED_ATTR}" CACHE INTERNAL "Compiler support for a deprecated attribute")
  156. else()
  157. _check_cxx_compiler_attribute("__declspec(deprecated)" COMPILER_HAS_DEPRECATED)
  158. endif()
  159. endif()
  160. endmacro()
  161. set(myDir ${CMAKE_CURRENT_LIST_DIR})
  162. macro(_DO_SET_MACRO_VALUES TARGET_LIBRARY)
  163. set(DEFINE_DEPRECATED)
  164. set(DEFINE_EXPORT)
  165. set(DEFINE_IMPORT)
  166. set(DEFINE_NO_EXPORT)
  167. if (COMPILER_HAS_DEPRECATED_ATTR)
  168. set(DEFINE_DEPRECATED "__attribute__ ((__deprecated__))")
  169. elseif(COMPILER_HAS_DEPRECATED)
  170. set(DEFINE_DEPRECATED "__declspec(deprecated)")
  171. endif()
  172. get_property(type TARGET ${TARGET_LIBRARY} PROPERTY TYPE)
  173. if(NOT ${type} STREQUAL "STATIC_LIBRARY")
  174. if(WIN32)
  175. set(DEFINE_EXPORT "__declspec(dllexport)")
  176. set(DEFINE_IMPORT "__declspec(dllimport)")
  177. elseif(COMPILER_HAS_HIDDEN_VISIBILITY AND USE_COMPILER_HIDDEN_VISIBILITY)
  178. set(DEFINE_EXPORT "__attribute__((visibility(\"default\")))")
  179. set(DEFINE_IMPORT "__attribute__((visibility(\"default\")))")
  180. set(DEFINE_NO_EXPORT "__attribute__((visibility(\"hidden\")))")
  181. endif()
  182. endif()
  183. endmacro()
  184. macro(_DO_GENERATE_EXPORT_HEADER TARGET_LIBRARY)
  185. # Option overrides
  186. set(options)
  187. set(oneValueArgs PREFIX_NAME BASE_NAME EXPORT_MACRO_NAME EXPORT_FILE_NAME DEPRECATED_NAME NO_EXPORT_MACRO_NAME STATIC_DEFINE)
  188. set(multiValueArgs)
  189. cmake_parse_arguments(_GEH "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  190. set(BASE_NAME "${TARGET_LIBRARY}")
  191. if(_GEH_BASE_NAME)
  192. set(BASE_NAME ${_GEH_BASE_NAME})
  193. endif()
  194. string(TOUPPER ${BASE_NAME} BASE_NAME_UPPER)
  195. string(TOLOWER ${BASE_NAME} BASE_NAME_LOWER)
  196. # Default options
  197. set(EXPORT_MACRO_NAME "${PREFIX}${BASE_NAME_UPPER}_EXPORT")
  198. set(NO_EXPORT_MACRO_NAME "${PREFIX}${BASE_NAME_UPPER}_NO_EXPORT")
  199. set(EXPORT_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/${BASE_NAME_LOWER}_export.h")
  200. set(DEPRECATED_NAME "${PREFIX}${BASE_NAME_UPPER}_DEPRECATED")
  201. set(STATIC_DEFINE "${PREFIX}${BASE_NAME_UPPER}_STATIC_DEFINE")
  202. if(_GEH_UNPARSED_ARGUMENTS)
  203. message(FATAL_ERROR "Unknown keywords given to GENERATE_EXPORT_HEADER(): \"${_GEH_UNPARSED_ARGUMENTS}\"")
  204. endif()
  205. if(_GEH_EXPORT_MACRO_NAME)
  206. set(EXPORT_MACRO_NAME ${PREFIX}${_GEH_EXPORT_MACRO_NAME})
  207. endif()
  208. if(_GEH_EXPORT_FILE_NAME)
  209. if(IS_ABSOLUTE _GEH_EXPORT_FILE_NAME)
  210. set(EXPORT_FILE_NAME ${_GEH_EXPORT_FILE_NAME})
  211. else()
  212. set(EXPORT_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/${_GEH_EXPORT_FILE_NAME}")
  213. endif()
  214. endif()
  215. if(_GEH_DEPRECATED_NAME)
  216. set(DEPRECATED_NAME ${PREFIX}${_GEH_DEPRECATED_NAME})
  217. endif()
  218. if(_GEH_NO_EXPORT_MACRO_NAME)
  219. set(NO_EXPORT_MACRO_NAME ${PREFIX}${_GEH_NO_EXPORT_MACRO_NAME})
  220. endif()
  221. if(_GEH_STATIC_DEFINE)
  222. set(STATIC_DEFINE ${PREFIX}${_GEH_STATIC_DEFINE})
  223. endif()
  224. set(INCLUDE_GUARD_NAME "${PREFIX}${EXPORT_MACRO_NAME}_H")
  225. get_target_property(EXPORT_IMPORT_CONDITION ${TARGET_LIBRARY} DEFINE_SYMBOL)
  226. if (NOT EXPORT_IMPORT_CONDITION)
  227. set(EXPORT_IMPORT_CONDITION ${TARGET_LIBRARY}_EXPORTS)
  228. endif()
  229. configure_file(${myDir}/exportheader.cmake.in ${EXPORT_FILE_NAME} @ONLY)
  230. endmacro()
  231. function(GENERATE_EXPORT_HEADER TARGET_LIBRARY)
  232. get_property(type TARGET ${TARGET_LIBRARY} PROPERTY TYPE)
  233. if(${type} STREQUAL "MODULE")
  234. message(WARNING "This macro should not be used with libraries of type MODULE")
  235. return()
  236. endif()
  237. if(NOT ${type} STREQUAL "STATIC_LIBRARY" AND NOT ${type} STREQUAL "SHARED_LIBRARY")
  238. message(WARNING "This macro can only be used with libraries")
  239. return()
  240. endif()
  241. _test_compiler_hidden_visibility()
  242. _test_compiler_has_deprecated()
  243. _do_set_macro_values(${TARGET_LIBRARY})
  244. _do_generate_export_header(${TARGET_LIBRARY} ${ARGN})
  245. endfunction()
  246. function(add_compiler_export_flags)
  247. _test_compiler_hidden_visibility()
  248. _test_compiler_has_deprecated()
  249. if(NOT (USE_COMPILER_HIDDEN_VISIBILITY AND COMPILER_HAS_HIDDEN_VISIBILITY))
  250. message(WARNING "Compiler doesn't have hidden visibility")
  251. return()
  252. endif()
  253. set (EXTRA_FLAGS "-fvisibility=hidden")
  254. if(COMPILER_HAS_HIDDEN_INLINE_VISIBILITY)
  255. set (EXTRA_FLAGS "${EXTRA_FLAGS} -fvisibility-inlines-hidden")
  256. endif()
  257. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_FLAGS}" PARENT_SCOPE)
  258. endfunction()