CheckFunctionExists.cmake 3.8 KB

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