Imported Upstream version 2.8.10.2
[platform/upstream/cmake.git] / Modules / CheckLibraryExists.cmake
1 # - Check if the function exists.
2 # CHECK_LIBRARY_EXISTS (LIBRARY FUNCTION LOCATION VARIABLE)
3 #
4 #  LIBRARY  - the name of the library you are looking for
5 #  FUNCTION - the name of the function
6 #  LOCATION - location where the library should be found
7 #  VARIABLE - variable to store the result
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_LIBRARIES = list of libraries to link
15
16 #=============================================================================
17 # Copyright 2002-2009 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
29 include("${CMAKE_CURRENT_LIST_DIR}/CMakeExpandImportedTargets.cmake")
30
31
32 macro(CHECK_LIBRARY_EXISTS LIBRARY FUNCTION LOCATION VARIABLE)
33   if("${VARIABLE}" MATCHES "^${VARIABLE}$")
34     set(MACRO_CHECK_LIBRARY_EXISTS_DEFINITION
35       "-DCHECK_FUNCTION_EXISTS=${FUNCTION} ${CMAKE_REQUIRED_FLAGS}")
36     message(STATUS "Looking for ${FUNCTION} in ${LIBRARY}")
37     set(CHECK_LIBRARY_EXISTS_LIBRARIES ${LIBRARY})
38     if(CMAKE_REQUIRED_LIBRARIES)
39       # this one translates potentially used imported library targets to their files on disk
40       CMAKE_EXPAND_IMPORTED_TARGETS(_ADJUSTED_CMAKE_REQUIRED_LIBRARIES  LIBRARIES  ${CMAKE_REQUIRED_LIBRARIES} CONFIGURATION "${CMAKE_TRY_COMPILE_CONFIGURATION}")
41       set(CHECK_LIBRARY_EXISTS_LIBRARIES
42         ${CHECK_LIBRARY_EXISTS_LIBRARIES} ${_ADJUSTED_CMAKE_REQUIRED_LIBRARIES})
43     endif()
44     try_compile(${VARIABLE}
45       ${CMAKE_BINARY_DIR}
46       ${CMAKE_ROOT}/Modules/CheckFunctionExists.c
47       COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
48       CMAKE_FLAGS
49       -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_LIBRARY_EXISTS_DEFINITION}
50       -DLINK_DIRECTORIES:STRING=${LOCATION}
51       "-DLINK_LIBRARIES:STRING=${CHECK_LIBRARY_EXISTS_LIBRARIES}"
52       OUTPUT_VARIABLE OUTPUT)
53
54     if(${VARIABLE})
55       message(STATUS "Looking for ${FUNCTION} in ${LIBRARY} - found")
56       set(${VARIABLE} 1 CACHE INTERNAL "Have library ${LIBRARY}")
57       file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
58         "Determining if the function ${FUNCTION} exists in the ${LIBRARY} "
59         "passed with the following output:\n"
60         "${OUTPUT}\n\n")
61     else()
62       message(STATUS "Looking for ${FUNCTION} in ${LIBRARY} - not found")
63       set(${VARIABLE} "" CACHE INTERNAL "Have library ${LIBRARY}")
64       file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
65         "Determining if the function ${FUNCTION} exists in the ${LIBRARY} "
66         "failed with the following output:\n"
67         "${OUTPUT}\n\n")
68     endif()
69   endif()
70 endmacro()