CheckFunctionExists.cmake 4.4 KB

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