summaryrefslogtreecommitdiff
path: root/CMakeModules/FindFFmpeg.cmake
diff options
context:
space:
mode:
Diffstat (limited to 'CMakeModules/FindFFmpeg.cmake')
-rw-r--r--CMakeModules/FindFFmpeg.cmake195
1 files changed, 195 insertions, 0 deletions
diff --git a/CMakeModules/FindFFmpeg.cmake b/CMakeModules/FindFFmpeg.cmake
new file mode 100644
index 000000000..eedf28aea
--- /dev/null
+++ b/CMakeModules/FindFFmpeg.cmake
@@ -0,0 +1,195 @@
1# SPDX-FileCopyrightText: 2019 Citra Emulator Project
2# SPDX-License-Identifier: GPL-2.0-or-later
3
4# FindFFmpeg
5# ----------
6#
7# Find the native FFmpeg includes and libraries
8#
9# This module defines the following variables:
10#
11# FFmpeg_INCLUDE_<component>: where to find <component>.h
12# FFmpeg_LIBRARY_<component>: where to find the <component> library
13# FFmpeg_INCLUDE_DIR: aggregate all the include paths
14# FFmpeg_LIBRARIES: aggregate all the paths to the libraries
15# FFmpeg_FOUND: True if all components have been found
16#
17# This module defines the following targets, which are prefered over variables:
18#
19# FFmpeg::<component>: Target to use <component> directly, with include path,
20# library and dependencies set up. If you are using a static build, you are
21# responsible for adding any external dependencies (such as zlib, bzlib...).
22#
23# <component> can be one of:
24# avcodec
25# avdevice
26# avfilter
27# avformat
28# avutil
29# postproc
30# swresample
31# swscale
32#
33
34set(_FFmpeg_ALL_COMPONENTS
35 avcodec
36 avdevice
37 avfilter
38 avformat
39 avutil
40 postproc
41 swresample
42 swscale
43)
44
45set(_FFmpeg_DEPS_avcodec avutil)
46set(_FFmpeg_DEPS_avdevice avcodec avformat avutil)
47set(_FFmpeg_DEPS_avfilter avutil)
48set(_FFmpeg_DEPS_avformat avcodec avutil)
49set(_FFmpeg_DEPS_postproc avutil)
50set(_FFmpeg_DEPS_swresample avutil)
51set(_FFmpeg_DEPS_swscale avutil)
52
53function(find_ffmpeg LIBNAME)
54 if(DEFINED ENV{FFMPEG_DIR})
55 set(FFMPEG_DIR $ENV{FFMPEG_DIR})
56 endif()
57
58 if(FFMPEG_DIR)
59 list(APPEND INCLUDE_PATHS
60 ${FFMPEG_DIR}
61 ${FFMPEG_DIR}/ffmpeg
62 ${FFMPEG_DIR}/lib${LIBNAME}
63 ${FFMPEG_DIR}/include/lib${LIBNAME}
64 ${FFMPEG_DIR}/include/ffmpeg
65 ${FFMPEG_DIR}/include
66 NO_DEFAULT_PATH
67 NO_CMAKE_FIND_ROOT_PATH
68 )
69 list(APPEND LIB_PATHS
70 ${FFMPEG_DIR}
71 ${FFMPEG_DIR}/lib
72 ${FFMPEG_DIR}/lib${LIBNAME}
73 NO_DEFAULT_PATH
74 NO_CMAKE_FIND_ROOT_PATH
75 )
76 else()
77 list(APPEND INCLUDE_PATHS
78 /usr/local/include/ffmpeg
79 /usr/local/include/lib${LIBNAME}
80 /usr/include/ffmpeg
81 /usr/include/lib${LIBNAME}
82 /usr/include/ffmpeg/lib${LIBNAME}
83 )
84
85 list(APPEND LIB_PATHS
86 /usr/local/lib
87 /usr/lib
88 )
89 endif()
90
91 find_path(FFmpeg_INCLUDE_${LIBNAME} lib${LIBNAME}/${LIBNAME}.h
92 HINTS ${INCLUDE_PATHS}
93 )
94
95 find_library(FFmpeg_LIBRARY_${LIBNAME} ${LIBNAME}
96 HINTS ${LIB_PATHS}
97 )
98
99 if(NOT FFMPEG_DIR AND (NOT FFmpeg_LIBRARY_${LIBNAME} OR NOT FFmpeg_INCLUDE_${LIBNAME}))
100 # Didn't find it in the usual paths, try pkg-config
101 find_package(PkgConfig QUIET)
102 pkg_check_modules(FFmpeg_PKGCONFIG_${LIBNAME} QUIET lib${LIBNAME})
103
104 find_path(FFmpeg_INCLUDE_${LIBNAME} lib${LIBNAME}/${LIBNAME}.h
105 ${FFmpeg_PKGCONFIG_${LIBNAME}_INCLUDE_DIRS}
106 )
107
108 find_library(FFmpeg_LIBRARY_${LIBNAME} ${LIBNAME}
109 ${FFmpeg_PKGCONFIG_${LIBNAME}_LIBRARY_DIRS}
110 )
111 endif()
112
113 if(FFmpeg_INCLUDE_${LIBNAME} AND FFmpeg_LIBRARY_${LIBNAME})
114 set(FFmpeg_INCLUDE_${LIBNAME} "${FFmpeg_INCLUDE_${LIBNAME}}" PARENT_SCOPE)
115 set(FFmpeg_LIBRARY_${LIBNAME} "${FFmpeg_LIBRARY_${LIBNAME}}" PARENT_SCOPE)
116
117 # Extract FFmpeg version from version.h
118 foreach(v MAJOR MINOR MICRO)
119 set(FFmpeg_${LIBNAME}_VERSION_${v} 0)
120 endforeach()
121 string(TOUPPER ${LIBNAME} LIBNAME_UPPER)
122 file(STRINGS "${FFmpeg_INCLUDE_${LIBNAME}}/lib${LIBNAME}/version.h" _FFmpeg_VERSION_H_CONTENTS REGEX "#define LIB${LIBNAME_UPPER}_VERSION_(MAJOR|MINOR|MICRO) ")
123 set(_FFmpeg_VERSION_REGEX "([0-9]+)")
124 foreach(v MAJOR MINOR MICRO)
125 if("${_FFmpeg_VERSION_H_CONTENTS}" MATCHES "#define LIB${LIBNAME_UPPER}_VERSION_${v}[\\t ]+${_FFmpeg_VERSION_REGEX}")
126 set(FFmpeg_${LIBNAME}_VERSION_${v} "${CMAKE_MATCH_1}")
127 endif()
128 endforeach()
129 set(FFmpeg_${LIBNAME}_VERSION "${FFmpeg_${LIBNAME}_VERSION_MAJOR}.${FFmpeg_${LIBNAME}_VERSION_MINOR}.${FFmpeg_${LIBNAME}_VERSION_MICRO}")
130 set(FFmpeg_${c}_VERSION "${FFmpeg_${LIBNAME}_VERSION}" PARENT_SCOPE)
131 unset(_FFmpeg_VERSION_REGEX)
132 unset(_FFmpeg_VERSION_H_CONTENTS)
133
134 set(FFmpeg_${c}_FOUND TRUE PARENT_SCOPE)
135 if(NOT FFmpeg_FIND_QUIETLY)
136 message("-- Found ${LIBNAME}: ${FFmpeg_INCLUDE_${LIBNAME}} ${FFmpeg_LIBRARY_${LIBNAME}} (version: ${FFmpeg_${LIBNAME}_VERSION})")
137 endif()
138 endif()
139endfunction()
140
141foreach(c ${_FFmpeg_ALL_COMPONENTS})
142 find_ffmpeg(${c})
143endforeach()
144
145foreach(c ${_FFmpeg_ALL_COMPONENTS})
146 if(FFmpeg_${c}_FOUND)
147 list(APPEND FFmpeg_INCLUDE_DIR ${FFmpeg_INCLUDE_${c}})
148 list(APPEND FFmpeg_LIBRARIES ${FFmpeg_LIBRARY_${c}})
149
150 add_library(FFmpeg::${c} IMPORTED UNKNOWN)
151 set_target_properties(FFmpeg::${c} PROPERTIES
152 IMPORTED_LOCATION ${FFmpeg_LIBRARY_${c}}
153 INTERFACE_INCLUDE_DIRECTORIES ${FFmpeg_INCLUDE_${c}}
154 )
155 if(_FFmpeg_DEPS_${c})
156 set(deps)
157 foreach(dep ${_FFmpeg_DEPS_${c}})
158 list(APPEND deps FFmpeg::${dep})
159 endforeach()
160
161 set_target_properties(FFmpeg::${c} PROPERTIES
162 INTERFACE_LINK_LIBRARIES "${deps}"
163 )
164 unset(deps)
165 endif()
166 endif()
167endforeach()
168
169if(FFmpeg_INCLUDE_DIR)
170 list(REMOVE_DUPLICATES FFmpeg_INCLUDE_DIR)
171endif()
172
173foreach(c ${FFmpeg_FIND_COMPONENTS})
174 list(APPEND _FFmpeg_REQUIRED_VARS FFmpeg_INCLUDE_${c} FFmpeg_LIBRARY_${c})
175endforeach()
176
177include(FindPackageHandleStandardArgs)
178find_package_handle_standard_args(FFmpeg
179 REQUIRED_VARS ${_FFmpeg_REQUIRED_VARS}
180 HANDLE_COMPONENTS
181)
182
183foreach(c ${_FFmpeg_ALL_COMPONENTS})
184 unset(_FFmpeg_DEPS_${c})
185endforeach()
186unset(_FFmpeg_ALL_COMPONENTS)
187unset(_FFmpeg_REQUIRED_VARS)
188
189include(FindPackageHandleStandardArgs)
190find_package_handle_standard_args(FFmpeg
191 REQUIRED_VARS
192 FFmpeg_LIBRARIES
193 FFmpeg_INCLUDE_DIR
194 HANDLE_COMPONENTS
195)