File: FindFFmpeg.cmake

package info (click to toggle)
obs-studio 30.2.3%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 47,852 kB
  • sloc: ansic: 202,137; cpp: 112,402; makefile: 868; python: 599; sh: 275; javascript: 19
file content (189 lines) | stat: -rw-r--r-- 6,582 bytes parent folder | download
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
# cmake-format: off
#
# This module defines the following variables:
#
# FFMPEG_FOUND - All required components and the core library were found
# FFMPEG_INCLUDE_DIRS - Combined list of all components include dirs
# FFMPEG_LIBRARIES - Combined list of all components libraries
# FFMPEG_VERSION_STRING - Version of the first component requested
#
# For each requested component the following variables are defined:
#
# FFMPEG_<component>_FOUND - The component was found
# FFMPEG_<component>_INCLUDE_DIRS - The components include dirs
# FFMPEG_<component>_LIBRARIES - The components libraries
# FFMPEG_<component>_VERSION_STRING - The components version string
# FFMPEG_<component>_VERSION_MAJOR - The components major version
# FFMPEG_<component>_VERSION_MINOR - The components minor version
# FFMPEG_<component>_VERSION_MICRO - The components micro version
#
# <component> is the uppercase name of the component
# cmake-format: on

find_package(PkgConfig QUIET)

if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  set(_lib_suffix 64)
else()
  set(_lib_suffix 32)
endif()

function(find_ffmpeg_library component header)
  string(TOUPPER "${component}" component_u)
  set(FFMPEG_${component_u}_FOUND
      FALSE
      PARENT_SCOPE)
  set(FFmpeg_${component}_FOUND
      FALSE
      PARENT_SCOPE)

  if(PKG_CONFIG_FOUND)
    pkg_check_modules(PC_FFMPEG_${component} QUIET lib${component})
  endif()

  find_path(
    FFMPEG_${component}_INCLUDE_DIR
    NAMES "lib${component}/${header}" "lib${component}/version.h"
    HINTS ENV FFMPEG_PATH ${FFMPEG_PATH} ${CMAKE_SOURCE_DIR}/${FFMPEG_PATH} ${PC_FFMPEG_${component}_INCLUDE_DIRS}
    PATHS /usr/include /usr/local/include /opt/local/include /sw/include
    PATH_SUFFIXES ffmpeg libav include)

  find_library(
    FFMPEG_${component}_LIBRARY
    NAMES "${component}" "lib${component}"
    HINTS ENV FFMPEG_PATH ${FFMPEG_PATH} ${CMAKE_SOURCE_DIR}/${FFMPEG_PATH} ${PC_FFMPEG_${component}_LIBRARY_DIRS}
    PATHS /usr/lib /usr/local/lib /opt/local/lib /sw/lib
    PATH_SUFFIXES
      lib${_lib_suffix}
      lib
      libs${_lib_suffix}
      libs
      bin${_lib_suffix}
      bin
      ../lib${_lib_suffix}
      ../lib
      ../libs${_lib_suffix}
      ../libs
      ../bin${_lib_suffix}
      ../bin)

  set(FFMPEG_${component_u}_INCLUDE_DIRS
      ${FFMPEG_${component}_INCLUDE_DIR}
      PARENT_SCOPE)
  set(FFMPEG_${component_u}_LIBRARIES
      ${FFMPEG_${component}_LIBRARY}
      PARENT_SCOPE)

  mark_as_advanced(FFMPEG_${component}_INCLUDE_DIR FFMPEG_${component}_LIBRARY)

  if(FFMPEG_${component}_INCLUDE_DIR AND FFMPEG_${component}_LIBRARY)
    set(FFMPEG_${component_u}_FOUND
        TRUE
        PARENT_SCOPE)
    set(FFmpeg_${component}_FOUND
        TRUE
        PARENT_SCOPE)

    list(APPEND FFMPEG_INCLUDE_DIRS ${FFMPEG_${component}_INCLUDE_DIR})
    list(REMOVE_DUPLICATES FFMPEG_INCLUDE_DIRS)
    set(FFMPEG_INCLUDE_DIRS
        "${FFMPEG_INCLUDE_DIRS}"
        PARENT_SCOPE)

    list(APPEND FFMPEG_LIBRARIES ${FFMPEG_${component}_LIBRARY})
    list(REMOVE_DUPLICATES FFMPEG_LIBRARIES)
    set(FFMPEG_LIBRARIES
        "${FFMPEG_LIBRARIES}"
        PARENT_SCOPE)

    set(FFMPEG_${component_u}_VERSION_STRING
        "unknown"
        PARENT_SCOPE)
    set(_vfile "${FFMPEG_${component}_INCLUDE_DIR}/lib${component}/version.h")

    if(EXISTS "${_vfile}")
      file(STRINGS "${_vfile}" _version_parse REGEX "^.*VERSION_(MAJOR|MINOR|MICRO)[ \t]+[0-9]+[ \t]*$")
      string(REGEX REPLACE ".*VERSION_MAJOR[ \t]+([0-9]+).*" "\\1" _major "${_version_parse}")
      string(REGEX REPLACE ".*VERSION_MINOR[ \t]+([0-9]+).*" "\\1" _minor "${_version_parse}")
      string(REGEX REPLACE ".*VERSION_MICRO[ \t]+([0-9]+).*" "\\1" _micro "${_version_parse}")

      set(FFMPEG_${component_u}_VERSION_MAJOR
          "${_major}"
          PARENT_SCOPE)
      set(FFMPEG_${component_u}_VERSION_MINOR
          "${_minor}"
          PARENT_SCOPE)
      set(FFMPEG_${component_u}_VERSION_MICRO
          "${_micro}"
          PARENT_SCOPE)

      set(FFMPEG_${component_u}_VERSION_STRING
          "${_major}.${_minor}.${_micro}"
          PARENT_SCOPE)
    else()
      message(STATUS "Failed parsing FFmpeg ${component} version")
    endif()
  endif()
