FindFFmpeg.cmake 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # - Try to find the required ffmpeg components(default: AVFORMAT, AVUTIL, AVCODEC)
  2. #
  3. # Once done this will define
  4. # FFMPEG_FOUND - System has the all required components.
  5. # FFMPEG_LIBRARIES - Link these to use the required ffmpeg components.
  6. #
  7. # For each of the components it will additionally set.
  8. # - AVCODEC
  9. # - AVDEVICE
  10. # - AVFORMAT
  11. # - AVFILTER
  12. # - AVUTIL
  13. # - POSTPROC
  14. # - SWSCALE
  15. # the following target will be defined
  16. # FFmpeg::SDL::<component> - link to this target to
  17. # the following variables will be defined
  18. # FFmpeg_<component>_FOUND - System has <component>
  19. # FFmpeg_<component>_INCLUDE_DIRS - Include directory necessary for using the <component> headers
  20. # FFmpeg_<component>_LIBRARIES - Link these to use <component>
  21. # FFmpeg_<component>_DEFINITIONS - Compiler switches required for using <component>
  22. # FFmpeg_<component>_VERSION - The components version
  23. #
  24. # Copyright (c) 2006, Matthias Kretz, <kretz@kde.org>
  25. # Copyright (c) 2008, Alexander Neundorf, <neundorf@kde.org>
  26. # Copyright (c) 2011, Michael Jansen, <kde@michael-jansen.biz>
  27. # Copyright (c) 2023, Sam lantinga, <slouken@libsdl.org>
  28. #
  29. # Redistribution and use is allowed according to the terms of the BSD license.
  30. # For details see the accompanying COPYING-CMAKE-SCRIPTS file.
  31. include(FindPackageHandleStandardArgs)
  32. include("${CMAKE_CURRENT_LIST_DIR}/PkgConfigHelper.cmake")
  33. # The default components were taken from a survey over other FindFFMPEG.cmake files
  34. if(NOT FFmpeg_FIND_COMPONENTS)
  35. set(FFmpeg_FIND_COMPONENTS AVCODEC AVFORMAT AVUTIL)
  36. foreach(_component IN LISTS FFmpeg_FIND_COMPONENTS)
  37. set(FFmpeg_FIND_REQUIRED_${_component} TRUE)
  38. endforeach()
  39. endif()
  40. find_package(PkgConfig QUIET)
  41. #
  42. ### Macro: find_component
  43. #
  44. # Checks for the given component by invoking pkgconfig and then looking up the libraries and
  45. # include directories.
  46. #
  47. macro(find_component _component _pkgconfig _library _header)
  48. # use pkg-config to get the directories and then use these values
  49. # in the FIND_PATH() and FIND_LIBRARY() calls
  50. if(PKG_CONFIG_FOUND)
  51. pkg_check_modules(PC_${_component} QUIET ${_pkgconfig})
  52. endif()
  53. find_path(FFmpeg_${_component}_INCLUDE_DIRS
  54. NAMES ${_header}
  55. HINTS
  56. ${PC_${_component}_INCLUDE_DIRS}
  57. PATH_SUFFIXES
  58. ffmpeg
  59. )
  60. find_library(FFmpeg_${_component}_LIBRARY
  61. NAMES ${_library}
  62. HINTS
  63. ${PC_${_component}_LIBRARY_DIRS}
  64. )
  65. if(FFmpeg_${_component}_INCLUDE_DIRS AND FFmpeg_${_component}_LIBRARY)
  66. set(FFmpeg_${_component}_FOUND TRUE)
  67. endif()
  68. if(PC_${_component}_FOUND)
  69. get_flags_from_pkg_config("${FFmpeg_${_component}_LIBRARY}" "PC_${_component}" "${_component}")
  70. endif()
  71. set(FFmpeg_${_component}_VERSION "${PC_${_component}_VERSION}")
  72. set(FFmpeg_${_component}_COMPILE_OPTIONS "${${_component}_options}" CACHE STRING "Extra compile options of FFmpeg ${_component}")
  73. set(FFmpeg_${_component}_LIBRARIES "${${_component}_link_libraries}" CACHE STRING "Extra link libraries of FFmpeg ${_component}")
  74. set(FFmpeg_${_component}_LINK_OPTIONS "${${_component}_link_options}" CACHE STRING "Extra link flags of FFmpeg ${_component}")
  75. set(FFmpeg_${_component}_LINK_DIRECTORIES "${${_component}_link_directories}" CACHE PATH "Extra link directories of FFmpeg ${_component}")
  76. mark_as_advanced(
  77. FFmpeg_${_component}_INCLUDE_DIRS
  78. FFmpeg_${_component}_LIBRARY
  79. FFmpeg_${_component}_COMPILE_OPTIONS
  80. FFmpeg_${_component}_LIBRARIES
  81. FFmpeg_${_component}_LINK_OPTIONS
  82. FFmpeg_${_component}_LINK_DIRECTORIES
  83. )
  84. endmacro()
  85. # Check for all possible component.
  86. find_component(AVCODEC libavcodec avcodec libavcodec/avcodec.h)
  87. find_component(AVFORMAT libavformat avformat libavformat/avformat.h)
  88. find_component(AVDEVICE libavdevice avdevice libavdevice/avdevice.h)
  89. find_component(AVUTIL libavutil avutil libavutil/avutil.h)
  90. find_component(AVFILTER libavfilter avfilter libavfilter/avfilter.h)
  91. find_component(SWSCALE libswscale swscale libswscale/swscale.h)
  92. find_component(POSTPROC libpostproc postproc libpostproc/postprocess.h)
  93. find_component(SWRESAMPLE libswresample swresample libswresample/swresample.h)
  94. # Compile the list of required vars
  95. set(_FFmpeg_REQUIRED_VARS)
  96. foreach(_component ${FFmpeg_FIND_COMPONENTS})
  97. list(APPEND _FFmpeg_REQUIRED_VARS FFmpeg_${_component}_INCLUDE_DIRS FFmpeg_${_component}_LIBRARY)
  98. endforeach ()
  99. # Give a nice error message if some of the required vars are missing.
  100. find_package_handle_standard_args(FFmpeg DEFAULT_MSG ${_FFmpeg_REQUIRED_VARS})
  101. set(FFMPEG_LIBRARIES)
  102. if(FFmpeg_FOUND)
  103. foreach(_component IN LISTS FFmpeg_FIND_COMPONENTS)
  104. if(FFmpeg_${_component}_FOUND)
  105. list(APPEND FFMPEG_LIBRARIES FFmpeg::SDL::${_component})
  106. if(NOT TARGET FFmpeg::SDL::${_component})
  107. add_library(FFmpeg::SDL::${_component} UNKNOWN IMPORTED)
  108. set_target_properties(FFmpeg::SDL::${_component} PROPERTIES
  109. IMPORTED_LOCATION "${FFmpeg_${_component}_LIBRARY}"
  110. INTERFACE_INCLUDE_DIRECTORIES "${FFmpeg_${_component}_INCLUDE_DIRS}"
  111. INTERFACE_COMPILE_OPTIONS "${FFmpeg_${_component}_COMPILE_OPTIONS}"
  112. INTERFACE_LINK_LIBRARIES "${FFmpeg_${_component}_LIBRARIES}"
  113. INTERFACE_LINK_OPTIONS "${FFmpeg_${_component}_LINK_OPTIONS}"
  114. INTERFACE_LINK_DIRECTORIES "${FFmpeg_${_component}_LINK_DIRECTORIES}"
  115. )
  116. endif()
  117. endif()
  118. endforeach()
  119. endif()