CheckFunctionExists.cmake 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. macro(CHECK_FUNCTION_EXISTS FUNCTION VARIABLE)
  40. if(NOT DEFINED "${VARIABLE}" OR "x${${VARIABLE}}" STREQUAL "x${VARIABLE}")
  41. set(MACRO_CHECK_FUNCTION_DEFINITIONS
  42. "-DCHECK_FUNCTION_EXISTS=${FUNCTION} ${CMAKE_REQUIRED_FLAGS}")
  43. if(NOT CMAKE_REQUIRED_QUIET)
  44. message(STATUS "Looking for ${FUNCTION}")
  45. endif()
  46. if(CMAKE_REQUIRED_LIBRARIES)
  47. set(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES
  48. LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  49. else()
  50. set(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES)
  51. endif()
  52. if(CMAKE_REQUIRED_INCLUDES)
  53. set(CHECK_FUNCTION_EXISTS_ADD_INCLUDES
  54. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
  55. else()
  56. set(CHECK_FUNCTION_EXISTS_ADD_INCLUDES)
  57. endif()
  58. if(CMAKE_C_COMPILER_LOADED)
  59. set(_cfe_source ${CMAKE_ROOT}/Modules/CheckFunctionExists.c)
  60. elseif(CMAKE_CXX_COMPILER_LOADED)
  61. set(_cfe_source ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckFunctionExists/CheckFunctionExists.cxx)
  62. configure_file(${CMAKE_ROOT}/Modules/CheckFunctionExists.c "${_cfe_source}" COPYONLY)
  63. else()
  64. message(FATAL_ERROR "CHECK_FUNCTION_EXISTS needs either C or CXX language enabled")
  65. endif()
  66. try_compile(${VARIABLE}
  67. ${CMAKE_BINARY_DIR}
  68. ${_cfe_source}
  69. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  70. ${CHECK_FUNCTION_EXISTS_ADD_LIBRARIES}
  71. CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
  72. "${CHECK_FUNCTION_EXISTS_ADD_INCLUDES}"
  73. OUTPUT_VARIABLE OUTPUT)
  74. unset(_cfe_source)
  75. if(${VARIABLE})
  76. set(${VARIABLE} 1 CACHE INTERNAL "Have function ${FUNCTION}")
  77. if(NOT CMAKE_REQUIRED_QUIET)
  78. message(STATUS "Looking for ${FUNCTION} - found")
  79. endif()
  80. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  81. "Determining if the function ${FUNCTION} exists passed with the following output:\n"
  82. "${OUTPUT}\n\n")
  83. else()
  84. if(NOT CMAKE_REQUIRED_QUIET)
  85. message(STATUS "Looking for ${FUNCTION} - not found")
  86. endif()
  87. set(${VARIABLE} "" CACHE INTERNAL "Have function ${FUNCTION}")
  88. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  89. "Determining if the function ${FUNCTION} exists failed with the following output:\n"
  90. "${OUTPUT}\n\n")
  91. endif()
  92. endif()
  93. endmacro()