FindFFmpeg.cmake 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 # Disabled
  26. # avfilter # Disabled
  27. # avformat # Disabled
  28. # avutil
  29. # postproc # Disabled
  30. # swresample # Disabled
  31. # swscale
  32. #
  33. set(_FFmpeg_ALL_COMPONENTS
  34. avcodec
  35. avutil
  36. swscale
  37. )
  38. set(_FFmpeg_DEPS_avcodec avutil)
  39. set(_FFmpeg_DEPS_avdevice avcodec avformat avutil)
  40. set(_FFmpeg_DEPS_avfilter avutil)
  41. set(_FFmpeg_DEPS_avformat avcodec avutil)
  42. set(_FFmpeg_DEPS_postproc avutil)
  43. set(_FFmpeg_DEPS_swresample avutil)
  44. set(_FFmpeg_DEPS_swscale avutil)
  45. function(find_ffmpeg LIBNAME)
  46. if(DEFINED ENV{FFMPEG_DIR})
  47. set(FFMPEG_DIR $ENV{FFMPEG_DIR})
  48. endif()
  49. if(FFMPEG_DIR)
  50. list(APPEND INCLUDE_PATHS
  51. ${FFMPEG_DIR}
  52. ${FFMPEG_DIR}/ffmpeg
  53. ${FFMPEG_DIR}/lib${LIBNAME}
  54. ${FFMPEG_DIR}/include/lib${LIBNAME}
  55. ${FFMPEG_DIR}/include/ffmpeg
  56. ${FFMPEG_DIR}/include
  57. NO_DEFAULT_PATH
  58. NO_CMAKE_FIND_ROOT_PATH
  59. )
  60. list(APPEND LIB_PATHS
  61. ${FFMPEG_DIR}
  62. ${FFMPEG_DIR}/lib
  63. ${FFMPEG_DIR}/lib${LIBNAME}
  64. NO_DEFAULT_PATH
  65. NO_CMAKE_FIND_ROOT_PATH
  66. )
  67. else()
  68. list(APPEND INCLUDE_PATHS
  69. /usr/local/include/ffmpeg
  70. /usr/local/include/lib${LIBNAME}
  71. /usr/include/ffmpeg
  72. /usr/include/lib${LIBNAME}
  73. /usr/include/ffmpeg/lib${LIBNAME}
  74. )
  75. list(APPEND LIB_PATHS
  76. /usr/local/lib
  77. /usr/lib
  78. )
  79. endif()
  80. find_path(FFmpeg_INCLUDE_${LIBNAME} lib${LIBNAME}/${LIBNAME}.h
  81. HINTS ${INCLUDE_PATHS}
  82. )
  83. find_library(FFmpeg_LIBRARY_${LIBNAME} ${LIBNAME}
  84. HINTS ${LIB_PATHS}
  85. )
  86. if(NOT FFMPEG_DIR AND (NOT FFmpeg_LIBRARY_${LIBNAME} OR NOT FFmpeg_INCLUDE_${LIBNAME}))
  87. # Didn't find it in the usual paths, try pkg-config
  88. find_package(PkgConfig QUIET)
  89. pkg_check_modules(FFmpeg_PKGCONFIG_${LIBNAME} QUIET lib${LIBNAME})
  90. find_path(FFmpeg_INCLUDE_${LIBNAME} lib${LIBNAME}/${LIBNAME}.h
  91. ${FFmpeg_PKGCONFIG_${LIBNAME}_INCLUDE_DIRS}
  92. )
  93. find_library(FFmpeg_LIBRARY_${LIBNAME} ${LIBNAME}
  94. ${FFmpeg_PKGCONFIG_${LIBNAME}_LIBRARY_DIRS}
  95. )
  96. endif()
  97. if(FFmpeg_INCLUDE_${LIBNAME} AND FFmpeg_LIBRARY_${LIBNAME})
  98. set(FFmpeg_INCLUDE_${LIBNAME} "${FFmpeg_INCLUDE_${LIBNAME}}" PARENT_SCOPE)
  99. set(FFmpeg_LIBRARY_${LIBNAME} "${FFmpeg_LIBRARY_${LIBNAME}}" PARENT_SCOPE)
  100. # Extract FFmpeg version from version.h
  101. foreach(v MAJOR MINOR MICRO)
  102. set(FFmpeg_${LIBNAME}_VERSION_${v} 0)
  103. endforeach()
  104. string(TOUPPER ${LIBNAME} LIBNAME_UPPER)
  105. file(STRINGS "${FFmpeg_INCLUDE_${LIBNAME}}/lib${LIBNAME}/version.h" _FFmpeg_VERSION_H_CONTENTS REGEX "#define LIB${LIBNAME_UPPER}_VERSION_(MAJOR|MINOR|MICRO) ")
  106. set(_FFmpeg_VERSION_REGEX "([0-9]+)")
  107. foreach(v MAJOR MINOR MICRO)
  108. if("${_FFmpeg_VERSION_H_CONTENTS}" MATCHES "#define LIB${LIBNAME_UPPER}_VERSION_${v}[\\t ]+${_FFmpeg_VERSION_REGEX}")
  109. set(FFmpeg_${LIBNAME}_VERSION_${v} "${CMAKE_MATCH_1}")
  110. endif()
  111. endforeach()
  112. set(FFmpeg_${LIBNAME}_VERSION "${FFmpeg_${LIBNAME}_VERSION_MAJOR}.${FFmpeg_${LIBNAME}_VERSION_MINOR}.${FFmpeg_${LIBNAME}_VERSION_MICRO}")
  113. set(FFmpeg_${c}_VERSION "${FFmpeg_${LIBNAME}_VERSION}" PARENT_SCOPE)
  114. unset(_FFmpeg_VERSION_REGEX)
  115. unset(_FFmpeg_VERSION_H_CONTENTS)
  116. set(FFmpeg_${c}_FOUND TRUE PARENT_SCOPE)
  117. if(NOT FFmpeg_FIND_QUIETLY)
  118. message("-- Found ${LIBNAME}: ${FFmpeg_INCLUDE_${LIBNAME}} ${FFmpeg_LIBRARY_${LIBNAME}} (version: ${FFmpeg_${LIBNAME}_VERSION})")
  119. endif()
  120. endif()
  121. endfunction()
  122. foreach(c ${_FFmpeg_ALL_COMPONENTS})
  123. find_ffmpeg(${c})
  124. endforeach()
  125. foreach(c ${_FFmpeg_ALL_COMPONENTS})
  126. if(FFmpeg_${c}_FOUND)
  127. list(APPEND FFmpeg_INCLUDE_DIR ${FFmpeg_INCLUDE_${c}})
  128. list(APPEND FFmpeg_LIBRARIES ${FFmpeg_LIBRARY_${c}})
  129. add_library(FFmpeg::${c} IMPORTED UNKNOWN)
  130. set_target_properties(FFmpeg::${c} PROPERTIES
  131. IMPORTED_LOCATION ${FFmpeg_LIBRARY_${c}}
  132. INTERFACE_INCLUDE_DIRECTORIES ${FFmpeg_INCLUDE_${c}}
  133. )
  134. if(_FFmpeg_DEPS_${c})
  135. set(deps)
  136. foreach(dep ${_FFmpeg_DEPS_${c}})
  137. list(APPEND deps FFmpeg::${dep})
  138. endforeach()
  139. set_target_properties(FFmpeg::${c} PROPERTIES
  140. INTERFACE_LINK_LIBRARIES "${deps}"
  141. )
  142. unset(deps)
  143. endif()
  144. endif()
  145. endforeach()
  146. if(FFmpeg_INCLUDE_DIR)
  147. list(REMOVE_DUPLICATES FFmpeg_INCLUDE_DIR)
  148. endif()
  149. foreach(c ${FFmpeg_FIND_COMPONENTS})
  150. list(APPEND _FFmpeg_REQUIRED_VARS FFmpeg_INCLUDE_${c} FFmpeg_LIBRARY_${c})
  151. endforeach()
  152. include(FindPackageHandleStandardArgs)
  153. find_package_handle_standard_args(FFmpeg
  154. REQUIRED_VARS ${_FFmpeg_REQUIRED_VARS}
  155. HANDLE_COMPONENTS
  156. )
  157. foreach(c ${_FFmpeg_ALL_COMPONENTS})
  158. unset(_FFmpeg_DEPS_${c})
  159. endforeach()
  160. unset(_FFmpeg_ALL_COMPONENTS)
  161. unset(_FFmpeg_REQUIRED_VARS)