| 1 | // Copyright 2025 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 | use std::path::Path; |
| 16 | use std::path::PathBuf; |
| 17 | use std::{env, path, process::Command}; |
| 18 | |
| 19 | fn main() { |
| 20 | if env::var("DOCS_RS").is_ok() { |
| 21 | return; |
| 22 | } |
| 23 | |
| 24 | let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap(); |
| 25 | let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap(); |
| 26 | let is_desktop = target_os == "linux" || target_os == "windows" || target_os == "macos"; |
| 27 | |
| 28 | println!("cargo:rerun-if-env-changed=LK_DEBUG_WEBRTC"); |
| 29 | println!("cargo:rerun-if-env-changed=LK_CUSTOM_WEBRTC"); |
| 30 | |
| 31 | let mut rust_files = vec![ |
| 32 | "src/peer_connection.rs", |
| 33 | "src/peer_connection_factory.rs", |
| 34 | "src/audio_device_controller.rs", |
| 35 | "src/media_stream.rs", |
| 36 | "src/media_stream_track.rs", |
| 37 | "src/audio_track.rs", |
| 38 | "src/video_track.rs", |
| 39 | "src/data_channel.rs", |
| 40 | "src/frame_cryptor.rs", |
| 41 | "src/jsep.rs", |
| 42 | "src/candidate.rs", |
| 43 | "src/rtp_parameters.rs", |
| 44 | "src/rtp_sender.rs", |
| 45 | "src/rtp_receiver.rs", |
| 46 | "src/rtp_transceiver.rs", |
| 47 | "src/rtc_error.rs", |
| 48 | "src/webrtc.rs", |
| 49 | "src/video_frame.rs", |
| 50 | "src/video_frame_buffer.rs", |
| 51 | "src/helper.rs", |
| 52 | "src/yuv_helper.rs", |
| 53 | "src/audio_resampler.rs", |
| 54 | "src/android.rs", |
| 55 | "src/prohibit_libsrtp_initialization.rs", |
| 56 | "src/apm.rs", |
| 57 | "src/noise_hook.rs", |
| 58 | "src/audio_mixer.rs", |
| 59 | "src/packet_trailer.rs", |
| 60 | ]; |
| 61 | |
| 62 | if is_desktop { |
| 63 | rust_files.push("src/desktop_capturer.rs"); |
| 64 | } |
| 65 | |
| 66 | let mut builder = cxx_build::bridges(rust_files); |
| 67 | |
| 68 | builder.files(&[ |
| 69 | "src/peer_connection.cpp", |
| 70 | "src/peer_connection_factory.cpp", |
| 71 | "src/audio_device_controller.cpp", |
| 72 | "src/media_stream.cpp", |
| 73 | "src/media_stream_track.cpp", |
| 74 | "src/audio_track.cpp", |
| 75 | "src/video_track.cpp", |
| 76 | "src/data_channel.cpp", |
| 77 | "src/jsep.cpp", |
| 78 | "src/candidate.cpp", |
| 79 | "src/rtp_receiver.cpp", |
| 80 | "src/rtp_sender.cpp", |
| 81 | "src/rtp_transceiver.cpp", |
| 82 | "src/rtp_parameters.cpp", |
| 83 | "src/rtc_error.cpp", |
| 84 | "src/webrtc.cpp", |
| 85 | "src/video_frame.cpp", |
| 86 | "src/video_frame_buffer.cpp", |
| 87 | "src/dmabuf_video_frame_buffer.cpp", |
| 88 | "src/encoded_video_frame_buffer.cpp", |
| 89 | "src/video_encoder_factory.cpp", |
| 90 | "src/passthrough_video_encoder.cpp", |
| 91 | "src/video_decoder_factory.cpp", |
| 92 | "src/synthetic_audio_device.cpp", |
| 93 | "src/adm_proxy.cpp", |
| 94 | "src/audio_resampler.cpp", |
| 95 | "src/frame_cryptor.cpp", |
| 96 | "src/global_task_queue.cpp", |
| 97 | "src/prohibit_libsrtp_initialization.cpp", |
| 98 | "src/apm.cpp", |
| 99 | "src/noise_hook.cpp", |
| 100 | "src/audio_mixer.cpp", |
| 101 | "src/av1_bitstream.cpp", |
| 102 | "src/packet_trailer.cpp", |
| 103 | "src/packet_trailer_av1.cpp", |
| 104 | ]); |
| 105 | |
| 106 | if is_desktop { |
| 107 | builder.file("src/desktop_capturer.cpp"); |
| 108 | } |
| 109 | |
| 110 | let webrtc_dir = webrtc_sys_build::webrtc_dir(); |
| 111 | let webrtc_include = webrtc_dir.join("include"); |
| 112 | let webrtc_lib = webrtc_dir.join("lib"); |
| 113 | |
| 114 | if !webrtc_dir.exists() { |
| 115 | webrtc_sys_build::download_webrtc().unwrap(); |
| 116 | } |
| 117 | |
| 118 | builder.includes(&[ |
| 119 | path::PathBuf::from("./include"), |
| 120 | webrtc_include.clone(), |
| 121 | webrtc_include.join("third_party/abseil-cpp/"), |
| 122 | webrtc_include.join("third_party/libyuv/include/"), |
| 123 | webrtc_include.join("third_party/libc++/"), |
| 124 | // For mac & ios |
| 125 | webrtc_include.join("sdk/objc"), |
| 126 | webrtc_include.join("sdk/objc/base"), |
| 127 | ]); |
| 128 | builder.define("WEBRTC_APM_DEBUG_DUMP", "0"); |
| 129 | |
| 130 | println!("cargo:rustc-link-search=native={}", webrtc_lib.to_str().unwrap()); |
| 131 | |
| 132 | for (key, value) in webrtc_sys_build::webrtc_defines() { |
| 133 | let value = value.as_deref(); |
| 134 | builder.define(key.as_str(), value); |
| 135 | } |
| 136 | |
| 137 | // Link webrtc library |
| 138 | println!("cargo:rustc-link-lib=static=webrtc"); |
| 139 | match target_os.as_str() { |
| 140 | "windows" => { |
| 141 | println!("cargo:rustc-link-lib=dylib=msdmo"); |
| 142 | println!("cargo:rustc-link-lib=dylib=wmcodecdspuuid"); |
| 143 | println!("cargo:rustc-link-lib=dylib=dmoguids"); |
| 144 | println!("cargo:rustc-link-lib=dylib=crypt32"); |
| 145 | println!("cargo:rustc-link-lib=dylib=iphlpapi"); |
| 146 | println!("cargo:rustc-link-lib=dylib=ole32"); |
| 147 | println!("cargo:rustc-link-lib=dylib=oleaut32"); |
| 148 | println!("cargo:rustc-link-lib=dylib=secur32"); |
| 149 | println!("cargo:rustc-link-lib=dylib=winmm"); |
| 150 | println!("cargo:rustc-link-lib=dylib=ws2_32"); |
| 151 | println!("cargo:rustc-link-lib=dylib=strmiids"); |
| 152 | println!("cargo:rustc-link-lib=dylib=d3d11"); |
| 153 | println!("cargo:rustc-link-lib=dylib=gdi32"); |
| 154 | println!("cargo:rustc-link-lib=dylib=dxgi"); |
| 155 | println!("cargo:rustc-link-lib=dylib=dwmapi"); |
| 156 | println!("cargo:rustc-link-lib=dylib=shcore"); |
| 157 | println!("cargo:rustc-link-lib=dylib=mfplat"); |
| 158 | println!("cargo:rustc-link-lib=dylib=mfuuid"); |
| 159 | println!("cargo:rustc-link-lib=dylib=mf"); |
| 160 | |
| 161 | builder |
| 162 | .file("src/mf/mf_encoder_factory.cpp") |
| 163 | .file("src/mf/mf_h264_encoder_impl.cpp") |
| 164 | .define("USE_MF_VIDEO_CODEC", "1") |
| 165 | .flag("/std:c++20") |
| 166 | .flag("/EHsc"); |
| 167 | } |
| 168 | "linux" => { |
| 169 | println!("cargo:rustc-link-lib=dylib=rt"); |
| 170 | println!("cargo:rustc-link-lib=dylib=dl"); |
| 171 | println!("cargo:rustc-link-lib=dylib=pthread"); |
| 172 | println!("cargo:rustc-link-lib=dylib=m"); |
| 173 | |
| 174 | configure_hermetic_libcxx(&mut builder, &webrtc_include); |
| 175 | |
| 176 | // In order to avoid any ABI mismatches we use the sysroot's headers. |
| 177 | add_gio_headers(&mut builder); |
| 178 | |
| 179 | // Do not use pkg_config::probe_library, because we only require headers. |
| 180 | for lib_name in ["glib-2.0", "gobject-2.0", "gio-2.0"] { |
| 181 | let lib = pkg_config::Config::new().cargo_metadata(false).probe(lib_name).unwrap(); |
| 182 | for path in lib.include_paths { |
| 183 | builder.include(path); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | add_lazy_load_so( |
| 188 | &mut builder, |
| 189 | "desktop_capturer", |
| 190 | ["drm", "gbm", "X11", "Xfixes", "Xdamage", "Xrandr", "Xcomposite", "Xext"] |
| 191 | .map(String::from) |
| 192 | .to_vec(), |
| 193 | ); |
| 194 | |
| 195 | let x86 = target_arch == "x86_64" || target_arch == "i686"; |
| 196 | let arm = target_arch == "aarch64" || target_arch.contains("arm"); |
| 197 | |
| 198 | if x86 { |
| 199 | if let Some(libva_include) = pkg_config::get_variable("libva", "includedir").ok() { |
| 200 | // Do not use pkg_config::probe_library because libva is dlopened |
| 201 | // and pkg_config::probe_library would link it. |
| 202 | builder |
| 203 | .include(libva_include) |
| 204 | .file("src/vaapi/vaapi_display_drm.cpp") |
| 205 | .file("src/vaapi/vaapi_h264_encoder_wrapper.cpp") |
| 206 | .file("src/vaapi/vaapi_encoder_factory.cpp") |
| 207 | .file("src/vaapi/h264_encoder_impl.cpp") |
| 208 | .flag("-DUSE_VAAPI_VIDEO_CODEC=1"); |
| 209 | |
| 210 | add_lazy_load_so( |
| 211 | &mut builder, |
| 212 | "vaapi", |
| 213 | ["va", "va-drm"].map(String::from).to_vec(), |
| 214 | ); |
| 215 | } else { |
| 216 | println!("cargo:warning=libva not found; building without hardware accelerated video codecs"); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | if arm { |
| 221 | println!("cargo:rerun-if-env-changed=LK_JETSON_MMAPI_DIR"); |
| 222 | let jetson_mmapi_dir = PathBuf::from(match env::var("LK_JETSON_MMAPI_DIR") { |
| 223 | Ok(p) => p, |
| 224 | Err(_) => "/usr/src/jetson_multimedia_api".to_owned(), |
| 225 | }); |
| 226 | let jetson_mmapi_include = jetson_mmapi_dir.join("include"); |
| 227 | if jetson_mmapi_include.exists() { |
| 228 | let jetson_classes_dir = jetson_mmapi_dir.join("samples/common/classes"); |
| 229 | |
| 230 | builder |
| 231 | .include(&jetson_mmapi_include) |
| 232 | .include("src/jetson") |
| 233 | .file("src/jetson/jetson_mmapi_encoder.cpp") |
| 234 | .file("src/jetson/jetson_plane_layout.cpp") |
| 235 | .file("src/jetson/jetson_runtime_loader.cpp") |
| 236 | .file("src/jetson/h264_encoder_impl.cpp") |
| 237 | .file("src/jetson/h265_encoder_impl.cpp") |
| 238 | .file("src/jetson/av1_encoder_impl.cpp") |
| 239 | .file("src/jetson/jetson_encoder_factory.cpp") |
| 240 | .flag("-DUSE_JETSON_VIDEO_CODEC=1"); |
| 241 | |
| 242 | let mmapi_sources = [ |
| 243 | "NvElement.cpp", |
| 244 | "NvV4l2Element.cpp", |
| 245 | "NvV4l2ElementPlane.cpp", |
| 246 | "NvVideoEncoder.cpp", |
| 247 | "NvBuffer.cpp", |
| 248 | "NvLogging.cpp", |
| 249 | "NvElementProfiler.cpp", |
| 250 | ]; |
| 251 | for src in &mmapi_sources { |
| 252 | let src_path = jetson_classes_dir.join(src); |
| 253 | if src_path.exists() { |
| 254 | builder.file(&src_path); |
| 255 | } else { |
| 256 | println!( |
| 257 | "cargo:warning=Jetson MMAPI source not found: {}", |
| 258 | src_path.display() |
| 259 | ); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | // libv4l2/libnvv4l2 and libnvbufsurface are dlopened at |
| 264 | // runtime (see src/jetson/jetson_runtime_loader.cpp), so |
| 265 | // the same aarch64 binary loads on non-Jetson systems |
| 266 | // where those libraries are absent. |
| 267 | add_lazy_load_so( |
| 268 | &mut builder, |
| 269 | "jetson", |
| 270 | ["v4l2", "nvbufsurface"].map(String::from).to_vec(), |
| 271 | ); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | if x86 || arm { |
| 276 | let cuda_home = PathBuf::from(match env::var("CUDA_HOME") { |
| 277 | Ok(p) => p, |
| 278 | Err(_) => "/usr/local/cuda".to_owned(), |
| 279 | }); |
| 280 | let cuda_include_dir = cuda_home.join("include"); |
| 281 | |
| 282 | // libcuda and libnvcuvid are dlopened, so do not link them. |
| 283 | if cuda_include_dir.join("cuda.h").exists() { |
| 284 | builder |
| 285 | .include(cuda_include_dir) |
| 286 | .flag("-Isrc/nvidia/NvCodec/include") |
| 287 | .flag("-Isrc/nvidia/NvCodec/NvCodec") |
| 288 | .file("src/nvidia/NvCodec/NvCodec/NvDecoder/NvDecoder.cpp") |
| 289 | .file("src/nvidia/NvCodec/NvCodec/NvEncoder/NvEncoder.cpp") |
| 290 | .file("src/nvidia/NvCodec/NvCodec/NvEncoder/NvEncoderCuda.cpp") |
| 291 | .file("src/nvidia/h264_encoder_impl.cpp") |
| 292 | .file("src/nvidia/h265_encoder_impl.cpp") |
| 293 | .file("src/nvidia/av1_encoder_impl.cpp") |
| 294 | .file("src/nvidia/h264_decoder_impl.cpp") |
| 295 | .file("src/nvidia/h265_decoder_impl.cpp") |
| 296 | .file("src/nvidia/nvidia_decoder_factory.cpp") |
| 297 | .file("src/nvidia/nvidia_encoder_factory.cpp") |
| 298 | .file("src/nvidia/cuda_context.cpp") |
| 299 | .flag("-Wno-deprecated-declarations") |
| 300 | .flag("-DUSE_NVIDIA_VIDEO_CODEC=1"); |
| 301 | |
| 302 | add_lazy_load_so( |
| 303 | &mut builder, |
| 304 | "nvidia", |
| 305 | ["cuda", "nvcuvid"].map(String::from).to_vec(), |
| 306 | ); |
| 307 | } else { |
| 308 | println!("cargo:warning=cuda.h not found; building without hardware accelerated video codec support for NVidia GPUs"); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | builder |
| 313 | .flag("-Wno-changes-meaning") |
| 314 | .flag("-Wno-deprecated-declarations") |
| 315 | .flag("-std=c++20"); |
| 316 | } |
| 317 | "macos" => { |
| 318 | println!("cargo:rustc-link-lib=framework=Foundation"); |
| 319 | println!("cargo:rustc-link-lib=framework=AVFoundation"); |
| 320 | // rtc_base's platform certificate verifier calls SecTrust. |
| 321 | println!("cargo:rustc-link-lib=framework=Security"); |
| 322 | println!("cargo:rustc-link-lib=framework=CoreAudio"); |
| 323 | println!("cargo:rustc-link-lib=framework=AudioToolbox"); |
| 324 | println!("cargo:rustc-link-lib=framework=AppKit"); |
| 325 | println!("cargo:rustc-link-lib=framework=CoreMedia"); |
| 326 | println!("cargo:rustc-link-lib=framework=CoreGraphics"); |
| 327 | println!("cargo:rustc-link-lib=framework=VideoToolbox"); |
| 328 | println!("cargo:rustc-link-lib=framework=CoreVideo"); |
| 329 | println!("cargo:rustc-link-lib=framework=OpenGL"); |
| 330 | println!("cargo:rustc-link-lib=framework=Metal"); |
| 331 | println!("cargo:rustc-link-lib=framework=MetalKit"); |
| 332 | println!("cargo:rustc-link-lib=framework=QuartzCore"); |
| 333 | println!("cargo:rustc-link-lib=framework=IOKit"); |
| 334 | println!("cargo:rustc-link-lib=framework=IOSurface"); |
| 335 | println!("cargo:rustc-link-lib=framework=ScreenCaptureKit"); |
| 336 | |
| 337 | let sysroot = configure_darwin_sysroot(&mut builder); |
| 338 | |
| 339 | builder |
| 340 | .file("src/objc_video_factory.mm") |
| 341 | .file("src/objc_video_frame_buffer.mm") |
| 342 | .flag("-stdlib=libc++") |
| 343 | .flag("-std=c++20") |
| 344 | .flag("-Wno-nullability-completeness"); |
| 345 | |
| 346 | let mut sck_builder = cc::Build::new(); |
| 347 | sck_builder |
| 348 | .cpp(true) |
| 349 | .file("src/sck_window_capturer.mm") |
| 350 | .includes(&[ |
| 351 | path::PathBuf::from("./include"), |
| 352 | webrtc_include.clone(), |
| 353 | webrtc_include.join("third_party/abseil-cpp/"), |
| 354 | webrtc_include.join("third_party/libyuv/include/"), |
| 355 | webrtc_include.join("third_party/libc++/"), |
| 356 | webrtc_include.join("sdk/objc"), |
| 357 | webrtc_include.join("sdk/objc/base"), |
| 358 | ]) |
| 359 | .flag(format!("-isysroot{}", sysroot)) |
| 360 | .flag("-stdlib=libc++") |
| 361 | .flag("-std=c++20") |
| 362 | .flag("-fobjc-arc") |
| 363 | .flag("-Wno-nullability-completeness"); |
| 364 | for (key, value) in webrtc_sys_build::webrtc_defines() { |
| 365 | sck_builder.define(key.as_str(), value.as_deref()); |
| 366 | } |
| 367 | sck_builder.warnings(false).compile("webrtcsys-sck-window"); |
| 368 | } |
| 369 | "ios" => { |
| 370 | println!("cargo:rustc-link-lib=framework=Foundation"); |
| 371 | println!("cargo:rustc-link-lib=framework=CoreFoundation"); |
| 372 | println!("cargo:rustc-link-lib=framework=AVFoundation"); |
| 373 | // rtc_base's platform certificate verifier calls SecTrust. |
| 374 | println!("cargo:rustc-link-lib=framework=Security"); |
| 375 | println!("cargo:rustc-link-lib=framework=CoreAudio"); |
| 376 | println!("cargo:rustc-link-lib=framework=UIKit"); |
| 377 | println!("cargo:rustc-link-lib=framework=CoreVideo"); |
| 378 | println!("cargo:rustc-link-lib=framework=CoreGraphics"); |
| 379 | println!("cargo:rustc-link-lib=framework=CoreMedia"); |
| 380 | println!("cargo:rustc-link-lib=framework=VideoToolbox"); |
| 381 | println!("cargo:rustc-link-lib=framework=AudioToolbox"); |
| 382 | println!("cargo:rustc-link-lib=framework=OpenGLES"); |
| 383 | println!("cargo:rustc-link-lib=framework=GLKit"); |
| 384 | println!("cargo:rustc-link-lib=framework=Metal"); |
| 385 | println!("cargo:rustc-link-lib=framework=MetalKit"); |
| 386 | println!("cargo:rustc-link-lib=framework=Network"); |
| 387 | println!("cargo:rustc-link-lib=framework=QuartzCore"); |
| 388 | |
| 389 | configure_darwin_sysroot(&mut builder); |
| 390 | |
| 391 | builder |
| 392 | .file("src/objc_video_factory.mm") |
| 393 | .file("src/objc_video_frame_buffer.mm") |
| 394 | .flag("-std=c++20"); |
| 395 | } |
| 396 | "android" => { |
| 397 | webrtc_sys_build::configure_jni_symbols().unwrap(); |
| 398 | |
| 399 | println!("cargo:rustc-link-lib=EGL"); |
| 400 | println!("cargo:rustc-link-lib=OpenSLES"); |
| 401 | println!("cargo:rustc-link-lib=c++_static"); |
| 402 | println!("cargo:rustc-link-lib=c++abi"); |
| 403 | |
| 404 | configure_android_sysroot(&mut builder); |
| 405 | builder |
| 406 | .file("src/android.cpp") |
| 407 | // Provides a weak stub for std::__ndk1::__hash_memory, which |
| 408 | // was removed from libc++_static.a exports in NDK r28. |
| 409 | .file("src/ndk_compat.cpp") |
| 410 | .flag("-std=c++20"); |
| 411 | } |
| 412 | _ => { |
| 413 | panic!("Unsupported target, {}", target_os); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | // TODO(theomonnom) Only add this define when building tests |
| 418 | builder.define("LIVEKIT_TEST", None); |
| 419 | builder.warnings(false).compile("webrtcsys-cxx"); |
| 420 | |
| 421 | for entry in glob::glob("./src/**/*.cpp").unwrap() { |
| 422 | println!("cargo:rerun-if-changed={}", entry.unwrap().display()); |
| 423 | } |
| 424 | |
| 425 | for entry in glob::glob("./src/**/*.mm").unwrap() { |
| 426 | println!("cargo:rerun-if-changed={}", entry.unwrap().display()); |
| 427 | } |
| 428 | |
| 429 | for entry in glob::glob("./include/**/*.h").unwrap() { |
| 430 | println!("cargo:rerun-if-changed={}", entry.unwrap().display()); |
| 431 | } |
| 432 | |
| 433 | if target_os.as_str() == "android" { |
| 434 | copy_libwebrtc_jar(&PathBuf::from(Path::new(&webrtc_dir))); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | fn copy_libwebrtc_jar(webrtc_dir: &PathBuf) { |
| 439 | let jar_path = webrtc_dir.join("libwebrtc.jar"); |
| 440 | let output_path = get_output_path(); |
| 441 | let output_jar_path = output_path.join("libwebrtc.jar"); |
| 442 | let res = std::fs::copy(jar_path, output_jar_path); |
| 443 | if let Err(e) = res { |
| 444 | println!("Failed to copy libwebrtc.jar: {}", e); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | fn get_output_path() -> PathBuf { |
| 449 | let manifest_dir_string = env::var("CARGO_MANIFEST_DIR").unwrap(); |
| 450 | let build_type = env::var("PROFILE").unwrap(); |
| 451 | let build_target = env::var("TARGET").unwrap(); |
| 452 | let path = |
| 453 | Path::new(&manifest_dir_string).join("../target").join(build_target).join(build_type); |
| 454 | return PathBuf::from(path); |
| 455 | } |
| 456 | |
| 457 | fn configure_darwin_sysroot(builder: &mut cc::Build) -> String { |
| 458 | let target_os = webrtc_sys_build::target_os(); |
| 459 | |
| 460 | let sdk = match target_os.as_str() { |
| 461 | "mac" => "macosx", |
| 462 | "ios-device" => "iphoneos", |
| 463 | "ios-simulator" => "iphonesimulator", |
| 464 | _ => panic!("Unsupported target_os: {}", target_os), |
| 465 | }; |
| 466 | |
| 467 | let clang_rt = match target_os.as_str() { |
| 468 | "mac" => "clang_rt.osx", |
| 469 | "ios-device" => "clang_rt.ios", |
| 470 | "ios-simulator" => "clang_rt.iossim", |
| 471 | _ => panic!("Unsupported target_os: {}", target_os), |
| 472 | }; |
| 473 | |
| 474 | println!("cargo:rustc-link-lib={}", clang_rt); |
| 475 | println!("cargo:rustc-link-arg=-ObjC"); |
| 476 | |
| 477 | let sysroot = Command::new("xcrun").args(["--sdk", sdk, "--show-sdk-path"]).output().unwrap(); |
| 478 | |
| 479 | let sysroot = String::from_utf8_lossy(&sysroot.stdout); |
| 480 | let sysroot = sysroot.trim().to_owned(); |
| 481 | |
| 482 | let search_dirs = Command::new("cc").arg("--print-search-dirs").output().unwrap(); |
| 483 | |
| 484 | let search_dirs = String::from_utf8_lossy(&search_dirs.stdout); |
| 485 | for line in search_dirs.lines() { |
| 486 | if line.contains("libraries: =") { |
| 487 | let path = line.split('=').nth(1).unwrap(); |
| 488 | let path = format!("{}/lib/darwin", path); |
| 489 | println!("cargo:rustc-link-search={}", path); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | builder.flag(format!("-isysroot{}", sysroot).as_str()); |
| 494 | sysroot |
| 495 | } |
| 496 | |
| 497 | fn configure_android_sysroot(builder: &mut cc::Build) { |
| 498 | let toolchain = webrtc_sys_build::android_ndk_toolchain().unwrap(); |
| 499 | let sysroot = toolchain.join("sysroot").canonicalize().unwrap(); |
| 500 | builder.flag(format!("-isysroot{}", sysroot.display()).as_str()); |
| 501 | } |
| 502 | |
| 503 | fn add_lazy_load_so(builder: &mut cc::Build, name: &str, libraries: Vec<String>) { |
| 504 | let target_arch = webrtc_sys_build::target_arch(); |
| 505 | for lib_name in libraries { |
| 506 | let mut arch_dir = "x86_64-linux-gnu"; |
| 507 | if target_arch.contains("arm64") { |
| 508 | arch_dir = "aarch64-linux-gnu"; |
| 509 | } |
| 510 | let implib_file_c_name = "src/lazy_load_deps_for/".to_owned() |
| 511 | + name |
| 512 | + "/" |
| 513 | + arch_dir |
| 514 | + "/lib" |
| 515 | + &lib_name |
| 516 | + ".so.init.c"; |
| 517 | let implib_file_asm_name = "src/lazy_load_deps_for/".to_owned() |
| 518 | + name |
| 519 | + "/" |
| 520 | + arch_dir |
| 521 | + "/lib" |
| 522 | + &lib_name |
| 523 | + ".so.tramp.S"; |
| 524 | builder.file(implib_file_c_name).file(implib_file_asm_name); |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | /// Compile against the same hermetic libc++ that is baked into libwebrtc.a. |
| 529 | /// |
| 530 | /// The Linux libwebrtc build sets `use_custom_libcxx=true`, so every std type in |
| 531 | /// its public API lives in the `std::__Cr` ABI namespace with libc++ layouts. |
| 532 | /// Using the host's libstdc++ here instead is not merely a mangling mismatch that |
| 533 | /// the linker would catch: `std::span` is layout-different between the two, so a |
| 534 | /// span handed to libwebrtc silently arrives with its pointer and size swapped. |
| 535 | /// |
| 536 | /// Mirrors the flags in the WebRTC checkout's `build/config/c++/BUILD.gn`. The |
| 537 | /// matching `_LIBCPP_*` defines come from webrtc.ninja via `webrtc_defines()`. |
| 538 | fn configure_hermetic_libcxx(builder: &mut cc::Build, webrtc_include: &path::Path) { |
| 539 | let libcxx = webrtc_include.join("third_party/libc++/src/include"); |
| 540 | let libcxxabi = webrtc_include.join("third_party/libc++abi/src/include"); |
| 541 | if !libcxx.join("span").exists() { |
| 542 | panic!( |
| 543 | "hermetic libc++ headers missing from {}.\n\ |
| 544 | This libwebrtc artifact predates use_custom_libcxx=true; rebuild it with \ |
| 545 | build_linux.sh or point LK_CUSTOM_WEBRTC at a newer one.", |
| 546 | libcxx.display() |
| 547 | ); |
| 548 | } |
| 549 | |
| 550 | // Chromium's libc++ is clang-only. At _LIBCPP_ABI_VERSION 2 it marks unique_ptr |
| 551 | // and shared_ptr __attribute__((trivial_abi)), which GCC accepts and silently |
| 552 | // ignores (a -Wattributes warning that cc's `-w` swallows). That attribute |
| 553 | // changes the calling convention, not just layout: libwebrtc.a returns |
| 554 | // std::unique_ptr in a register, while a GCC caller reads it back from an sret |
| 555 | // slot the callee never wrote, yielding a garbage pointer at the first use. |
| 556 | if env::var_os("CXX").is_none() { |
| 557 | if Command::new("clang++").arg("--version").output().is_err() { |
| 558 | panic!( |
| 559 | "clang++ is required to build webrtc-sys on Linux: libwebrtc.a is built \ |
| 560 | against Chromium's hermetic libc++, whose trivial_abi annotations GCC \ |
| 561 | ignores, which silently breaks the calling convention for std::unique_ptr \ |
| 562 | and std::shared_ptr. Install clang, or set CXX to a clang.", |
| 563 | ); |
| 564 | } |
| 565 | builder.compiler("clang++"); |
| 566 | } |
| 567 | |
| 568 | check_clang_version(builder, &libcxx); |
| 569 | |
| 570 | builder |
| 571 | .flag("-nostdinc++") |
| 572 | .flag(format!("-isystem{}", libcxx.display())) |
| 573 | .flag(format!("-isystem{}", libcxxabi.display())) |
| 574 | // Holds __config_site, which pins _LIBCPP_ABI_NAMESPACE=__Cr. |
| 575 | .include(webrtc_include.join("buildtools/third_party/libc++")); |
| 576 | |
| 577 | // libc++/libc++abi are already archived into libwebrtc.a, so linking the |
| 578 | // host libstdc++ on top would only add a second, incompatible stdlib. |
| 579 | builder.cpp_link_stdlib(None); |
| 580 | |
| 581 | // The cxx crate builds its own runtime (cxx.cc) with the host default stdlib, |
| 582 | // so the rust::String <-> std::string conversions it exports are mangled for |
| 583 | // libstdc++ and cannot satisfy the std::__Cr call sites in the generated |
| 584 | // bridges. Compile a second copy with the flags above to provide those. |
| 585 | // DEP_CXXBRIDGE1_HEADER is `cargo:HEADER` from the cxx crate: <root>/include/cxx.h. |
| 586 | let cxx_h = env::var("DEP_CXXBRIDGE1_HEADER") |
| 587 | .expect("cxx crate did not export HEADER; cannot locate its cxx.cc"); |
| 588 | let cxx_root = path::Path::new(&cxx_h) |
| 589 | .parent() |
| 590 | .and_then(path::Path::parent) |
| 591 | .expect("unexpected DEP_CXXBRIDGE1_HEADER layout"); |
| 592 | builder.file(cxx_root.join("src/cxx.cc")); |
| 593 | } |
| 594 | |
| 595 | /// The hermetic libc++ tracks LLVM trunk, so it freely uses builtins that only |
| 596 | /// exist in a recent clang (`__builtin_popcountg`, `__is_nothrow_convertible`, |
| 597 | /// `__GCC_DESTRUCTIVE_SIZE`, ...). A compiler below its floor does not fail with |
| 598 | /// "your clang is too old" — it fails deep inside <limits> and <span> with |
| 599 | /// hundreds of lines about `dynamic_extent` not being a constant expression, in |
| 600 | /// headers the user never wrote. Catch it up front instead. |
| 601 | fn check_clang_version(builder: &cc::Build, libcxx: &path::Path) { |
| 602 | let min = libcxx_min_clang_major(libcxx); |
| 603 | |
| 604 | let compiler = builder.get_compiler(); |
| 605 | let defines = compiler |
| 606 | .to_command() |
| 607 | .args(["-dM", "-E", "-x", "c++", "/dev/null"]) |
| 608 | .output() |
| 609 | .unwrap_or_else(|e| panic!("failed to run {}: {e}", compiler.path().display())); |
| 610 | |
| 611 | let major = String::from_utf8_lossy(&defines.stdout).lines().find_map(|line| { |
| 612 | line.strip_prefix("#define __clang_major__ ").and_then(|v| v.trim().parse::<u32>().ok()) |
| 613 | }); |
| 614 | |
| 615 | match major { |
| 616 | Some(major) if major >= min => {} |
| 617 | Some(major) => panic!( |
| 618 | "{} is clang {major}, but the hermetic libc++ shipped with this libwebrtc \ |
| 619 | requires clang {min} or later. Install a newer clang and point CC/CXX at it, \ |
| 620 | or use the exact toolchain libwebrtc was built with (see CR_CLANG_REVISION in \ |
| 621 | the artifact's webrtc.ninja).", |
| 622 | compiler.path().display(), |
| 623 | ), |
| 624 | None => panic!( |
| 625 | "{} does not define __clang_major__, so it is not a clang. libwebrtc.a is built \ |
| 626 | against Chromium's hermetic libc++, which requires clang {min} or later; GCC \ |
| 627 | additionally ignores its trivial_abi annotations, silently breaking the calling \ |
| 628 | convention for std::unique_ptr and std::shared_ptr.", |
| 629 | compiler.path().display(), |
| 630 | ), |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | /// libc++ states its own floor in `__configuration/compiler.h`, as |
| 635 | /// `#if _LIBCPP_CLANG_VER < 2101` (major * 100 + minor). Read it from the |
| 636 | /// artifact rather than hardcoding, so a libwebrtc bump moves the floor with it. |
| 637 | fn libcxx_min_clang_major(libcxx: &path::Path) -> u32 { |
| 638 | const FALLBACK: u32 = 21; |
| 639 | |
| 640 | let header = libcxx.join("__configuration/compiler.h"); |
| 641 | let Ok(source) = std::fs::read_to_string(&header) else { |
| 642 | return FALLBACK; |
| 643 | }; |
| 644 | |
| 645 | source |
| 646 | .lines() |
| 647 | .find_map(|line| { |
| 648 | let (_, rest) = line.split_once("_LIBCPP_CLANG_VER < ")?; |
| 649 | let ver: u32 = rest.trim().parse().ok()?; |
| 650 | Some(ver / 100) |
| 651 | }) |
| 652 | .unwrap_or(FALLBACK) |
| 653 | } |
| 654 | |
| 655 | fn add_gio_headers(builder: &mut cc::Build) { |
| 656 | let webrtc_dir = webrtc_sys_build::webrtc_dir(); |
| 657 | let target_arch = webrtc_sys_build::target_arch(); |
| 658 | let target_arch_sysroot = match target_arch.as_str() { |
| 659 | "arm64" => "arm64", |
| 660 | "x64" => "amd64", |
| 661 | _ => panic!("unsupported arch"), |
| 662 | }; |
| 663 | let sysroot_path = format!("include/build/linux/debian_bullseye_{target_arch_sysroot}-sysroot"); |
| 664 | let sysroot = webrtc_dir.join(sysroot_path); |
| 665 | let glib_path = sysroot.join("usr/include/glib-2.0"); |
| 666 | println!("cargo:info=add_gio_headers {}", glib_path.display()); |
| 667 | |
| 668 | builder.include(&glib_path); |
| 669 | let arch_specific_path = match target_arch.as_str() { |
| 670 | "x64" => "x86_64-linux-gnu", |
| 671 | "arm64" => "aarch64-linux-gnu", |
| 672 | _ => panic!("unsupported target"), |
| 673 | }; |
| 674 | |
| 675 | let glib_path_config = sysroot.join("usr/lib"); |
| 676 | let glib_path_config = glib_path_config.join(arch_specific_path); |
| 677 | let glib_path_config = glib_path_config.join("glib-2.0/include"); |
| 678 | builder.include(&glib_path_config); |
| 679 | } |