|
| 1 | +# Copyright 2026 LiveKit, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +set(LIVEKIT_UNIFFI_CPP_GENERATED_DIR "${LIVEKIT_BINARY_DIR}/generated/uniffi") |
| 16 | +set(LIVEKIT_UNIFFI_CPP_SOURCE |
| 17 | + "${LIVEKIT_UNIFFI_CPP_GENERATED_DIR}/livekit_ffi.cpp") |
| 18 | +set(LIVEKIT_UNIFFI_CPP_HEADER |
| 19 | + "${LIVEKIT_UNIFFI_CPP_GENERATED_DIR}/livekit_ffi.hpp") |
| 20 | +set(LIVEKIT_UNIFFI_CPP_SCAFFOLDING_HEADER |
| 21 | + "${LIVEKIT_UNIFFI_CPP_GENERATED_DIR}/livekit_ffi_scaffolding.hpp") |
| 22 | + |
| 23 | +# Release Linux builds strip the UniFFI metadata symbols, so generate from a |
| 24 | +# separate unstripped Debug library there. PE and Mach-O retain the metadata, |
| 25 | +# allowing Windows and macOS to reuse the livekit-ffi library already built for |
| 26 | +# the selected CMake configuration. |
| 27 | +if(UNIX AND NOT APPLE) |
| 28 | + set(LIVEKIT_UNIFFI_METADATA_TARGET_DIR |
| 29 | + "${RUST_ROOT}/target/uniffi-cpp-metadata") |
| 30 | + set(LIVEKIT_UNIFFI_METADATA_LIBRARY |
| 31 | + "${LIVEKIT_UNIFFI_METADATA_TARGET_DIR}/debug/liblivekit_ffi.so") |
| 32 | +endif() |
| 33 | + |
| 34 | +file(GLOB_RECURSE LIVEKIT_UNIFFI_RUST_SOURCES CONFIGURE_DEPENDS |
| 35 | + "${RUST_ROOT}/livekit-ffi/src/*.rs" |
| 36 | + "${RUST_ROOT}/livekit-ffi/protocol/*.proto" |
| 37 | + "${RUST_ROOT}/livekit-ffi/build.rs" |
| 38 | + "${RUST_ROOT}/livekit-ffi/Cargo.toml" |
| 39 | +) |
| 40 | +list(APPEND LIVEKIT_UNIFFI_RUST_SOURCES |
| 41 | + "${RUST_ROOT}/Cargo.toml" |
| 42 | + "${RUST_ROOT}/Cargo.lock" |
| 43 | + "${RUST_ROOT}/rust-toolchain.toml" |
| 44 | +) |
| 45 | + |
| 46 | +file(GLOB_RECURSE LIVEKIT_UNIFFI_BINDGEN_SOURCES CONFIGURE_DEPENDS |
| 47 | + "${RUST_ROOT}/tools/bindgens/src/*.rs" |
| 48 | + "${RUST_ROOT}/tools/bindgens/Cargo.toml" |
| 49 | +) |
| 50 | + |
| 51 | +if(UNIX AND NOT APPLE) |
| 52 | + add_custom_command( |
| 53 | + OUTPUT "${LIVEKIT_UNIFFI_METADATA_LIBRARY}" |
| 54 | + COMMAND "${CMAKE_COMMAND}" |
| 55 | + -DCFG=Debug |
| 56 | + -DRUST_ROOT=${RUST_ROOT} |
| 57 | + -DCARGO=${CARGO_EXECUTABLE} |
| 58 | + -DPROTOC_PATH=${Protobuf_PROTOC_EXECUTABLE} |
| 59 | + -DGCC_LIB_DIR=${GCC_LIB_DIR} |
| 60 | + -DCARGO_TARGET_DIR=${LIVEKIT_UNIFFI_METADATA_TARGET_DIR} |
| 61 | + -P "${RUN_CARGO_SCRIPT}" |
| 62 | + WORKING_DIRECTORY "${RUST_ROOT}" |
| 63 | + DEPENDS ${LIVEKIT_UNIFFI_RUST_SOURCES} |
| 64 | + COMMENT "Building unstripped livekit-ffi metadata library" |
| 65 | + VERBATIM |
| 66 | + ) |
| 67 | + add_custom_target(build_livekit_ffi_metadata |
| 68 | + DEPENDS "${LIVEKIT_UNIFFI_METADATA_LIBRARY}") |
| 69 | +else() |
| 70 | + set(LIVEKIT_UNIFFI_METADATA_LIBRARY "$<TARGET_FILE:livekit_ffi>") |
| 71 | + add_custom_target(build_livekit_ffi_metadata) |
| 72 | +endif() |
| 73 | + |
| 74 | +# Keep Cargo invocations serialized on fresh runners, and ensure the selected |
| 75 | +# livekit-ffi library exists before it is inspected on Windows and macOS. |
| 76 | +add_dependencies(build_livekit_ffi_metadata build_rust_ffi) |
| 77 | + |
| 78 | +add_custom_command( |
| 79 | + OUTPUT |
| 80 | + "${LIVEKIT_UNIFFI_CPP_SOURCE}" |
| 81 | + "${LIVEKIT_UNIFFI_CPP_HEADER}" |
| 82 | + "${LIVEKIT_UNIFFI_CPP_SCAFFOLDING_HEADER}" |
| 83 | + COMMAND "${CMAKE_COMMAND}" -E make_directory |
| 84 | + "${LIVEKIT_UNIFFI_CPP_GENERATED_DIR}" |
| 85 | + COMMAND "${CMAKE_COMMAND}" -E env |
| 86 | + "CARGO_TARGET_DIR=${RUST_ROOT}/target/bindgens" |
| 87 | + "CARGO_ENCODED_RUSTFLAGS=" |
| 88 | + "RUSTFLAGS=" |
| 89 | + "${CARGO_EXECUTABLE}" run --locked |
| 90 | + --package bindgens |
| 91 | + --bin uniffi-bindgen-cpp |
| 92 | + -- |
| 93 | + --library "${LIVEKIT_UNIFFI_METADATA_LIBRARY}" |
| 94 | + --out-dir "${LIVEKIT_UNIFFI_CPP_GENERATED_DIR}" |
| 95 | + WORKING_DIRECTORY "${RUST_ROOT}" |
| 96 | + DEPENDS |
| 97 | + build_livekit_ffi_metadata |
| 98 | + ${LIVEKIT_UNIFFI_RUST_SOURCES} |
| 99 | + ${LIVEKIT_UNIFFI_BINDGEN_SOURCES} |
| 100 | + COMMENT "Generating UniFFI C++ bindings" |
| 101 | + VERBATIM |
| 102 | +) |
| 103 | +add_custom_target(generate_livekit_ffi_uniffi_cpp |
| 104 | + DEPENDS |
| 105 | + "${LIVEKIT_UNIFFI_CPP_SOURCE}" |
| 106 | + "${LIVEKIT_UNIFFI_CPP_HEADER}" |
| 107 | + "${LIVEKIT_UNIFFI_CPP_SCAFFOLDING_HEADER}" |
| 108 | +) |
| 109 | + |
| 110 | +set_source_files_properties( |
| 111 | + "${LIVEKIT_UNIFFI_CPP_SOURCE}" |
| 112 | + "${LIVEKIT_UNIFFI_CPP_HEADER}" |
| 113 | + "${LIVEKIT_UNIFFI_CPP_SCAFFOLDING_HEADER}" |
| 114 | + PROPERTIES GENERATED TRUE |
| 115 | +) |
| 116 | + |
| 117 | +add_library(livekit_uniffi_cpp OBJECT |
| 118 | + "${LIVEKIT_UNIFFI_CPP_SOURCE}" |
| 119 | +) |
| 120 | +set_target_properties(livekit_uniffi_cpp PROPERTIES |
| 121 | + CXX_VISIBILITY_PRESET hidden |
| 122 | + C_VISIBILITY_PRESET hidden |
| 123 | + VISIBILITY_INLINES_HIDDEN ON |
| 124 | +) |
| 125 | +add_dependencies(livekit_uniffi_cpp generate_livekit_ffi_uniffi_cpp) |
| 126 | +target_include_directories(livekit_uniffi_cpp |
| 127 | + PUBLIC "${LIVEKIT_UNIFFI_CPP_GENERATED_DIR}" |
| 128 | +) |
| 129 | +target_link_libraries(livekit_uniffi_cpp PRIVATE livekit_ffi) |
0 commit comments