Skip to content

Commit a3eb92e

Browse files
authored
Merge pull request #340 from OpenShot/ffmpeg-targets
FFmpeg: Create and use targets in CMake build
2 parents 3d44c77 + 56e8d8a commit a3eb92e

File tree

5 files changed

+236
-156
lines changed

5 files changed

+236
-156
lines changed

cmake/Modules/FindFFmpeg.cmake

Lines changed: 209 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,97 @@
11
# 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 additionally set.
11-
# - AVCODEC
12-
# - AVDEVICE
13-
# - AVFORMAT
14-
# - AVFILTER
15-
# - AVUTIL
16-
# - POSTPROC
17-
# - SWSCALE
18-
# - SWRESAMPLE
19-
# - AVRESAMPLE
20-
# the following variables will be defined
21-
# <component>_FOUND - System has <component>
22-
# <component>_INCLUDE_DIRS - Include directory necessary for using the <component> headers
23-
# <component>_LIBRARIES - Link these to use <component>
24-
# <component>_DEFINITIONS - Compiler switches required for using <component>
25-
# <component>_VERSION - The components version
26-
#
27-
# Copyright (c) 2006, Matthias Kretz, <[email protected]>
28-
# Copyright (c) 2008, Alexander Neundorf, <[email protected]>
29-
# Copyright (c) 2011, Michael Jansen, <[email protected]>
30-
#
31-
# Redistribution and use is allowed according to the terms of the BSD license.
32-
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
2+
#[=======================================================================[.rst:
3+
FindFFmpeg
4+
----------
5+
Try to find the requested ffmpeg components(default: avformat, avutil, avcodec)
6+
7+
IMPORTED targets
8+
^^^^^^^^^^^^^^^^
9+
10+
This module defines :prop_tgt:`IMPORTED` targets ``FFmpeg:<component>`` for
11+
each found component (see below).
12+
13+
Components
14+
^^^^^^^^^^
15+
16+
The module recognizes the following components:
17+
18+
::
19+
20+
avcodec - target FFmpeg::avcodec
21+
avdevice - target FFmpeg::avdevice
22+
avformat - target FFmpeg::avformat
23+
avfilter - target FFmpeg::avfilter
24+
avutil - target FFmpeg::avutil
25+
postproc - target FFmpeg::postproc
26+
swscale - target FFmpeg::swscale
27+
swresample - target FFmpeg::swresample
28+
avresample - target FFmpeg::avresample
29+
30+
Result Variables
31+
^^^^^^^^^^^^^^^^
32+
33+
This module defines the following variables:
34+
35+
::
36+
37+
FFMPEG_FOUND - System has the all required components.
38+
FFMPEG_INCLUDE_DIRS - Include directory necessary for using the required components headers.
39+
FFMPEG_LIBRARIES - Link these to use the required ffmpeg components.
40+
FFMPEG_DEFINITIONS - Compiler switches required for using the required ffmpeg components.
41+
42+
For each component, ``<component>_FOUND`` will be set if the component is available.
43+
44+
For each ``<component>_FOUND``, the following variables will be defined:
45+
46+
::
47+
48+
<component>_INCLUDE_DIRS - Include directory necessary for using the <component> headers
49+
<component>_LIBRARIES - Link these to use <component>
50+
<component>_DEFINITIONS - Compiler switches required for using <component>
51+
<component>_VERSION - The components version
3352
53+
Backwards compatibility
54+
^^^^^^^^^^^^^^^^^^^^^^^
55+
56+
For compatibility with previous versions of this module, uppercase names
57+
for FFmpeg and for all components are also recognized, and all-uppercase
58+
versions of the cache variables are also created.
59+
60+
Copyright (c) 2006, Matthias Kretz, <[email protected]>
61+
Copyright (c) 2008, Alexander Neundorf, <[email protected]>
62+
Copyright (c) 2011, Michael Jansen, <[email protected]>
63+
Copyright (c) 2019, FeRD (Frank Dana) <[email protected]>
64+
65+
Redistribution and use is allowed according to the terms of the BSD license.
66+
For details see the accompanying COPYING-CMAKE-SCRIPTS file.
67+
#]=======================================================================]
3468
include(FindPackageHandleStandardArgs)
3569

36-
# The default components were taken from a survey over other FindFFMPEG.cmake files
70+
set(FFmpeg_ALL_COMPONENTS avcodec avdevice avformat avfilter avutil postproc swscale swresample avresample)
71+
72+
# Default to all components, if not specified
73+
if (FFMPEG_FIND_COMPONENTS AND NOT FFmpeg_FIND_COMPONENTS)
74+
set(FFmpeg_FIND_COMPONENTS ${FFMPEG_FIND_COMPONENTS})
75+
endif ()
3776
if (NOT FFmpeg_FIND_COMPONENTS)
38-
set(FFmpeg_FIND_COMPONENTS AVCODEC AVFORMAT AVUTIL)
77+
set(FFmpeg_FIND_COMPONENTS ${FFmpeg_ALL_COMPONENTS})
3978
endif ()
4079

