Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmake: try system ffmpeg first #78

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 37 additions & 36 deletions 3rdparty/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,46 +33,47 @@ endif()

add_subdirectory(LibAtrac9)

set(FFMPEG_PATH ${CMAKE_CURRENT_SOURCE_DIR}/FFmpeg)
add_custom_command(
OUTPUT ${FFMPEG_PATH}/config.h
COMMAND ./configure
COMMENT "Configuring FFmpeg..."
WORKING_DIRECTORY ${FFMPEG_PATH}
)
add_custom_target(ffmpeg-configure DEPENDS ${FFMPEG_PATH}/config.h)

function(import_ffmpeg_library name)
if (${CMAKE_GENERATOR} STREQUAL "Unix Makefiles")
set(MAKE_COMMAND $(MAKE) -j$(nproc))
elseif (${CMAKE_GENERATOR} STREQUAL "Ninja")
set(MAKE_COMMAND make -j$$(nproc))
else()
set(MAKE_COMMAND make)
endif()

if(NOT ffmpeg_FOUND)
set(FFMPEG_PATH ${CMAKE_CURRENT_SOURCE_DIR}/FFmpeg)
add_custom_command(
OUTPUT "${FFMPEG_PATH}/lib${name}/lib${name}.a"
COMMAND ${MAKE_COMMAND} -C ${FFMPEG_PATH} "lib${name}/lib${name}.a"
COMMENT "Building lib${name}/lib${name}.a"
DEPENDS ffmpeg-configure
OUTPUT ${FFMPEG_PATH}/config.h
COMMAND ./configure
COMMENT "Configuring FFmpeg..."
WORKING_DIRECTORY ${FFMPEG_PATH}
)
add_custom_target(ffmpeg-configure DEPENDS ${FFMPEG_PATH}/config.h)

add_custom_target(ffmpeg-build-${name} DEPENDS "${FFMPEG_PATH}/lib${name}/lib${name}.a")
function(import_ffmpeg_library name)
if (${CMAKE_GENERATOR} STREQUAL "Unix Makefiles")
set(MAKE_COMMAND $(MAKE) -j$(nproc))
elseif (${CMAKE_GENERATOR} STREQUAL "Ninja")
set(MAKE_COMMAND make -j$$(nproc))
else()
set(MAKE_COMMAND make)
endif()

add_library(ffmpeg::${name} STATIC IMPORTED GLOBAL)
set_property(TARGET ffmpeg::${name} PROPERTY IMPORTED_LOCATION "${FFMPEG_PATH}/lib${name}/lib${name}.a")
set_property(TARGET ffmpeg::${name} PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${FFMPEG_PATH}")
add_dependencies(ffmpeg::${name} ffmpeg-build-${name})
endfunction()
add_custom_command(
OUTPUT "${FFMPEG_PATH}/lib${name}/lib${name}.a"
COMMAND ${MAKE_COMMAND} -C ${FFMPEG_PATH} "lib${name}/lib${name}.a"
COMMENT "Building lib${name}/lib${name}.a"
DEPENDS ffmpeg-configure
WORKING_DIRECTORY ${FFMPEG_PATH}
)

import_ffmpeg_library(avcodec)
import_ffmpeg_library(avformat)
import_ffmpeg_library(avfilter)
import_ffmpeg_library(avdevice)
import_ffmpeg_library(avutil)
import_ffmpeg_library(swscale)
import_ffmpeg_library(swresample)
import_ffmpeg_library(postproc)
add_custom_target(ffmpeg-build-${name} DEPENDS "${FFMPEG_PATH}/lib${name}/lib${name}.a")

add_library(ffmpeg::${name} STATIC IMPORTED GLOBAL)
set_property(TARGET ffmpeg::${name} PROPERTY IMPORTED_LOCATION "${FFMPEG_PATH}/lib${name}/lib${name}.a")
set_property(TARGET ffmpeg::${name} PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${FFMPEG_PATH}")
add_dependencies(ffmpeg::${name} ffmpeg-build-${name})
endfunction()

import_ffmpeg_library(avcodec)
import_ffmpeg_library(avformat)
import_ffmpeg_library(avfilter)
import_ffmpeg_library(avdevice)
import_ffmpeg_library(avutil)
import_ffmpeg_library(swscale)
import_ffmpeg_library(swresample)
import_ffmpeg_library(postproc)
endif()
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ set(CMAKE_BUILD_RPATH_USE_ORIGIN on)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

find_package(ffmpeg 7.0.2 MODULE COMPONENTS avcodec avutil swresample)
find_package(nlohmann_json CONFIG)
add_subdirectory(3rdparty EXCLUDE_FROM_ALL)

Expand Down
29 changes: 29 additions & 0 deletions cmake/Findffmpeg.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
if(NOT avutil IN_LIST ffmpeg_FIND_COMPONENTS)
list(APPEND ffmpeg_FIND_COMPONENTS avutil)
endif()

find_package(PkgConfig QUIET)
foreach(c IN LISTS ffmpeg_FIND_COMPONENTS)
pkg_search_module(ffmpeg_${c} QUIET IMPORTED_TARGET lib${c})
endforeach()

find_file(ffmpeg_VERSION_FILE libavutil/ffversion.h HINTS "${ffmpeg_avutil_INCLUDEDIR}")
if(ffmpeg_VERSION_FILE)
file(STRINGS "${ffmpeg_VERSION_FILE}" ffmpeg_VERSION_LINE REGEX "FFMPEG_VERSION")
string(REGEX MATCH "[0-9.]+" ffmpeg_VERSION "${ffmpeg_VERSION_LINE}")
unset(ffmpeg_VERSION_LINE)
unset(ffmpeg_VERSION_FILE)
endif()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(ffmpeg
REQUIRED_VARS ffmpeg_avutil_LINK_LIBRARIES
VERSION_VAR ffmpeg_VERSION
HANDLE_COMPONENTS
)

foreach(c IN LISTS ffmpeg_FIND_COMPONENTS)
if(ffmpeg_FOUND AND ffmpeg_${c}_FOUND AND NOT TARGET ffmpeg::${c})
add_library(ffmpeg::${c} ALIAS PkgConfig::ffmpeg_${c})
endif()
endforeach()
Loading