FindFFMPEG.cmake (7223B)
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 set(major_version_header_path "${FFMPEG_${component}_INCLUDE_DIR}/lib${component}/version_major.h") 101 if (EXISTS "${major_version_header_path}") 102 string(TOUPPER "${component}" component_upper) 103 file(STRINGS "${major_version_header_path}" major_version 104 REGEX "#define *LIB${component_upper}_VERSION_MAJOR ") 105 file(STRINGS "${version_header_path}" version 106 REGEX "#define *LIB${component_upper}_VERSION_(MINOR|MICRO) ") 107 string(REGEX REPLACE ".*_MAJOR *\([0-9]*\).*" "\\1" major "${major_version}") 108 string(REGEX REPLACE ".*_MINOR *\([0-9]*\).*" "\\1" minor "${version}") 109 string(REGEX REPLACE ".*_MICRO *\([0-9]*\).*" "\\1" micro "${version}") 110 if (NOT major STREQUAL "" AND 111 NOT minor STREQUAL "" AND 112 NOT micro STREQUAL "") 113 set("FFMPEG_${component}_VERSION" "${major}.${minor}.${micro}" 114 PARENT_SCOPE) 115 endif () 116 elseif (EXISTS "${version_header_path}") 117 string(TOUPPER "${component}" component_upper) 118 file(STRINGS "${version_header_path}" version 119 REGEX "#define *LIB${component_upper}_VERSION_(MAJOR|MINOR|MICRO) ") 120 string(REGEX REPLACE ".*_MAJOR *\([0-9]*\).*" "\\1" major "${version}") 121 string(REGEX REPLACE ".*_MINOR *\([0-9]*\).*" "\\1" minor "${version}") 122 string(REGEX REPLACE ".*_MICRO *\([0-9]*\).*" "\\1" micro "${version}") 123 if (NOT major STREQUAL "" AND 124 NOT minor STREQUAL "" AND 125 NOT micro STREQUAL "") 126 set("FFMPEG_${component}_VERSION" "${major}.${minor}.${micro}" 127 PARENT_SCOPE) 128 endif () 129 endif () 130 else () 131 set("FFMPEG_${component}_FOUND" 0 132 PARENT_SCOPE) 133 set(what) 134 if (NOT FFMPEG_${component}_LIBRARY) 135 set(what "library") 136 endif () 137 if (NOT FFMPEG_${component}_INCLUDE_DIR) 138 if (what) 139 string(APPEND what " or headers") 140 else () 141 set(what "headers") 142 endif () 143 endif () 144 set("FFMPEG_${component}_NOT_FOUND_MESSAGE" 145 "Could not find the ${what} for ${component}." 146 PARENT_SCOPE) 147 endif () 148 endif () 149 endfunction () 150 151 _ffmpeg_find(avutil avutil.h) 152 _ffmpeg_find(avresample avresample.h 153 avutil) 154 _ffmpeg_find(swresample swresample.h 155 avutil) 156 _ffmpeg_find(swscale swscale.h 157 avutil) 158 _ffmpeg_find(avcodec avcodec.h 159 avutil) 160 _ffmpeg_find(avformat avformat.h 161 avcodec avutil) 162 _ffmpeg_find(avfilter avfilter.h 163 avutil) 164 _ffmpeg_find(avdevice avdevice.h 165 avformat avutil) 166 167 if (TARGET FFMPEG::avutil) 168 set(_ffmpeg_version_header_path "${FFMPEG_avutil_INCLUDE_DIR}/libavutil/ffversion.h") 169 if (EXISTS "${_ffmpeg_version_header_path}") 170 file(STRINGS "${_ffmpeg_version_header_path}" _ffmpeg_version 171 REGEX "FFMPEG_VERSION") 172 string(REGEX REPLACE ".*\"n?\(.*\)\"" "\\1" FFMPEG_VERSION "${_ffmpeg_version}") 173 unset(_ffmpeg_version) 174 else () 175 set(FFMPEG_VERSION FFMPEG_VERSION-NOTFOUND) 176 endif () 177 unset(_ffmpeg_version_header_path) 178 endif () 179 180 set(FFMPEG_INCLUDE_DIRS) 181 set(FFMPEG_LIBRARIES) 182 set(_ffmpeg_required_vars) 183 foreach (_ffmpeg_component IN LISTS FFMPEG_FIND_COMPONENTS) 184 if (TARGET "FFMPEG::${_ffmpeg_component}") 185 set(FFMPEG_${_ffmpeg_component}_INCLUDE_DIRS 186 "${FFMPEG_${_ffmpeg_component}_INCLUDE_DIR}") 187 set(FFMPEG_${_ffmpeg_component}_LIBRARIES 188 "${FFMPEG_${_ffmpeg_component}_LIBRARY}") 189 list(APPEND FFMPEG_INCLUDE_DIRS 190 "${FFMPEG_${_ffmpeg_component}_INCLUDE_DIRS}") 191 list(APPEND FFMPEG_LIBRARIES 192 "${FFMPEG_${_ffmpeg_component}_LIBRARIES}") 193 if (FFMEG_FIND_REQUIRED_${_ffmpeg_component}) 194 list(APPEND _ffmpeg_required_vars 195 "FFMPEG_${_ffmpeg_required_vars}_INCLUDE_DIRS" 196 "FFMPEG_${_ffmpeg_required_vars}_LIBRARIES") 197 endif () 198 endif () 199 endforeach () 200 unset(_ffmpeg_component) 201 202 if (FFMPEG_INCLUDE_DIRS) 203 list(REMOVE_DUPLICATES FFMPEG_INCLUDE_DIRS) 204 endif () 205 206 include(FindPackageHandleStandardArgs) 207 find_package_handle_standard_args(FFMPEG 208 REQUIRED_VARS FFMPEG_INCLUDE_DIRS FFMPEG_LIBRARIES ${_ffmpeg_required_vars} 209 VERSION_VAR FFMPEG_VERSION 210 HANDLE_COMPONENTS) 211 unset(_ffmpeg_required_vars) 212