80+
4181
#
4282
### Macro: set_component_found
4383
#
4484
# Marks the given component as found if both *_LIBRARIES AND *_INCLUDE_DIRS is present.
4585
#
4686
macro(set_component_found _component )
47-
if (${_component}_LIBRARIES AND ${_component}_INCLUDE_DIRS)
48-
# message(STATUS " - ${_component} found.")
49-
set(${_component}_FOUND TRUE)
50-
else ()
51-
# message(STATUS " - ${_component} not found.")
52-
endif ()
87+
if (${_component}_LIBRARIES AND ${_component}_INCLUDE_DIRS)
88+
# message(STATUS "FFmpeg - ${_component} found.")
89+
set(${_component}_FOUND TRUE)
90+
else ()
91+
if (NOT FFmpeg_FIND_QUIETLY AND NOT FFMPEG_FIND_QUIETLY)
92+
message(STATUS "FFmpeg - ${_component} not found.")
93+
endif ()
94+
endif ()
5395
endmacro()
5496

5597
#
@@ -60,102 +102,146 @@ endmacro()
60102
#
61103
macro(find_component _component _pkgconfig _library _header)
62104

63-
if (NOT WIN32)
64-
# use pkg-config to get the directories and then use these values
65-
# in the FIND_PATH() and FIND_LIBRARY() calls
66-
find_package(PkgConfig)
67-
if (PKG_CONFIG_FOUND)
68-
pkg_check_modules(PC_${_component} ${_pkgconfig})
69-
endif ()
70-
endif (NOT WIN32)
71-
72-
find_path(${_component}_INCLUDE_DIRS ${_header}
73-
HINTS
74-
/opt/
75-
/opt/include/
76-
${PC_LIB${_component}_INCLUDEDIR}
77-
${PC_LIB${_component}_INCLUDE_DIRS}
78-
$ENV{FFMPEGDIR}/include/
79-
$ENV{FFMPEGDIR}/include/ffmpeg/
80-
PATH_SUFFIXES
81-
ffmpeg
82-
)
83-
84-
find_library(${_component}_LIBRARIES NAMES ${_library}
85-
HINTS
86-
${PC_LIB${_component}_LIBDIR}
87-
${PC_LIB${_component}_LIBRARY_DIRS}
88-
$ENV{FFMPEGDIR}/lib/
89-
$ENV{FFMPEGDIR}/lib/ffmpeg/
90-
$ENV{FFMPEGDIR}/bin/
91-
)
92-
93-
set(${_component}_DEFINITIONS ${PC_${_component}_CFLAGS_OTHER} CACHE STRING "The ${_component} CFLAGS.")
94-
set(${_component}_VERSION ${PC_${_component}_VERSION} CACHE STRING "The ${_component} version number.")
95-
96-
set_component_found(${_component})
97-
98-
mark_as_advanced(
99-
${_component}_INCLUDE_DIRS
100-
${_component}_LIBRARIES
101-
${_component}_DEFINITIONS
102-
${_component}_VERSION)
105+
if (NOT WIN32)
106+
# use pkg-config to get the directories and then use these values
107+
# in the FIND_PATH() and FIND_LIBRARY() calls
108+
find_package(PkgConfig)
109+
if (PKG_CONFIG_FOUND)
110+
pkg_check_modules(PC_${_component} ${_pkgconfig})
111+
endif ()
112+
endif (NOT WIN32)
113+
114+
find_path(${_component}_INCLUDE_DIRS ${_header}
115+
HINTS
116+
/opt/
117+
/opt/include/
118+
${PC_${_component}_INCLUDEDIR}
119+
${PC_${_component}_INCLUDE_DIRS}
120+
$ENV{FFMPEGDIR}/include/
121+
$ENV{FFMPEGDIR}/include/ffmpeg/
122+
PATH_SUFFIXES
123+
ffmpeg
124+
)
125+
126+
find_library(${_component}_LIBRARIES NAMES ${_library}
127+
HINTS
128+
${PC_${_component}_LIBDIR}
129+
${PC_${_component}_LIBRARY_DIRS}
130+
$ENV{FFMPEGDIR}/lib/
131+
$ENV{FFMPEGDIR}/lib/ffmpeg/
132+
$ENV{FFMPEGDIR}/bin/
133+
)
134+
135+
set(${_component}_DEFINITIONS ${PC_${_component}_CFLAGS_OTHER} CACHE STRING "The ${_component} CFLAGS.")
136+
set(${_component}_VERSION ${PC_${_component}_VERSION} CACHE STRING "The ${_component} version number.")
137+
138+
set_component_found(${_component})
139+
140+
mark_as_advanced(
141+
${_component}_INCLUDE_DIRS
142+
${_component}_LIBRARIES
143+
${_component}_DEFINITIONS
144+
${_component}_VERSION
145+
)
103146

