CheckLibraryExists.cmake 2.8 KB

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