CheckLibraryExists.cmake 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #.rst:
  2. # CheckLibraryExists
  3. # ------------------
  4. #
  5. # Check if the function exists.
  6. #
  7. # CHECK_LIBRARY_EXISTS (LIBRARY FUNCTION LOCATION VARIABLE)
  8. #
  9. # ::
  10. #
  11. # LIBRARY - the name of the library you are looking for
  12. # FUNCTION - the name of the function
  13. # LOCATION - location where the library should be found
  14. # VARIABLE - variable to store the result
  15. #
  16. #
  17. #
  18. # The following variables may be set before calling this macro to modify
  19. # the way the check is run:
  20. #
  21. # ::
  22. #
  23. # CMAKE_REQUIRED_FLAGS = string of compile command line flags
  24. # CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
  25. # CMAKE_REQUIRED_LIBRARIES = list of libraries to link
  26. # CMAKE_REQUIRED_QUIET = execute quietly without messages
  27. #=============================================================================
  28. # Copyright 2002-2009 Kitware, Inc.
  29. #
  30. # Distributed under the OSI-approved BSD License (the "License");
  31. # see accompanying file Copyright.txt for details.
  32. #
  33. # This software is distributed WITHOUT ANY WARRANTY; without even the
  34. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  35. # See the License for more information.
  36. #=============================================================================
  37. # (To distribute this file outside of CMake, substitute the full
  38. # License text for the above reference.)
  39. macro(CHECK_LIBRARY_EXISTS LIBRARY FUNCTION LOCATION VARIABLE)
  40. if("${VARIABLE}" MATCHES "^${VARIABLE}$")
  41. set(MACRO_CHECK_LIBRARY_EXISTS_DEFINITION
  42. "-DCHECK_FUNCTION_EXISTS=${FUNCTION} ${CMAKE_REQUIRED_FLAGS}")
  43. if(NOT CMAKE_REQUIRED_QUIET)
  44. message(STATUS "Looking for ${FUNCTION} in ${LIBRARY}")
  45. endif()
  46. set(CHECK_LIBRARY_EXISTS_LIBRARIES ${LIBRARY})
  47. if(CMAKE_REQUIRED_LIBRARIES)
  48. set(CHECK_LIBRARY_EXISTS_LIBRARIES
  49. ${CHECK_LIBRARY_EXISTS_LIBRARIES} ${CMAKE_REQUIRED_LIBRARIES})
  50. endif()
  51. try_compile(${VARIABLE}
  52. ${CMAKE_BINARY_DIR}
  53. ${CMAKE_ROOT}/Modules/CheckFunctionExists.c
  54. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  55. LINK_LIBRARIES ${CHECK_LIBRARY_EXISTS_LIBRARIES}
  56. CMAKE_FLAGS
  57. -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_LIBRARY_EXISTS_DEFINITION}
  58. -DLINK_DIRECTORIES:STRING=${LOCATION}
  59. OUTPUT_VARIABLE OUTPUT)
  60. if(${VARIABLE})
  61. if(NOT CMAKE_REQUIRED_QUIET)
  62. message(STATUS "Looking for ${FUNCTION} in ${LIBRARY} - found")
  63. endif()
  64. set(${VARIABLE} 1 CACHE INTERNAL "Have library ${LIBRARY}")
  65. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  66. "Determining if the function ${FUNCTION} exists in the ${LIBRARY} "
  67. "passed with the following output:\n"
  68. "${OUTPUT}\n\n")
  69. else()
  70. if(NOT CMAKE_REQUIRED_QUIET)
  71. message(STATUS "Looking for ${FUNCTION} in ${LIBRARY} - not found")
  72. endif()
  73. set(${VARIABLE} "" CACHE INTERNAL "Have library ${LIBRARY}")
  74. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  75. "Determining if the function ${FUNCTION} exists in the ${LIBRARY} "
  76. "failed with the following output:\n"
  77. "${OUTPUT}\n\n")
  78. endif()
  79. endif()
  80. endmacro()