From d951d2e4c4540012fe43d637368e4c545a06c636 Mon Sep 17 00:00:00 2001 From: Michael Maltese Date: Mon, 22 May 2017 12:13:19 -0700 Subject: [PATCH] CMake: libav/ffmpeg cleanups Use @Orphis's FindFFmpeg module from ppsspp: https://github.com/hrydgard/ppsspp/commit/2149d3db7fb960d140bdee31a9f0f4837934127b From that commit: > This new module should be able to handle both libraries in the regular > paths and fallback to pkg-config. > It is also able to find dynamic libraries, not just static libraries. > It will generate imported targets with the name FFmpeg:: that you > can use in your scripts. --- CMake/CheckLib.cmake | 71 ----------- CMake/FindFFmpeg.cmake | 163 +++++++++++++++++++++++++ CMakeLists.txt | 16 ++- Source/Core/VideoCommon/CMakeLists.txt | 14 ++- 4 files changed, 182 insertions(+), 82 deletions(-) create mode 100644 CMake/FindFFmpeg.cmake diff --git a/CMake/CheckLib.cmake b/CMake/CheckLib.cmake index b98357b99f..64c05e9ba0 100644 --- a/CMake/CheckLib.cmake +++ b/CMake/CheckLib.cmake @@ -56,74 +56,3 @@ macro(check_lib var pc lib) endif() endmacro() -macro(check_libav) - if(PKG_CONFIG_FOUND) - pkg_check_modules(LIBAV libavcodec>=54.35.0 libavformat>=54.20.4 - libswscale>=2.1.1 libavutil>=52.3.0) - endif() - - if(NOT LIBAV_FOUND) - if(WIN32) - add_library(avcodec STATIC IMPORTED) - set_target_properties(avcodec PROPERTIES - INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/Externals/ffmpeg/include - IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/Externals/ffmpeg/lib/avcodec.lib - ) - - add_library(avformat STATIC IMPORTED) - set_target_properties(avformat PROPERTIES - INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/Externals/ffmpeg/include - IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/Externals/ffmpeg/lib/avformat.lib - ) - - add_library(avutil STATIC IMPORTED) - set_target_properties(avutil PROPERTIES - INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/Externals/ffmpeg/include - IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/Externals/ffmpeg/lib/avutil.lib - ) - - add_library(swresample STATIC IMPORTED) - set_target_properties(swresample PROPERTIES - INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/Externals/ffmpeg/include - IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/Externals/ffmpeg/lib/swresample.lib - ) - - add_library(swscale STATIC IMPORTED) - set_target_properties(swscale PROPERTIES - INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/Externals/ffmpeg/include - IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/Externals/ffmpeg/lib/swscale.lib - ) - - set(LIBAV_FOUND ON) - set(LIBAV_LIBRARIES avcodec avformat avutil swresample swscale) - else() - # Attempt to find it through static means - set(LIBAV_LDFLAGS avformat avcodec swscale avutil) - set(CMAKE_REQUIRED_LIBRARIES ${LIBAV_LDFLAGS}) - CHECK_CXX_SOURCE_COMPILES( - "extern \"C\" { - #include - #include - #include - #include - } - int main(int argc, char **argv) - { - av_register_all(); - return 0; - }" - LIBAV_FOUND) - unset(CMAKE_REQUIRED_LIBRARIES) - endif() - endif() - - if(LIBAV_FOUND) - message(STATUS "libav/ffmpeg found, enabling AVI frame dumps") - add_definitions(-DHAVE_LIBAV) - if(LIBAV_INCLUDE_DIRS) - include_directories(${LIBAV_INCLUDE_DIRS}) - endif() - else() - message(STATUS "libav/ffmpeg not found, disabling AVI frame dumps") - endif() -endmacro() diff --git a/CMake/FindFFmpeg.cmake b/CMake/FindFFmpeg.cmake new file mode 100644 index 0000000000..a8c482dc21 --- /dev/null +++ b/CMake/FindFFmpeg.cmake @@ -0,0 +1,163 @@ +# FindFFmpeg +# ---------- +# +# Find the native FFmpeg includes and libraries +# +# This module defines the following variables: +# +# FFmpeg_INCLUDE_: where to find .h +# FFmpeg_LIBRARY_: where to find the library +# FFmpeg_INCLUDES: 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::: Target to use 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...). +# +# can be one of: +# avcodec +# avdevice +# avfilter +# avformat +# 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} REQUIRED 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) + set(FFmpeg_${c}_FOUND TRUE PARENT_SCOPE) + if(NOT FFmpeg_FIND_QUIETLY) + message("-- Found ${LIBNAME}: ${FFmpeg_INCLUDE_${LIBNAME}} ${FFmpeg_LIBRARY_${LIBNAME}}") + endif() + endif() +endfunction() + +foreach(c ${_FFmpeg_ALL_COMPONENTS}) + find_ffmpeg(${c}) +endforeach() + +foreach(c ${_FFmpeg_ALL_COMPONENTS}) + if(FFmpeg_${c}_FOUND) + list(APPEND FFmpeg_INCLUDES ${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() + +list(REMOVE_DUPLICATES FFmpeg_INCLUDES) + +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) diff --git a/CMakeLists.txt b/CMakeLists.txt index 67424f11b0..975a06ad99 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -477,12 +477,18 @@ if(USE_X11) pkg_check_modules(X11_INPUT REQUIRED xi>=1.5.0) endif() -if(ENCODE_FRAMEDUMPS) - check_libav() - if(LIBAV_FOUND) - LIST(APPEND LIBS ${LIBAV_LDFLAGS}) - endif() +if(ENCODE_FRAMEDUMPS) + if(WIN32) + set(FFMPEG_DIR Externals/ffmpeg) + endif() + find_package(FFmpeg COMPONENTS avcodec avformat avutil swscale) + if(FFmpeg_FOUND) + message(STATUS "libav/ffmpeg found, enabling AVI frame dumps") + add_definitions(-DHAVE_LIBAV) + else() + message(STATUS "libav/ffmpeg not found, disabling AVI frame dumps") + endif() endif() if(NOT ANDROID) diff --git a/Source/Core/VideoCommon/CMakeLists.txt b/Source/Core/VideoCommon/CMakeLists.txt index 7eef4f715d..a50cd2774a 100644 --- a/Source/Core/VideoCommon/CMakeLists.txt +++ b/Source/Core/VideoCommon/CMakeLists.txt @@ -62,12 +62,14 @@ else() set(SRCS ${SRCS} TextureDecoder_Generic.cpp) endif() -if(LIBAV_FOUND OR WIN32) - set(SRCS ${SRCS} AVIDump.cpp) -endif() - add_dolphin_library(videocommon "${SRCS}" "${LIBS}") -if(LIBAV_FOUND) - target_link_libraries(videocommon PRIVATE ${LIBS} ${LIBAV_LIBRARIES}) +if(FFmpeg_FOUND) + target_sources(videocommon PRIVATE AVIDump.cpp) + target_link_libraries(videocommon PRIVATE + FFmpeg::avcodec + FFmpeg::avformat + FFmpeg::avutil + FFmpeg::swscale + ) endif()