CheckFunctionExists.cmake 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #.rst:
  2. # CheckFunctionExists
  3. # -------------------
  4. #
  5. # Check if a C function can be linked
  6. #
  7. # CHECK_FUNCTION_EXISTS(<function> <variable>)
  8. #
  9. # Check that the <function> is provided by libraries on the system and
  10. # store the result in a <variable>. This does not verify that any
  11. # system header file declares the function, only that it can be found at
  12. # link time (consider using CheckSymbolExists).
  13. #
  14. # The following variables may be set before calling this macro to modify
  15. # the way the check is run:
  16. #
  17. # ::
  18. #
  19. # CMAKE_REQUIRED_FLAGS = string of compile command line flags
  20. # CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
  21. # CMAKE_REQUIRED_INCLUDES = list of include directories
  22. # CMAKE_REQUIRED_LIBRARIES = list of libraries to link
  23. # CMAKE_REQUIRED_QUIET = execute quietly without messages
  24. #=============================================================================
  25. # Copyright 2002-2011 Kitware, Inc.
  26. #
  27. # Distributed under the OSI-approved BSD License (the "License");
  28. # see accompanying file Copyright.txt for details.
  29. #
  30. # This software is distributed WITHOUT ANY WARRANTY; without even the
  31. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  32. # See the License for more information.
  33. #=============================================================================
  34. # (To distribute this file outside of CMake, substitute the full
  35. # License text for the above reference.)
  36. macro(CHECK_FUNCTION_EXISTS FUNCTION VARIABLE)
  37. if(NOT DEFINED "${VARIABLE}")
  38. set(MACRO_CHECK_FUNCTION_DEFINITIONS
  39. "-DCHECK_FUNCTION_EXISTS=${FUNCTION} ${CMAKE_REQUIRED_FLAGS}")
  40. if(NOT CMAKE_REQUIRED_QUIET)
  41. message(STATUS "Looking for ${FUNCTION}")
  42. endif()
  43. if(CMAKE_REQUIRED_LIBRARIES)
  44. set(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES
  45. LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  46. else()
  47. set(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES)
  48. endif()
  49. if(CMAKE_REQUIRED_INCLUDES)
  50. set(CHECK_FUNCTION_EXISTS_ADD_INCLUDES
  51. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
  52. else()
  53. set(CHECK_FUNCTION_EXISTS_ADD_INCLUDES)
  54. endif()
  55. try_compile(${VARIABLE}
  56. ${CMAKE_BINARY_DIR}
  57. ${CMAKE_ROOT}/Modules/CheckFunctionExists.c
  58. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  59. ${CHECK_FUNCTION_EXISTS_ADD_LIBRARIES}
  60. CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
  61. "${CHECK_FUNCTION_EXISTS_ADD_INCLUDES}"
  62. OUTPUT_VARIABLE OUTPUT)
  63. if(${VARIABLE})
  64. set(${VARIABLE} 1 CACHE INTERNAL "Have function ${FUNCTION}")
  65. if(NOT CMAKE_REQUIRED_QUIET)
  66. message(STATUS "Looking for ${FUNCTION} - found")
  67. endif()
  68. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  69. "Determining if the function ${FUNCTION} exists passed with the following output:\n"
  70. "${OUTPUT}\n\n")
  71. else()
  72. if(NOT CMAKE_REQUIRED_QUIET)
  73. message(STATUS "Looking for ${FUNCTION} - not found")
  74. endif()
  75. set(${VARIABLE} "" CACHE INTERNAL "Have function ${FUNCTION}")
  76. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  77. "Determining if the function ${FUNCTION} exists failed with the following output:\n"
  78. "${OUTPUT}\n\n")
  79. endif()
  80. endif()
  81. endmacro()