Skip to content

Commit 5135833

Browse files
change liblivekit to dll and link protobuf statically (#37)
* change liblivekit to dll and link protobuf statically * a try to fix the windows build * another try to fix the windows build * fix the windows problem that we have two protobuf versions * fix the cannot open input file '..\lib\livekit.lib'
1 parent c2fa0c9 commit 5135833

File tree

7 files changed

+244
-170
lines changed

7 files changed

+244
-170
lines changed

.github/workflows/builds.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ on:
3232
env:
3333
CARGO_TERM_COLOR: always
3434
# vcpkg binary caching for Windows
35-
VCPKG_BINARY_SOURCES: "clear;x-gha,readwrite"
35+
VCPKG_DEFAULT_TRIPLET: x64-windows-static-md
36+
VCPKG_DEFAULT_HOST_TRIPLET: x64-windows-static-md
37+
VCPKG_TARGET_TRIPLET: x64-windows-static-md
3638

3739
jobs:
3840
build:

.github/workflows/make-release.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ on:
1414
env:
1515
CARGO_TERM_COLOR: always
1616
VCPKG_BINARY_SOURCES: "clear;x-gha,readwrite"
17+
VCPKG_DEFAULT_TRIPLET: x64-windows-static-md
18+
VCPKG_DEFAULT_HOST_TRIPLET: x64-windows-static-md
19+
VCPKG_TARGET_TRIPLET: x64-windows-static-md
1720

1821
jobs:
1922
build:

CMakeLists.txt

Lines changed: 35 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,20 @@ project(livekit VERSION ${LIVEKIT_VERSION} LANGUAGES C CXX)
88

99
set(LIVEKIT_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR})
1010
set(LIVEKIT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
11+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
1112

1213
option(LIVEKIT_BUILD_EXAMPLES "Build LiveKit examples" OFF)
1314

1415
# vcpkg is only used on Windows; Linux/macOS use system package managers
1516
if(WIN32)
1617
option(LIVEKIT_USE_VCPKG "Use vcpkg for dependency management (Windows only)" ON)
18+
set(LIVEKIT_USE_SYSTEM_PROTOBUF ON CACHE BOOL "Use system protobuf (vcpkg) on Windows" FORCE)
19+
# Force /MD or /MDd for ALL targets (including FetchContent subprojects)
20+
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL" CACHE STRING "" FORCE)
21+
add_compile_options(/utf-8)
1722
else()
1823
option(LIVEKIT_USE_VCPKG "Use vcpkg for dependency management" OFF)
24+
set(LIVEKIT_USE_SYSTEM_PROTOBUF OFF CACHE BOOL "Use vendored protobuf on non-Windows" FORCE)
1925
endif()
2026

2127
set(CMAKE_CXX_STANDARD 17)
@@ -58,15 +64,6 @@ if(LIVEKIT_IS_TOPLEVEL)
5864
endforeach()
5965
endif()
6066

61-
62-
if(MSVC)
63-
# Use dynamic CRT (/MD) for compatibility with Qt and other /MD libraries.
64-
# The livekit_ffi.dll isolates the /MT libwebrtc dependency, so livekit.lib
65-
# can safely use /MD.
66-
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
67-
add_compile_options(/utf-8)
68-
endif()
69-
7067
set(FFI_PROTO_DIR ${LIVEKIT_ROOT_DIR}/client-sdk-rust/livekit-ffi/protocol)
7168
set(FFI_PROTO_FILES
7269
${FFI_PROTO_DIR}/handle.proto
@@ -85,24 +82,33 @@ set(FFI_PROTO_FILES
8582
set(PROTO_BINARY_DIR ${LIVEKIT_BINARY_DIR}/generated)
8683
file(MAKE_DIRECTORY ${PROTO_BINARY_DIR})
8784

88-
# Try to find Protobuf via CONFIG mode first (vcpkg), then fall back to MODULE mode (apt/brew)
89-
find_package(Protobuf CONFIG QUIET)
90-
if(NOT Protobuf_FOUND)
91-
find_package(Protobuf REQUIRED)
92-
endif()
93-
94-
# Ensure protoc executable is found (some systems may not set Protobuf_PROTOC_EXECUTABLE)
95-
if(NOT Protobuf_PROTOC_EXECUTABLE)
85+
# Livekit static protobuf.
86+
include(protobuf)
87+
# Ensure protoc executable is found.
88+
if(TARGET protobuf::protoc)
89+
set(Protobuf_PROTOC_EXECUTABLE "$<TARGET_FILE:protobuf::protoc>")
90+
elseif(NOT Protobuf_PROTOC_EXECUTABLE)
9691
find_program(Protobuf_PROTOC_EXECUTABLE NAMES protoc REQUIRED)
9792
endif()
9893
message(STATUS "Using protoc: ${Protobuf_PROTOC_EXECUTABLE}")
9994

10095
add_library(livekit_proto OBJECT ${FFI_PROTO_FILES})
96+
if(TARGET protobuf::libprotobuf)
97+
set(LIVEKIT_PROTOBUF_TARGET protobuf::libprotobuf)
98+
else()
99+
message(FATAL_ERROR "No protobuf library target found (expected protobuf::libprotobuf)")
100+
endif()
101101
target_include_directories(livekit_proto PRIVATE
102-
"$<BUILD_INTERFACE:${PROTO_BINARY_DIR}>"
102+
"${PROTO_BINARY_DIR}"
103103
${Protobuf_INCLUDE_DIRS}
104104
)
105-
target_link_libraries(livekit_proto PRIVATE protobuf::libprotobuf)
105+
target_link_libraries(livekit_proto PRIVATE ${LIVEKIT_PROTOBUF_TARGET})
106+
if(TARGET absl::base)
107+
get_target_property(_absl_inc absl::base INTERFACE_INCLUDE_DIRECTORIES)
108+
if(_absl_inc)
109+
target_include_directories(livekit_proto PRIVATE ${_absl_inc})
110+
endif()
111+
endif()
106112

107113
# Manually generate protobuf files to avoid path prefix issues
108114
set(PROTO_SRCS)
@@ -268,7 +274,7 @@ add_custom_target(build_rust_ffi
268274

269275
# Note: protozero_plugin.o removal is no longer needed since we use dynamic libraries on Unix
270276

271-
add_library(livekit STATIC
277+
add_library(livekit SHARED
272278
src/audio_frame.cpp
273279
src/audio_source.cpp
274280
src/audio_stream.cpp
@@ -301,6 +307,10 @@ add_library(livekit STATIC
301307
src/video_utils.cpp
302308
src/video_utils.h
303309
)
310+
if(WIN32)
311+
set_target_properties(livekit PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON)
312+
endif()
313+
304314

305315
target_sources(livekit PRIVATE $<TARGET_OBJECTS:livekit_proto>)
306316

@@ -318,8 +328,7 @@ target_include_directories(livekit
318328
target_link_libraries(livekit
319329
PRIVATE
320330
livekit_ffi
321-
PUBLIC
322-
protobuf::libprotobuf
331+
${LIVEKIT_PROTOBUF_TARGET}
323332
)
324333

325334
message(STATUS "Protobuf: version=${Protobuf_VERSION}; protoc=${Protobuf_PROTOC_EXECUTABLE}")
@@ -386,60 +395,6 @@ if(LIVEKIT_IS_TOPLEVEL)
386395
COMMENT "Copying liblivekit_ffi.so to output directory"
387396
)
388397
endif()
389-
390-
# Copy third-party DLL and LIB dependencies to lib directory (Windows only)
391-
if(WIN32 AND LIVEKIT_USE_VCPKG)
392-
# Determine vcpkg directories based on configuration
393-
set(VCPKG_BIN_DIR_DEBUG "${LIVEKIT_ROOT_DIR}/vcpkg_installed/x64-windows/debug/bin")
394-
set(VCPKG_BIN_DIR_RELEASE "${LIVEKIT_ROOT_DIR}/vcpkg_installed/x64-windows/bin")
395-
set(VCPKG_LIB_DIR_DEBUG "${LIVEKIT_ROOT_DIR}/vcpkg_installed/x64-windows/debug/lib")
396-
set(VCPKG_LIB_DIR_RELEASE "${LIVEKIT_ROOT_DIR}/vcpkg_installed/x64-windows/lib")
397-
398-
# vcpkg uses 'd' suffix for debug builds (e.g., libprotobufd.dll)
399-
# We need to copy with the correct source name but keep the output name consistent
400-
401-
# abseil_dll.dll (same name for both debug and release)
402-
add_custom_command(
403-
TARGET livekit POST_BUILD
404-
COMMAND ${CMAKE_COMMAND} -E copy_if_different
405-
"$<IF:$<CONFIG:Debug>,${VCPKG_BIN_DIR_DEBUG}/abseil_dll.dll,${VCPKG_BIN_DIR_RELEASE}/abseil_dll.dll>"
406-
"$<TARGET_FILE_DIR:livekit>/abseil_dll.dll"
407-
COMMENT "Copying abseil_dll.dll to lib directory"
408-
VERBATIM
409-
)
410-
411-
# libprotobuf.dll - debug version has 'd' suffix
412-
add_custom_command(
413-
TARGET livekit POST_BUILD
414-
COMMAND ${CMAKE_COMMAND} -E copy_if_different
415-
"$<IF:$<CONFIG:Debug>,${VCPKG_BIN_DIR_DEBUG}/libprotobufd.dll,${VCPKG_BIN_DIR_RELEASE}/libprotobuf.dll>"
416-
"$<TARGET_FILE_DIR:livekit>/$<IF:$<CONFIG:Debug>,libprotobufd.dll,libprotobuf.dll>"
417-
COMMENT "Copying libprotobuf DLL to lib directory"
418-
VERBATIM
419-
)
420-
421-
# abseil_dll.lib (same name for both debug and release)
422-
add_custom_command(
423-
TARGET livekit POST_BUILD
424-
COMMAND ${CMAKE_COMMAND} -E copy_if_different
425-
"$<IF:$<CONFIG:Debug>,${VCPKG_LIB_DIR_DEBUG}/abseil_dll.lib,${VCPKG_LIB_DIR_RELEASE}/abseil_dll.lib>"
426-
"$<TARGET_FILE_DIR:livekit>/abseil_dll.lib"
427-
COMMENT "Copying abseil_dll.lib to lib directory"
428-
VERBATIM
429-
)
430-
431-
# libprotobuf.lib - debug version has 'd' suffix
432-
add_custom_command(
433-
TARGET livekit POST_BUILD
434-
COMMAND ${CMAKE_COMMAND} -E copy_if_different
435-
"$<IF:$<CONFIG:Debug>,${VCPKG_LIB_DIR_DEBUG}/libprotobufd.lib,${VCPKG_LIB_DIR_RELEASE}/libprotobuf.lib>"
436-
"$<TARGET_FILE_DIR:livekit>/$<IF:$<CONFIG:Debug>,libprotobufd.lib,libprotobuf.lib>"
437-
COMMENT "Copying libprotobuf LIB to lib directory"
438-
VERBATIM
439-
)
440-
441-
message(STATUS "Third-party dependencies will be copied to lib directory")
442-
endif()
443398
endif()
444399

445400
if(APPLE)
@@ -465,7 +420,7 @@ if(APPLE)
465420
find_library(FW_METALKIT MetalKit REQUIRED)
466421
find_library(FW_SCREENCAPTUREKIT ScreenCaptureKit REQUIRED)
467422

468-
target_link_libraries(livekit PUBLIC
423+
target_link_libraries(livekit PRIVATE
469424
${FW_COREAUDIO}
470425
${FW_AUDIOTOOLBOX}
471426
${FW_COREFOUNDATION}
@@ -485,40 +440,11 @@ if(APPLE)
485440
${FW_SCREENCAPTUREKIT}
486441
)
487442

488-
target_link_options(livekit INTERFACE "LINKER:-ObjC")
489-
endif()
490-
491-
if(Protobuf_VERSION VERSION_GREATER_EQUAL 6.0)
492-
find_package(absl CONFIG QUIET)
493-
if(NOT absl_FOUND)
494-
find_package(Abseil QUIET)
495-
endif()
496-
497-
if(absl_FOUND)
498-
target_link_libraries(livekit PUBLIC
499-
absl::log
500-
absl::check
501-
absl::strings
502-
absl::base
503-
)
504-
elseif(Abseil_FOUND)
505-
target_link_libraries(livekit PUBLIC
506-
Abseil::log
507-
Abseil::check
508-
Abseil::strings
509-
Abseil::base
510-
)
511-
else()
512-
message(FATAL_ERROR
513-
"Protobuf ${Protobuf_VERSION} requires Abseil but no CMake package was found.\n"
514-
"Install Abseil or use Protobuf < 6.")
515-
endif()
516-
else()
517-
message(STATUS "Protobuf < 6 detected; skipping Abseil linking.")
443+
target_link_options(livekit PRIVATE "LINKER:-ObjC")
518444
endif()
519445

520446
if(WIN32)
521-
target_link_libraries(livekit PUBLIC
447+
target_link_libraries(livekit PRIVATE
522448
ntdll
523449
userenv
524450
winmm
@@ -535,7 +461,7 @@ endif()
535461

536462
if(UNIX AND NOT APPLE)
537463
find_package(OpenSSL REQUIRED)
538-
target_link_libraries(livekit PUBLIC OpenSSL::SSL OpenSSL::Crypto)
464+
target_link_libraries(livekit PRIVATE OpenSSL::SSL OpenSSL::Crypto)
539465
endif()
540466

541467
if(MSVC)
@@ -582,42 +508,6 @@ if(WIN32)
582508
CONFIGURATIONS Debug
583509
)
584510
endif()
585-
586-
# Protobuf
587-
if(TARGET protobuf::libprotobuf)
588-
install(IMPORTED_RUNTIME_ARTIFACTS protobuf::libprotobuf
589-
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
590-
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
591-
)
592-
install(FILES
593-
$<TARGET_PROPERTY:protobuf::libprotobuf,IMPORTED_IMPLIB_RELEASE>
594-
DESTINATION ${CMAKE_INSTALL_LIBDIR}
595-
CONFIGURATIONS Release RelWithDebInfo MinSizeRel
596-
)
597-
install(FILES
598-
$<TARGET_PROPERTY:protobuf::libprotobuf,IMPORTED_IMPLIB_DEBUG>
599-
DESTINATION ${CMAKE_INSTALL_LIBDIR}
600-
CONFIGURATIONS Debug
601-
)
602-
endif()
603-
604-
# Abseil
605-
if(TARGET absl::abseil_dll)
606-
install(IMPORTED_RUNTIME_ARTIFACTS absl::abseil_dll
607-
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
608-
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
609-
)
610-
install(FILES
611-
$<TARGET_PROPERTY:absl::abseil_dll,IMPORTED_IMPLIB_RELEASE>
612-
DESTINATION ${CMAKE_INSTALL_LIBDIR}
613-
CONFIGURATIONS Release RelWithDebInfo MinSizeRel
614-
)
615-
install(FILES
616-
$<TARGET_PROPERTY:absl::abseil_dll,IMPORTED_IMPLIB_DEBUG>
617-
DESTINATION ${CMAKE_INSTALL_LIBDIR}
618-
CONFIGURATIONS Debug
619-
)
620-
endif()
621511
endif()
622512

623513
# Install public headers

CMakePresets.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
"value": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
1616
},
1717
"VCPKG_INSTALLED_DIR": "${sourceDir}/vcpkg_installed",
18-
"LIVEKIT_USE_VCPKG": "ON"
18+
"LIVEKIT_USE_VCPKG": "ON",
19+
"VCPKG_TARGET_TRIPLET": "x64-windows-static-md",
20+
"VCPKG_HOST_TRIPLET": "x64-windows-static-md"
1921
}
2022
},
2123
{
@@ -26,6 +28,9 @@
2628
"architecture": {
2729
"value": "x64",
2830
"strategy": "set"
31+
},
32+
"cacheVariables": {
33+
"VCPKG_TARGET_TRIPLET": "x64-windows-static-md"
2934
}
3035
},
3136
{

cmake/LiveKitConfig.cmake.in

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
11
@PACKAGE_INIT@
22

3-
include(CMakeFindDependencyMacro)
4-
5-
find_dependency(Protobuf CONFIG REQUIRED)
6-
find_dependency(absl CONFIG QUIET)
7-
if(NOT absl_FOUND)
8-
find_dependency(Abseil REQUIRED)
9-
endif()
10-
113
include("${CMAKE_CURRENT_LIST_DIR}/LiveKitTargets.cmake")
124

0 commit comments

Comments
 (0)