1
0

FindFFmpeg.cmake 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #
  2. # This module defines the following variables:
  3. #
  4. # FFMPEG_FOUND - All required components and the core library were found
  5. # FFMPEG_INCLUDE_DIRS - Combined list of all components include dirs
  6. # FFMPEG_LIBRARIES - Combined list of all components libraries
  7. # FFMPEG_VERSION_STRING - Version of the first component requested
  8. #
  9. # For each requested component the following variables are defined:
  10. #
  11. # FFMPEG_<component>_FOUND - The component was found
  12. # FFMPEG_<component>_INCLUDE_DIRS - The components include dirs
  13. # FFMPEG_<component>_LIBRARIES - The components libraries
  14. # FFMPEG_<component>_VERSION_STRING - The components version string
  15. # FFMPEG_<component>_VERSION_MAJOR - The components major version
  16. # FFMPEG_<component>_VERSION_MINOR - The components minor version
  17. # FFMPEG_<component>_VERSION_MICRO - The components micro version
  18. #
  19. # <component> is the uppercase name of the component
  20. find_package(PkgConfig QUIET)
  21. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  22. set(_lib_suffix 64)
  23. else()
  24. set(_lib_suffix 32)
  25. endif()
  26. function(find_ffmpeg_library component header)
  27. string(TOUPPER "${component}" component_u)
  28. set(FFMPEG_${component_u}_FOUND
  29. FALSE
  30. PARENT_SCOPE)
  31. set(FFmpeg_${component}_FOUND
  32. FALSE
  33. PARENT_SCOPE)
  34. if(PKG_CONFIG_FOUND)
  35. pkg_check_modules(PC_FFMPEG_${component} QUIET lib${component})
  36. endif()
  37. find_path(
  38. FFMPEG_${component}_INCLUDE_DIR
  39. NAMES "lib${component}/${header}" "lib${component}/version.h"
  40. HINTS ENV FFMPEG_PATH ${FFMPEG_PATH} ${CMAKE_SOURCE_DIR}/${FFMPEG_PATH}
  41. ${PC_FFMPEG_${component}_INCLUDE_DIRS}
  42. PATHS /usr/include /usr/local/include /opt/local/include /sw/include
  43. PATH_SUFFIXES ffmpeg libav include)
  44. find_library(
  45. FFMPEG_${component}_LIBRARY
  46. NAMES "${component}" "lib${component}"
  47. HINTS ENV FFMPEG_PATH ${FFMPEG_PATH} ${CMAKE_SOURCE_DIR}/${FFMPEG_PATH}
  48. ${PC_FFMPEG_${component}_LIBRARY_DIRS}
  49. PATHS /usr/lib /usr/local/lib /opt/local/lib /sw/lib
  50. PATH_SUFFIXES
  51. lib${_lib_suffix}
  52. lib
  53. libs${_lib_suffix}
  54. libs
  55. bin${_lib_suffix}
  56. bin
  57. ../lib${_lib_suffix}
  58. ../lib
  59. ../libs${_lib_suffix}
  60. ../libs
  61. ../bin${_lib_suffix}
  62. ../bin)
  63. set(FFMPEG_${component_u}_INCLUDE_DIRS
  64. ${FFMPEG_${component}_INCLUDE_DIR}
  65. PARENT_SCOPE)
  66. set(FFMPEG_${component_u}_LIBRARIES
  67. ${FFMPEG_${component}_LIBRARY}
  68. PARENT_SCOPE)
  69. mark_as_advanced(FFMPEG_${component}_INCLUDE_DIR FFMPEG_${component}_LIBRARY)
  70. if(FFMPEG_${component}_INCLUDE_DIR AND FFMPEG_${component}_LIBRARY)
  71. set(FFMPEG_${component_u}_FOUND
  72. TRUE
  73. PARENT_SCOPE)
  74. set(FFmpeg_${component}_FOUND
  75. TRUE
  76. PARENT_SCOPE)
  77. list(APPEND FFMPEG_INCLUDE_DIRS ${FFMPEG_${component}_INCLUDE_DIR})
  78. list(REMOVE_DUPLICATES FFMPEG_INCLUDE_DIRS)
  79. set(FFMPEG_INCLUDE_DIRS
  80. "${FFMPEG_INCLUDE_DIRS}"
  81. PARENT_SCOPE)
  82. list(APPEND FFMPEG_LIBRARIES ${FFMPEG_${component}_LIBRARY})
  83. list(REMOVE_DUPLICATES FFMPEG_LIBRARIES)
  84. set(FFMPEG_LIBRARIES
  85. "${FFMPEG_LIBRARIES}"
  86. PARENT_SCOPE)
  87. set(FFMPEG_${component_u}_VERSION_STRING
  88. "unknown"
  89. PARENT_SCOPE)
  90. set(_vfile "${FFMPEG_${component}_INCLUDE_DIR}/lib${component}/version.h")
  91. if(EXISTS "${_vfile}")
  92. file(STRINGS "${_vfile}" _version_parse
  93. REGEX "^.*VERSION_(MAJOR|MINOR|MICRO)[ \t]+[0-9]+[ \t]*$")
  94. string(REGEX REPLACE ".*VERSION_MAJOR[ \t]+([0-9]+).*" "\\1" _major
  95. "${_version_parse}")
  96. string(REGEX REPLACE ".*VERSION_MINOR[ \t]+([0-9]+).*" "\\1" _minor
  97. "${_version_parse}")
  98. string(REGEX REPLACE ".*VERSION_MICRO[ \t]+([0-9]+).*" "\\1" _micro
  99. "${_version_parse}")
  100. set(FFMPEG_${component_u}_VERSION_MAJOR
  101. "${_major}"
  102. PARENT_SCOPE)
  103. set(FFMPEG_${component_u}_VERSION_MINOR
  104. "${_minor}"
  105. PARENT_SCOPE)
  106. set(FFMPEG_${component_u}_VERSION_MICRO
  107. "${_micro}"
  108. PARENT_SCOPE)
  109. set(FFMPEG_${component_u}_VERSION_STRING
  110. "${_major}.${_minor}.${_micro}"
  111. PARENT_SCOPE)
  112. else()
  113. message(STATUS "Failed parsing FFmpeg ${component} version")
  114. endif()
  115. endif()
  116. endfunction()
  117. set(FFMPEG_INCLUDE_DIRS)
  118. set(FFMPEG_LIBRARIES)
  119. if(NOT FFmpeg_FIND_COMPONENTS)
  120. message(FATAL_ERROR "No FFmpeg components requested")
  121. endif()
  122. list(GET FFmpeg_FIND_COMPONENTS 0 _first_comp)
  123. string(TOUPPER "${_first_comp}" _first_comp)
  124. foreach(component ${FFmpeg_FIND_COMPONENTS})
  125. if(component STREQUAL "avcodec")
  126. find_ffmpeg_library("${component}" "avcodec.h")
  127. elseif(component STREQUAL "avdevice")
  128. find_ffmpeg_library("${component}" "avdevice.h")
  129. elseif(component STREQUAL "avfilter")
  130. find_ffmpeg_library("${component}" "avfilter.h")
  131. elseif(component STREQUAL "avformat")
  132. find_ffmpeg_library("${component}" "avformat.h")
  133. elseif(component STREQUAL "avresample")
  134. find_ffmpeg_library("${component}" "avresample.h")
  135. elseif(component STREQUAL "avutil")
  136. find_ffmpeg_library("${component}" "avutil.h")
  137. elseif(component STREQUAL "postproc")
  138. find_ffmpeg_library("${component}" "postprocess.h")
  139. elseif(component STREQUAL "swresample")
  140. find_ffmpeg_library("${component}" "swresample.h")
  141. elseif(component STREQUAL "swscale")
  142. find_ffmpeg_library("${component}" "swscale.h")
  143. else()
  144. message(FATAL_ERROR "Unknown FFmpeg component requested: ${component}")
  145. endif()
  146. endforeach()
  147. include(FindPackageHandleStandardArgs)
  148. find_package_handle_standard_args(
  149. FFmpeg
  150. FOUND_VAR FFMPEG_FOUND
  151. REQUIRED_VARS FFMPEG_${_first_comp}_LIBRARIES
  152. FFMPEG_${_first_comp}_INCLUDE_DIRS
  153. VERSION_VAR FFMPEG_${_first_comp}_VERSION_STRING
  154. HANDLE_COMPONENTS)
  155. if(FFMPEG_FOUND)
  156. foreach(component ${FFmpeg_FIND_COMPONENTS})
  157. if(NOT TARGET FFmpeg::${component})
  158. string(TOUPPER ${component} component_u)
  159. if(FFMPEG_${component_u}_FOUND)
  160. if(IS_ABSOLUTE "${FFMPEG_${component_u}_LIBRARIES}")
  161. add_library(FFmpeg::${component} UNKNOWN IMPORTED)
  162. set_target_properties(
  163. FFmpeg::${component}
  164. PROPERTIES IMPORTED_LOCATION "${FFMPEG_${component_u}_LIBRARIES}")
  165. else()
  166. add_library(FFmpeg::${component} INTERFACE IMPORTED)
  167. set_target_properties(
  168. FFmpeg::${component}
  169. PROPERTIES IMPORTED_LIBNAME "${FFMPEG_${component_u}_LIBRARIES}")
  170. endif()
  171. set_target_properties(
  172. FFmpeg::${component}
  173. PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
  174. "${FFMPEG_${component_u}_INCLUDE_DIRS}")
  175. endif()
  176. endif()
  177. endforeach()
  178. endif()