aboutsummaryrefslogtreecommitdiffhomepage
path: root/CMakeModules/FindFFmpeg.cmake
blob: 5cb1f3c8a47d6c0bddb002c78f61e646a3cfb8bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# SPDX-FileCopyrightText: 2019 Citra Emulator Project
# SPDX-License-Identifier: GPL-2.0-or-later

# FindFFmpeg
# ----------
#
# Find the native FFmpeg includes and libraries
#
# This module defines the following variables:
#
#  FFmpeg_INCLUDE_<component>: where to find <component>.h
#  FFmpeg_LIBRARY_<component>: where to find the <component> library
#  FFmpeg_INCLUDE_DIR: aggregate all the include paths
#  FFmpeg_LIBRARIES: aggregate all the paths to the libraries
#  FFmpeg_FOUND: True if all components have been found
#
# This module defines the following targets, which are preferred over variables:
#
#  FFmpeg::<component>: Target to use <component> directly, with include path,
#    library and dependencies set up. If you are using a static build, you are
#    responsible for adding any external dependencies (such as zlib, bzlib...).
#
# <component> can be one of:
#   avcodec
#   avdevice
#   avfilter
#   avformat
#   avutil
#   postproc
#   swresample
#   swscale
#

set(_FFmpeg_ALL_COMPONENTS
    avcodec
    avdevice
    avfilter
    avformat
    avutil
    postproc
    swresample
    swscale
)

set(_FFmpeg_DEPS_avcodec avutil)
set(_FFmpeg_DEPS_avdevice avcodec avformat avutil)
set(_FFmpeg_DEPS_avfilter avutil)
set(_FFmpeg_DEPS_avformat avcodec avutil)
set(_FFmpeg_DEPS_postproc avutil)
set(_FFmpeg_DEPS_swresample avutil)
set(_FFmpeg_DEPS_swscale avutil)

function(find_ffmpeg LIBNAME)
  if(DEFINED ENV{FFMPEG_DIR})
    set(FFMPEG_DIR $ENV{FFMPEG_DIR})
  endif()

  if(FFMPEG_DIR)
    list(APPEND INCLUDE_PATHS
      ${FFMPEG_DIR}
      ${FFMPEG_DIR}/ffmpeg
      ${FFMPEG_DIR}/lib${LIBNAME}
      ${FFMPEG_DIR}/include/lib${LIBNAME}
      ${FFMPEG_DIR}/include/ffmpeg
      ${FFMPEG_DIR}/include
      NO_DEFAULT_PATH
      NO_CMAKE_FIND_ROOT_PATH
    )
    list(APPEND LIB_PATHS
      ${FFMPEG_DIR}
      ${FFMPEG_DIR}/lib
      ${FFMPEG_DIR}/lib${LIBNAME}
      NO_DEFAULT_PATH
      NO_CMAKE_FIND_ROOT_PATH
    )
  else()
    list(APPEND INCLUDE_PATHS
      /usr/local/include/ffmpeg
      /usr/local/include/lib${LIBNAME}
      /usr/include/ffmpeg
      /usr/include/lib${LIBNAME}
      /usr/include/ffmpeg/lib${LIBNAME}
    )

    list(APPEND LIB_PATHS
      /usr/local/lib
      /usr/lib
    )
  endif()

  find_path(FFmpeg_INCLUDE_${LIBNAME} lib${LIBNAME}/${LIBNAME}.h
    HINTS ${INCLUDE_PATHS}
  )

  find_library(FFmpeg_LIBRARY_${LIBNAME} ${LIBNAME}
    HINTS ${LIB_PATHS}
  )

  if(NOT FFMPEG_DIR AND (NOT FFmpeg_LIBRARY_${LIBNAME} OR NOT FFmpeg_INCLUDE_${LIBNAME}))
    # Didn't find it in the usual paths, try pkg-config
    find_package(PkgConfig QUIET)
    pkg_check_modules(FFmpeg_PKGCONFIG_${LIBNAME} QUIET lib${LIBNAME})

    find_path(FFmpeg_INCLUDE_${LIBNAME} lib${LIBNAME}/${LIBNAME}.h
      ${FFmpeg_PKGCONFIG_${LIBNAME}_INCLUDE_DIRS}
    )

    find_library(FFmpeg_LIBRARY_${LIBNAME} ${LIBNAME}
      ${FFmpeg_PKGCONFIG_${LIBNAME}_LIBRARY_DIRS}
    )
  endif()

  if(FFmpeg_INCLUDE_${LIBNAME} AND FFmpeg_LIBRARY_${LIBNAME})
    set(FFmpeg_INCLUDE_${LIBNAME} "${FFmpeg_INCLUDE_${LIBNAME}}" PARENT_SCOPE)
    set(FFmpeg_LIBRARY_${LIBNAME} "${FFmpeg_LIBRARY_${LIBNAME}}" PARENT_SCOPE)

    # Extract FFmpeg version from version.h
    foreach(v MAJOR MINOR MICRO)
      set(FFmpeg_${LIBNAME}_VERSION_${v} 0)
    endforeach()
    string(TOUPPER ${LIBNAME} LIBNAME_UPPER)
    file(STRINGS "${FFmpeg_INCLUDE_${LIBNAME}}/lib${LIBNAME}/version.h" _FFmpeg_VERSION_H_CONTENTS REGEX "#define LIB${LIBNAME_UPPER}_VERSION_(MAJOR|MINOR|MICRO) ")
    set(_FFmpeg_VERSION_REGEX "([0-9]+)")
    foreach(v MAJOR MINOR MICRO)
      if("${_FFmpeg_VERSION_H_CONTENTS}" MATCHES "#define LIB${LIBNAME_UPPER}_VERSION_${v}[\\t ]+${_FFmpeg_VERSION_REGEX}")
        set(FFmpeg_${LIBNAME}_VERSION_${v} "${CMAKE_MATCH_1}")
      endif()
    endforeach()
    set(FFmpeg_${LIBNAME}_VERSION "${FFmpeg_${LIBNAME}_VERSION_MAJOR}.${FFmpeg_${LIBNAME}_VERSION_MINOR}.${FFmpeg_${LIBNAME}_VERSION_MICRO}")
    set(FFmpeg_${c}_VERSION "${FFmpeg_${LIBNAME}_VERSION}" PARENT_SCOPE)
    unset(_FFmpeg_VERSION_REGEX)
    unset(_FFmpeg_VERSION_H_CONTENTS)

    set(FFmpeg_${c}_FOUND TRUE PARENT_SCOPE)
    if(NOT FFmpeg_FIND_QUIETLY)
      message("--  Found ${LIBNAME}: ${FFmpeg_INCLUDE_${LIBNAME}} ${FFmpeg_LIBRARY_${LIBNAME}} (version: ${FFmpeg_${LIBNAME}_VERSION})")
    endif()
  endif()
