Debian Codegraph @

CMake/FindFFmpeg.cmake

raw
1 # FindFFmpeg
2 # ----------
3 #
4 # Find the native FFmpeg includes and libraries
5 #
6 # This module defines the following variables:
7 #
8 # FFmpeg_INCLUDE_<component>: where to find <component>.h
9 # FFmpeg_LIBRARY_<component>: where to find the <component> library
10 # FFmpeg_INCLUDES: aggregate all the include paths
11 # FFmpeg_LIBRARIES: aggregate all the paths to the libraries
12 # FFmpeg_FOUND: True if all components have been found
13 #
14 # This module defines the following targets, which are preferred over variables:
15 #
16 # FFmpeg::<component>: Target to use <component> directly, with include path,
17 # library and dependencies set up. If you are using a static build, you are
18 # responsible for adding any external dependencies (such as zlib, bzlib...).
19 #
20 # <component> can be one of:
21 # avcodec
22 # avdevice
23 # avfilter
24 # avformat
25 # postproc
26 # swresample
27 # swscale
28 #
29
30 set(_FFmpeg_ALL_COMPONENTS
31 avcodec
32 avdevice
33 avfilter
34 avformat
35 avutil
36 postproc
37 swresample
38 swscale
39 )
40
41 set(_FFmpeg_DEPS_avcodec avutil)
42 set(_FFmpeg_DEPS_avdevice avcodec avformat avutil)
43 set(_FFmpeg_DEPS_avfilter avutil)
44 set(_FFmpeg_DEPS_avformat avcodec avutil)
45 set(_FFmpeg_DEPS_postproc avutil)
46 set(_FFmpeg_DEPS_swresample avutil)
47 set(_FFmpeg_DEPS_swscale avutil)
48
49 function(find_ffmpeg LIBNAME)
50 if(DEFINED ENV{FFMPEG_DIR})
51 set(FFMPEG_DIR $ENV{FFMPEG_DIR})
52 endif()
53
54 if(FFMPEG_DIR)
55 list(APPEND INCLUDE_PATHS
56 ${FFMPEG_DIR}
57 ${FFMPEG_DIR}/ffmpeg
58 ${FFMPEG_DIR}/lib${LIBNAME}
59 ${FFMPEG_DIR}/include/lib${LIBNAME}
60 ${FFMPEG_DIR}/include/ffmpeg
61 ${FFMPEG_DIR}/include
62 NO_DEFAULT_PATH
63 NO_CMAKE_FIND_ROOT_PATH
64 )
65 list(APPEND LIB_PATHS
66 ${FFMPEG_DIR}
67 ${FFMPEG_DIR}/lib
68 ${FFMPEG_DIR}/lib${LIBNAME}
69 NO_DEFAULT_PATH
70 NO_CMAKE_FIND_ROOT_PATH
71 )
72 else()
73 list(APPEND INCLUDE_PATHS
74 /usr/local/include/ffmpeg
75 /usr/local/include/lib${LIBNAME}
76 /usr/include/ffmpeg
77 /usr/include/lib${LIBNAME}
78 /usr/include/ffmpeg/lib${LIBNAME}
79 )
80
81 list(APPEND LIB_PATHS
82 /usr/local/lib
83 /usr/lib
84 )
85 endif()
86
87 find_path(FFmpeg_INCLUDE_${LIBNAME} lib${LIBNAME}/${LIBNAME}.h
88 HINTS ${INCLUDE_PATHS}
89 )
90
91 find_library(FFmpeg_LIBRARY_${LIBNAME} ${LIBNAME}
92 HINTS ${LIB_PATHS}
93 )
94
95 if(NOT FFMPEG_DIR AND (NOT FFmpeg_LIBRARY_${LIBNAME} OR NOT FFmpeg_INCLUDE_${LIBNAME}))
96 # Didn't find it in the usual paths, try pkg-config
97 find_package(PkgConfig QUIET)
98 pkg_check_modules(FFmpeg_PKGCONFIG_${LIBNAME} QUIET lib${LIBNAME})
99
100 find_path(FFmpeg_INCLUDE_${LIBNAME} lib${LIBNAME}/${LIBNAME}.h
101 ${FFmpeg_PKGCONFIG_${LIBNAME}_INCLUDE_DIRS}
102 )
103
104 find_library(FFmpeg_LIBRARY_${LIBNAME} ${LIBNAME}
105 ${FFmpeg_PKGCONFIG_${LIBNAME}_LIBRARY_DIRS}
106 )
107 endif()
108
109 if(FFmpeg_INCLUDE_${LIBNAME} AND FFmpeg_LIBRARY_${LIBNAME})
110 set(FFmpeg_INCLUDE_${LIBNAME} "${FFmpeg_INCLUDE_${LIBNAME}}" PARENT_SCOPE)
111 set(FFmpeg_LIBRARY_${LIBNAME} "${FFmpeg_LIBRARY_${LIBNAME}}" PARENT_SCOPE)
112 set(FFmpeg_${c}_FOUND TRUE PARENT_SCOPE)
113 if(NOT FFmpeg_FIND_QUIETLY)
114 message("-- Found ${LIBNAME}: ${FFmpeg_INCLUDE_${LIBNAME}} ${FFmpeg_LIBRARY_${LIBNAME}}")
115 endif()
116 endif()
117 endfunction()
118
119 foreach(c ${_FFmpeg_ALL_COMPONENTS})
120 find_ffmpeg(${c})
121 endforeach()
122
123 foreach(c ${_FFmpeg_ALL_COMPONENTS})
124 if(FFmpeg_${c}_FOUND)
125 list(APPEND FFmpeg_INCLUDES ${FFmpeg_INCLUDE_${c}})
126 list(APPEND FFmpeg_LIBRARIES ${FFmpeg_LIBRARY_${c}})
127
128 add_library(FFmpeg::${c} IMPORTED UNKNOWN)
129 set_target_properties(FFmpeg::${c} PROPERTIES
130 IMPORTED_LOCATION ${FFmpeg_LIBRARY_${c}}
131 INTERFACE_INCLUDE_DIRECTORIES ${FFmpeg_INCLUDE_${c}}
132 )
133 if(_FFmpeg_DEPS_${c})
134 set(deps)
135 foreach(dep ${_FFmpeg_DEPS_${c}})
136 list(APPEND deps FFmpeg::${dep})
137 endforeach()
138
139 set_target_properties(FFmpeg::${c} PROPERTIES
140 INTERFACE_LINK_LIBRARIES "${deps}"
141 )
142 unset(deps)
143 endif()
144 endif()
145 endforeach()
146
147 if(FFmpeg_INCLUDES)
148 list(REMOVE_DUPLICATES FFmpeg_INCLUDES)
149 endif()
150
151 foreach(c ${FFmpeg_FIND_COMPONENTS})
152 list(APPEND _FFmpeg_REQUIRED_VARS FFmpeg_INCLUDE_${c} FFmpeg_LIBRARY_${c})
153 endforeach()
154
155 include(FindPackageHandleStandardArgs)
156 find_package_handle_standard_args(FFmpeg
157 REQUIRED_VARS ${_FFmpeg_REQUIRED_VARS}
158 HANDLE_COMPONENTS
159 )
160
161 foreach(c ${_FFmpeg_ALL_COMPONENTS})
162 unset(_FFmpeg_DEPS_${c})
163 endforeach()
164 unset(_FFmpeg_ALL_COMPONENTS)
165 unset(_FFmpeg_REQUIRED_VARS)