FindFFMPEG.cmake 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #[==[
  2. Provides the following variables:
  3. * `FFMPEG_INCLUDE_DIRS`: Include directories necessary to use FFMPEG.
  4. * `FFMPEG_LIBRARIES`: Libraries necessary to use FFMPEG. Note that this only
  5. includes libraries for the components requested.
  6. * `FFMPEG_VERSION`: The version of FFMPEG found.
  7. The following components are supported:
  8. * `avcodec`
  9. * `avdevice`
  10. * `avfilter`
  11. * `avformat`
  12. * `avresample`
  13. * `avutil`
  14. * `swresample`
  15. * `swscale`
  16. For each component, the following are provided:
  17. * `FFMPEG_<component>_FOUND`: Libraries for the component.
  18. * `FFMPEG_<component>_INCLUDE_DIRS`: Include directories for
  19. the component.
  20. * `FFMPEG_<component>_LIBRARIES`: Libraries for the component.
  21. * `FFMPEG::<component>`: A target to use with `target_link_libraries`.
  22. Note that only components requested with `COMPONENTS` or `OPTIONAL_COMPONENTS`
  23. are guaranteed to set these variables or provide targets.
  24. #]==]
  25. function (_ffmpeg_find component headername)
  26. find_path("FFMPEG_${component}_INCLUDE_DIR"
  27. NAMES
  28. "lib${component}/${headername}"
  29. PATHS
  30. "${FFMPEG_ROOT}/include"
  31. ~/Library/Frameworks
  32. /Library/Frameworks
  33. /usr/local/include
  34. /usr/include
  35. /sw/include # Fink
  36. /opt/local/include # DarwinPorts
  37. /opt/csw/include # Blastwave
  38. /opt/include
  39. /usr/freeware/include
  40. PATH_SUFFIXES
  41. ffmpeg
  42. DOC "FFMPEG's ${component} include directory")
  43. mark_as_advanced("FFMPEG_${component}_INCLUDE_DIR")
  44. # On Windows, static FFMPEG is sometimes built as `lib<name>.a`.
  45. if (WIN32)
  46. list(APPEND CMAKE_FIND_LIBRARY_SUFFIXES ".a" ".lib")
  47. list(APPEND CMAKE_FIND_LIBRARY_PREFIXES "" "lib")
  48. endif ()
  49. find_library("FFMPEG_${component}_LIBRARY"
  50. NAMES
  51. "${component}"
  52. PATHS
  53. "${FFMPEG_ROOT}/lib"
  54. ~/Library/Frameworks
  55. /Library/Frameworks
  56. /usr/local/lib
  57. /usr/local/lib64
  58. /usr/lib
  59. /usr/lib64
  60. /sw/lib
  61. /opt/local/lib
  62. /opt/csw/lib
  63. /opt/lib
  64. /usr/freeware/lib64
  65. "${FFMPEG_ROOT}/bin"
  66. DOC "FFMPEG's ${component} library")
  67. mark_as_advanced("FFMPEG_${component}_LIBRARY")
  68. if (FFMPEG_${component}_LIBRARY AND FFMPEG_${component}_INCLUDE_DIR)
  69. set(_deps_found TRUE)
  70. set(_deps_link)
  71. foreach (_ffmpeg_dep IN LISTS ARGN)
  72. if (TARGET "FFMPEG::${_ffmpeg_dep}")
  73. list(APPEND _deps_link "FFMPEG::${_ffmpeg_dep}")
  74. else ()
  75. set(_deps_found FALSE)
  76. endif ()
  77. endforeach ()
  78. if (_deps_found)
  79. if (NOT TARGET "FFMPEG::${component}")
  80. add_library("FFMPEG::${component}" UNKNOWN IMPORTED)
  81. set_target_properties("FFMPEG::${component}" PROPERTIES
  82. IMPORTED_LOCATION "${FFMPEG_${component}_LIBRARY}"
  83. INTERFACE_INCLUDE_DIRECTORIES "${FFMPEG_${component}_INCLUDE_DIR}"
  84. IMPORTED_LINK_INTERFACE_LIBRARIES "${_deps_link}")
  85. endif ()
  86. set("FFMPEG_${component}_FOUND" 1
  87. PARENT_SCOPE)
  88. set(version_header_path "${FFMPEG_${component}_INCLUDE_DIR}/lib${component}/version.h")
  89. if (EXISTS "${version_header_path}")
  90. string(TOUPPER "${component}" component_upper)
  91. file(STRINGS "${version_header_path}" version
  92. REGEX "#define *LIB${component_upper}_VERSION_(MAJOR|MINOR|MICRO) ")
  93. string(REGEX REPLACE ".*_MAJOR *\([0-9]*\).*" "\\1" major "${version}")
  94. string(REGEX REPLACE ".*_MINOR *\([0-9]*\).*" "\\1" minor "${version}")
  95. string(REGEX REPLACE ".*_MICRO *\([0-9]*\).*" "\\1" micro "${version}")
  96. if (NOT major STREQUAL "" AND
  97. NOT minor STREQUAL "" AND
  98. NOT micro STREQUAL "")
  99. set("FFMPEG_${component}_VERSION" "${major}.${minor}.${micro}"
  100. PARENT_SCOPE)
  101. endif ()
  102. endif ()
  103. else ()
  104. set("FFMPEG_${component}_FOUND" 0
  105. PARENT_SCOPE)
  106. set(what)
  107. if (NOT FFMPEG_${component}_LIBRARY)
  108. set(what "library")
  109. endif ()
  110. if (NOT FFMPEG_${component}_INCLUDE_DIR)
  111. if (what)
  112. string(APPEND what " or headers")
  113. else ()
  114. set(what "headers")
  115. endif ()
  116. endif ()
  117. set("FFMPEG_${component}_NOT_FOUND_MESSAGE"
  118. "Could not find the ${what} for ${component}."
  119. PARENT_SCOPE)
  120. endif ()
  121. endif ()
  122. endfunction ()
  123. _ffmpeg_find(avutil avutil.h)
  124. _ffmpeg_find(avresample avresample.h
  125. avutil)
  126. _ffmpeg_find(swresample swresample.h
  127. avutil)
  128. _ffmpeg_find(swscale swscale.h
  129. avutil)
  130. _ffmpeg_find(avcodec avcodec.h
  131. avutil)
  132. _ffmpeg_find(avformat avformat.h
  133. avcodec avutil)
  134. _ffmpeg_find(avfilter avfilter.h
  135. avutil)
  136. _ffmpeg_find(avdevice avdevice.h
  137. avformat avutil)
  138. if (TARGET FFMPEG::avutil)
  139. set(_ffmpeg_version_header_path "${FFMPEG_avutil_INCLUDE_DIR}/libavutil/ffversion.h")
  140. if (EXISTS "${_ffmpeg_version_header_path}")
  141. file(STRINGS "${_ffmpeg_version_header_path}" _ffmpeg_version
  142. REGEX "FFMPEG_VERSION")
  143. string(REGEX REPLACE ".*\"n?\(.*\)\"" "\\1" FFMPEG_VERSION "${_ffmpeg_version}")
  144. unset(_ffmpeg_version)
  145. else ()
  146. set(FFMPEG_VERSION FFMPEG_VERSION-NOTFOUND)
  147. endif ()
  148. unset(_ffmpeg_version_header_path)
  149. endif ()
  150. set(FFMPEG_INCLUDE_DIRS)
  151. set(FFMPEG_LIBRARIES)
  152. set(_ffmpeg_required_vars)
  153. foreach (_ffmpeg_component IN LISTS FFMPEG_FIND_COMPONENTS)
  154. if (TARGET "FFMPEG::${_ffmpeg_component}")
  155. set(FFMPEG_${_ffmpeg_component}_INCLUDE_DIRS
  156. "${FFMPEG_${_ffmpeg_component}_INCLUDE_DIR}")
  157. set(FFMPEG_${_ffmpeg_component}_LIBRARIES
  158. "${FFMPEG_${_ffmpeg_component}_LIBRARY}")
  159. list(APPEND FFMPEG_INCLUDE_DIRS
  160. "${FFMPEG_${_ffmpeg_component}_INCLUDE_DIRS}")
  161. list(APPEND FFMPEG_LIBRARIES
  162. "${FFMPEG_${_ffmpeg_component}_LIBRARIES}")
  163. if (FFMEG_FIND_REQUIRED_${_ffmpeg_component})
  164. list(APPEND _ffmpeg_required_vars
  165. "FFMPEG_${_ffmpeg_required_vars}_INCLUDE_DIRS"
  166. "FFMPEG_${_ffmpeg_required_vars}_LIBRARIES")
  167. endif ()
  168. endif ()
  169. endforeach ()
  170. unset(_ffmpeg_component)
  171. if (FFMPEG_INCLUDE_DIRS)
  172. list(REMOVE_DUPLICATES FFMPEG_INCLUDE_DIRS)
  173. endif ()
  174. include(FindPackageHandleStandardArgs)
  175. find_package_handle_standard_args(FFMPEG
  176. REQUIRED_VARS FFMPEG_INCLUDE_DIRS FFMPEG_LIBRARIES ${_ffmpeg_required_vars}
  177. VERSION_VAR FFMPEG_VERSION
  178. HANDLE_COMPONENTS)
  179. unset(_ffmpeg_required_vars)