CheckFunctionExists.cmake 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  36. else()
  37. set(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES)
  38. endif()
  39. if(CMAKE_REQUIRED_INCLUDES)
  40. set(CHECK_FUNCTION_EXISTS_ADD_INCLUDES
  41. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
  42. else()
  43. set(CHECK_FUNCTION_EXISTS_ADD_INCLUDES)
  44. endif()
  45. try_compile(${VARIABLE}
  46. ${CMAKE_BINARY_DIR}
  47. ${CMAKE_ROOT}/Modules/CheckFunctionExists.c
  48. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  49. ${CHECK_FUNCTION_EXISTS_ADD_LIBRARIES}
  50. CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
  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()
  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()
  66. endif()
  67. endmacro()