CheckLibraryExists.cmake 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 distributed 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(CMAKE_REQUIRED_LIBRARIES)
  37. TRY_COMPILE(${VARIABLE}
  38. ${CMAKE_BINARY_DIR}
  39. ${CMAKE_ROOT}/Modules/CheckFunctionExists.c
  40. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  41. CMAKE_FLAGS
  42. -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_LIBRARY_EXISTS_DEFINITION}
  43. -DLINK_DIRECTORIES:STRING=${LOCATION}
  44. "-DLINK_LIBRARIES:STRING=${CHECK_LIBRARY_EXISTS_LIBRARIES}"
  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(${VARIABLE})
  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(${VARIABLE})
  61. ENDIF("${VARIABLE}" MATCHES "^${VARIABLE}$")
  62. ENDMACRO(CHECK_LIBRARY_EXISTS)