CheckFunctionExists.cmake 3.0 KB

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