CheckLibraryExists.cmake 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #.rst:
  4. # CheckLibraryExists
  5. # ------------------
  6. #
  7. # Check if the function exists.
  8. #
  9. # CHECK_LIBRARY_EXISTS (LIBRARY FUNCTION LOCATION VARIABLE)
  10. #
  11. # ::
  12. #
  13. # LIBRARY - the name of the library you are looking for
  14. # FUNCTION - the name of the function
  15. # LOCATION - location where the library should be found
  16. # VARIABLE - variable to store the result
  17. # Will be created as an internal cache variable.
  18. #
  19. #
  20. #
  21. # The following variables may be set before calling this macro to modify
  22. # the way the check is run:
  23. #
  24. # ::
  25. #
  26. # CMAKE_REQUIRED_FLAGS = string of compile command line flags
  27. # CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
  28. # CMAKE_REQUIRED_LIBRARIES = list of libraries to link
  29. # CMAKE_REQUIRED_QUIET = execute quietly without messages
  30. include_guard(GLOBAL)
  31. macro(CHECK_LIBRARY_EXISTS LIBRARY FUNCTION LOCATION VARIABLE)
  32. if(NOT DEFINED "${VARIABLE}")
  33. set(MACRO_CHECK_LIBRARY_EXISTS_DEFINITION
  34. "-DCHECK_FUNCTION_EXISTS=${FUNCTION} ${CMAKE_REQUIRED_FLAGS}")
  35. if(NOT CMAKE_REQUIRED_QUIET)
  36. message(STATUS "Looking for ${FUNCTION} in ${LIBRARY}")
  37. endif()
  38. set(CHECK_LIBRARY_EXISTS_LIBRARIES ${LIBRARY})
  39. if(CMAKE_REQUIRED_LIBRARIES)
  40. set(CHECK_LIBRARY_EXISTS_LIBRARIES
  41. ${CHECK_LIBRARY_EXISTS_LIBRARIES} ${CMAKE_REQUIRED_LIBRARIES})
  42. endif()
  43. if(CMAKE_C_COMPILER_LOADED)
  44. set(_cle_source ${CMAKE_ROOT}/Modules/CheckFunctionExists.c)
  45. elseif(CMAKE_CXX_COMPILER_LOADED)
  46. set(_cle_source ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CheckLibraryExists/CheckFunctionExists.cxx)
  47. configure_file(${CMAKE_ROOT}/Modules/CheckFunctionExists.c "${_cle_source}" COPYONLY)
  48. else()
  49. message(FATAL_ERROR "CHECK_FUNCTION_EXISTS needs either C or CXX language enabled")
  50. endif()
  51. try_compile(${VARIABLE}
  52. ${CMAKE_BINARY_DIR}
  53. ${_cle_source}
  54. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  55. LINK_LIBRARIES ${CHECK_LIBRARY_EXISTS_LIBRARIES}
  56. CMAKE_FLAGS
  57. -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_LIBRARY_EXISTS_DEFINITION}
  58. -DLINK_DIRECTORIES:STRING=${LOCATION}
  59. OUTPUT_VARIABLE OUTPUT)
  60. unset(_cle_source)
  61. if(${VARIABLE})
  62. if(NOT CMAKE_REQUIRED_QUIET)
  63. message(STATUS "Looking for ${FUNCTION} in ${LIBRARY} - found")
  64. endif()
  65. set(${VARIABLE} 1 CACHE INTERNAL "Have library ${LIBRARY}")
  66. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  67. "Determining if the function ${FUNCTION} exists in the ${LIBRARY} "
  68. "passed with the following output:\n"
  69. "${OUTPUT}\n\n")
  70. else()
  71. if(NOT CMAKE_REQUIRED_QUIET)
  72. message(STATUS "Looking for ${FUNCTION} in ${LIBRARY} - not found")
  73. endif()
  74. set(${VARIABLE} "" CACHE INTERNAL "Have library ${LIBRARY}")
  75. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  76. "Determining if the function ${FUNCTION} exists in the ${LIBRARY} "
  77. "failed with the following output:\n"
  78. "${OUTPUT}\n\n")
  79. endif()
  80. endif()
  81. endmacro()