1
0

CheckFunctionExists.cmake 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. include("${CMAKE_CURRENT_LIST_DIR}/CMakeExpandImportedTargets.cmake")
  29. macro(CHECK_FUNCTION_EXISTS FUNCTION VARIABLE)
  30. if("${VARIABLE}" MATCHES "^${VARIABLE}$")
  31. set(MACRO_CHECK_FUNCTION_DEFINITIONS
  32. "-DCHECK_FUNCTION_EXISTS=${FUNCTION} ${CMAKE_REQUIRED_FLAGS}")
  33. message(STATUS "Looking for ${FUNCTION}")
  34. if(CMAKE_REQUIRED_LIBRARIES)
  35. # this one translates potentially used imported library targets to their files on disk
  36. CMAKE_EXPAND_IMPORTED_TARGETS(_ADJUSTED_CMAKE_REQUIRED_LIBRARIES LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} CONFIGURATION "${CMAKE_TRY_COMPILE_CONFIGURATION}")
  37. set(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES
  38. "-DLINK_LIBRARIES:STRING=${_ADJUSTED_CMAKE_REQUIRED_LIBRARIES}")
  39. else()
  40. set(CHECK_FUNCTION_EXISTS_ADD_LIBRARIES)
  41. endif()
  42. if(CMAKE_REQUIRED_INCLUDES)
  43. set(CHECK_FUNCTION_EXISTS_ADD_INCLUDES
  44. "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}")
  45. else()
  46. set(CHECK_FUNCTION_EXISTS_ADD_INCLUDES)
  47. endif()
  48. try_compile(${VARIABLE}
  49. ${CMAKE_BINARY_DIR}
  50. ${CMAKE_ROOT}/Modules/CheckFunctionExists.c
  51. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  52. CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
  53. "${CHECK_FUNCTION_EXISTS_ADD_LIBRARIES}"
  54. "${CHECK_FUNCTION_EXISTS_ADD_INCLUDES}"
  55. OUTPUT_VARIABLE OUTPUT)
  56. if(${VARIABLE})
  57. set(${VARIABLE} 1 CACHE INTERNAL "Have function ${FUNCTION}")
  58. message(STATUS "Looking for ${FUNCTION} - found")
  59. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  60. "Determining if the function ${FUNCTION} exists passed with the following output:\n"
  61. "${OUTPUT}\n\n")
  62. else()
  63. message(STATUS "Looking for ${FUNCTION} - not found")
  64. set(${VARIABLE} "" CACHE INTERNAL "Have function ${FUNCTION}")
  65. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  66. "Determining if the function ${FUNCTION} exists failed with the following output:\n"
  67. "${OUTPUT}\n\n")
  68. endif()
  69. endif()
  70. endmacro()