104147
endmacro()
105148

106149

107150
# Check for cached results. If there are skip the costly part.
108-
if (NOT FFMPEG_LIBRARIES)
109-
110-
# Check for all possible component.
111-
find_component(AVCODEC libavcodec avcodec libavcodec/avcodec.h)
112-
find_component(AVFORMAT libavformat avformat libavformat/avformat.h)
113-
find_component(AVDEVICE libavdevice avdevice libavdevice/avdevice.h)
114-
find_component(AVUTIL libavutil avutil libavutil/avutil.h)
115-
find_component(AVFILTER libavfilter avfilter libavfilter/avfilter.h)
116-
find_component(SWSCALE libswscale swscale libswscale/swscale.h)
117-
find_component(POSTPROC libpostproc postproc libpostproc/postprocess.h)
118-
find_component(SWRESAMPLE libswresample swresample libswresample/swresample.h)
119-
find_component(AVRESAMPLE libavresample avresample libavresample/avresample.h)
120-
121-
# Check if the required components were found and add their stuff to the FFMPEG_* vars.
122-
foreach (_component ${FFmpeg_FIND_COMPONENTS})
123-
if (${_component}_FOUND)
124-
# message(STATUS "Required component ${_component} present.")
125-
set(FFMPEG_LIBRARIES ${FFMPEG_LIBRARIES} ${${_component}_LIBRARIES})
126-
set(FFMPEG_DEFINITIONS ${FFMPEG_DEFINITIONS} ${${_component}_DEFINITIONS})
127-
list(APPEND FFMPEG_INCLUDE_DIRS ${${_component}_INCLUDE_DIRS})
128-
else ()
129-
# message(STATUS "Required component ${_component} missing.")
130-
endif ()
131-
endforeach ()
132-
133-
# Build the include path with duplicates removed.
134-
if (FFMPEG_INCLUDE_DIRS)
135-
list(REMOVE_DUPLICATES FFMPEG_INCLUDE_DIRS)
136-
endif ()
137-
138-
# cache the vars.
139-
set(FFMPEG_INCLUDE_DIRS ${FFMPEG_INCLUDE_DIRS} CACHE STRING "The FFmpeg include directories." FORCE)
140-
set(FFMPEG_LIBRARIES ${FFMPEG_LIBRARIES} CACHE STRING "The FFmpeg libraries." FORCE)
141-
set(FFMPEG_DEFINITIONS ${FFMPEG_DEFINITIONS} CACHE STRING "The FFmpeg cflags." FORCE)
142-
143-
mark_as_advanced(FFMPEG_INCLUDE_DIRS
144-
FFMPEG_LIBRARIES
145-
FFMPEG_DEFINITIONS)
151+
if (NOT FFmpeg_LIBRARIES)
152+
153+
# Check for all possible component.
154+
find_component(avcodec libavcodec avcodec libavcodec/avcodec.h)
155+
find_component(avdevice libavdevice avdevice libavdevice/avdevice.h)
156+
find_component(avformat libavformat avformat libavformat/avformat.h)
157+
find_component(avfilter libavfilter avfilter libavfilter/avfilter.h)
158+
find_component(avutil libavutil avutil libavutil/avutil.h)
159+
find_component(postproc libpostproc postproc libpostproc/postprocess.h)
160+
find_component(swscale libswscale swscale libswscale/swscale.h)
161+
find_component(swresample libswresample swresample libswresample/swresample.h)
162+
find_component(avresample libavresample avresample libavresample/avresample.h)
163+
else()
164+
# Just set the noncached _FOUND vars for the components.
165+
foreach(_component ${FFmpeg_ALL_COMPONENTS})
166+
set_component_found(${_component})
167+
endforeach ()
168+
endif()
169+
170+
# Check if the requested components were found and add their stuff to the FFmpeg_* vars.
171+
foreach (_component ${FFmpeg_FIND_COMPONENTS})
172+
string(TOLOWER "${_component}" _component)
173+
if (${_component}_FOUND)
174+
# message(STATUS "Requested component ${_component} present.")
175+
set(FFmpeg_LIBRARIES ${FFmpeg_LIBRARIES} ${${_component}_LIBRARIES})
176+
set(FFmpeg_DEFINITIONS ${FFmpeg_DEFINITIONS} ${${_component}_DEFINITIONS})
177+
list(APPEND FFmpeg_INCLUDE_DIRS ${${_component}_INCLUDE_DIRS})
178+
else ()
179+
# message(STATUS "Requested component ${_component} missing.")
180+
endif ()
181+
endforeach ()
146182

