FindFFmpeg.cmake 5.4 KB

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