CheckLibraryExists.cmake 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. # Copyright 2002-2009 Kitware, Inc.
  17. #
  18. # Distributed under the OSI-approved BSD License (the "License");
  19. # see accompanying file Copyright.txt for details.
  20. #
  21. # This software is distributed WITHOUT ANY WARRANTY; without even the
  22. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  23. # See the License for more information.
  24. #=============================================================================
  25. # (To distribute this file outside of CMake, substitute the full
  26. # License text for the above reference.)
  27. INCLUDE("${CMAKE_CURRENT_LIST_DIR}/CMakeExpandImportedTargets.cmake")
  28. MACRO(CHECK_LIBRARY_EXISTS LIBRARY FUNCTION LOCATION VARIABLE)
  29. IF("${VARIABLE}" MATCHES "^${VARIABLE}$")
  30. SET(MACRO_CHECK_LIBRARY_EXISTS_DEFINITION
  31. "-DCHECK_FUNCTION_EXISTS=${FUNCTION} ${CMAKE_REQUIRED_FLAGS}")
  32. MESSAGE(STATUS "Looking for ${FUNCTION} in ${LIBRARY}")
  33. SET(CHECK_LIBRARY_EXISTS_LIBRARIES ${LIBRARY})
  34. IF(CMAKE_REQUIRED_LIBRARIES)
  35. # this one translates potentially used imported library targets to their files on disk
  36. CMAKE_EXPAND_IMPORTED_TARGETS(_ADJUSTED_CMAKE_REQUIRED_LIBRARIES LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} CONFIGURATION "${CMAKE_TRY_COMPILE_CONFIGURATION}")
  37. SET(CHECK_LIBRARY_EXISTS_LIBRARIES
  38. ${CHECK_LIBRARY_EXISTS_LIBRARIES} ${_ADJUSTED_CMAKE_REQUIRED_LIBRARIES})
  39. ENDIF(CMAKE_REQUIRED_LIBRARIES)
  40. TRY_COMPILE(${VARIABLE}
  41. ${CMAKE_BINARY_DIR}
  42. ${CMAKE_ROOT}/Modules/CheckFunctionExists.c
  43. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  44. CMAKE_FLAGS
  45. -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_LIBRARY_EXISTS_DEFINITION}
  46. -DLINK_DIRECTORIES:STRING=${LOCATION}
  47. "-DLINK_LIBRARIES:STRING=${CHECK_LIBRARY_EXISTS_LIBRARIES}"
  48. OUTPUT_VARIABLE OUTPUT)
  49. IF(${VARIABLE})
  50. MESSAGE(STATUS "Looking for ${FUNCTION} in ${LIBRARY} - found")
  51. SET(${VARIABLE} 1 CACHE INTERNAL "Have library ${LIBRARY}")
  52. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  53. "Determining if the function ${FUNCTION} exists in the ${LIBRARY} "
  54. "passed with the following output:\n"
  55. "${OUTPUT}\n\n")
  56. ELSE(${VARIABLE})
  57. MESSAGE(STATUS "Looking for ${FUNCTION} in ${LIBRARY} - not found")
  58. SET(${VARIABLE} "" CACHE INTERNAL "Have library ${LIBRARY}")
  59. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  60. "Determining if the function ${FUNCTION} exists in the ${LIBRARY} "
  61. "failed with the following output:\n"
  62. "${OUTPUT}\n\n")
  63. ENDIF(${VARIABLE})
  64. ENDIF("${VARIABLE}" MATCHES "^${VARIABLE}$")
  65. ENDMACRO(CHECK_LIBRARY_EXISTS)