Imported Upstream version 2.8.11.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
30
31 macro(CHECK_LIBRARY_EXISTS LIBRARY FUNCTION LOCATION VARIABLE)
32   if("${VARIABLE}" MATCHES "^${VARIABLE}$")
33     set(MACRO_CHECK_LIBRARY_EXISTS_DEFINITION
34       "-DCHECK_FUNCTION_EXISTS=${FUNCTION} ${CMAKE_REQUIRED_FLAGS}")
35     message(STATUS "Looking for ${FUNCTION} in ${LIBRARY}")
36     set(CHECK_LIBRARY_EXISTS_LIBRARIES ${LIBRARY})
37     if(CMAKE_REQUIRED_LIBRARIES)
38       set(CHECK_LIBRARY_EXISTS_LIBRARIES
39         ${CHECK_LIBRARY_EXISTS_LIBRARIES} ${CMAKE_REQUIRED_LIBRARIES})
40     endif()
41     try_compile(${VARIABLE}
42       ${CMAKE_BINARY_DIR}
43       ${CMAKE_ROOT}/Modules/CheckFunctionExists.c
44       COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
45       LINK_LIBRARIES ${CHECK_LIBRARY_EXISTS_LIBRARIES}
46       CMAKE_FLAGS
47       -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_LIBRARY_EXISTS_DEFINITION}
48       -DLINK_DIRECTORIES:STRING=${LOCATION}
49       OUTPUT_VARIABLE OUTPUT)
50
51     if(${VARIABLE})
52       message(STATUS "Looking for ${FUNCTION} in ${LIBRARY} - found")
53       set(${VARIABLE} 1 CACHE INTERNAL "Have library ${LIBRARY}")
54       file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
55         "Determining if the function ${FUNCTION} exists in the ${LIBRARY} "
56         "passed with the following output:\n"
57         "${OUTPUT}\n\n")
58     else()
59       message(STATUS "Looking for ${FUNCTION} in ${LIBRARY} - not found")
60       set(${VARIABLE} "" CACHE INTERNAL "Have library ${LIBRARY}")
61       file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
62         "Determining if the function ${FUNCTION} exists in the ${LIBRARY} "
63         "failed with the following output:\n"
64         "${OUTPUT}\n\n")
65     endif()
66   endif()
67 endmacro()