Merge pull request #3276 from Carbenium/cmake-ffmpeg

Build: Add CMake package module for FFmpeg
This commit is contained in:
Isaac Connor 2021-06-04 17:35:45 -04:00 committed by GitHub
commit 9cce9744d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 202 additions and 88 deletions

View File

@ -438,130 +438,83 @@ else()
message(FATAL_ERROR "ZoneMinder requires mysqlclient but it was not found on your system") message(FATAL_ERROR "ZoneMinder requires mysqlclient but it was not found on your system")
endif() endif()
set(PATH_FFMPEG "") find_package(FFMPEG COMPONENTS avformat)
set(OPT_FFMPEG "no") if(FFMPEG_avformat_FOUND)
# avformat (using find_library and find_path)
find_library(AVFORMAT_LIBRARIES avformat)
if(AVFORMAT_LIBRARIES)
set(HAVE_LIBAVFORMAT 1) set(HAVE_LIBAVFORMAT 1)
list(APPEND ZM_BIN_LIBS "${AVFORMAT_LIBRARIES}") set(HAVE_LIBAVFORMAT_AVFORMAT_H 1)
find_path(AVFORMAT_INCLUDE_DIR "libavformat/avformat.h" /usr/include/ffmpeg) list(APPEND ZM_BIN_LIBS "FFMPEG::avformat")
if(AVFORMAT_INCLUDE_DIR)
include_directories("${AVFORMAT_INCLUDE_DIR}")
set(CMAKE_REQUIRED_INCLUDES "${AVFORMAT_INCLUDE_DIR}")
endif()
mark_as_advanced(FORCE AVFORMAT_LIBRARIES AVFORMAT_INCLUDE_DIR)
check_include_file("libavformat/avformat.h" HAVE_LIBAVFORMAT_AVFORMAT_H)
set(optlibsfound "${optlibsfound} AVFormat") set(optlibsfound "${optlibsfound} AVFormat")
else() else()
set(optlibsnotfound "${optlibsnotfound} AVFormat") set(optlibsnotfound "${optlibsnotfound} AVFormat")
endif() endif()
# avcodec (using find_library and find_path) find_package(FFMPEG COMPONENTS avcodec)
find_library(AVCODEC_LIBRARIES avcodec) if(FFMPEG_avcodec_FOUND)
if(AVCODEC_LIBRARIES)
set(HAVE_LIBAVCODEC 1) set(HAVE_LIBAVCODEC 1)
list(APPEND ZM_BIN_LIBS "${AVCODEC_LIBRARIES}") set(HAVE_LIBAVCODEC_AVCODEC_H 1)
find_path(AVCODEC_INCLUDE_DIR "libavcodec/avcodec.h" /usr/include/ffmpeg) list(APPEND ZM_BIN_LIBS "${FFMPEG_avcodec_LIBRARIES}")
if(AVCODEC_INCLUDE_DIR)
include_directories("${AVCODEC_INCLUDE_DIR}")
set(CMAKE_REQUIRED_INCLUDES "${AVCODEC_INCLUDE_DIR}")
endif()
mark_as_advanced(FORCE AVCODEC_LIBRARIES AVCODEC_INCLUDE_DIR)
check_include_file("libavcodec/avcodec.h" HAVE_LIBAVCODEC_AVCODEC_H)
set(optlibsfound "${optlibsfound} AVCodec") set(optlibsfound "${optlibsfound} AVCodec")
else() else()
message(WARNING "\nWhile it should be possible to build ZM without AVCODEC the result will pretty useless.") message(WARNING "\nWhile it should be possible to build ZM without AVCODEC the result will pretty useless.")
set(optlibsnotfound "${optlibsnotfound} AVCodec") set(optlibsnotfound "${optlibsnotfound} AVCodec")
endif() endif()
# avdevice (using find_library and find_path) find_package(FFMPEG COMPONENTS avdevice)
find_library(AVDEVICE_LIBRARIES avdevice) if(FFMPEG_avdevice_FOUND)
if(AVDEVICE_LIBRARIES)
set(HAVE_LIBAVDEVICE 1) set(HAVE_LIBAVDEVICE 1)
list(APPEND ZM_BIN_LIBS "${AVDEVICE_LIBRARIES}") set(HAVE_LIBAVDEVICE_AVDEVICE_H 1)
find_path(AVDEVICE_INCLUDE_DIR "libavdevice/avdevice.h" /usr/include/ffmpeg) list(APPEND ZM_BIN_LIBS "${FFMPEG_avdevice_LIBRARIES}")
if(AVDEVICE_INCLUDE_DIR)
include_directories("${AVDEVICE_INCLUDE_DIR}")
set(CMAKE_REQUIRED_INCLUDES "${AVDEVICE_INCLUDE_DIR}")
endif()
mark_as_advanced(FORCE AVDEVICE_LIBRARIES AVDEVICE_INCLUDE_DIR)
check_include_file("libavdevice/avdevice.h" HAVE_LIBAVDEVICE_AVDEVICE_H)
set(optlibsfound "${optlibsfound} AVDevice") set(optlibsfound "${optlibsfound} AVDevice")
else() else()
set(optlibsnotfound "${optlibsnotfound} AVDevice") set(optlibsnotfound "${optlibsnotfound} AVDevice")
endif() endif()
# avutil (using find_library and find_path) find_package(FFMPEG COMPONENTS avutil)
find_library(AVUTIL_LIBRARIES avutil) if(FFMPEG_avutil_FOUND)
if(AVUTIL_LIBRARIES)
set(HAVE_LIBAVUTIL 1) set(HAVE_LIBAVUTIL 1)
list(APPEND ZM_BIN_LIBS "${AVUTIL_LIBRARIES}") set(HAVE_LIBAVUTIL_AVUTIL_H 1)
find_path(AVUTIL_INCLUDE_DIR "libavutil/avutil.h" /usr/include/ffmpeg) set(CMAKE_REQUIRED_INCLUDES ${FFMPEG_avutil_INCLUDE_DIRS})
if(AVUTIL_INCLUDE_DIR)
include_directories("${AVUTIL_INCLUDE_DIR}")
set(CMAKE_REQUIRED_INCLUDES "${AVUTIL_INCLUDE_DIR}")
endif()
mark_as_advanced(FORCE AVUTIL_LIBRARIES AVUTIL_INCLUDE_DIR)
check_include_file("libavutil/avutil.h" HAVE_LIBAVUTIL_AVUTIL_H)
check_include_file("libavutil/mathematics.h" HAVE_LIBAVUTIL_MATHEMATICS_H) check_include_file("libavutil/mathematics.h" HAVE_LIBAVUTIL_MATHEMATICS_H)
check_include_file("libavutil/hwcontext.h" HAVE_LIBAVUTIL_HWCONTEXT_H) check_include_file("libavutil/hwcontext.h" HAVE_LIBAVUTIL_HWCONTEXT_H)
list(APPEND ZM_BIN_LIBS "${FFMPEG_avutil_LIBRARIES}")
set(optlibsfound "${optlibsfound} AVUtil") set(optlibsfound "${optlibsfound} AVUtil")
else() else()
set(optlibsnotfound "${optlibsnotfound} AVUtil") set(optlibsnotfound "${optlibsnotfound} AVUtil")
endif() endif()
# swscale (using find_library and find_path) find_package(FFMPEG COMPONENTS swscale)
find_library(SWSCALE_LIBRARIES swscale) if(FFMPEG_swscale_FOUND)
if(SWSCALE_LIBRARIES)
set(HAVE_LIBSWSCALE 1) set(HAVE_LIBSWSCALE 1)
list(APPEND ZM_BIN_LIBS "${SWSCALE_LIBRARIES}") set(HAVE_LIBSWSCALE_SWSCALE_H 1)
find_path(SWSCALE_INCLUDE_DIR "libswscale/swscale.h" /usr/include/ffmpeg) list(APPEND ZM_BIN_LIBS "${FFMPEG_swscale_LIBRARIES}")
if(SWSCALE_INCLUDE_DIR)
include_directories("${SWSCALE_INCLUDE_DIR}")
set(CMAKE_REQUIRED_INCLUDES "${SWSCALE_INCLUDE_DIR}")
endif()
mark_as_advanced(FORCE SWSCALE_LIBRARIES SWSCALE_INCLUDE_DIR)
check_include_file("libswscale/swscale.h" HAVE_LIBSWSCALE_SWSCALE_H)
set(optlibsfound "${optlibsfound} SWScale") set(optlibsfound "${optlibsfound} SWScale")
else() else()
set(optlibsnotfound "${optlibsnotfound} SWScale") set(optlibsnotfound "${optlibsnotfound} SWScale")
endif() endif()
# SWresample (using find_library and find_path) find_package(FFMPEG COMPONENTS swresample)
find_library(SWRESAMPLE_LIBRARIES swresample) if(FFMPEG_swresample_FOUND)
if(SWRESAMPLE_LIBRARIES)
set(HAVE_LIBSWRESAMPLE 1) set(HAVE_LIBSWRESAMPLE 1)
list(APPEND ZM_BIN_LIBS "${SWRESAMPLE_LIBRARIES}") set(HAVE_LIBSWRESAMPLE_SWRESAMPLE_H 1)
find_path(SWRESAMPLE_INCLUDE_DIR "libswresample/swresample.h" /usr/include/ffmpeg) list(APPEND ZM_BIN_LIBS "${FFMPEG_swresample_LIBRARIES}")
if(SWRESAMPLE_INCLUDE_DIR)
include_directories("${SWRESAMPLE_INCLUDE_DIR}")
set(CMAKE_REQUIRED_INCLUDES "${SWRESAMPLE_INCLUDE_DIR}")
endif()
mark_as_advanced(FORCE SWRESAMPLE_LIBRARIES SWRESAMPLE_INCLUDE_DIR)
check_include_file("libswresample/swresample.h" HAVE_LIBSWRESAMPLE_SWRESAMPLE_H)
set(optlibsfound "${optlibsfound} SWResample") set(optlibsfound "${optlibsfound} SWResample")
else() else()
set(optlibsnotfound "${optlibsnotfound} SWResample") set(optlibsnotfound "${optlibsnotfound} SWResample")
# AVresample (using find_library and find_path) find_package(FFMPEG COMPONENTS avresample)
find_library(AVRESAMPLE_LIBRARIES avresample) if(FFMPEG_avresample_FOUND)
if(AVRESAMPLE_LIBRARIES)
set(HAVE_LIBAVRESAMPLE 1) set(HAVE_LIBAVRESAMPLE 1)
list(APPEND ZM_BIN_LIBS "${AVRESAMPLE_LIBRARIES}") set(HAVE_LIBAVRESAMPLE_AVRESAMPLE_H 1)
find_path(AVRESAMPLE_INCLUDE_DIR "libavresample/avresample.h" /usr/include/ffmpeg) list(APPEND ZM_BIN_LIBS "${FFMPEG_avresample_LIBRARIES}")
if(AVRESAMPLE_INCLUDE_DIR)
include_directories("${AVRESAMPLE_INCLUDE_DIR}")
set(CMAKE_REQUIRED_INCLUDES "${AVRESAMPLE_INCLUDE_DIR}")
endif()
mark_as_advanced(FORCE AVRESAMPLE_LIBRARIES AVRESAMPLE_INCLUDE_DIR)
check_include_file("libavresample/avresample.h" HAVE_LIBAVRESAMPLE_AVRESAMPLE_H)
set(optlibsfound "${optlibsfound} AVResample") set(optlibsfound "${optlibsfound} AVResample")
else() else()
set(optlibsnotfound "${optlibsnotfound} AVResample") set(optlibsnotfound "${optlibsnotfound} AVResample")
endif() endif()
endif() endif()
set(PATH_FFMPEG "")
set(OPT_FFMPEG "no")
# Find the path to the ffmpeg executable # Find the path to the ffmpeg executable
find_program(FFMPEG_EXECUTABLE find_program(FFMPEG_EXECUTABLE
NAMES ffmpeg avconv NAMES ffmpeg avconv

View File

@ -0,0 +1,166 @@
#[=======================================================================[.rst:
FindFFMPEG
----------
Find the FFmpeg and associated libraries.
This module accepts following COMPONENTS::
avcodec
avdevice
avfilter
avformat
avutil
swresample
swscale
avresample
IMPORTED Targets
^^^^^^^^^^^^^^^^
This module defines the following :prop_tgt:`IMPORTED` targets:
``FFMPEG::<component>``
The FFmpeg component.
Result Variables
^^^^^^^^^^^^^^^^
``FFMPEG_INCLUDE_DIRS``
Include directories necessary to use FFmpeg.
``FFMPEG_LIBRARIES``
Libraries necessary to use FFmpeg. Note that this only includes libraries for the components requested.
``FFMPEG_VERSION``
The version of FFMPEG found (avutil).
For each component, the following are provided:
``FFMPEG_<component>_FOUND``
FFmpeg component was found.
``FFMPEG_<component>_INCLUDE_DIRS``
Include directories for the component.
``FFMPEG_<component>_LIBRARIES``
Libraries for the component.
#]=======================================================================]
function(_ffmpeg_find component pkgconfig_name header)
find_package(PkgConfig)
pkg_check_modules(PC_FFMPEG_${component} ${pkgconfig_name})
find_path(FFMPEG_${component}_INCLUDE_DIR
NAMES "lib${component}/${header}"
HINTS
${PC_FFMPEG_${component}_INCLUDEDIR}
${PC_FFMPEG_${component}_INCLUDE_DIRS}
PATH_SUFFIXES
ffmpeg)
mark_as_advanced("FFMPEG_${component}_INCLUDE_DIR")
find_library(FFMPEG_${component}_LIBRARY
NAMES
${component}
${PC_FFMPEG_${component}_LIBRARIES}
HINTS
${PC_FFMPEG_${component}_LIBDIR}
${PC_FFMPEG_${component}_LIBRARY_DIRS})
mark_as_advanced("${component}_LIBRARY")
if(FFMPEG_${component}_LIBRARY AND FFMPEG_${component}_INCLUDE_DIR)
set(_deps_found TRUE)
set(_deps_link)
foreach(_ffmpeg_dep IN LISTS ARGN)
if(TARGET "FFMPEG::${_ffmpeg_dep}")
list(APPEND _deps_link "FFMPEG::${_ffmpeg_dep}")
else()
set(_deps_found FALSE)
endif()
endforeach()
if(_deps_found)
if(NOT TARGET "FFMPEG::${component}")
add_library("FFMPEG::${component}" UNKNOWN IMPORTED)
set_target_properties("FFMPEG::${component}" PROPERTIES
IMPORTED_LOCATION "${FFMPEG_${component}_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${FFMPEG_${component}_INCLUDE_DIR}"
IMPORTED_LINK_INTERFACE_LIBRARIES "${_deps_link}")
endif()
set(FFMPEG_${component}_FOUND 1 PARENT_SCOPE)
set(FFMPEG_${component}_VERSION "${PC_FFMPEG_${component}_VERSION}" PARENT_SCOPE)
else()
set("FFMPEG_${component}_FOUND" 0 PARENT_SCOPE)
set(what)
if(NOT FFMPEG_${component}_LIBRARY)
set(what "library")
endif()
if(NOT FFMPEG_${component}_INCLUDE_DIR)
if(what)
string(APPEND what " or headers")
else()
set(what "headers")
endif()
endif()
set("FFMPEG_${component}_NOT_FOUND_MESSAGE"
"Could not find the ${what} for ${component}."
PARENT_SCOPE)
endif()
endif()
endfunction()
_ffmpeg_find(avutil libavutil avutil.h)
_ffmpeg_find(swresample libswresample swresample.h
avutil)
_ffmpeg_find(swscale libswscale swscale.h
avutil)
_ffmpeg_find(avcodec libavcodec avcodec.h
avutil)
_ffmpeg_find(avformat libavformat avformat.h
avcodec avutil)
_ffmpeg_find(avfilter libavfilter avfilter.h
avutil)
_ffmpeg_find(avdevice libavdevice avdevice.h
avformat avutil)
_ffmpeg_find(avresample libavresample avresample.h
avutil)
if(TARGET FFMPEG::avutil)
set(FFMPEG_VERSION "${FFMPEG_avutil_VERSION}")
endif()
set(FFMPEG_INCLUDE_DIRS)
set(FFMPEG_LIBRARIES)
set(_ffmpeg_required_vars)
foreach(_ffmpeg_component IN LISTS FFMPEG_FIND_COMPONENTS)
if(TARGET "FFMPEG::${_ffmpeg_component}")
set(FFMPEG_${_ffmpeg_component}_INCLUDE_DIRS
"${FFMPEG_${_ffmpeg_component}_INCLUDE_DIR}")
set(FFMPEG_${_ffmpeg_component}_LIBRARIES
"${FFMPEG_${_ffmpeg_component}_LIBRARY}")
list(APPEND FFMPEG_INCLUDE_DIRS
"${FFMPEG_${_ffmpeg_component}_INCLUDE_DIRS}")
list(APPEND FFMPEG_LIBRARIES
"${FFMPEG_${_ffmpeg_component}_LIBRARIES}")
if(FFMEG_FIND_REQUIRED_${_ffmpeg_component})
list(APPEND _ffmpeg_required_vars
"FFMPEG_${_ffmpeg_required_vars}_INCLUDE_DIRS"
"FFMPEG_${_ffmpeg_required_vars}_LIBRARIES")
endif()
endif()
endforeach()
unset(_ffmpeg_component)
if(FFMPEG_INCLUDE_DIRS)
list(REMOVE_DUPLICATES FFMPEG_INCLUDE_DIRS)
endif()
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(FFMPEG
REQUIRED_VARS
FFMPEG_INCLUDE_DIRS
FFMPEG_LIBRARIES
${_ffmpeg_required_vars}
VERSION_VAR
FFMPEG_VERSION
HANDLE_COMPONENTS)
unset(_ffmpeg_required_vars)

View File

@ -23,7 +23,7 @@ Result Variables
``LIBJWT_FOUND`` ``LIBJWT_FOUND``
System has libjwt System has libjwt
``LIBJWT_INCLUDE_DIR`` ``LIBJWT_INCLUDE_DIRS``
The libjwt include directory The libjwt include directory
``LIBJWT_LIBRARIES`` ``LIBJWT_LIBRARIES``
The libraries needed to use libjwt The libraries needed to use libjwt

View File

@ -80,6 +80,7 @@ target_link_libraries(zm
libbcrypt::bcrypt libbcrypt::bcrypt
RtspServer::RtspServer RtspServer::RtspServer
martinmoene::span-lite martinmoene::span-lite
${ZM_BIN_LIBS}
PRIVATE PRIVATE
zm-core-interface) zm-core-interface)
@ -102,7 +103,6 @@ target_link_libraries(zmc
zm-core-interface zm-core-interface
zm zm
${ZM_EXTRA_LIBS} ${ZM_EXTRA_LIBS}
${ZM_BIN_LIBS}
${CMAKE_DL_LIBS}) ${CMAKE_DL_LIBS})
target_link_libraries(zms target_link_libraries(zms
@ -110,7 +110,6 @@ target_link_libraries(zms
zm-core-interface zm-core-interface
zm zm
${ZM_EXTRA_LIBS} ${ZM_EXTRA_LIBS}
${ZM_BIN_LIBS}
${CMAKE_DL_LIBS}) ${CMAKE_DL_LIBS})
target_link_libraries(zmu target_link_libraries(zmu
@ -118,9 +117,7 @@ target_link_libraries(zmu
zm-core-interface zm-core-interface
zm zm
${ZM_EXTRA_LIBS} ${ZM_EXTRA_LIBS}
${ZM_BIN_LIBS} ${CMAKE_DL_LIBS})
${CMAKE_DL_LIBS}
bcrypt)
# Generate man files for the binaries destined for the bin folder # Generate man files for the binaries destined for the bin folder
if(BUILD_MAN) if(BUILD_MAN)
@ -141,8 +138,6 @@ if(HAVE_RTSP_SERVER)
zm-core-interface zm-core-interface
zm zm
${ZM_EXTRA_LIBS} ${ZM_EXTRA_LIBS}
${ZM_BIN_LIBS} ${CMAKE_DL_LIBS})
${CMAKE_DL_LIBS}
bcrypt)
install(TARGETS zm_rtsp_server RUNTIME DESTINATION "${CMAKE_INSTALL_FULL_BINDIR}" PERMISSIONS OWNER_WRITE OWNER_READ OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) install(TARGETS zm_rtsp_server RUNTIME DESTINATION "${CMAKE_INSTALL_FULL_BINDIR}" PERMISSIONS OWNER_WRITE OWNER_READ OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
endif() endif()