CheckSharedLibraryExists.cmake 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. # ARGCOUNT - number of arguments for stdcall functions
  7. # LOCATION - location where the library should be found
  8. # VARIABLE - variable to store the result
  9. #
  10. # The following variables may be set before calling this macro to
  11. # modify the way the check is run:
  12. #
  13. # CMAKE_REQUIRED_FLAGS = string of compile command line flags
  14. # CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
  15. # CMAKE_REQUIRED_LIBRARIES = list of libraries to link
  16. MACRO(CHECK_SHARED_LIBRARY_EXISTS LIBRARY FUNCTION ARGCOUNT LOCATION VARIABLE)
  17. IF("${VARIABLE}" MATCHES "^${VARIABLE}$")
  18. SET(MACRO_CHECK_LIBRARY_EXISTS_DEFINITION
  19. "-DCHECK_SHARED_FUNCTION_EXISTS=${FUNCTION} ${CMAKE_REQUIRED_FLAGS}")
  20. IF(WIN32)
  21. IF(${ARGCOUNT} GREATER 0)
  22. SET(ARGSTACK "void*")
  23. SET(CALLSTACK "NULL")
  24. SET(CURARG 1)
  25. WHILE(${ARGCOUNT} GREATER ${CURARG})
  26. SET(ARGSTACK "${ARGSTACK},void*")
  27. SET(CALLSTACK "${CALLSTACK},NULL")
  28. MATH(EXPR CURARG "${CURARG} + 1")
  29. ENDWHILE(${ARGCOUNT} GREATER ${CURARG})
  30. SET(MACRO_CHECK_LIBRARY_EXISTS_DEFINITION
  31. "-D_WIN32 -DARGSTACK=\"${ARGSTACK}\" -DCALLSTACK=\"${CALLSTACK}\" ${MACRO_CHECK_LIBRARY_EXISTS_DEFINITION}")
  32. ELSE(${ARGCOUNT} GREATER 0)
  33. SET(MACRO_CHECK_LIBRARY_EXISTS_DEFINITION
  34. "-D_WIN32 ${MACRO_CHECK_LIBRARY_EXISTS_DEFINITION}")
  35. ENDIF(${ARGCOUNT} GREATER 0)
  36. ENDIF(WIN32)
  37. MESSAGE(STATUS "Looking for ${FUNCTION} in ${LIBRARY}")
  38. SET(CHECK_LIBRARY_EXISTS_LIBRARIES ${LIBRARY})
  39. IF(CMAKE_REQUIRED_LIBRARIES)
  40. SET(CHECK_LIBRARY_EXISTS_LIBRARIES
  41. ${CHECK_LIBRARY_EXISTS_LIBRARIES} ${CMAKE_REQUIRED_LIBRARIES})
  42. ENDIF(CMAKE_REQUIRED_LIBRARIES)
  43. TRY_COMPILE(${VARIABLE}
  44. ${CMAKE_BINARY_DIR}
  45. ${CMAKE_SOURCE_DIR}/cmake/CheckSharedFunctionExists.c
  46. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  47. CMAKE_FLAGS
  48. -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_LIBRARY_EXISTS_DEFINITION}
  49. -DLINK_DIRECTORIES:STRING=${LOCATION}
  50. "-DLINK_LIBRARIES:STRING=${CHECK_LIBRARY_EXISTS_LIBRARIES}"
  51. OUTPUT_VARIABLE OUTPUT)
  52. IF(${VARIABLE})
  53. MESSAGE(STATUS "Looking for ${FUNCTION} in ${LIBRARY} - found")
  54. SET(${VARIABLE} 1 CACHE INTERNAL "Have library ${LIBRARY}")
  55. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  56. "Determining if the function ${FUNCTION} exists in the ${LIBRARY} "
  57. "passed with the following output:\n"
  58. "${OUTPUT}\n\n")
  59. ELSE(${VARIABLE})
  60. MESSAGE(STATUS "Looking for ${FUNCTION} in ${LIBRARY} - not found")
  61. SET(${VARIABLE} "" CACHE INTERNAL "Have library ${LIBRARY}")
  62. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  63. "Determining if the function ${FUNCTION} exists in the ${LIBRARY} "
  64. "failed with the following output:\n"
  65. "${OUTPUT}\n\n")
  66. ENDIF(${VARIABLE})
  67. ENDIF("${VARIABLE}" MATCHES "^${VARIABLE}$")
  68. ENDMACRO(CHECK_SHARED_LIBRARY_EXISTS)