CMake: Port citra-emu/citra FindFFmpeg.cmake

Also renames related CMake variables to match both the Find*FFmpeg* and
variables defined within the file. Fixes odd errors produced by the old
FindFFmpeg.

Citra's FindFFmpeg is slightly modified here: adds Citra's copyright at
the beginning, renames FFmpeg_INCLUDES to FFmpeg_INCLUDE_DIR, disables a
few components in _FFmpeg_ALL_COMPONENTS, and adds the missing avutil
component to the comment above.
This commit is contained in:
lat9nq 2021-02-05 15:39:19 -05:00
parent 0913aaa42a
commit 1d19eac415
3 changed files with 214 additions and 128 deletions

View file

@ -391,45 +391,45 @@ if (YUZU_USE_BUNDLED_FFMPEG)
# Build FFmpeg from externals # Build FFmpeg from externals
message(STATUS "Using FFmpeg from externals") message(STATUS "Using FFmpeg from externals")
set(FFMPEG_PREFIX ${PROJECT_SOURCE_DIR}/externals/ffmpeg) set(FFmpeg_PREFIX ${PROJECT_SOURCE_DIR}/externals/ffmpeg)
set(FFMPEG_BUILD_DIR ${PROJECT_BINARY_DIR}/externals/ffmpeg) set(FFmpeg_BUILD_DIR ${PROJECT_BINARY_DIR}/externals/ffmpeg)
set(FFMPEG_MAKEFILE ${FFMPEG_BUILD_DIR}/Makefile) set(FFmpeg_MAKEFILE ${FFmpeg_BUILD_DIR}/Makefile)
make_directory(${FFMPEG_BUILD_DIR}) make_directory(${FFmpeg_BUILD_DIR})
# Read version string from external # Read version string from external
file(READ ${FFMPEG_PREFIX}/RELEASE FFMPEG_VERSION) file(READ ${FFmpeg_PREFIX}/RELEASE FFmpeg_VERSION)
set(FFMPEG_FOUND NO) set(FFmpeg_FOUND NO)
if (NOT FFMPEG_VERSION STREQUAL "") if (NOT FFmpeg_VERSION STREQUAL "")
set(FFMPEG_FOUND YES) set(FFmpeg_FOUND YES)
endif() endif()
set(FFMPEG_COMPONENTS set(FFmpeg_COMPONENTS
avcodec avcodec
avutil avutil
swscale) swscale)
foreach(COMPONENT ${FFMPEG_COMPONENTS}) foreach(COMPONENT ${FFmpeg_COMPONENTS})
set(FFMPEG_${COMPONENT}_PREFIX "${FFMPEG_BUILD_DIR}/lib${COMPONENT}") set(FFmpeg_${COMPONENT}_PREFIX "${FFmpeg_BUILD_DIR}/lib${COMPONENT}")
set(FFMPEG_${COMPONENT}_LIB_NAME "lib${COMPONENT}.a") set(FFmpeg_${COMPONENT}_LIB_NAME "lib${COMPONENT}.a")
set(FFMPEG_${COMPONENT}_LIBRARY "${FFMPEG_${COMPONENT}_PREFIX}/${FFMPEG_${COMPONENT}_LIB_NAME}") set(FFmpeg_${COMPONENT}_LIBRARY "${FFmpeg_${COMPONENT}_PREFIX}/${FFmpeg_${COMPONENT}_LIB_NAME}")
set(FFMPEG_LIBRARIES set(FFmpeg_LIBRARIES
${FFMPEG_LIBRARIES} ${FFmpeg_LIBRARIES}
${FFMPEG_${COMPONENT}_LIBRARY} ${FFmpeg_${COMPONENT}_LIBRARY}
CACHE PATH "Paths to FFmpeg libraries" FORCE) CACHE PATH "Paths to FFmpeg libraries" FORCE)
endforeach() endforeach()
set(FFMPEG_INCLUDE_DIR set(FFmpeg_INCLUDE_DIR
${FFMPEG_PREFIX} ${FFmpeg_PREFIX}
CACHE PATH "Path to FFmpeg headers" FORCE) CACHE PATH "Path to FFmpeg headers" FORCE)
# `configure` parameters builds only exactly what yuzu needs from FFmpeg # `configure` parameters builds only exactly what yuzu needs from FFmpeg
# `--disable-{vaapi,vdpau}` is needed to avoid linking issues # `--disable-{vaapi,vdpau}` is needed to avoid linking issues
add_custom_command( add_custom_command(
OUTPUT OUTPUT
${FFMPEG_MAKEFILE} ${FFmpeg_MAKEFILE}
COMMAND COMMAND
/bin/bash ${FFMPEG_PREFIX}/configure /bin/bash ${FFmpeg_PREFIX}/configure
--disable-avdevice --disable-avdevice
--disable-avfilter --disable-avfilter
--disable-avformat --disable-avformat
@ -445,25 +445,25 @@ if (YUZU_USE_BUNDLED_FFMPEG)
--enable-decoder=h264 --enable-decoder=h264
--enable-decoder=vp9 --enable-decoder=vp9
WORKING_DIRECTORY WORKING_DIRECTORY
${FFMPEG_BUILD_DIR} ${FFmpeg_BUILD_DIR}
) )
add_custom_command( add_custom_command(
OUTPUT OUTPUT
${FFMPEG_LIBRARIES} ${FFmpeg_LIBRARIES}
COMMAND COMMAND
make make
WORKING_DIRECTORY WORKING_DIRECTORY
${FFMPEG_BUILD_DIR} ${FFmpeg_BUILD_DIR}
) )
# ALL makes this custom target build every time # ALL makes this custom target build every time
# but it won't actually build if the DEPENDS parameter is up to date # but it won't actually build if the DEPENDS parameter is up to date
add_custom_target(ffmpeg-build ALL DEPENDS ${FFMPEG_LIBRARIES}) add_custom_target(ffmpeg-build ALL DEPENDS ${FFmpeg_LIBRARIES})
add_custom_target(ffmpeg-configure ALL DEPENDS ${FFMPEG_MAKEFILE}) add_custom_target(ffmpeg-configure ALL DEPENDS ${FFmpeg_MAKEFILE})
if (FFMPEG_FOUND) if (FFmpeg_FOUND)
message(STATUS "Found FFmpeg version ${FFMPEG_VERSION}") message(STATUS "Found FFmpeg version ${FFmpeg_VERSION}")
add_dependencies(ffmpeg-build ffmpeg-configure) add_dependencies(ffmpeg-build ffmpeg-configure)
else() else()
@ -471,22 +471,26 @@ if (YUZU_USE_BUNDLED_FFMPEG)
endif() endif()
else() # WIN32 else() # WIN32
# Use yuzu FFmpeg binaries # Use yuzu FFmpeg binaries
set(FFMPEG_EXT_NAME "ffmpeg-4.2.1") set(FFmpeg_EXT_NAME "ffmpeg-4.2.1")
set(FFMPEG_PATH "${CMAKE_BINARY_DIR}/externals/${FFMPEG_EXT_NAME}") set(FFmpeg_PATH "${CMAKE_BINARY_DIR}/externals/${FFmpeg_EXT_NAME}")
download_bundled_external("ffmpeg/" ${FFMPEG_EXT_NAME} "") download_bundled_external("ffmpeg/" ${FFmpeg_EXT_NAME} "")
set(FFMPEG_FOUND YES) set(FFmpeg_FOUND YES)
set(FFMPEG_INCLUDE_DIR "${FFMPEG_PATH}/include" CACHE PATH "Path to FFmpeg headers" FORCE) set(FFmpeg_INCLUDE_DIR "${FFmpeg_PATH}/include" CACHE PATH "Path to FFmpeg headers" FORCE)
set(FFMPEG_LIBRARY_DIR "${FFMPEG_PATH}/bin" CACHE PATH "Path to FFmpeg library directory" FORCE) set(FFmpeg_LIBRARY_DIR "${FFmpeg_PATH}/bin" CACHE PATH "Path to FFmpeg library directory" FORCE)
set(FFMPEG_DLL_DIR "${FFMPEG_PATH}/bin" CACHE PATH "Path to FFmpeg dll's" FORCE) set(FFmpeg_DLL_DIR "${FFmpeg_PATH}/bin" CACHE PATH "Path to FFmpeg dll's" FORCE)
set(FFMPEG_LIBRARIES set(FFmpeg_LIBRARIES
${FFMPEG_LIBRARY_DIR}/swscale.lib ${FFmpeg_LIBRARY_DIR}/swscale.lib
${FFMPEG_LIBRARY_DIR}/avcodec.lib ${FFmpeg_LIBRARY_DIR}/avcodec.lib
${FFMPEG_LIBRARY_DIR}/avutil.lib ${FFmpeg_LIBRARY_DIR}/avutil.lib
CACHE PATH "Paths to FFmpeg libraries" FORCE) CACHE PATH "Paths to FFmpeg libraries" FORCE)
endif() endif()
else() else()
# Use system installed FFmpeg # Use system installed FFmpeg
find_package(FFmpeg REQUIRED VERSION 4.0) find_package(FFmpeg REQUIRED)
if (NOT FFmpeg_FOUND)
message(FATAL_ERROR "FFmpeg not found")
endif()
endif() endif()
# Prefer the -pthread flag on Linux. # Prefer the -pthread flag on Linux.

