CheckFunctionExists.cmake 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. CheckFunctionExists
  5. -------------------
  6. Check once if a C function can be linked from system libraries.
  7. .. command:: check_function_exists
  8. .. code-block:: cmake
  9. check_function_exists(<function> <variable>)
  10. Checks that the ``<function>`` is provided by libraries on the system and store
  11. the result in internal cache variable ``<variable>``.
  12. The following variables may be set before calling this macro to modify the
  13. way the check is run:
  14. .. include:: /module/CMAKE_REQUIRED_FLAGS.txt
  15. .. include:: /module/CMAKE_REQUIRED_DEFINITIONS.txt
  16. .. include:: /module/CMAKE_REQUIRED_INCLUDES.txt
  17. .. include:: /module/CMAKE_REQUIRED_LINK_OPTIONS.txt
  18. .. include:: /module/CMAKE_REQUIRED_LIBRARIES.txt
  19. .. include:: /module/CMAKE_REQUIRED_LINK_DIRECTORIES.txt
  20. .. include:: /module/CMAKE_REQUIRED_QUIET.txt
  21. .. note::
  22. Prefer using :module:`CheckSymbolExists` or :module:`CheckSourceCompiles`
  23. instead of this module, for the following reasons:
  24. * ``check_function_exists()`` can't detect functions that are inlined
  25. in headers or specified as a macro.
  26. * ``check_function_exists()`` can't detect anything in the 32-bit
  27. versions of the Win32 API, because of a mismatch in calling conventions.
  28. * ``check_function_exists()`` only verifies linking, it does not verify
  29. that the function is declared in system headers.
  30. #]=======================================================================]
  31. include_guard(GLOBAL)
  32. macro(CHECK_FUNCTION_EXISTS FUNCTION VARIABLE)
  33. if(NOT DEFINED "${VARIABLE}" OR "x${${VARIABLE}}" STREQUAL "x${VARIABLE}")
  34. set(MACRO_CHECK_FUNCTION_DEFINITIONS
  35. "-DCHECK_FUNCTION_EXISTS=${FUNCTION} ${CMAKE_REQUIRED_FLAGS}")
  36. if(NOT CMAKE_REQUIRED_QUIET)
  37. message(CHECK_START "Looking for ${FUNCTION}")
  38. endif()
  39. if(CMAKE_REQUIRED_LINK_OPTIONS)
  40. set(CHECK_FUNCTION_EXISTS_ADD_LINK_OPTIONS
  41. LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
  42. else()
  43. set(CHECK_FUNCTION_EXISTS_ADD_LINK_OPTIONS)
  44. endif()
  45. if(CMAKE_REQUIRED_LIBRARIES)
  46. set(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES
  47. LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  48. else()
  49. set(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES)
  50. endif()
  51. if(CMAKE_REQUIRED_LINK_DIRECTORIES)
  52. set(_CFE_LINK_DIRECTORIES
  53. "-DLINK_DIRECTORIES:STRING=${CMAKE_REQUIRED_LINK_DIRECTORIES}")
  54. else()
  55. set(_CFE_LINK_DIRECTORIES)
  56. endif()
  57. if(CMAKE_REQUIRED_INCLUDES)
  58. set(CHECK_FUNCTION_EXISTS_ADD_INCLUDES
  59. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
  60. else()
  61. set(CHECK_FUNCTION_EXISTS_ADD_INCLUDES)
  62. endif()
  63. if(CMAKE_C_COMPILER_LOADED)
  64. set(_cfe_source CheckFunctionExists.c)
  65. elseif(CMAKE_CXX_COMPILER_LOADED)
  66. set(_cfe_source CheckFunctionExists.cxx)
  67. else()
  68. message(FATAL_ERROR "CHECK_FUNCTION_EXISTS needs either C or CXX language enabled")
  69. endif()
  70. try_compile(${VARIABLE}
  71. SOURCE_FROM_FILE "${_cfe_source}" "${CMAKE_ROOT}/Modules/CheckFunctionExists.c"
  72. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  73. ${CHECK_FUNCTION_EXISTS_ADD_LINK_OPTIONS}
  74. ${CHECK_FUNCTION_EXISTS_ADD_LIBRARIES}
  75. CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
  76. "${CHECK_FUNCTION_EXISTS_ADD_INCLUDES}"
  77. "${_CFE_LINK_DIRECTORIES}"
  78. )
  79. unset(_cfe_source)
  80. unset(_CFE_LINK_DIRECTORIES)
  81. if(${VARIABLE})
  82. set(${VARIABLE} 1 CACHE INTERNAL "Have function ${FUNCTION}")
  83. if(NOT CMAKE_REQUIRED_QUIET)
  84. message(CHECK_PASS "found")
  85. endif()
  86. else()
  87. if(NOT CMAKE_REQUIRED_QUIET)
  88. message(CHECK_FAIL "not found")
  89. endif()
  90. set(${VARIABLE} "" CACHE INTERNAL "Have function ${FUNCTION}")
  91. endif()
  92. endif()
  93. endmacro()