Skip to content

Commit

Permalink
Static ffmpeg build for Linux
Browse files Browse the repository at this point in the history
Previously the system version of ffmpeg was linked dynamically by accident
  • Loading branch information
MGraefe committed Apr 25, 2020
1 parent 6879295 commit 23b0cbf
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 28 deletions.
43 changes: 29 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,15 @@ else()
endif()


set(ffmpegLibHint ${defFFmpegLibHint} CACHE PATH "directory with static ffmpeg libs")
set(ffmpegLibHint "${CMAKE_CURRENT_SOURCE_DIR}/${defFFmpegLibHint}" CACHE PATH "directory with static ffmpeg libs")
set(libSuffix ${defLibSuffix} CACHE STRING "output library suffix")
set(pluginDir ${defPluginDir} CACHE PATH "TS3 plugin directory (for file copy)")


find_path(ffmpegIncludeDir libavcodec/avcodec.h HINTS ${ffmpegIncHint})
find_library(avcodec avcodec HINTS ${ffmpegLibHint})
find_library(avformat avformat HINTS ${ffmpegLibHint})
find_library(avutil avutil HINTS ${ffmpegLibHint})
find_library(swresample swresample HINTS ${ffmpegLibHint})
find_path(ffmpegIncludeDir libavcodec/avcodec.h PATHS ${ffmpegIncHint} NO_DEFAULT_PATH)
find_library(avcodec avcodec PATHS ${ffmpegLibHint} NO_DEFAULT_PATH)
find_library(avformat avformat PATHS ${ffmpegLibHint} NO_DEFAULT_PATH)
find_library(avutil avutil PATHS ${ffmpegLibHint} NO_DEFAULT_PATH)
find_library(swresample swresample PATHS ${ffmpegLibHint} NO_DEFAULT_PATH)

find_package(Qt5 COMPONENTS Core Widgets Gui Network REQUIRED)

Expand All @@ -55,6 +54,9 @@ set(CMAKE_AUTORCC ON)
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)

# Turn fPIC on
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# version advance script (generates version.h file)
add_custom_command(
OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/src/version/version.h"
Expand All @@ -67,27 +69,40 @@ set_property(SOURCE "src/version/version.h" PROPERTY SKIP_AUTOGEN ON)

# actual library definition
add_library(rp_soundboard SHARED ${sources} "src/version/version.h")
target_link_libraries(rp_soundboard
Qt5::Core Qt5::Widgets Qt5::Gui Qt5::Network
${avcodec} ${avformat} ${avutil} ${swresample}
)
target_link_libraries(rp_soundboard Qt5::Core Qt5::Widgets Qt5::Gui Qt5::Network)
if (WIN32)
# only link this way on Windows. Linux requires VERY special linker commands, see below
target_link_libraries(rp_soundboard ${avcodec} ${avformat} ${avutil} ${swresample})
endif()

target_include_directories(rp_soundboard PUBLIC "pluginsdk/include" ${ffmpegIncludeDir})
set_target_properties(rp_soundboard PROPERTIES
SUFFIX ${libSuffix}
RUNTIME_OUTPUT_DIRECTORY_DEBUG debug
RUNTIME_OUTPUT_DIRECTORY_RELEASE release
)

# Special platform dependent compile options
if (MSVC)
target_sources(rp_soundboard PRIVATE "src/windows/resource.h" "src/windows/Resource.rc")
target_link_libraries(rp_soundboard wsock32 ws2_32 secur32) # some windows stuff
target_compile_options(rp_soundboard PRIVATE /MP) # multiprocessor compiling
elseif(APPLE)
target_compile_definitions(rp_soundboard PRIVATE "MACOS")
else()
target_compile_definitions(rp_soundboard PRIVATE "LINUX")
if(APPLE)
target_compile_definitions(rp_soundboard PRIVATE "MACOS")
else()
target_compile_definitions(rp_soundboard PRIVATE "LINUX")
endif()
# Compile options that are required to NOT get the "recompile with -fPIC"
# error when linking the ffmpeg libs. Took me many hours to find this out...
set_target_properties(rp_soundboard PROPERTIES LINK_FLAGS
"-Wl,-Bsymbolic -Wl,--whole-archive \
${avcodec} ${avformat} ${avutil} ${swresample} \
-Wl,--no-whole-archive"
)
endif()


# copy to teamspeak dir command stuff
set(COPY_DLL_TO_TEAMSPEAK_DIR TRUE CACHE BOOL "Copy the soundboard DLL to teamspeaks directory")
if (COPY_DLL_TO_TEAMSPEAK_DIR)
Expand Down
1 change: 1 addition & 0 deletions ffmpeg/build-scripts/build_ffmpeg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ fi
echo "Machine =" $machine $arch

opts="\
--enable-pic \
--disable-programs \
--disable-doc \
--disable-avdevice \
Expand Down
37 changes: 23 additions & 14 deletions ffmpeg/build-scripts/copy_binaries.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,36 @@

os="$(uname -s)"
case "$os" in
Linux*) prefix="lib_lin"
postfix="a" ;;
Darwin*) prefix="lib_mac"
postfix="a" ;;
*) prefix="lib_win"
postfix="lib" ;;
Linux*)
prefix="lib_lin"
postfix="a"
infix="lib"
;;
Darwin*)
prefix="lib_mac"
postfix="a"
infix="lib"
;;
*)
prefix="lib_win"
infix=""
postfix="lib"
;;
esac

pushd ..

mkdir -p "${prefix}_x64"
mkdir -p "${prefix}_x86"

cp ffmpeg/x64/lib/libavcodec.a ${prefix}_x64/avcodec.$postfix
cp ffmpeg/x64/lib/libavformat.a ${prefix}_x64/avformat.$postfix
cp ffmpeg/x64/lib/libavutil.a ${prefix}_x64/avutil.$postfix
cp ffmpeg/x64/lib/libswresample.a ${prefix}_x64/swresample.$postfix
cp ffmpeg/x64/lib/libavcodec.a ${prefix}_x64/${infix}avcodec.$postfix
cp ffmpeg/x64/lib/libavformat.a ${prefix}_x64/${infix}avformat.$postfix
cp ffmpeg/x64/lib/libavutil.a ${prefix}_x64/${infix}avutil.$postfix
cp ffmpeg/x64/lib/libswresample.a ${prefix}_x64/${infix}swresample.$postfix
cp -R ffmpeg/x64/include ${prefix}_x64/include

cp ffmpeg/x86/lib/libavcodec.a ${prefix}_x86/avcodec.$postfix
cp ffmpeg/x86/lib/libavformat.a ${prefix}_x86/avformat.$postfix
cp ffmpeg/x86/lib/libavutil.a ${prefix}_x86/avutil.$postfix
cp ffmpeg/x86/lib/libswresample.a ${prefix}_x86/swresample.$postfix
cp ffmpeg/x86/lib/libavcodec.a ${prefix}_x86/${infix}avcodec.$postfix
cp ffmpeg/x86/lib/libavformat.a ${prefix}_x86/${infix}avformat.$postfix
cp ffmpeg/x86/lib/libavutil.a ${prefix}_x86/${infix}avutil.$postfix
cp ffmpeg/x86/lib/libswresample.a ${prefix}_x86/${infix}swresample.$postfix
cp -R ffmpeg/x86/include ${prefix}_x86/include

0 comments on commit 23b0cbf

Please sign in to comment.