View file

@ -1,100 +1,182 @@
# - Try to find ffmpeg libraries (libavcodec, libavformat and libavutil) # FindFFmpeg
# Once done this will define # ----------
# #
# FFMPEG_FOUND - system has ffmpeg or libav # Copyright 2019 Citra Emulator Project
# FFMPEG_INCLUDE_DIR - the ffmpeg include directory # Licensed under GPLv2 or any later version
# FFMPEG_LIBRARIES - Link these to use ffmpeg
# FFMPEG_LIBAVCODEC
# FFMPEG_LIBAVFORMAT
# FFMPEG_LIBAVUTIL
# #
# Copyright (c) 2008 Andreas Schneider <mail@cynapses.org> # Find the native FFmpeg includes and libraries
# Modified for other libraries by Lasse Kärkkäinen <tronic>
# Modified for Hedgewars by Stepik777
# Modified for FFmpeg-example Tuukka Pasanen 2018
# Modified for yuzu toastUnlimted 2020
# #
# Redistribution and use is allowed according to the terms of the New # This module defines the following variables:
# BSD license. #
# 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 prefered 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 # Disabled
# avfilter # Disabled
# avformat # Disabled
# avutil
# postproc # Disabled
# swresample # Disabled
# swscale
# #
include(FindPackageHandleStandardArgs) set(_FFmpeg_ALL_COMPONENTS
avcodec
find_package_handle_standard_args(FFMPEG avutil
FOUND_VAR FFMPEG_FOUND swscale
REQUIRED_VARS
FFMPEG_LIBRARY
FFMPEG_INCLUDE_DIR
VERSION_VAR FFMPEG_VERSION
) )
if(FFMPEG_LIBRARIES AND FFMPEG_INCLUDE_DIR) set(_FFmpeg_DEPS_avcodec avutil)
# in cache already set(_FFmpeg_DEPS_avdevice avcodec avformat avutil)
set(FFMPEG_FOUND TRUE) 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() else()
# use pkg-config to get the directories and then use these values list(APPEND INCLUDE_PATHS
# in the FIND_PATH() and FIND_LIBRARY() calls /usr/local/include/ffmpeg
find_package(PkgConfig) /usr/local/include/lib${LIBNAME}
if(PKG_CONFIG_FOUND) /usr/include/ffmpeg
pkg_check_modules(_FFMPEG_AVCODEC libavcodec) /usr/include/lib${LIBNAME}
pkg_check_modules(_FFMPEG_AVUTIL libavutil) /usr/include/ffmpeg/lib${LIBNAME}
pkg_check_modules(_FFMPEG_SWSCALE libswscale) )
endif()
find_path(FFMPEG_AVCODEC_INCLUDE_DIR list(APPEND LIB_PATHS
NAMES libavcodec/avcodec.h
PATHS ${_FFMPEG_AVCODEC_INCLUDE_DIRS}
/usr/include
/usr/local/include
/opt/local/include
/sw/include
PATH_SUFFIXES ffmpeg libav)
find_library(FFMPEG_LIBAVCODEC
NAMES avcodec
PATHS ${_FFMPEG_AVCODEC_LIBRARY_DIRS}
/usr/lib
/usr/local/lib /usr/local/lib
/opt/local/lib
/sw/lib)
find_library(FFMPEG_LIBAVUTIL
NAMES avutil
PATHS ${_FFMPEG_AVUTIL_LIBRARY_DIRS}
/usr/lib /usr/lib
/usr/local/lib )
/opt/local/lib
/sw/lib)
find_library(FFMPEG_LIBSWSCALE
NAMES swscale
PATHS ${_FFMPEG_SWSCALE_LIBRARY_DIRS}
/usr/lib
/usr/local/lib
/opt/local/lib
/sw/lib)
if(FFMPEG_LIBAVCODEC AND FFMPEG_LIBAVUTIL AND FFMPEG_LIBSWSCALE)
set(FFMPEG_FOUND TRUE)
endif() endif()
if(FFMPEG_FOUND) find_path(FFmpeg_INCLUDE_${LIBNAME} lib${LIBNAME}/${LIBNAME}.h
set(FFMPEG_INCLUDE_DIR ${FFMPEG_AVCODEC_INCLUDE_DIR}) HINTS ${INCLUDE_PATHS}
set(FFMPEG_LIBRARIES )
${FFMPEG_LIBAVCODEC}
${FFMPEG_LIBAVUTIL} find_library(FFmpeg_LIBRARY_${LIBNAME} ${LIBNAME}
${FFMPEG_LIBSWSCALE}) 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() endif()
if(FFMPEG_FOUND) if(FFmpeg_INCLUDE_${LIBNAME} AND FFmpeg_LIBRARY_${LIBNAME})
if(NOT FFMPEG_FIND_QUIETLY) set(FFmpeg_INCLUDE_${LIBNAME} "${FFmpeg_INCLUDE_${LIBNAME}}" PARENT_SCOPE)
message(STATUS set(FFmpeg_LIBRARY_${LIBNAME} "${FFmpeg_LIBRARY_${LIBNAME}}" PARENT_SCOPE)
"Found FFMPEG or Libav: ${FFMPEG_LIBRARIES}, ${FFMPEG_INCLUDE_DIR}")
# 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() endif()
else() endforeach()
if(FFMPEG_FIND_REQUIRED) set(FFmpeg_${LIBNAME}_VERSION "${FFmpeg_${LIBNAME}_VERSION_MAJOR}.${FFmpeg_${LIBNAME}_VERSION_MINOR}.${FFmpeg_${LIBNAME}_VERSION_MICRO}")
message(FATAL_ERROR set(FFmpeg_${c}_VERSION "${FFmpeg_${LIBNAME}_VERSION}" PARENT_SCOPE)
"Could not find libavcodec or libavutil or libswscale") 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()
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()
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)

View file

@ -277,8 +277,8 @@ if (YUZU_USE_BUNDLED_FFMPEG AND NOT WIN32)
add_dependencies(video_core ffmpeg-build) add_dependencies(video_core ffmpeg-build)
endif() endif()
target_include_directories(video_core PRIVATE ${FFMPEG_INCLUDE_DIR}) target_include_directories(video_core PRIVATE ${FFmpeg_INCLUDE_DIR})
target_link_libraries(video_core PRIVATE ${FFMPEG_LIBRARIES}) target_link_libraries(video_core PRIVATE ${FFmpeg_LIBRARIES})
add_dependencies(video_core host_shaders) add_dependencies(video_core host_shaders)
target_include_directories(video_core PRIVATE ${HOST_SHADERS_INCLUDE}) target_include_directories(video_core PRIVATE ${HOST_SHADERS_INCLUDE})