CheckFunctionExists.cmake 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #
  7. # Check if a C function can be linked::
  8. #
  9. # check_function_exists(<function> <variable>)
  10. #
  11. # Check that the ``<function>`` is provided by libraries on the system and store
  12. # the result in a ``<variable>``. ``<variable>`` will be created as an internal
  13. # cache variable.
  14. #
  15. # The following variables may be set before calling this macro to modify the
  16. # way the check is run:
  17. #
  18. # ::
  19. #
  20. # CMAKE_REQUIRED_FLAGS = string of compile command line flags
  21. # CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
  22. # CMAKE_REQUIRED_INCLUDES = list of include directories
  23. # CMAKE_REQUIRED_LIBRARIES = list of libraries to link
  24. # CMAKE_REQUIRED_QUIET = execute quietly without messages
  25. #
  26. # .. note::
  27. #
  28. # Prefer using :Module:`CheckSymbolExists` instead of this module,
  29. # for the following reasons:
  30. #
  31. # * ``check_function_exists()`` can't detect functions that are inlined
  32. # in headers or specified as a macro.
  33. #
  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. #
  37. # * ``check_function_exists()`` only verifies linking, it does not verify
  38. # that the function is declared in system headers.
  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_LIBRARIES)
  48. set(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES
  49. LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  50. else()
  51. set(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES)
  52. endif()
  53. if(CMAKE_REQUIRED_INCLUDES)
  54. set(CHECK_FUNCTION_EXISTS_ADD_INCLUDES
  55. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
  56. else()
  57. set(CHECK_FUNCTION_EXISTS_ADD_INCLUDES)
  58. endif()
  59. if(CMAKE_C_COMPILER_LOADED)
  60. set(_cfe_source ${CMAKE_ROOT}/Modules/CheckFunctionExists.c)
  61. elseif(CMAKE_CXX_COMPILER_LOADED)
  62. set(_cfe_source ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckFunctionExists/CheckFunctionExists.cxx)
  63. configure_file(${CMAKE_ROOT}/Modules/CheckFunctionExists.c "${_cfe_source}" COPYONLY)
  64. else()
  65. message(FATAL_ERROR "CHECK_FUNCTION_EXISTS needs either C or CXX language enabled")
  66. endif()
  67. try_compile(${VARIABLE}
  68. ${CMAKE_BINARY_DIR}
  69. ${_cfe_source}
  70. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  71. ${CHECK_FUNCTION_EXISTS_ADD_LIBRARIES}
  72. CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
  73. "${CHECK_FUNCTION_EXISTS_ADD_INCLUDES}"
  74. OUTPUT_VARIABLE OUTPUT)
  75. unset(_cfe_source)
  76. if(${VARIABLE})
  77. set(${VARIABLE} 1 CACHE INTERNAL "Have function ${FUNCTION}")
  78. if(NOT CMAKE_REQUIRED_QUIET)
  79. message(STATUS "Looking for ${FUNCTION} - found")
  80. endif()
  81. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  82. "Determining if the function ${FUNCTION} exists passed with the following output:\n"
  83. "${OUTPUT}\n\n")
  84. else()
  85. if(NOT CMAKE_REQUIRED_QUIET)
  86. message(STATUS "Looking for ${FUNCTION} - not found")
  87. endif()
  88. set(${VARIABLE} "" CACHE INTERNAL "Have function ${FUNCTION}")
  89. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  90. "Determining if the function ${FUNCTION} exists failed with the following output:\n"
  91. "${OUTPUT}\n\n")
  92. endif()
  93. endif()
  94. endmacro()