FindFFmpeg.cmake 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #
  2. # This module defines the following variables:
  3. #
  4. # FFMPEG_FOUND - All required components and the core library were found
  5. # FFMPEG_INCLUDE_DIRS - Combined list of all components include dirs
  6. # FFMPEG_LIBRARIES - Combined list of all components libraries
  7. # FFMPEG_VERSION_STRING - Version of the first component requested
  8. #
  9. # For each requested component the following variables are defined:
  10. #
  11. # FFMPEG_<component>_FOUND - The component was found
  12. # FFMPEG_<component>_INCLUDE_DIRS - The components include dirs
  13. # FFMPEG_<component>_LIBRARIES - The components libraries
  14. # FFMPEG_<component>_VERSION_STRING - The components version string
  15. # FFMPEG_<component>_VERSION_MAJOR - The components major version
  16. # FFMPEG_<component>_VERSION_MINOR - The components minor version
  17. # FFMPEG_<component>_VERSION_MICRO - The components micro version
  18. #
  19. # <component> is the uppercase name of the component
  20. find_package(PkgConfig QUIET)
  21. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  22. set(_lib_suffix 64)
  23. else()
  24. set(_lib_suffix 32)
  25. endif()
  26. function(find_ffmpeg_library component header)
  27. string(TOUPPER "${component}" component_u)
  28. set(FFMPEG_${component_u}_FOUND FALSE PARENT_SCOPE)
  29. set(FFmpeg_${component}_FOUND FALSE PARENT_SCOPE)
  30. if(PKG_CONFIG_FOUND)
  31. pkg_check_modules(PC_FFMPEG_${component} QUIET lib${component})
  32. endif()
  33. find_path(FFMPEG_${component}_INCLUDE_DIR
  34. NAMES
  35. "lib${component}/${header}" "lib${component}/version.h"
  36. HINTS
  37. ENV FFmpegPath${_lib_suffix}
  38. ENV FFmpegPath
  39. ENV DepsPath${_lib_suffix}
  40. ENV DepsPath
  41. ${FFmpegPath${_lib_suffix}}
  42. ${FFmpegPath}
  43. ${DepsPath${_lib_suffix}}
  44. ${DepsPath}
  45. ${PC_FFMPEG_${component}_INCLUDE_DIRS}
  46. PATHS
  47. /usr/include /usr/local/include /opt/local/include /sw/include
  48. PATH_SUFFIXES ffmpeg libav include)
  49. find_library(FFMPEG_${component}_LIBRARY
  50. NAMES
  51. "${component}" "lib${component}"
  52. HINTS
  53. ENV FFmpegPath${_lib_suffix}
  54. ENV FFmpegPath
  55. ENV DepsPath${_lib_suffix}
  56. ENV DepsPath
  57. ${FFmpegPath${_lib_suffix}}
  58. ${FFmpegPath}
  59. ${DepsPath${_lib_suffix}}
  60. ${DepsPath}
  61. ${PC_FFMPEG_${component}_LIBRARY_DIRS}
  62. PATHS
  63. /usr/lib /usr/local/lib /opt/local/lib /sw/lib
  64. PATH_SUFFIXES
  65. lib${_lib_suffix} lib
  66. libs${_lib_suffix} libs
  67. bin${_lib_suffix} bin
  68. ../lib${_lib_suffix} ../lib
  69. ../libs${_lib_suffix} ../libs
  70. ../bin${_lib_suffix} ../bin)
  71. set(FFMPEG_${component_u}_INCLUDE_DIRS ${FFMPEG_${component}_INCLUDE_DIR} PARENT_SCOPE)
  72. set(FFMPEG_${component_u}_LIBRARIES ${FFMPEG_${component}_LIBRARY} PARENT_SCOPE)
  73. mark_as_advanced(FFMPEG_${component}_INCLUDE_DIR FFMPEG_${component}_LIBRARY)
  74. if(FFMPEG_${component}_INCLUDE_DIR AND FFMPEG_${component}_LIBRARY)
  75. set(FFMPEG_${component_u}_FOUND TRUE PARENT_SCOPE)
  76. set(FFmpeg_${component}_FOUND TRUE PARENT_SCOPE)
  77. list(APPEND FFMPEG_INCLUDE_DIRS ${FFMPEG_${component}_INCLUDE_DIR})
  78. list(REMOVE_DUPLICATES FFMPEG_INCLUDE_DIRS)
  79. set(FFMPEG_INCLUDE_DIRS "${FFMPEG_INCLUDE_DIRS}" PARENT_SCOPE)
  80. list(APPEND FFMPEG_LIBRARIES ${FFMPEG_${component}_LIBRARY})
  81. list(REMOVE_DUPLICATES FFMPEG_LIBRARIES)
  82. set(FFMPEG_LIBRARIES "${FFMPEG_LIBRARIES}" PARENT_SCOPE)
  83. set(FFMPEG_${component_u}_VERSION_STRING "unknown" PARENT_SCOPE)
  84. set(_vfile "${FFMPEG_${component}_INCLUDE_DIR}/lib${component}/version.h")
  85. if(EXISTS "${_vfile}")
  86. file(STRINGS "${_vfile}" _version_parse REGEX "^.*VERSION_(MAJOR|MINOR|MICRO)[ \t]+[0-9]+[ \t]*$")
  87. string(REGEX REPLACE ".*VERSION_MAJOR[ \t]+([0-9]+).*" "\\1" _major "${_version_parse}")
  88. string(REGEX REPLACE ".*VERSION_MINOR[ \t]+([0-9]+).*" "\\1" _minor "${_version_parse}")
  89. string(REGEX REPLACE ".*VERSION_MICRO[ \t]+([0-9]+).*" "\\1" _micro "${_version_parse}")
  90. set(FFMPEG_${component_u}_VERSION_MAJOR "${_major}" PARENT_SCOPE)
  91. set(FFMPEG_${component_u}_VERSION_MINOR "${_minor}" PARENT_SCOPE)
  92. set(FFMPEG_${component_u}_VERSION_MICRO "${_micro}" PARENT_SCOPE)
  93. set(FFMPEG_${component_u}_VERSION_STRING "${_major}.${_minor}.${_micro}" PARENT_SCOPE)
  94. else()
  95. message(STATUS "Failed parsing FFmpeg ${component} version")
  96. endif()
  97. endif()
  98. endfunction()
  99. set(FFMPEG_INCLUDE_DIRS)
  100. set(FFMPEG_LIBRARIES)
  101. if(NOT FFmpeg_FIND_COMPONENTS)
  102. message(FATAL_ERROR "No FFmpeg components requested")
  103. endif()
  104. list(GET FFmpeg_FIND_COMPONENTS 0 _first_comp)
  105. string(TOUPPER "${_first_comp}" _first_comp)
  106. foreach(component ${FFmpeg_FIND_COMPONENTS})
  107. if(component STREQUAL "avcodec")
  108. find_ffmpeg_library("${component}" "avcodec.h")
  109. elseif(component STREQUAL "avdevice")
  110. find_ffmpeg_library("${component}" "avdevice.h")
  111. elseif(component STREQUAL "avfilter")
  112. find_ffmpeg_library("${component}" "avfilter.h")
  113. elseif(component STREQUAL "avformat")
  114. find_ffmpeg_library("${component}" "avformat.h")
  115. elseif(component STREQUAL "avresample")
  116. find_ffmpeg_library("${component}" "avresample.h")
  117. elseif(component STREQUAL "avutil")
  118. find_ffmpeg_library("${component}" "avutil.h")
  119. elseif(component STREQUAL "postproc")
  120. find_ffmpeg_library("${component}" "postprocess.h")
  121. elseif(component STREQUAL "swresample")
  122. find_ffmpeg_library("${component}" "swresample.h")
  123. elseif(component STREQUAL "swscale")
  124. find_ffmpeg_library("${component}" "swscale.h")
  125. else()
  126. message(FATAL_ERROR "Unknown FFmpeg component requested: ${component}")
  127. endif()
  128. endforeach()
  129. include(FindPackageHandleStandardArgs)
  130. find_package_handle_standard_args(FFmpeg
  131. FOUND_VAR FFMPEG_FOUND
  132. REQUIRED_VARS FFMPEG_${_first_comp}_LIBRARIES FFMPEG_${_first_comp}_INCLUDE_DIRS
  133. VERSION_VAR FFMPEG_${_first_comp}_VERSION_STRING
  134. HANDLE_COMPONENTS)