FindLibx264.cmake 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #[=======================================================================[.rst
  2. FindLibx264
  3. ----------
  4. FindModule for Libx264 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 ``Libx264::Libx264``.
  11. Result Variables
  12. ^^^^^^^^^^^^^^^^
  13. This module sets the following variables:
  14. ``Libx264_FOUND``
  15. True, if all required components and the core library were found.
  16. ``Libx264_VERSION``
  17. Detected version of found Libx264 libraries.
  18. Cache variables
  19. ^^^^^^^^^^^^^^^
  20. The following cache variables may also be set:
  21. ``Libx264_LIBRARY``
  22. Path to the library component of Libx264.
  23. ``Libx264_INCLUDE_DIR``
  24. Directory containing ``x264.h``.
  25. #]=======================================================================]
  26. include(FindPackageHandleStandardArgs)
  27. find_package(PkgConfig QUIET)
  28. if(PKG_CONFIG_FOUND)
  29. pkg_search_module(PC_Libx264 QUIET x264)
  30. endif()
  31. # Libx264_set_soname: Set SONAME on imported library target
  32. macro(Libx264_set_soname)
  33. if(CMAKE_HOST_SYSTEM_NAME MATCHES "Darwin")
  34. execute_process(
  35. COMMAND sh -c "otool -D '${Libx264_LIBRARY}' | grep -v '${Libx264_LIBRARY}'"
  36. OUTPUT_VARIABLE _output
  37. RESULT_VARIABLE _result
  38. )
  39. if(_result EQUAL 0 AND _output MATCHES "^@rpath/")
  40. set_property(TARGET Libx264::Libx264 PROPERTY IMPORTED_SONAME "${_output}")
  41. endif()
  42. elseif(CMAKE_HOST_SYSTEM_NAME MATCHES "Linux|FreeBSD")
  43. execute_process(
  44. COMMAND sh -c "objdump -p '${Libx264_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 Libx264::Libx264 PROPERTY IMPORTED_SONAME "${_soname}")
  51. unset(_soname)
  52. endif()
  53. endif()
  54. unset(_output)
  55. unset(_result)
  56. endmacro()
  57. # Libx264_find_dll: Find DLL for corresponding import library
  58. macro(Libx264_find_dll)
  59. cmake_path(GET Libx264_IMPLIB PARENT_PATH _implib_path)
  60. cmake_path(SET _bin_path NORMALIZE "${_implib_path}/../bin")
  61. string(REGEX REPLACE "[0-9]+\\.([0-9]+)\\.[0-9]+.*" "\\1" _dll_version "${Libx264_VERSION}")
  62. find_program(
  63. Libx264_LIBRARY
  64. NAMES libx264-${_dll_version}.dll x264-${_dll_version}.dll libx264.dll x264.dll
  65. HINTS ${_implib_path} ${_bin_path}
  66. DOC "Libx264 DLL location"
  67. )
  68. if(NOT Libx264_LIBRARY)
  69. set(Libx264_LIBRARY "${Libx264_IMPLIB}")
  70. endif()
  71. unset(_implib_path)
  72. unset(_bin_path)
  73. unset(_dll_version)
  74. endmacro()
  75. find_path(
  76. Libx264_INCLUDE_DIR
  77. NAMES x264.h
  78. HINTS ${PC_Libx264_INCLUDE_DIRS}
  79. PATHS /usr/include /usr/local/include
  80. DOC "Libx264 include directory"
  81. )
  82. if(PC_Libx264_VERSION VERSION_GREATER 0)
  83. set(Libx264_VERSION ${PC_Libx264_VERSION})
  84. elseif(EXISTS "${Libx264_INCLUDE_DIR}/x264_config.h")
  85. file(STRINGS "${Libx264_INCLUDE_DIR}/x264_config.h" _VERSION_STRING REGEX "#define[ \t]+X264_POINTVER[ \t]+.+")
  86. string(REGEX REPLACE ".*#define[ \t]+X264_POINTVER[ \t]+\"([^ \t]+).*\".*" "\\1" Libx264_VERSION "${_VERSION_STRING}")
  87. else()
  88. if(NOT Libx264_FIND_QUIETLY)
  89. message(AUTHOR_WARNING "Failed to find Libx264 version.")
  90. endif()
  91. set(Libx264_VERSION 0.0.0)
  92. endif()
  93. if(CMAKE_HOST_SYSTEM_NAME MATCHES "Windows")
  94. find_library(Libx264_IMPLIB NAMES x264 libx264 DOC "Libx264 import library location")
  95. libx264_find_dll()
  96. else()
  97. find_library(
  98. Libx264_LIBRARY
  99. NAMES x264 libx264
  100. HINTS ${PC_Libx264_LIBRARY_DIRS}
  101. PATHS /usr/lib /usr/local/lib
  102. DOC "Libx264 location"
  103. )
  104. endif()
  105. if(CMAKE_HOST_SYSTEM_NAME MATCHES "Darwin|Windows")
  106. set(Libx264_ERROR_REASON "Ensure that obs-deps is provided as part of CMAKE_PREFIX_PATH.")
  107. elseif(CMAKE_HOST_SYSTEM_NAME MATCHES "Linux|FreeBSD")
  108. set(Libx264_ERROR_REASON "Ensure that x264 is installed on the system.")
  109. endif()
  110. find_package_handle_standard_args(
  111. Libx264
  112. REQUIRED_VARS Libx264_LIBRARY Libx264_INCLUDE_DIR
  113. VERSION_VAR Libx264_VERSION
  114. REASON_FAILURE_MESSAGE "${Libx264_ERROR_REASON}"
  115. )
  116. mark_as_advanced(Libx264_INCLUDE_DIR Libx264_LIBRARY Libx264_IMPLIB)
  117. unset(Libx264_ERROR_REASON)
  118. if(Libx264_FOUND)
  119. if(NOT TARGET Libx264::Libx264)
  120. if(IS_ABSOLUTE "${Libx264_LIBRARY}")
  121. if(DEFINED Libx264_IMPLIB)
  122. if(Libx264_IMPLIB STREQUAL Libx264_LIBRARY)
  123. add_library(Libx264::Libx264 STATIC IMPORTED)
  124. else()
  125. add_library(Libx264::Libx264 SHARED IMPORTED)
  126. set_property(TARGET Libx264::Libx264 PROPERTY IMPORTED_IMPLIB "${Libx264_IMPLIB}")
  127. endif()
  128. else()
  129. add_library(Libx264::Libx264 UNKNOWN IMPORTED)
  130. endif()
  131. set_property(TARGET Libx264::Libx264 PROPERTY IMPORTED_LOCATION "${Libx264_LIBRARY}")
  132. else()
  133. add_library(Libx264::Libx264 INTERFACE IMPORTED)
  134. set_property(TARGET Libx264::Libx264 PROPERTY IMPORTED_LIBNAME "${Libx264_LIBRARY}")
  135. endif()
  136. libx264_set_soname()
  137. set_target_properties(
  138. Libx264::Libx264
  139. PROPERTIES
  140. INTERFACE_COMPILE_OPTIONS "${PC_Libx264_CFLAGS_OTHER}"
  141. INTERFACE_INCLUDE_DIRECTORIES "${Libx264_INCLUDE_DIR}"
  142. VERSION ${Libx264_VERSION}
  143. )
  144. endif()
  145. endif()
  146. include(FeatureSummary)
  147. set_package_properties(
  148. Libx264
  149. PROPERTIES
  150. URL "https://www.videolan.org/developers/x264.html"
  151. DESCRIPTION
  152. "x264 is a free software library and application for encoding video streams into the H.264/MPEG-4 AVC compression format."
  153. )