CheckLibraryExists.cmake 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. Check once if the function exists in system or specified library.
  7. .. command:: CHECK_LIBRARY_EXISTS
  8. .. code-block:: cmake
  9. CHECK_LIBRARY_EXISTS(LIBRARY FUNCTION LOCATION VARIABLE)
  10. ::
  11. LIBRARY - the name of the library you are looking for
  12. FUNCTION - the name of the function
  13. LOCATION - location where the library should be found
  14. VARIABLE - internal cache variable to store the result
  15. Prefer using :module:`CheckSymbolExists` or :module:`CheckSourceCompiles`
  16. instead of this module for more robust detection if a function is available in
  17. a library.
  18. The following variables may be set before calling this macro to modify
  19. the way the check is run:
  20. .. include:: /module/CMAKE_REQUIRED_FLAGS.txt
  21. .. include:: /module/CMAKE_REQUIRED_DEFINITIONS.txt
  22. .. include:: /module/CMAKE_REQUIRED_LINK_OPTIONS.txt
  23. .. include:: /module/CMAKE_REQUIRED_LIBRARIES.txt
  24. .. include:: /module/CMAKE_REQUIRED_LINK_DIRECTORIES.txt
  25. .. include:: /module/CMAKE_REQUIRED_QUIET.txt
  26. #]=======================================================================]
  27. include_guard(GLOBAL)
  28. macro(CHECK_LIBRARY_EXISTS LIBRARY FUNCTION LOCATION VARIABLE)
  29. if(NOT DEFINED "${VARIABLE}")
  30. set(MACRO_CHECK_LIBRARY_EXISTS_DEFINITION
  31. "-DCHECK_FUNCTION_EXISTS=${FUNCTION} ${CMAKE_REQUIRED_FLAGS}")
  32. if(NOT CMAKE_REQUIRED_QUIET)
  33. message(CHECK_START "Looking for ${FUNCTION} in ${LIBRARY}")
  34. endif()
  35. set(CHECK_LIBRARY_EXISTS_LINK_OPTIONS)
  36. if(CMAKE_REQUIRED_LINK_OPTIONS)
  37. set(CHECK_LIBRARY_EXISTS_LINK_OPTIONS
  38. LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
  39. endif()
  40. set(CHECK_LIBRARY_EXISTS_LIBRARIES ${LIBRARY})
  41. if(CMAKE_REQUIRED_LIBRARIES)
  42. set(CHECK_LIBRARY_EXISTS_LIBRARIES
  43. ${CHECK_LIBRARY_EXISTS_LIBRARIES} ${CMAKE_REQUIRED_LIBRARIES})
  44. endif()
  45. if(CMAKE_REQUIRED_LINK_DIRECTORIES)
  46. set(_CLE_LINK_DIRECTORIES
  47. "-DLINK_DIRECTORIES:STRING=${LOCATION};${CMAKE_REQUIRED_LINK_DIRECTORIES}")
  48. else()
  49. set(_CLE_LINK_DIRECTORIES "-DLINK_DIRECTORIES:STRING=${LOCATION}")
  50. endif()
  51. if(CMAKE_C_COMPILER_LOADED)
  52. set(_cle_source CheckFunctionExists.c)
  53. elseif(CMAKE_CXX_COMPILER_LOADED)
  54. set(_cle_source CheckFunctionExists.cxx)
  55. else()
  56. message(FATAL_ERROR "CHECK_FUNCTION_EXISTS needs either C or CXX language enabled")
  57. endif()
  58. try_compile(${VARIABLE}
  59. SOURCE_FROM_FILE "${_cle_source}" "${CMAKE_ROOT}/Modules/CheckFunctionExists.c"
  60. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  61. ${CHECK_LIBRARY_EXISTS_LINK_OPTIONS}
  62. LINK_LIBRARIES ${CHECK_LIBRARY_EXISTS_LIBRARIES}
  63. CMAKE_FLAGS
  64. -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_LIBRARY_EXISTS_DEFINITION}
  65. "${_CLE_LINK_DIRECTORIES}"
  66. )
  67. unset(_cle_source)
  68. unset(_CLE_LINK_DIRECTORIES)
  69. if(${VARIABLE})
  70. if(NOT CMAKE_REQUIRED_QUIET)
  71. message(CHECK_PASS "found")
  72. endif()
  73. set(${VARIABLE} 1 CACHE INTERNAL "Have library ${LIBRARY}")
  74. else()
  75. if(NOT CMAKE_REQUIRED_QUIET)
  76. message(CHECK_FAIL "not found")
  77. endif()
  78. set(${VARIABLE} "" CACHE INTERNAL "Have library ${LIBRARY}")
  79. endif()
  80. endif()
  81. endmacro()