FindLibrist.cmake 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #[=======================================================================[.rst
  2. FindLibrist
  3. ----------
  4. FindModule for Librist and associated libraries
  5. .. versionchanged:: 3.0
  6. Updated FindModule to CMake standards
  7. Imported Targets
  8. ^^^^^^^^^^^^^^^^
  9. .. versionadded:: 2.0
  10. This module defines the :prop_tgt:`IMPORTED` target ``Librist::Librist``.
  11. Result Variables
  12. ^^^^^^^^^^^^^^^^
  13. This module sets the following variables:
  14. ``Librist_FOUND``
  15. True, if all required components and the core library were found.
  16. ``Librist_VERSION``
  17. Detected version of found Librist libraries.
  18. Cache variables
  19. ^^^^^^^^^^^^^^^
  20. The following cache variables may also be set:
  21. ``Librist_LIBRARY``
  22. Path to the library component of Librist.
  23. ``Librist_INCLUDE_DIR``
  24. Directory containing ``librist.h``.
  25. #]=======================================================================]
  26. include(FindPackageHandleStandardArgs)
  27. find_package(PkgConfig QUIET)
  28. if(PKG_CONFIG_FOUND)
  29. pkg_search_module(PC_Librist QUIET librist)
  30. endif()
  31. # Librist_set_soname: Set SONAME on imported library target
  32. macro(Librist_set_soname)
  33. if(CMAKE_HOST_SYSTEM_NAME MATCHES "Darwin")
  34. execute_process(
  35. COMMAND sh -c "otool -D '${Librist_LIBRARY}' | grep -v '${Librist_LIBRARY}'"
  36. OUTPUT_VARIABLE _output
  37. RESULT_VARIABLE _result
  38. )
  39. if(_result EQUAL 0 AND _output MATCHES "^@rpath/")
  40. set_property(TARGET Librist::Librist PROPERTY IMPORTED_SONAME "${_output}")
  41. endif()
  42. elseif(CMAKE_HOST_SYSTEM_NAME MATCHES "Linux|FreeBSD")
  43. execute_process(
  44. COMMAND sh -c "objdump -p '${Librist_LIBRARY}' | grep SONAME"
  45. OUTPUT_VARIABLE _output
  46. RESULT_VARIABLE _result
  47. )
  48. if(_result EQUAL 0)
  49. string(REGEX REPLACE "[ \t]+SONAME[ \t]+([^ \t]+)" "\\1" _soname "${_output}")
  50. set_property(TARGET Librist::Librist PROPERTY IMPORTED_SONAME "${_soname}")
  51. unset(_soname)
  52. endif()
  53. endif()
  54. unset(_output)
  55. unset(_result)
  56. endmacro()
  57. find_path(
  58. Librist_INCLUDE_DIR
  59. NAMES librist.h librist/librist.h
  60. HINTS ${PC_Librist_INCLUDE_DIRS}
  61. PATHS /usr/include /usr/local/include
  62. DOC "Librist include directory"
  63. )
  64. if(PC_Librist_VERSION VERSION_GREATER 0)
  65. set(Librist_VERSION ${PC_Librist_VERSION})
  66. elseif(EXISTS "${Librist_INCLUDE_DIR}/version.h")
  67. file(STRINGS "${_VERSION_FILE}" _VERSION_STRING REGEX "^.*VERSION_(MAJOR|MINOR|PATCH)[ \t]+[0-9]+[ \t]*$")
  68. string(REGEX REPLACE ".*VERSION_MAJOR[ \t]+([0-9]+).*" "\\1" _VERSION_MAJOR "${_VERSION_STRING}")
  69. string(REGEX REPLACE ".*VERSION_MINOR[ \t]+([0-9]+).*" "\\1" _VERSION_MINOR "${_VERSION_STRING}")
  70. string(REGEX REPLACE ".*VERSION_PATCH[ \t]+([0-9]+).*" "\\1" _VERSION_PATCH "${_VERSION_STRING}")
  71. set(Librist_VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_MICRO}")
  72. else()
  73. if(NOT Librist_FIND_QUIETLY)
  74. message(AUTHOR_WARNING "Failed to find Librist version.")
  75. endif()
  76. set(Librist_VERSION 0.0.0)
  77. endif()
  78. find_library(
  79. Librist_LIBRARY
  80. NAMES librist rist
  81. HINTS ${PC_Librist_LIBRARY_DIRS}
  82. PATHS /usr/lib /usr/local/lib
  83. DOC "Librist location"
  84. )
  85. if(CMAKE_HOST_SYSTEM_NAME MATCHES "Darwin|Windows")
  86. set(Librist_ERROR_REASON "Ensure that obs-deps is provided as part of CMAKE_PREFIX_PATH.")
  87. elseif(CMAKE_HOST_SYSTEM_NAME MATCHES "Linux|FreeBSD")
  88. set(Librist_ERROR_REASON "Ensure librist libraries are available in local library paths.")
  89. endif()
  90. find_package_handle_standard_args(
  91. Librist
  92. REQUIRED_VARS Librist_LIBRARY Librist_INCLUDE_DIR
  93. VERSION_VAR Librist_VERSION
  94. REASON_FAILURE_MESSAGE "${Librist_ERROR_REASON}"
  95. )
  96. mark_as_advanced(Librist_INCLUDE_DIR Librist_LIBRARY)
  97. unset(Librist_ERROR_REASON)
  98. if(Librist_FOUND)
  99. if(NOT TARGET Librist::Librist)
  100. if(IS_ABSOLUTE "${Librist_LIBRARY}")
  101. add_library(Librist::Librist UNKNOWN IMPORTED)
  102. set_property(TARGET Librist::Librist PROPERTY IMPORTED_LOCATION "${Librist_LIBRARY}")
  103. else()
  104. add_library(Librist::Librist INTERFACE IMPORTED)
  105. set_property(TARGET Librist::Librist PROPERTY IMPORTED_LIBNAME "${Librist_LIBRARY}")
  106. endif()
  107. librist_set_soname()
  108. set_target_properties(
  109. Librist::Librist
  110. PROPERTIES
  111. INTERFACE_COMPILE_OPTIONS "${PC_Librist_CFLAGS_OTHER}"
  112. INTERFACE_INCLUDE_DIRECTORIES "${Librist_INCLUDE_DIR}"
  113. VERSION ${Librist_VERSION}
  114. )
  115. endif()
  116. endif()
  117. include(FeatureSummary)
  118. set_package_properties(
  119. Librist
  120. PROPERTIES
  121. URL "https://code.videolan.org/rist/librist"
  122. DESCRIPTION "A library that can be used to easily add the RIST protocol to your application."
  123. )