endfunction()

foreach(c ${_FFmpeg_ALL_COMPONENTS})
  find_ffmpeg(${c})
endforeach()

foreach(c ${_FFmpeg_ALL_COMPONENTS})
  if(FFmpeg_${c}_FOUND)
    list(APPEND FFmpeg_INCLUDE_DIR ${FFmpeg_INCLUDE_${c}})
    list(APPEND FFmpeg_LIBRARIES ${FFmpeg_LIBRARY_${c}})

    add_library(FFmpeg::${c} IMPORTED UNKNOWN)
    set_target_properties(FFmpeg::${c} PROPERTIES
      IMPORTED_LOCATION ${FFmpeg_LIBRARY_${c}}
      INTERFACE_INCLUDE_DIRECTORIES ${FFmpeg_INCLUDE_${c}}
    )
    if(_FFmpeg_DEPS_${c})
      set(deps)
      foreach(dep ${_FFmpeg_DEPS_${c}})
        list(APPEND deps FFmpeg::${dep})
      endforeach()

      set_target_properties(FFmpeg::${c} PROPERTIES
        INTERFACE_LINK_LIBRARIES "${deps}"
      )
      unset(deps)
    endif()
  endif()
endforeach()

if(FFmpeg_INCLUDE_DIR)
  list(REMOVE_DUPLICATES FFmpeg_INCLUDE_DIR)
endif()

foreach(c ${FFmpeg_FIND_COMPONENTS})
  list(APPEND _FFmpeg_REQUIRED_VARS FFmpeg_INCLUDE_${c} FFmpeg_LIBRARY_${c})
endforeach()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(FFmpeg
  REQUIRED_VARS ${_FFmpeg_REQUIRED_VARS}
  HANDLE_COMPONENTS
)

foreach(c ${_FFmpeg_ALL_COMPONENTS})
  unset(_FFmpeg_DEPS_${c})
endforeach()
unset(_FFmpeg_ALL_COMPONENTS)
unset(_FFmpeg_REQUIRED_VARS)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(FFmpeg
  REQUIRED_VARS
    FFmpeg_LIBRARIES
    FFmpeg_INCLUDE_DIR
  HANDLE_COMPONENTS
)