FindFFmpeg.cmake 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. # vim: ts=2 sw=2
  2. # - Try to find the required ffmpeg components(default: AVFORMAT, AVUTIL, AVCODEC)
  3. #
  4. # Once done this will define
  5. # FFMPEG_FOUND - System has the all required components.
  6. # FFMPEG_INCLUDE_DIRS - Include directory necessary for using the required components headers.
  7. # FFMPEG_LIBRARIES - Link these to use the required ffmpeg components.
  8. # FFMPEG_DEFINITIONS - Compiler switches required for using the required ffmpeg components.
  9. #
  10. # For each of the components it will additionaly set.
  11. # - AVCODEC
  12. # - AVDEVICE
  13. # - AVFORMAT
  14. # - AVUTIL
  15. # - POSTPROC
  16. # - SWSCALE
  17. # - SWRESAMPLE
  18. # the following variables will be defined
  19. # <component>_FOUND - System has <component>
  20. # <component>_INCLUDE_DIRS - Include directory necessary for using the <component> headers
  21. # <component>_LIBRARIES - Link these to use <component>
  22. # <component>_DEFINITIONS - Compiler switches required for using <component>
  23. # <component>_VERSION - The components version
  24. #
  25. # Copyright (c) 2006, Matthias Kretz, <[email protected]>
  26. # Copyright (c) 2008, Alexander Neundorf, <[email protected]>
  27. # Copyright (c) 2011, Michael Jansen, <[email protected]>
  28. #
  29. # Redistribution and use is allowed according to the terms of the BSD license.
  30. include(FindPackageHandleStandardArgs)
  31. if(NOT FFmpeg_FIND_COMPONENTS)
  32. set(FFmpeg_FIND_COMPONENTS AVFORMAT AVCODEC AVUTIL)
  33. endif()
  34. #
  35. ### Macro: set_component_found
  36. #
  37. # Marks the given component as found if both *_LIBRARIES AND *_INCLUDE_DIRS is present.
  38. #
  39. macro(set_component_found _component)
  40. if(${_component}_LIBRARIES AND ${_component}_INCLUDE_DIRS)
  41. # message(STATUS " - ${_component} found.")
  42. set(${_component}_FOUND TRUE)
  43. else()
  44. # message(STATUS " - ${_component} not found.")
  45. endif()
  46. endmacro()
  47. #
  48. ### Macro: find_component
  49. #
  50. # Checks for the given component by invoking pkgconfig and then looking up the libraries and
  51. # include directories.
  52. #
  53. macro(find_component _component _pkgconfig _library _header)
  54. if(NOT WIN32)
  55. # use pkg-config to get the directories and then use these values
  56. # in the FIND_PATH() and FIND_LIBRARY() calls
  57. find_package(PkgConfig)
  58. if(PKG_CONFIG_FOUND)
  59. pkg_check_modules(PC_${_component} ${_pkgconfig})
  60. endif()
  61. endif()
  62. find_path(${_component}_INCLUDE_DIRS ${_header}
  63. HINTS
  64. ${FFMPEGSDK_INC}
  65. ${PC_LIB${_component}_INCLUDEDIR}
  66. ${PC_LIB${_component}_INCLUDE_DIRS}
  67. PATH_SUFFIXES
  68. ffmpeg
  69. )
  70. find_library(${_component}_LIBRARIES NAMES ${_library}
  71. HINTS
  72. ${FFMPEGSDK_LIB}
  73. ${PC_LIB${_component}_LIBDIR}
  74. ${PC_LIB${_component}_LIBRARY_DIRS}
  75. )
  76. if(DEFINED ${PC_${_component}_VERSION})
  77. set(${_component}_VERSION ${PC_${_component}_VERSION} CACHE STRING
  78. "The ${_component} version number." FORCE)
  79. elseif(EXISTS "${${_component}_INCLUDE_DIRS}/${_pkgconfig}/version.h")
  80. if(EXISTS "${${_component}_INCLUDE_DIRS}/${_pkgconfig}/version_major.h")
  81. file(STRINGS "${${_component}_INCLUDE_DIRS}/${_pkgconfig}/version_major.h" majorver
  82. REGEX "^#define[ \t]+LIB${_component}_VERSION_MAJOR[ \t]+[0-9]+$")
  83. else()
  84. file(STRINGS "${${_component}_INCLUDE_DIRS}/${_pkgconfig}/version.h" majorver
  85. REGEX "^#define[ \t]+LIB${_component}_VERSION_MAJOR[ \t]+[0-9]+$")
  86. endif()
  87. file(STRINGS "${${_component}_INCLUDE_DIRS}/${_pkgconfig}/version.h" minorver
  88. REGEX "^#define[ \t]+LIB${_component}_VERSION_MINOR[ \t]+[0-9]+$")
  89. file(STRINGS "${${_component}_INCLUDE_DIRS}/${_pkgconfig}/version.h" microver
  90. REGEX "^#define[ \t]+LIB${_component}_VERSION_MICRO[ \t]+[0-9]+$")
  91. string(REGEX REPLACE "^#define[ \t]+LIB${_component}_VERSION_MAJOR[ \t]+([0-9]+)$" "\\1"
  92. majorver "${majorver}")
  93. string(REGEX REPLACE "^#define[ \t]+LIB${_component}_VERSION_MINOR[ \t]+([0-9]+)$" "\\1"
  94. minorver "${minorver}")
  95. string(REGEX REPLACE "^#define[ \t]+LIB${_component}_VERSION_MICRO[ \t]+([0-9]+)$" "\\1"
  96. microver "${microver}")
  97. set(${_component}_VERSION "${majorver}.${minorver}.${microver}" CACHE STRING
  98. "The ${_component} version number." FORCE)
  99. unset(microver)
  100. unset(minorver)
  101. unset(majorver)
  102. endif()
  103. set(${_component}_DEFINITIONS ${PC_${_component}_CFLAGS_OTHER} CACHE STRING
  104. "The ${_component} CFLAGS." FORCE)
  105. set_component_found(${_component})
  106. mark_as_advanced(
  107. ${_component}_INCLUDE_DIRS
  108. ${_component}_LIBRARIES
  109. ${_component}_DEFINITIONS
  110. ${_component}_VERSION)
  111. endmacro()
  112. set(FFMPEGSDK $ENV{FFMPEG_HOME})
  113. if(FFMPEGSDK)
  114. set(FFMPEGSDK_INC "${FFMPEGSDK}/include")
  115. set(FFMPEGSDK_LIB "${FFMPEGSDK}/lib")
  116. endif()
  117. # Check for all possible components.
  118. find_component(AVCODEC libavcodec avcodec libavcodec/avcodec.h)
  119. find_component(AVFORMAT libavformat avformat libavformat/avformat.h)
  120. find_component(AVDEVICE libavdevice avdevice libavdevice/avdevice.h)
  121. find_component(AVUTIL libavutil avutil libavutil/avutil.h)
  122. find_component(SWSCALE libswscale swscale libswscale/swscale.h)
  123. find_component(SWRESAMPLE libswresample swresample libswresample/swresample.h)
  124. find_component(POSTPROC libpostproc postproc libpostproc/postprocess.h)
  125. # Check if the required components were found and add their stuff to the FFMPEG_* vars.
  126. foreach(_component ${FFmpeg_FIND_COMPONENTS})
  127. if(${_component}_FOUND)
  128. # message(STATUS "Required component ${_component} present.")
  129. set(FFMPEG_LIBRARIES ${FFMPEG_LIBRARIES} ${${_component}_LIBRARIES})
  130. set(FFMPEG_DEFINITIONS ${FFMPEG_DEFINITIONS} ${${_component}_DEFINITIONS})
  131. list(APPEND FFMPEG_INCLUDE_DIRS ${${_component}_INCLUDE_DIRS})
  132. else()
  133. # message(STATUS "Required component ${_component} missing.")
  134. endif()
  135. endforeach()
  136. # Add libz if it exists (needed for static ffmpeg builds)
  137. find_library(_FFmpeg_HAVE_LIBZ NAMES z)
  138. if(_FFmpeg_HAVE_LIBZ)
  139. set(FFMPEG_LIBRARIES ${FFMPEG_LIBRARIES} ${_FFmpeg_HAVE_LIBZ})
  140. endif()
  141. # Build the include path and library list with duplicates removed.
  142. if(FFMPEG_INCLUDE_DIRS)
  143. list(REMOVE_DUPLICATES FFMPEG_INCLUDE_DIRS)
  144. endif()
  145. if(FFMPEG_LIBRARIES)
  146. list(REMOVE_DUPLICATES FFMPEG_LIBRARIES)
  147. endif()
  148. # cache the vars.
  149. set(FFMPEG_INCLUDE_DIRS ${FFMPEG_INCLUDE_DIRS} CACHE STRING "The FFmpeg include directories." FORCE)
  150. set(FFMPEG_LIBRARIES ${FFMPEG_LIBRARIES} CACHE STRING "The FFmpeg libraries." FORCE)
  151. set(FFMPEG_DEFINITIONS ${FFMPEG_DEFINITIONS} CACHE STRING "The FFmpeg cflags." FORCE)
  152. mark_as_advanced(FFMPEG_INCLUDE_DIRS FFMPEG_LIBRARIES FFMPEG_DEFINITIONS)
  153. # Now set the noncached _FOUND vars for the components.
  154. foreach(_component AVCODEC AVDEVICE AVFORMAT AVUTIL POSTPROCESS SWRESAMPLE SWSCALE)
  155. set_component_found(${_component})
  156. endforeach ()
  157. # Compile the list of required vars
  158. set(_FFmpeg_REQUIRED_VARS FFMPEG_LIBRARIES FFMPEG_INCLUDE_DIRS)
  159. foreach(_component ${FFmpeg_FIND_COMPONENTS})
  160. list(APPEND _FFmpeg_REQUIRED_VARS ${_component}_LIBRARIES ${_component}_INCLUDE_DIRS)
  161. endforeach()
  162. # Give a nice error message if some of the required vars are missing.
  163. find_package_handle_standard_args(FFmpeg DEFAULT_MSG ${_FFmpeg_REQUIRED_VARS})