FindFFmpeg.cmake 4.3 KB

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