CheckFunctionExists.cmake 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 if a C function can be linked
  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 a ``<variable>``, which will be created as an internal
  12. cache variable.
  13. The following variables may be set before calling this macro to modify the
  14. way the check is run:
  15. ``CMAKE_REQUIRED_FLAGS``
  16. string of compile command line flags.
  17. ``CMAKE_REQUIRED_DEFINITIONS``
  18. a :ref:`;-list <CMake Language Lists>` of macros to define (-DFOO=bar).
  19. ``CMAKE_REQUIRED_INCLUDES``
  20. a :ref:`;-list <CMake Language Lists>` of header search paths to pass to
  21. the compiler.
  22. ``CMAKE_REQUIRED_LINK_OPTIONS``
  23. .. versionadded:: 3.14
  24. a :ref:`;-list <CMake Language Lists>` of options to add to the link command.
  25. ``CMAKE_REQUIRED_LIBRARIES``
  26. a :ref:`;-list <CMake Language Lists>` of libraries to add to the link
  27. command. See policy :policy:`CMP0075`.
  28. ``CMAKE_REQUIRED_QUIET``
  29. .. versionadded:: 3.1
  30. execute quietly without messages.
  31. .. note::
  32. Prefer using :Module:`CheckSymbolExists` instead of this module,
  33. for the following reasons:
  34. * ``check_function_exists()`` can't detect functions that are inlined
  35. in headers or specified as a macro.
  36. * ``check_function_exists()`` can't detect anything in the 32-bit
  37. versions of the Win32 API, because of a mismatch in calling conventions.
  38. * ``check_function_exists()`` only verifies linking, it does not verify
  39. that the function is declared in system headers.
  40. #]=======================================================================]
  41. include_guard(GLOBAL)
  42. macro(CHECK_FUNCTION_EXISTS FUNCTION VARIABLE)
  43. if(NOT DEFINED "${VARIABLE}" OR "x${${VARIABLE}}" STREQUAL "x${VARIABLE}")
  44. set(MACRO_CHECK_FUNCTION_DEFINITIONS
  45. "-DCHECK_FUNCTION_EXISTS=${FUNCTION} ${CMAKE_REQUIRED_FLAGS}")
  46. if(NOT CMAKE_REQUIRED_QUIET)
  47. message(CHECK_START "Looking for ${FUNCTION}")
  48. endif()
  49. if(CMAKE_REQUIRED_LINK_OPTIONS)
  50. set(CHECK_FUNCTION_EXISTS_ADD_LINK_OPTIONS
  51. LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
  52. else()
  53. set(CHECK_FUNCTION_EXISTS_ADD_LINK_OPTIONS)
  54. endif()
  55. if(CMAKE_REQUIRED_LIBRARIES)
  56. set(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES
  57. LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  58. else()
  59. set(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES)
  60. endif()
  61. if(CMAKE_REQUIRED_INCLUDES)
  62. set(CHECK_FUNCTION_EXISTS_ADD_INCLUDES
  63. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
  64. else()
  65. set(CHECK_FUNCTION_EXISTS_ADD_INCLUDES)
  66. endif()
  67. if(CMAKE_C_COMPILER_LOADED)
  68. set(_cfe_source ${CMAKE_ROOT}/Modules/CheckFunctionExists.c)
  69. elseif(CMAKE_CXX_COMPILER_LOADED)
  70. set(_cfe_source ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckFunctionExists/CheckFunctionExists.cxx)
  71. configure_file(${CMAKE_ROOT}/Modules/CheckFunctionExists.c "${_cfe_source}" COPYONLY)
  72. else()
  73. message(FATAL_ERROR "CHECK_FUNCTION_EXISTS needs either C or CXX language enabled")
  74. endif()
  75. try_compile(${VARIABLE}
  76. ${CMAKE_BINARY_DIR}
  77. ${_cfe_source}
  78. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  79. ${CHECK_FUNCTION_EXISTS_ADD_LINK_OPTIONS}
  80. ${CHECK_FUNCTION_EXISTS_ADD_LIBRARIES}
  81. CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
  82. "${CHECK_FUNCTION_EXISTS_ADD_INCLUDES}"
  83. OUTPUT_VARIABLE OUTPUT)
  84. unset(_cfe_source)
  85. if(${VARIABLE})
  86. set(${VARIABLE} 1 CACHE INTERNAL "Have function ${FUNCTION}")
  87. if(NOT CMAKE_REQUIRED_QUIET)
  88. message(CHECK_PASS "found")
  89. endif()
  90. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  91. "Determining if the function ${FUNCTION} exists passed with the following output:\n"
  92. "${OUTPUT}\n\n")
  93. else()
  94. if(NOT CMAKE_REQUIRED_QUIET)
  95. message(CHECK_FAIL "not found")
  96. endif()
  97. set(${VARIABLE} "" CACHE INTERNAL "Have function ${FUNCTION}")
  98. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  99. "Determining if the function ${FUNCTION} exists failed with the following output:\n"
  100. "${OUTPUT}\n\n")
  101. endif()
  102. endif()
  103. endmacro()