Skip to content

Commit

Permalink
fix static linking - generates huge static blob
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Oct 18, 2024
1 parent f12ce9d commit b63c817
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 37 deletions.
3 changes: 2 additions & 1 deletion cmake/FindOpus.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ endif()

if(OPUS_LIBRARIES)
if(OPUS_USE_STATIC_LIBS)
find_library(LIBM NAMES "libm.a" "libm.tbd")
# on linux with glibc you CANT statically link libm without statically linking all of glibc. DONT DO IT.
#find_library(LIBM NAMES "libm.a" "libm.tbd")
else()
find_library(LIBM NAMES m)
endif()
Expand Down
96 changes: 60 additions & 36 deletions library/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,27 @@ STRING(REPLACE "AVX" "" AVX_TYPE ${AVX_TYPE})
add_compile_definitions(AVX_TYPE=${AVX_TYPE})
add_compile_options(${AVX_FLAG})

if(NOT BUILD_SHARED_LIBS)
if(UNIX)
message("-- Building ${Green}static${ColourReset} library.")

if(UNIX AND NOT APPLE)
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
message("-- INFO: Changed lib suffix to ${CMAKE_FIND_LIBRARY_SUFFIXES}")
endif()

set(OPENSSL_USE_STATIC_LIBS TRUE)
set(OPUS_USE_STATIC_LIBS TRUE)

else()
message(WARNING "-- Building of static library not supported on non UNIX systems.")
endif()
else()
message("-- Building ${Green}dynamic${ColourReset} library.")
endif()



if(WIN32 AND NOT MINGW)
# Fake an ssl version number to satisfy MLSPP
set(OPENSSL_VERSION "1.1.1f")
Expand Down Expand Up @@ -109,24 +130,6 @@ if (UNIX)
endif()
endif()

if(NOT BUILD_SHARED_LIBS)
if(UNIX)
message("-- Building ${Green}static${ColourReset} library.")

if(UNIX AND NOT APPLE)
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
message("-- INFO: Changed lib suffix to .a")
endif()

set(OPENSSL_USE_STATIC_LIBS ON)
set(OPUS_USE_STATIC_LIBS TRUE)
else()
message(WARNING "-- Building of static library not supported on non UNIX systems.")
endif()
else()
message("-- Building ${Green}dynamic${ColourReset} library.")
endif()

if (BUILD_VOICE_SUPPORT)
if (MINGW OR NOT WIN32)
include("${CMAKE_CURRENT_SOURCE_DIR}/../cmake/FindOpus.cmake")
Expand Down Expand Up @@ -154,6 +157,7 @@ set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
if(MINGW OR NOT WIN32)
find_package(ZLIB REQUIRED)
message("-- ZLIB: ${Green}${ZLIB_LIBRARIES}${ColourReset}")
endif(MINGW OR NOT WIN32)

if(APPLE)
Expand All @@ -165,6 +169,7 @@ if(APPLE)
find_package(OpenSSL REQUIRED)
else()
if(MINGW OR NOT WIN32)
set(OPENSSL_USE_STATIC_LIBS TRUE)
find_package(OpenSSL REQUIRED)
endif()
endif()
Expand Down Expand Up @@ -278,30 +283,33 @@ foreach (fullmodname ${subdirlist})
endif()

if (HAVE_VOICE)
# Private statically linked dependencies
if(NOT BUILD_SHARED_LIBS AND NOT APPLE)
target_link_libraries(${modname} PRIVATE mlspp.a
target_link_libraries(${modname} PUBLIC ${OPUS_LIBRARIES})
include_directories(${OPUS_INCLUDE_DIRS})
endif()
endforeach()

if (HAVE_VOICE)
# Private statically linked dependencies
if(NOT BUILD_SHARED_LIBS AND NOT APPLE)
target_link_libraries(dpp PRIVATE
mlspp.a
mls_vectors.a
bytes.a
tls_syntax.a
hpke.a
)
else()
target_link_libraries(${modname} PRIVATE
)
message("-- INFO: Linking static dependencies")
else()
target_link_libraries(dpp PRIVATE
mlspp
mls_vectors
bytes
tls_syntax
hpke
)
endif()
endif()

if (HAVE_VOICE)
target_link_libraries(${modname} PUBLIC ${OPUS_LIBRARIES})
include_directories(${OPUS_INCLUDE_DIRS})
)
message("-- INFO: Linking dynamic dependencies")
endif()
endforeach()
endif()

target_compile_features(dpp PUBLIC cxx_std_17)
target_compile_features(dpp PRIVATE cxx_constexpr)
Expand Down Expand Up @@ -379,6 +387,22 @@ if(DPP_FORMATTERS)
target_compile_definitions(dpp PUBLIC DPP_FORMATTERS)
endif()

if (NOT BUILD_SHARED_LIBS)
add_library(dppstatic STATIC
$<TARGET_OBJECTS:dpp>
$<TARGET_OBJECTS:hpke>
$<TARGET_OBJECTS:tls_syntax>
$<TARGET_OBJECTS:mls_vectors>
$<TARGET_OBJECTS:mlspp>
$<TARGET_OBJECTS:bytes>
)
if (HAVE_VOICE)
target_link_libraries(dppstatic ${ZLIB_LIBRARIES} ${OPENSSL_LIBRARIES} ${OPUS_LIBRARIES} -static-libgcc -static-libstdc++)
else()
target_link_libraries(dppstatic ${ZLIB_LIBRARIES} ${OPENSSL_LIBRARIES})
endif()
endif()

if (DPP_BUILD_TEST)
enable_testing(${CMAKE_CURRENT_SOURCE_DIR}/..)
file(GLOB testnamelist "${CMAKE_CURRENT_SOURCE_DIR}/../src/*")
Expand All @@ -397,11 +421,11 @@ if (DPP_BUILD_TEST)
if (MSVC)
target_compile_options(${testname} PRIVATE /utf-8)
endif()
set (static_if_needed "")
if(NOT BUILD_SHARED_LIBS)
set (static_if_needed "-static")
if(BUILD_SHARED_LIBS)
target_link_libraries(${testname} PUBLIC ${modname})
else()
target_link_libraries(${testname} PUBLIC dppstatic)
endif()
target_link_libraries(${testname} PUBLIC ${modname} ${static_if_needed})
endif()
endforeach()
add_test(
Expand Down

0 comments on commit b63c817

Please sign in to comment.