CheckFunctionExists.cmake 3.2 KB

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