obs-streamFX/cmake/modules/FindFFmpeg.cmake

294 lines
9.3 KiB
CMake

# AUTOGENERATED COPYRIGHT HEADER START
# Copyright (C) 2020-2024 Michael Fabian 'Xaymar' Dirks <info@xaymar.com>
# AUTOGENERATED COPYRIGHT HEADER END
#
# 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 componenets 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_minimum_required(VERSION 3.26.0...3.28.1)
include(FindPackageHandleStandardArgs)
include(CMakeParseArguments)
find_package(PkgConfig QUIET)
function(find_ffmpeg_component)
cmake_parse_arguments(
_ARGS "" "COMPONENT;OUTPUT" "" ${ARGN}
)
string(TOLOWER "${_ARGS_COMPONENT}" lcomponent)
# Do nothing if the target already exists.
if(TARGET FFmpeg::${lcomponent})
set(FFmpeg_${lcomponent}_FOUND TRUE PARENT_SCOPE)
return()
endif()
set(FFmpeg_${lcomponent}_FOUND OFF PARENT_SCOPE)
# pkg-config
if(PKG_CONFIG_FOUND)
pkg_check_modules(PC_FFmpeg_${lcomponent} QUIET lib${lcomponent})
endif()
# Find headers
find_path(FFmpeg_${lcomponent}_INCLUDE_DIR
NAMES
"${lcomponent}.h"
"lib${lcomponent}.h"
HINTS
${PC_FFmpeg_${lcomponent}_INCLUDE_DIRS}
${FFmpeg_DIR}
PATH_SUFFIXES
"include/lib${lcomponent}"
"lib${lcomponent}"
"include/${lcomponent}"
"${lcomponent}"
"include"
DOC "${lcomponent}: Path to include directory"
)
mark_as_advanced(FFmpeg_${lcomponent}_INCLUDE_DIR)
# Find library
math(EXPR LIBSUFFIX "8*${CMAKE_SIZEOF_VOID_P}")
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(FIND_LIBRARY_USE_LIB64_PATHS ON)
set(FIND_LIBRARY_USE_LIB32_PATHS OFF)
set(FIND_LIBRARY_USE_LIBX32_PATHS OFF)
else()
set(FIND_LIBRARY_USE_LIB64_PATHS OFF)
set(FIND_LIBRARY_USE_LIB32_PATHS ON)
set(FIND_LIBRARY_USE_LIBX32_PATHS OFF)
endif()
find_library(FFmpeg_${lcomponent}_LIBRARY
NAMES
"${lcomponent}"
"lib${lcomponent}"
HINTS
${PC_FFmpeg_${lcomponent}_LIBRARY_DIRS}
${FFmpeg_DIR}
PATHS
"/lib${LIBSUFFIX}"
"/lib"
"/usr/lib${LIBSUFFIX}"
"/usr/lib"
"/usr/local/lib${LIBSUFFIX}"
"/usr/local/lib"
PATH_SUFFIXES
"lib${LIBSUFFIX}/lib${lcomponent}"
"lib/lib${lcomponent}"
"lib${lcomponent}"
"lib${LIBSUFFIX}/${lcomponent}"
"lib/${lcomponent}"
"${lcomponent}"
DOC "${lcomponent}: Path to library file"
)
mark_as_advanced(FFmpeg_${lcomponent}_LIBRARY)
if((FFmpeg_${lcomponent}_LIBRARY) AND (FFmpeg_${lcomponent}_INCLUDE_DIR))
# The include path should be the parent directory.
cmake_path(GET FFmpeg_${lcomponent}_INCLUDE_DIR PARENT_PATH _INCLUDE_DIR)
# Detect the library version.
set(_VERSION "")
if((EXISTS "${FFmpeg_${lcomponent}_INCLUDE_DIR}/version_major.h") AND (EXISTS "${FFmpeg_${lcomponent}_INCLUDE_DIR}/version.h"))
# Parse version_major.h:
# - #define LIB${lcomponent}_VERSION_MAJOR 58
# and version.h:
# - #define LIB${lcomponent}_VERSION_MINOR 3
# - #define LIB${lcomponent}_VERSION_MICRO 100
file(STRINGS "${FFmpeg_${lcomponent}_INCLUDE_DIR}/version_major.h" _FILE_VERSION REGEX "^.*_VERSION_(MAJOR|MINOR|MICRO)[ \t]+[1-9]+[0-9]*.*$")
file(STRINGS "${FFmpeg_${lcomponent}_INCLUDE_DIR}/version.h" _FILE_VERSION_MINOR REGEX "^.*_VERSION_(MAJOR|MINOR|MICRO)[ \t]+[1-9]+[0-9]*.*$")
list(APPEND _FILE_VERSION ${_FILE_VERSION_MINOR})
elseif((EXISTS "${FFmpeg_${lcomponent}_INCLUDE_DIR}/version.h"))
# Parse version.h:
# - #define LIB${lcomponent}_VERSION_MAJOR 58
# - #define LIB${lcomponent}_VERSION_MINOR 3
# - #define LIB${lcomponent}_VERSION_MICRO 100
file(STRINGS "${FFmpeg_${lcomponent}_INCLUDE_DIR}/version.h" _FILE_VERSION REGEX "^.*_VERSION_(MAJOR|MINOR|MICRO)[ \t]+[1-9]+[0-9]*.*$")
else()
message(WARNING "${lcomponent}: No version header found, defaulting to 0.0.0.")
set(FFmpeg_${lcomponent}_VERSION_MAJOR "0")
set(FFmpeg_${lcomponent}_VERSION_MINOR "0")
set(FFmpeg_${lcomponent}_VERSION_PATCH "0")
endif()
foreach(_FILE_VERSION_PART IN LISTS _FILE_VERSION)
if(_FILE_VERSION_PART MATCHES "^.*MAJOR[ \t]+([1-9]+[0-9]*).*$")
set(FFmpeg_${lcomponent}_VERSION_MAJOR ${CMAKE_MATCH_1})
elseif(_FILE_VERSION_PART MATCHES "^.*MINOR[ \t]+([1-9]+[0-9]*).*$")
set(FFmpeg_${lcomponent}_VERSION_MINOR ${CMAKE_MATCH_1})
elseif(_FILE_VERSION_PART MATCHES "^.*MICRO[ \t]+([1-9]+[0-9]*).*$")
set(FFmpeg_${lcomponent}_VERSION_PATCH ${CMAKE_MATCH_1})
endif()
endforeach()
set(FFmpeg_${lcomponent}_VERSION "${FFmpeg_${lcomponent}_VERSION_MAJOR}.${FFmpeg_${lcomponent}_VERSION_MINOR}.${FFmpeg_${lcomponent}_VERSION_PATCH}")
set(FFmpeg_${lcomponent}_VERSION "${FFmpeg_${lcomponent}_VERSION}" PARENT_SCOPE)
# If possible (shared/module library), find the binary file for it.
find_file(FFmpeg_${lcomponent}_BINARY
NAMES
"${lcomponent}${CMAKE_SHARED_LIBRARY_SUFFIX}.${FFmpeg_${lcomponent}_VERSION_MAJOR}.${FFmpeg_${lcomponent}_VERSION_MINOR}"
"lib${lcomponent}${CMAKE_SHARED_LIBRARY_SUFFIX}.${FFmpeg_${lcomponent}_VERSION_MAJOR}.${FFmpeg_${lcomponent}_VERSION_MINOR}"
"${lcomponent}${CMAKE_SHARED_LIBRARY_SUFFIX}.${FFmpeg_${lcomponent}_VERSION_MAJOR}"
"lib${lcomponent}${CMAKE_SHARED_LIBRARY_SUFFIX}.${FFmpeg_${lcomponent}_VERSION_MAJOR}"
"${lcomponent}-${FFmpeg_${lcomponent}_VERSION_MAJOR}${CMAKE_SHARED_LIBRARY_SUFFIX}"
"lib${lcomponent}-${FFmpeg_${lcomponent}_VERSION_MAJOR}${CMAKE_SHARED_LIBRARY_SUFFIX}"
"${lcomponent}${CMAKE_SHARED_LIBRARY_SUFFIX}"
"lib${lcomponent}${CMAKE_SHARED_LIBRARY_SUFFIX}"
HINTS
${PC_FFmpeg_${lcomponent}_LIBRARY_DIRS}
${FFmpeg_DIR}
PATHS
"/bin${LIBSUFFIX}"
"/bin"
"/usr/bin${LIBSUFFIX}"
"/usr/bin"
"/usr/local/bin${LIBSUFFIX}"
"/usr/local/bin"
"/lib${LIBSUFFIX}"
"/lib"
"/usr/lib${LIBSUFFIX}"
"/usr/lib"
"/usr/local/lib${LIBSUFFIX}"
"/usr/local/lib"
PATH_SUFFIXES
"bin${LIBSUFFIX}/lib${lcomponent}"
"bin/lib${lcomponent}"
"lib${lcomponent}"
"bin${LIBSUFFIX}/${lcomponent}"
"bin/${lcomponent}"
"${lcomponent}"
"bin"
DOC "${lcomponent}: Path to binary file (optional)"
)
if(FFmpeg_${lcomponent}_BINARY)
set(_TARGET_TYPE SHARED)
else()
set(_TARGET_TYPE UNKNOWN)
endif()
# ToDo: Detect if static or shared. Usually should be shared, due to FFmpeg's size.
add_library(FFmpeg::${lcomponent} ${_TARGET_TYPE} IMPORTED)
target_include_directories(FFmpeg::${lcomponent}
INTERFACE "${_INCLUDE_DIR}"
)
set_target_properties(FFmpeg::${lcomponent} PROPERTIES
VERSION ${FFmpeg_${lcomponent}_VERSION}
SOVERSION ${FFmpeg_${lcomponent}_VERSION}
)
if(APPLE OR UNIX) # Unix/Mac, Unix/Linux
set_target_properties(FFmpeg::${lcomponent} PROPERTIES
IMPORTED_LOCATION "${FFmpeg_${lcomponent}_LIBRARY}"
)
if(FFmpeg_${lcomponent}_BINARY)
set_target_properties(FFmpeg::${lcomponent} PROPERTIES
LIBRARY_OUTPUT_NAME "${FFmpeg_${lcomponent}_BINARY}"
)
endif()
else() # Windows
set_target_properties(FFmpeg::${lcomponent} PROPERTIES
IMPORTED_IMPLIB "${FFmpeg_${lcomponent}_LIBRARY}"
)
if(FFmpeg_${lcomponent}_BINARY)
set_target_properties(FFmpeg::${lcomponent} PROPERTIES
IMPORTED_LOCATION "${FFmpeg_${lcomponent}_BINARY}"
LIBRARY_OUTPUT_NAME "${FFmpeg_${lcomponent}_BINARY}"
)
endif()
endif()
set(FFmpeg_${lcomponent}_FOUND ON PARENT_SCOPE)
message(STATUS "${lcomponent}: Found v${FFmpeg_${lcomponent}_VERSION}")
endif()
endfunction()
# Components
# - avcodec: libavcodec, avcodec.h
# - avdevice: libavdevice, avdevice.h
# - avfilter: libavfilter, avfilter.h
# - avformat: libavformat, avformat.h
# - avutil: libavutil, avutil.h
# - swresample: libswresample, swresample.h
# - swscale: libswscale, swscale.h
set(_COMPONENTS
"avcodec"
"avformat"
"avfilter"
"swresample"
"swscale"
"avdevice"
"avutil"
)
set(FFmpeg_LIBRARIES)
set(FFmpeg_VERSION "0.0.0")
if(NOT FFmpeg_FIND_COMPONENTS)
message(WARNING "No specific component requested, defaulting to everything")
set(FFmpeg_FIND_COMPONENT ${_COMPONENTS})
endif()
set(FFmpeg_FOUND ON)
foreach(component IN LISTS FFmpeg_FIND_COMPONENTS)
string(TOUPPER "${component}" lcomponent)
string(TOLOWER "${component}" lcomponent)
# Error out if an invalid component is requested.
if(NOT lcomponent IN_LIST _COMPONENTS)
message(FATAL_ERROR "Requested component '${lcomponent}' is unknown to us.")
endif()
find_ffmpeg_component(COMPONENT ${lcomponent})
if(NOT TARGET FFmpeg::${lcomponent})
if(FFmpeg_FIND_REQUIRED_${lcomponent})
set(FFmpeg_FOUND OFF)
endif()
endif()
endforeach()
# Set version based on priority list.
foreach(component IN LISTS _COMPONENTS)
string(TOUPPER "${lcomponent}" lcomponent)
string(TOLOWER "${lcomponent}" lcomponent)
if(TARGET FFmpeg::${lcomponent})
set(FFmpeg_VERSION ${FFmpeg_${lcomponent}_VERSION})
break()
endif()
endforeach()
find_package_handle_standard_args(FFmpeg
FOUND_VAR FFmpeg_FOUND
HANDLE_COMPONENTS
NAME_MISMATCHED
)