Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Modules / CheckLibraryExists.cmake
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:
5 CheckLibraryExists
6 ------------------
7
8 Check 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
26 The following variables may be set before calling this macro to modify
27 the 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
43 include_guard(GLOBAL)
44
45 macro(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 CheckFunctionExists.c)
65     elseif(CMAKE_CXX_COMPILER_LOADED)
66       set(_cle_source CheckFunctionExists.cxx)
67     else()
68       message(FATAL_ERROR "CHECK_FUNCTION_EXISTS needs either C or CXX language enabled")
69     endif()
70
71     try_compile(${VARIABLE}
72       SOURCE_FROM_FILE "${_cle_source}" "${CMAKE_ROOT}/Modules/CheckFunctionExists.c"
73       COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
74       ${CHECK_LIBRARY_EXISTS_LINK_OPTIONS}
75       LINK_LIBRARIES ${CHECK_LIBRARY_EXISTS_LIBRARIES}
76       CMAKE_FLAGS
77       -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_LIBRARY_EXISTS_DEFINITION}
78       -DLINK_DIRECTORIES:STRING=${LOCATION}
79       OUTPUT_VARIABLE OUTPUT)
80     unset(_cle_source)
81
82     if(${VARIABLE})
83       if(NOT CMAKE_REQUIRED_QUIET)
84         message(CHECK_PASS "found")
85       endif()
86       set(${VARIABLE} 1 CACHE INTERNAL "Have library ${LIBRARY}")
87       file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
88         "Determining if the function ${FUNCTION} exists in the ${LIBRARY} "
89         "passed with the following output:\n"
90         "${OUTPUT}\n\n")
91     else()
92       if(NOT CMAKE_REQUIRED_QUIET)
93         message(CHECK_FAIL "not found")
94       endif()
95       set(${VARIABLE} "" CACHE INTERNAL "Have library ${LIBRARY}")
96       file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
97         "Determining if the function ${FUNCTION} exists in the ${LIBRARY} "
98         "failed with the following output:\n"
99         "${OUTPUT}\n\n")
100     endif()
101   endif()
102 endmacro()