FindFFmpeg.cmake 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. # SPDX-FileCopyrightText: 2019 Citra Emulator Project
  2. # SPDX-License-Identifier: GPL-2.0-or-later
  3. # FindFFmpeg
  4. # ----------
  5. #
  6. # Find the native FFmpeg includes and libraries
  7. #
  8. # This module defines the following variables:
  9. #
  10. # FFmpeg_INCLUDE_<component>: where to find <component>.h
  11. # FFmpeg_LIBRARY_<component>: where to find the <component> library
  12. # FFmpeg_INCLUDE_DIR: aggregate all the include paths
  13. # FFmpeg_LIBRARIES: aggregate all the paths to the libraries
  14. # FFmpeg_FOUND: True if all components have been found
  15. #
  16. # This module defines the following targets, which are preferred over variables:
  17. #
  18. # FFmpeg::<component>: Target to use <component> directly, with include path,
  19. # library and dependencies set up. If you are using a static build, you are
  20. # responsible for adding any external dependencies (such as zlib, bzlib...).
  21. #
  22. # <component> can be one of:
  23. # avcodec
  24. # avdevice
  25. # avfilter
  26. # avformat
  27. # avutil
  28. # postproc
  29. # swresample
  30. # swscale
  31. #
  32. set(_FFmpeg_ALL_COMPONENTS
  33. avcodec
  34. avdevice
  35. avfilter
  36. avformat
  37. avutil
  38. postproc
  39. swresample
  40. swscale
  41. )
  42. set(_FFmpeg_DEPS_avcodec avutil)
  43. set(_FFmpeg_DEPS_avdevice avcodec avformat avutil)
  44. set(_FFmpeg_DEPS_avfilter avutil)
  45. set(_FFmpeg_DEPS_avformat avcodec avutil)
  46. set(_FFmpeg_DEPS_postproc avutil)
  47. set(_FFmpeg_DEPS_swresample avutil)
  48. set(_FFmpeg_DEPS_swscale avutil)
  49. function(find_ffmpeg LIBNAME)
  50. if(DEFINED ENV{FFMPEG_DIR})
  51. set(FFMPEG_DIR $ENV{FFMPEG_DIR})
  52. endif()
  53. if(FFMPEG_DIR)
  54. list(APPEND INCLUDE_PATHS
  55. ${FFMPEG_DIR}
  56. ${FFMPEG_DIR}/ffmpeg
  57. ${FFMPEG_DIR}/lib${LIBNAME}
  58. ${FFMPEG_DIR}/include/lib${LIBNAME}
  59. ${FFMPEG_DIR}/include/ffmpeg
  60. ${FFMPEG_DIR}/include
  61. NO_DEFAULT_PATH
  62. NO_CMAKE_FIND_ROOT_PATH
  63. )
  64. list(APPEND LIB_PATHS
  65. ${FFMPEG_DIR}
  66. ${FFMPEG_DIR}/lib
  67. ${FFMPEG_DIR}/lib${LIBNAME}
  68. NO_DEFAULT_PATH
  69. NO_CMAKE_FIND_ROOT_PATH
  70. )
  71. else()
  72. list(APPEND INCLUDE_PATHS
  73. /usr/local/include/ffmpeg
  74. /usr/local/include/lib${LIBNAME}
  75. /usr/include/ffmpeg
  76. /usr/include/lib${LIBNAME}
  77. /usr/include/ffmpeg/lib${LIBNAME}
  78. )
  79. list(APPEND LIB_PATHS
  80. /usr/local/lib
  81. /usr/lib
  82. )
  83. endif()
  84. find_path(FFmpeg_INCLUDE_${LIBNAME} lib${LIBNAME}/${LIBNAME}.h
  85. HINTS ${INCLUDE_PATHS}
  86. )
  87. find_library(FFmpeg_LIBRARY_${LIBNAME} ${LIBNAME}
  88. HINTS ${LIB_PATHS}
  89. )
  90. if(NOT FFMPEG_DIR AND (NOT FFmpeg_LIBRARY_${LIBNAME} OR NOT FFmpeg_INCLUDE_${LIBNAME}))
  91. # Didn't find it in the usual paths, try pkg-config
  92. find_package(PkgConfig QUIET)
  93. pkg_check_modules(FFmpeg_PKGCONFIG_${LIBNAME} QUIET lib${LIBNAME})
  94. find_path(FFmpeg_INCLUDE_${LIBNAME} lib${LIBNAME}/${LIBNAME}.h
  95. ${FFmpeg_PKGCONFIG_${LIBNAME}_INCLUDE_DIRS}
  96. )
  97. find_library(FFmpeg_LIBRARY_${LIBNAME} ${LIBNAME}
  98. ${FFmpeg_PKGCONFIG_${LIBNAME}_LIBRARY_DIRS}
  99. )
  100. endif()
  101. if(FFmpeg_INCLUDE_${LIBNAME} AND FFmpeg_LIBRARY_${LIBNAME})
  102. set(FFmpeg_INCLUDE_${LIBNAME} "${FFmpeg_INCLUDE_${LIBNAME}}" PARENT_SCOPE)
  103. set(FFmpeg_LIBRARY_${LIBNAME} "${FFmpeg_LIBRARY_${LIBNAME}}" PARENT_SCOPE)
  104. # Extract FFmpeg version from version.h
  105. foreach(v MAJOR MINOR MICRO)
  106. set(FFmpeg_${LIBNAME}_VERSION_${v} 0)
  107. endforeach()
  108. string(TOUPPER ${LIBNAME} LIBNAME_UPPER)
  109. file(STRINGS "${FFmpeg_INCLUDE_${LIBNAME}}/lib${LIBNAME}/version.h" _FFmpeg_VERSION_H_CONTENTS REGEX "#define LIB${LIBNAME_UPPER}_VERSION_(MAJOR|MINOR|MICRO) ")
  110. set(_FFmpeg_VERSION_REGEX "([0-9]+)")
  111. foreach(v MAJOR MINOR MICRO)
  112. if("${_FFmpeg_VERSION_H_CONTENTS}" MATCHES "#define LIB${LIBNAME_UPPER}_VERSION_${v}[\\t ]+${_FFmpeg_VERSION_REGEX}")
  113. set(FFmpeg_${LIBNAME}_VERSION_${v} "${CMAKE_MATCH_1}")
  114. endif()
  115. endforeach()
  116. set(FFmpeg_${LIBNAME}_VERSION "${FFmpeg_${LIBNAME}_VERSION_MAJOR}.${FFmpeg_${LIBNAME}_VERSION_MINOR}.${FFmpeg_${LIBNAME}_VERSION_MICRO}")
  117. set(FFmpeg_${c}_VERSION "${FFmpeg_${LIBNAME}_VERSION}" PARENT_SCOPE)
  118. unset(_FFmpeg_VERSION_REGEX)
  119. unset(_FFmpeg_VERSION_H_CONTENTS)
  120. set(FFmpeg_${c}_FOUND TRUE PARENT_SCOPE)
  121. if(NOT FFmpeg_FIND_QUIETLY)
  122. message("-- Found ${LIBNAME}: ${FFmpeg_INCLUDE_${LIBNAME}} ${FFmpeg_LIBRARY_${LIBNAME}} (version: ${FFmpeg_${LIBNAME}_VERSION})")
  123. endif()
  124. endif()
  125. endfunction()
  126. foreach(c ${_FFmpeg_ALL_COMPONENTS})
  127. find_ffmpeg(${c})
  128. endforeach()
  129. foreach(c ${_FFmpeg_ALL_COMPONENTS})
  130. if(FFmpeg_${c}_FOUND)
  131. list(APPEND FFmpeg_INCLUDE_DIR ${FFmpeg_INCLUDE_${c}})
  132. list(APPEND FFmpeg_LIBRARIES ${FFmpeg_LIBRARY_${c}})
  133. add_library(FFmpeg::${c} IMPORTED UNKNOWN)
  134. set_target_properties(FFmpeg::${c} PROPERTIES
  135. IMPORTED_LOCATION ${FFmpeg_LIBRARY_${c}}
  136. INTERFACE_INCLUDE_DIRECTORIES ${FFmpeg_INCLUDE_${c}}
  137. )
  138. if(_FFmpeg_DEPS_${c})
  139. set(deps)
  140. foreach(dep ${_FFmpeg_DEPS_${c}})
  141. list(APPEND deps FFmpeg::${dep})
  142. endforeach()
  143. set_target_properties(FFmpeg::${c} PROPERTIES
  144. INTERFACE_LINK_LIBRARIES "${deps}"
  145. )
  146. unset(deps)
  147. endif()
  148. endif()
  149. endforeach()
  150. if(FFmpeg_INCLUDE_DIR)
  151. list(REMOVE_DUPLICATES FFmpeg_INCLUDE_DIR)
  152. endif()
  153. foreach(c ${FFmpeg_FIND_COMPONENTS})
  154. list(APPEND _FFmpeg_REQUIRED_VARS FFmpeg_INCLUDE_${c} FFmpeg_LIBRARY_${c})
  155. endforeach()
  156. include(FindPackageHandleStandardArgs)
  157. find_package_handle_standard_args(FFmpeg
  158. REQUIRED_VARS ${_FFmpeg_REQUIRED_VARS}
  159. HANDLE_COMPONENTS
  160. )
  161. foreach(c ${_FFmpeg_ALL_COMPONENTS})
  162. unset(_FFmpeg_DEPS_${c})
  163. endforeach()
  164. unset(_FFmpeg_ALL_COMPONENTS)
  165. unset(_FFmpeg_REQUIRED_VARS)
  166. include(FindPackageHandleStandardArgs)
  167. find_package_handle_standard_args(FFmpeg
  168. REQUIRED_VARS
  169. FFmpeg_LIBRARIES
  170. FFmpeg_INCLUDE_DIR
  171. HANDLE_COMPONENTS
  172. )