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