FindFFMPEG.cmake
Go to the documentation of this file.
1 #[==[
2 Provides the following variables:
3 
4  * `FFMPEG_INCLUDE_DIRS`: Include directories necessary to use FFMPEG.
5  * `FFMPEG_LIBRARIES`: Libraries necessary to use FFMPEG. Note that this only
6  includes libraries for the components requested.
7  * `FFMPEG_VERSION`: The version of FFMPEG found.
8 
9 The following components are supported:
10 
11  * `avcodec`
12  * `avdevice`
13  * `avfilter`
14  * `avformat`
15  * `avresample`
16  * `avutil`
17  * `swresample`
18  * `swscale`
19 
20 For each component, the following are provided:
21 
22  * `FFMPEG_<component>_FOUND`: Libraries for the component.
23  * `FFMPEG_<component>_INCLUDE_DIRS`: Include directories for
24  the component.
25  * `FFMPEG_<component>_LIBRARIES`: Libraries for the component.
26  * `FFMPEG::<component>`: A target to use with `target_link_libraries`.
27 
28 Note that only components requested with `COMPONENTS` or `OPTIONAL_COMPONENTS`
29 are guaranteed to set these variables or provide targets.
30 #]==]
31 
32 function (_ffmpeg_find component headername)
33  find_path("FFMPEG_${component}_INCLUDE_DIR"
34  NAMES
35  "lib${component}/${headername}"
36  PATHS
37  "${FFMPEG_ROOT}/include"
38  ~/Library/Frameworks
39  /Library/Frameworks
40  /usr/local/include
41  /usr/include
42  /sw/include # Fink
43  /opt/local/include # DarwinPorts
44  /opt/csw/include # Blastwave
45  /opt/include
46  /usr/freeware/include
47  PATH_SUFFIXES
48  ffmpeg
49  DOC "FFMPEG's ${component} include directory")
50  mark_as_advanced("FFMPEG_${component}_INCLUDE_DIR")
51 
52  # On Windows, static FFMPEG is sometimes built as `lib<name>.a`.
53  if (WIN32)
54  list(APPEND CMAKE_FIND_LIBRARY_SUFFIXES ".a" ".lib")
55  list(APPEND CMAKE_FIND_LIBRARY_PREFIXES "" "lib")
56  endif ()
57 
58  find_library("FFMPEG_${component}_LIBRARY"
59  NAMES
60  "${component}"
61  PATHS
62  "${FFMPEG_ROOT}/lib"
63  ~/Library/Frameworks
64  /Library/Frameworks
65  /usr/local/lib
66  /usr/local/lib64
67  /usr/lib
68  /usr/lib64
69  /sw/lib
70  /opt/local/lib
71  /opt/csw/lib
72  /opt/lib
73  /usr/freeware/lib64
74  "${FFMPEG_ROOT}/bin"
75  DOC "FFMPEG's ${component} library")
76  mark_as_advanced("FFMPEG_${component}_LIBRARY")
77 
78  if (FFMPEG_${component}_LIBRARY AND FFMPEG_${component}_INCLUDE_DIR)
79  set(_deps_found TRUE)
80  set(_deps_link)
81  foreach (_ffmpeg_dep IN LISTS ARGN)
82  if (TARGET "FFMPEG::${_ffmpeg_dep}")
83  list(APPEND _deps_link "FFMPEG::${_ffmpeg_dep}")
84  else ()
85  set(_deps_found FALSE)
86  endif ()
87  endforeach ()
88  if (_deps_found)
89  if (NOT TARGET "FFMPEG::${component}")
90  add_library("FFMPEG::${component}" UNKNOWN IMPORTED)
91  set_target_properties("FFMPEG::${component}" PROPERTIES
92  IMPORTED_LOCATION "${FFMPEG_${component}_LIBRARY}"
93  INTERFACE_INCLUDE_DIRECTORIES "${FFMPEG_${component}_INCLUDE_DIR}"
94  IMPORTED_LINK_INTERFACE_LIBRARIES "${_deps_link}")
95  endif ()
96  set("FFMPEG_${component}_FOUND" 1
97  PARENT_SCOPE)
98 
99  set(version_header_path "${FFMPEG_${component}_INCLUDE_DIR}/lib${component}/version.h")
100  if (EXISTS "${version_header_path}")
101  string(TOUPPER "${component}" component_upper)
102  file(STRINGS "${version_header_path}" version
103  REGEX "#define *LIB${component_upper}_VERSION_(MAJOR|MINOR|MICRO) ")
104  string(REGEX REPLACE ".*_MAJOR *\([0-9]*\).*" "\\1" major "${version}")
105  string(REGEX REPLACE ".*_MINOR *\([0-9]*\).*" "\\1" minor "${version}")
106  string(REGEX REPLACE ".*_MICRO *\([0-9]*\).*" "\\1" micro "${version}")
107  if (NOT major STREQUAL "" AND
108  NOT minor STREQUAL "" AND
109  NOT micro STREQUAL "")
110  set("FFMPEG_${component}_VERSION" "${major}.${minor}.${micro}"
111  PARENT_SCOPE)
112  endif ()
113  endif ()
114  else ()
115  set("FFMPEG_${component}_FOUND" 0
116  PARENT_SCOPE)
117  set(what)
118  if (NOT FFMPEG_${component}_LIBRARY)
119  set(what "library")
120  endif ()
121  if (NOT FFMPEG_${component}_INCLUDE_DIR)
122  if (what)
123  string(APPEND what " or headers")
124  else ()
125  set(what "headers")
126  endif ()
127  endif ()
128  set("FFMPEG_${component}_NOT_FOUND_MESSAGE"
129  "Could not find the ${what} for ${component}."
130  PARENT_SCOPE)
131  endif ()
132  endif ()
133 endfunction ()
134 
135 _ffmpeg_find(avutil avutil.h)
136 _ffmpeg_find(avresample avresample.h
137  avutil)
138 _ffmpeg_find(swresample swresample.h
139  avutil)
140 _ffmpeg_find(swscale swscale.h
141  avutil)
142 _ffmpeg_find(avcodec avcodec.h
143  avutil)
144 _ffmpeg_find(avformat avformat.h
145  avcodec avutil)
146 _ffmpeg_find(avfilter avfilter.h
147  avutil)
148 _ffmpeg_find(avdevice avdevice.h
149  avformat avutil)
150 
151 if (TARGET FFMPEG::avutil)
152  set(_ffmpeg_version_header_path "${FFMPEG_avutil_INCLUDE_DIR}/libavutil/ffversion.h")
153  if (EXISTS "${_ffmpeg_version_header_path}")
154  file(STRINGS "${_ffmpeg_version_header_path}" _ffmpeg_version
155  REGEX "FFMPEG_VERSION")
156  string(REGEX REPLACE ".*\"n?\(.*\)\"" "\\1" FFMPEG_VERSION "${_ffmpeg_version}")
157  unset(_ffmpeg_version)
158  else ()
159  set(FFMPEG_VERSION FFMPEG_VERSION-NOTFOUND)
160  endif ()
161  unset(_ffmpeg_version_header_path)
162 endif ()
163 
164 set(FFMPEG_INCLUDE_DIRS)
165 set(FFMPEG_LIBRARIES)
166 set(_ffmpeg_required_vars)
167 foreach (_ffmpeg_component IN LISTS FFMPEG_FIND_COMPONENTS)
168  if (TARGET "FFMPEG::${_ffmpeg_component}")
169  set(FFMPEG_${_ffmpeg_component}_INCLUDE_DIRS
170  "${FFMPEG_${_ffmpeg_component}_INCLUDE_DIR}")
171  set(FFMPEG_${_ffmpeg_component}_LIBRARIES
172  "${FFMPEG_${_ffmpeg_component}_LIBRARY}")
173  list(APPEND FFMPEG_INCLUDE_DIRS
174  "${FFMPEG_${_ffmpeg_component}_INCLUDE_DIRS}")
175  list(APPEND FFMPEG_LIBRARIES
176  "${FFMPEG_${_ffmpeg_component}_LIBRARIES}")
177  if (FFMEG_FIND_REQUIRED_${_ffmpeg_component})
178  list(APPEND _ffmpeg_required_vars
179  "FFMPEG_${_ffmpeg_required_vars}_INCLUDE_DIRS"
180  "FFMPEG_${_ffmpeg_required_vars}_LIBRARIES")
181  endif ()
182  endif ()
183 endforeach ()
184 unset(_ffmpeg_component)
185 
186 if (FFMPEG_INCLUDE_DIRS)
187  list(REMOVE_DUPLICATES FFMPEG_INCLUDE_DIRS)
188 endif ()
189 
190 include(FindPackageHandleStandardArgs)
191 find_package_handle_standard_args(FFMPEG
192  REQUIRED_VARS FFMPEG_INCLUDE_DIRS FFMPEG_LIBRARIES ${_ffmpeg_required_vars}
193  VERSION_VAR FFMPEG_VERSION
194  HANDLE_COMPONENTS)
195 unset(_ffmpeg_required_vars)
component
boost::graph_traits< vtkGraph *>::vertex_descriptor target(boost::graph_traits< vtkGraph *>::edge_descriptor e, vtkGraph *)
version
function _ffmpeg_find(component, headername)
Provides the following variables: