FindFunctionLibrary.cmake 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # - Check which library is needed to link a C function
  2. # find_function_library( <function> <variable> [<library> ...] )
  3. #
  4. # Check which library provides the <function>.
  5. # Sets <variable> to 0 if found in the global libraries.
  6. # Sets <variable> to the library path if found in the provided libraries.
  7. # Raises a FATAL_ERROR if not found.
  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_INCLUDES = list of include directories
  15. # CMAKE_REQUIRED_LIBRARIES = list of libraries to link
  16. include( CheckFunctionExists )
  17. macro( find_function_library FUNC VAR )
  18. if( "${VAR}" MATCHES "^${VAR}$" )
  19. CHECK_FUNCTION_EXISTS( ${FUNC} ${VAR} )
  20. if( ${VAR} )
  21. message( STATUS "Found ${FUNC} in global libraries" )
  22. set( ${VAR} 0 CACHE INTERNAL "Found ${FUNC} in global libraries" )# global
  23. else()
  24. foreach( LIB IN ITEMS ${ARGN} )
  25. message( STATUS "Looking for ${FUNC} in ${LIB}" )
  26. find_library( ${LIB}_LIBRARY ${LIB} )
  27. mark_as_advanced( ${LIB}_LIBRARY )
  28. if( ${LIB}_LIBRARY )
  29. unset( ${VAR} CACHE )
  30. set( CMAKE_REQUIRED_LIBRARIES ${${LIB}_LIBRARY} )
  31. CHECK_FUNCTION_EXISTS( ${FUNC} ${VAR} )
  32. set( CMAKE_REQUIRED_LIBRARIES )
  33. if( ${VAR} )
  34. message( STATUS "Found ${FUNC} in ${LIB}: ${${LIB}_LIBRARY}" )
  35. set( ${VAR} ${${LIB}_LIBRARY} CACHE INTERNAL "Found ${FUNC} in ${LIB}" )# lib
  36. break()
  37. endif()
  38. endif()
  39. endforeach()
  40. if( NOT ${VAR} )
  41. message( FATAL_ERROR "Function ${FUNC} not found" )
  42. endif()
  43. endif()
  44. endif()
  45. endmacro( find_function_library )