cmake: use *_STATIC_* variables when linking to a static ffmpeg

This commit is contained in:
Anonymous Maarten 2023-10-07 18:20:24 +02:00 committed by Sam Lantinga
parent 88f2fb9dcf
commit ebf5e08fa1
4 changed files with 86 additions and 100 deletions

View File

@ -87,6 +87,11 @@ jobs:
run: |
cp -rv /vita/dependencies/* ${VITASDK}/arm-vita-eabi
- name: Fix vita.toolchain.cmake
run: |
# cache PKG_CONFIG_PATH
sed -i -E 's/set\( PKG_CONFIG_EXECUTABLE "\$\{VITASDK}\/bin\/arm-vita-eabi-pkg-config" )/set( PKG_CONFIG_EXECUTABLE "${VITASDK}\/bin\/arm-vita-eabi-pkg-config" CACHE PATH "Path of pkg-config executable" )/' ${VITASDK}/share/vita.toolchain.cmake
- name: Configure (CMake)
run: |
cmake -S . -B build -G Ninja \

View File

@ -1,11 +1,8 @@
# vim: ts=2 sw=2
# - Try to find the required ffmpeg components(default: AVFORMAT, AVUTIL, AVCODEC)
#
# Once done this will define
# FFMPEG_FOUND - System has the all required components.
# FFMPEG_INCLUDE_DIRS - Include directory necessary for using the required components headers.
# FFMPEG_LIBRARIES - Link these to use the required ffmpeg components.
# FFMPEG_DEFINITIONS - Compiler switches required for using the required ffmpeg components.
#
# For each of the components it will additionally set.
# - AVCODEC
@ -15,40 +12,35 @@
# - AVUTIL
# - POSTPROC
# - SWSCALE
# the following target will be defined
# FFmpeg::SDL::<component> - link to this target to
# the following variables will be defined
# <component>_FOUND - System has <component>
# <component>_INCLUDE_DIRS - Include directory necessary for using the <component> headers
# <component>_LIBRARIES - Link these to use <component>
# <component>_DEFINITIONS - Compiler switches required for using <component>
# <component>_VERSION - The components version
# FFmpeg_<component>_FOUND - System has <component>
# FFmpeg_<component>_INCLUDE_DIRS - Include directory necessary for using the <component> headers
# FFmpeg_<component>_LIBRARIES - Link these to use <component>
# FFmpeg_<component>_DEFINITIONS - Compiler switches required for using <component>
# FFmpeg_<component>_VERSION - The components version
#
# Copyright (c) 2006, Matthias Kretz, <kretz@kde.org>
# Copyright (c) 2008, Alexander Neundorf, <neundorf@kde.org>
# Copyright (c) 2011, Michael Jansen, <kde@michael-jansen.biz>
# Copyright (c) 2023, Sam lantinga, <slouken@libsdl.org>
#
# Redistribution and use is allowed according to the terms of the BSD license.
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
include(FindPackageHandleStandardArgs)
include("${CMAKE_CURRENT_LIST_DIR}/PkgConfigHelper.cmake")
# The default components were taken from a survey over other FindFFMPEG.cmake files
if (NOT FFmpeg_FIND_COMPONENTS)
if(NOT FFmpeg_FIND_COMPONENTS)
set(FFmpeg_FIND_COMPONENTS AVCODEC AVFORMAT AVUTIL)
endif ()
foreach(_component IN LISTS FFmpeg_FIND_COMPONENTS)
set(FFmpeg_FIND_REQUIRED_${_component} TRUE)
endforeach()
endif()
#
### Macro: set_component_found
#
# Marks the given component as found if both *_LIBRARIES AND *_INCLUDE_DIRS is present.
#
macro(set_component_found _component )
if (${_component}_LIBRARIES AND ${_component}_INCLUDE_DIRS)
# message(STATUS " - ${_component} found.")
set(${_component}_FOUND TRUE)
else ()
# message(STATUS " - ${_component} not found.")
endif ()
endmacro()
find_package(PkgConfig QUIET)
#
### Macro: find_component
@ -58,94 +50,89 @@ endmacro()
#
macro(find_component _component _pkgconfig _library _header)
if (NOT WIN32)
# use pkg-config to get the directories and then use these values
# in the FIND_PATH() and FIND_LIBRARY() calls
find_package(PkgConfig)
if (PKG_CONFIG_FOUND)
pkg_check_modules(PC_${_component} ${_pkgconfig})
endif ()
endif (NOT WIN32)
# use pkg-config to get the directories and then use these values
# in the FIND_PATH() and FIND_LIBRARY() calls
if(PKG_CONFIG_FOUND)
pkg_check_modules(PC_${_component} QUIET ${_pkgconfig})
endif()
find_path(${_component}_INCLUDE_DIRS ${_header}
find_path(FFmpeg_${_component}_INCLUDE_DIRS
NAMES ${_header}
HINTS
${PC_${_component}_INCLUDEDIR}
${PC_${_component}_INCLUDE_DIRS}
PATH_SUFFIXES
ffmpeg
)
find_library(${_component}_LIBRARIES NAMES ${_library}
HINTS
${PC_${_component}_LIBDIR}
find_library(FFmpeg_${_component}_LIBRARY
NAMES ${_library}
HINTS
${PC_${_component}_LIBRARY_DIRS}
)
set(${_component}_DEFINITIONS ${PC_${_component}_CFLAGS_OTHER} CACHE STRING "The ${_component} CFLAGS.")
set(${_component}_VERSION ${PC_${_component}_VERSION} CACHE STRING "The ${_component} version number.")
if(FFmpeg_${_component}_INCLUDE_DIRS AND FFmpeg_${_component}_LIBRARY)
set(FFmpeg_${_component}_FOUND TRUE)
endif()
set_component_found(${_component})
if(PC_${_component}_FOUND)
get_flags_from_pkg_config("${FFmpeg_${_component}_LIBRARY}" "PC_${_component}" "${_component}")
endif()
set(FFmpeg_${_component}_VERSION "${PC_${_component}_VERSION}")
set(FFmpeg_${_component}_COMPILE_OPTIONS "${${_component}_options}" CACHE STRING "Extra compile options of FFmpeg ${_component}")
set(FFmpeg_${_component}_LIBRARIES "${${_component}_link_libraries}" CACHE STRING "Extra link libraries of FFmpeg ${_component}")
set(FFmpeg_${_component}_LINK_OPTIONS "${${_component}_link_options}" CACHE STRING "Extra link flags of FFmpeg ${_component}")
set(FFmpeg_${_component}_LINK_DIRECTORIES "${${_component}_link_directories}" CACHE PATH "Extra link directories of FFmpeg ${_component}")
mark_as_advanced(
${_component}_INCLUDE_DIRS
${_component}_LIBRARIES
${_component}_DEFINITIONS
${_component}_VERSION)
FFmpeg_${_component}_INCLUDE_DIRS
FFmpeg_${_component}_LIBRARY
FFmpeg_${_component}_COMPILE_OPTIONS
FFmpeg_${_component}_LIBRARIES
FFmpeg_${_component}_LINK_OPTIONS
FFmpeg_${_component}_LINK_DIRECTORIES
)
endmacro()
# Check for cached results. If there are skip the costly part.
if (NOT FFMPEG_LIBRARIES)
# Check for all possible component.
find_component(AVCODEC libavcodec avcodec libavcodec/avcodec.h)
find_component(AVFORMAT libavformat avformat libavformat/avformat.h)
find_component(AVDEVICE libavdevice avdevice libavdevice/avdevice.h)
find_component(AVUTIL libavutil avutil libavutil/avutil.h)
find_component(AVFILTER libavfilter avfilter libavfilter/avfilter.h)
find_component(SWSCALE libswscale swscale libswscale/swscale.h)
find_component(POSTPROC libpostproc postproc libpostproc/postprocess.h)
find_component(SWRESAMPLE libswresample swresample libswresample/swresample.h)
# Check if the required components were found and add their stuff to the FFMPEG_* vars.
foreach (_component ${FFmpeg_FIND_COMPONENTS})
if (${_component}_FOUND)
# message(STATUS "Required component ${_component} present.")
set(FFMPEG_LIBRARIES ${FFMPEG_LIBRARIES} ${${_component}_LIBRARIES})
set(FFMPEG_DEFINITIONS ${FFMPEG_DEFINITIONS} ${${_component}_DEFINITIONS})
list(APPEND FFMPEG_INCLUDE_DIRS ${${_component}_INCLUDE_DIRS})
else ()
# message(STATUS "Required component ${_component} missing.")
endif ()
endforeach ()
# Build the include path with duplicates removed.
if (FFMPEG_INCLUDE_DIRS)
list(REMOVE_DUPLICATES FFMPEG_INCLUDE_DIRS)
endif ()
# cache the vars.
set(FFMPEG_INCLUDE_DIRS ${FFMPEG_INCLUDE_DIRS} CACHE STRING "The FFmpeg include directories." FORCE)
set(FFMPEG_LIBRARIES ${FFMPEG_LIBRARIES} CACHE STRING "The FFmpeg libraries." FORCE)
set(FFMPEG_DEFINITIONS ${FFMPEG_DEFINITIONS} CACHE STRING "The FFmpeg cflags." FORCE)
mark_as_advanced(FFMPEG_INCLUDE_DIRS
FFMPEG_LIBRARIES
FFMPEG_DEFINITIONS)
endif ()
# Now set the noncached _FOUND vars for the components.
foreach (_component AVCODEC AVDEVICE AVFORMAT AVUTIL POSTPROCESS SWSCALE)
set_component_found(${_component})
endforeach ()
# Check for all possible component.
find_component(AVCODEC libavcodec avcodec libavcodec/avcodec.h)
find_component(AVFORMAT libavformat avformat libavformat/avformat.h)
find_component(AVDEVICE libavdevice avdevice libavdevice/avdevice.h)
find_component(AVUTIL libavutil avutil libavutil/avutil.h)
find_component(AVFILTER libavfilter avfilter libavfilter/avfilter.h)
find_component(SWSCALE libswscale swscale libswscale/swscale.h)
find_component(POSTPROC libpostproc postproc libpostproc/postprocess.h)
find_component(SWRESAMPLE libswresample swresample libswresample/swresample.h)
# Compile the list of required vars
set(_FFmpeg_REQUIRED_VARS FFMPEG_LIBRARIES FFMPEG_INCLUDE_DIRS)
foreach (_component ${FFmpeg_FIND_COMPONENTS})
list(APPEND _FFmpeg_REQUIRED_VARS ${_component}_LIBRARIES ${_component}_INCLUDE_DIRS)
set(_FFmpeg_REQUIRED_VARS)
foreach(_component ${FFmpeg_FIND_COMPONENTS})
list(APPEND _FFmpeg_REQUIRED_VARS FFmpeg_${_component}_INCLUDE_DIRS FFmpeg_${_component}_LIBRARY)
endforeach ()
# Give a nice error message if some of the required vars are missing.
find_package_handle_standard_args(FFmpeg DEFAULT_MSG ${_FFmpeg_REQUIRED_VARS})
set(FFMPEG_LIBRARIES)
if(FFmpeg_FOUND)
foreach(_component IN LISTS FFmpeg_FIND_COMPONENTS)
if(FFmpeg_${_component}_FOUND)
list(APPEND FFMPEG_LIBRARIES FFmpeg::SDL::${_component})
if(NOT TARGET FFmpeg::SDL::${_component})
add_library(FFmpeg::SDL::${_component} UNKNOWN IMPORTED)
set_target_properties(FFmpeg::SDL::${_component} PROPERTIES
IMPORTED_LOCATION "${FFmpeg_${_component}_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${FFmpeg_${_component}_INCLUDE_DIRS}"
INTERFACE_COMPILE_OPTIONS "${FFmpeg_${_component}_COMPILE_OPTIONS}"
INTERFACE_LINK_LIBRARIES "${FFmpeg_${_component}_LIBRARIES}"
INTERFACE_LINK_OPTIONS "${FFmpeg_${_component}_LINK_OPTIONS}"
INTERFACE_LINK_DIRECTORIES "${FFmpeg_${_component}_LINK_DIRECTORIES}"
)
endif()
endif()
endforeach()
endif()

View File

@ -2,13 +2,11 @@
function(get_flags_from_pkg_config _library _pc_prefix _out_prefix)
if("${_library}" MATCHES "${CMAKE_STATIC_LIBRARY_SUFFIX}$")
set(_include_dirs ${_pc_prefix}_STATIC_INCLUDE_DIRS)
set(_cflags ${_pc_prefix}_STATIC_CFLAGS_OTHER)
set(_link_libraries ${_pc_prefix}_STATIC_LIBRARIES)
set(_link_options ${_pc_prefix}_STATIC_LDFLAGS_OTHER)
set(_library_dirs ${_pc_prefix}_STATIC_LIBRARY_DIRS)
else()
set(_include_dirs ${_pc_prefix}_INCLUDE_DIRS)
set(_cflags ${_pc_prefix}_CFLAGS_OTHER)
set(_link_libraries ${_pc_prefix}_LIBRARIES)
set(_link_options ${_pc_prefix}_LDFLAGS_OTHER)
@ -21,9 +19,6 @@ function(get_flags_from_pkg_config _library _pc_prefix _out_prefix)
# Work around CMake's flag deduplication when pc files use `-framework A` instead of `-Wl,-framework,A`
string(REPLACE "-framework;" "-Wl,-framework," "_filtered_link_options" "${${_link_options}}")
set(${_out_prefix}_include_dirs
"${${_include_dirs}}"
PARENT_SCOPE)
set(${_out_prefix}_compile_options
"${${_cflags}}"
PARENT_SCOPE)

View File

@ -184,9 +184,8 @@ endif()
set(FFmpeg_FIND_COMPONENTS AVCODEC AVFORMAT AVUTIL SWSCALE)
include("${SDL3_SOURCE_DIR}/cmake/FindFFmpeg.cmake")
if(FFMPEG_FOUND)
if(FFmpeg_FOUND)
add_sdl_test_executable(testspriteffmpeg NO_C90 SOURCES testspriteffmpeg.c ${icon_bmp_header})
target_include_directories(testspriteffmpeg PRIVATE ${FFMPEG_INCLUDE_DIRS})
target_link_libraries(testspriteffmpeg PRIVATE ${FFMPEG_LIBRARIES})
else()
message(STATUS "Cannot find ffmpeg, skipping testspriteffmpeg")