endfunction()

set(FFMPEG_INCLUDE_DIRS)
set(FFMPEG_LIBRARIES)

if(NOT FFmpeg_FIND_COMPONENTS)
  message(FATAL_ERROR "No FFmpeg components requested")
endif()

list(GET FFmpeg_FIND_COMPONENTS 0 _first_comp)
string(TOUPPER "${_first_comp}" _first_comp)

foreach(component ${FFmpeg_FIND_COMPONENTS})
  if(component STREQUAL "avcodec")
    find_ffmpeg_library("${component}" "avcodec.h")
  elseif(component STREQUAL "avdevice")
    find_ffmpeg_library("${component}" "avdevice.h")
  elseif(component STREQUAL "avfilter")
    find_ffmpeg_library("${component}" "avfilter.h")
  elseif(component STREQUAL "avformat")
    find_ffmpeg_library("${component}" "avformat.h")
  elseif(component STREQUAL "avresample")
    find_ffmpeg_library("${component}" "avresample.h")
  elseif(component STREQUAL "avutil")
    find_ffmpeg_library("${component}" "avutil.h")
  elseif(component STREQUAL "postproc")
    find_ffmpeg_library("${component}" "postprocess.h")
  elseif(component STREQUAL "swresample")
    find_ffmpeg_library("${component}" "swresample.h")
  elseif(component STREQUAL "swscale")
    find_ffmpeg_library("${component}" "swscale.h")
  else()
    message(FATAL_ERROR "Unknown FFmpeg component requested: ${component}")
  endif()
endforeach()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
  FFmpeg
  FOUND_VAR FFMPEG_FOUND
  REQUIRED_VARS FFMPEG_${_first_comp}_LIBRARIES FFMPEG_${_first_comp}_INCLUDE_DIRS
  VERSION_VAR FFMPEG_${_first_comp}_VERSION_STRING
  HANDLE_COMPONENTS)

if(FFMPEG_FOUND)
  foreach(component ${FFmpeg_FIND_COMPONENTS})
    if(NOT TARGET FFmpeg::${component})
      string(TOUPPER ${component} component_u)
      if(FFMPEG_${component_u}_FOUND)
        if(IS_ABSOLUTE "${FFMPEG_${component_u}_LIBRARIES}")
          add_library(FFmpeg::${component} UNKNOWN IMPORTED)
          set_target_properties(FFmpeg::${component} PROPERTIES IMPORTED_LOCATION "${FFMPEG_${component_u}_LIBRARIES}")
        else()
          add_library(FFmpeg::${component} INTERFACE IMPORTED)
          set_target_properties(FFmpeg::${component} PROPERTIES IMPORTED_LIBNAME "${FFMPEG_${component_u}_LIBRARIES}")
        endif()

        set_target_properties(FFmpeg::${component} PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
                                                              "${FFMPEG_${component_u}_INCLUDE_DIRS}")
      endif()
    endif()
  endforeach()
endif()