183+
# Build the include path with duplicates removed.
184+
if (FFmpeg_INCLUDE_DIRS)
185+
list(REMOVE_DUPLICATES FFmpeg_INCLUDE_DIRS)
147186
endif ()
148187

149-
# Now set the noncached _FOUND vars for the components.
150-
foreach (_component AVCODEC AVDEVICE AVFORMAT AVUTIL POSTPROCESS SWSCALE SWRESAMPLE AVRESAMPLE)
151-
set_component_found(${_component})
152-
endforeach ()
188+
# cache the vars.
189+
set(FFmpeg_INCLUDE_DIRS ${FFmpeg_INCLUDE_DIRS} CACHE STRING "The FFmpeg include directories." FORCE)
190+
set(FFmpeg_LIBRARIES ${FFmpeg_LIBRARIES} CACHE STRING "The FFmpeg libraries." FORCE)
191+
set(FFmpeg_DEFINITIONS ${FFmpeg_DEFINITIONS} CACHE STRING "The FFmpeg cflags." FORCE)
192+
193+
mark_as_advanced(FFmpeg_INCLUDE_DIRS
194+
FFmpeg_LIBRARIES
195+
FFmpeg_DEFINITIONS)
196+
197+
# Backwards compatibility
198+
foreach(_suffix INCLUDE_DIRS LIBRARIES DEFINITIONS)
199+
get_property(_help CACHE FFmpeg_${_suffix} PROPERTY HELPSTRING)
200+
set(FFMPEG_${_suffix} ${FFmpeg_${_suffix}} CACHE STRING "${_help}" FORCE)
201+
mark_as_advanced(FFMPEG_${_suffix})
202+
endforeach()
203+
foreach(_component ${FFmpeg_ALL_COMPONENTS})
204+
if(${_component}_FOUND)
205+
string(TOUPPER "${_component}" _uc_component)
206+
set(${_uc_component}_FOUND TRUE)
207+
foreach(_suffix INCLUDE_DIRS LIBRARIES DEFINITIONS VERSION)
208+
get_property(_help CACHE ${_component}_${_suffix} PROPERTY HELPSTRING)
209+
set(${_uc_component}_${_suffix} ${${_component}_${_suffix}} CACHE STRING "${_help}" FORCE)
210+
mark_as_advanced(${_uc_component}_${_suffix})
211+
endforeach()
212+
endif()
213+
endforeach()
153214

154215
# Compile the list of required vars
155-
set(_FFmpeg_REQUIRED_VARS FFMPEG_LIBRARIES FFMPEG_INCLUDE_DIRS)
216+
set(_FFmpeg_REQUIRED_VARS FFmpeg_LIBRARIES FFmpeg_INCLUDE_DIRS)
156217
foreach (_component ${FFmpeg_FIND_COMPONENTS})
157-
list(APPEND _FFmpeg_REQUIRED_VARS ${_component}_LIBRARIES ${_component}_INCLUDE_DIRS)
218+
list(APPEND _FFmpeg_REQUIRED_VARS
219+
${_component}_LIBRARIES
220+
${_component}_INCLUDE_DIRS)
158221
endforeach ()
159222

160223
# Give a nice error message if some of the required vars are missing.
161224
find_package_handle_standard_args(FFmpeg DEFAULT_MSG ${_FFmpeg_REQUIRED_VARS})
225+
226+
# Export targets for each found component
227+
foreach (_component ${FFmpeg_ALL_COMPONENTS})
228+
229+
if(${_component}_FOUND)
230+
# message(STATUS "Creating IMPORTED target FFmpeg::${_component}")
231+
232+
if(NOT TARGET FFmpeg::${_component})
233+
add_library(FFmpeg::${_component} UNKNOWN IMPORTED)
234+
235+
set_target_properties(FFmpeg::${_component} PROPERTIES
236+
INTERFACE_INCLUDE_DIRECTORIES "${${_component}_INCLUDE_DIRS}")
237+
238+
set_property(TARGET FFmpeg::${_component} APPEND PROPERTY
239+
INTERFACE_COMPILE_DEFINITIONS "${${_component}_DEFINITIONS}")
240+
241+
set_property(TARGET FFmpeg::${_component} APPEND PROPERTY
242+
IMPORTED_LOCATION "${${_component}_LIBRARIES}")
243+
endif()
244+
245+
endif()
246+
247+
endforeach()

0 commit comments

Comments
 (0)