1use serde::Deserialize;
2use std::{
3 env,
4 path::{Path, PathBuf},
5 process::Command,
6};
7
8const SWIFT_PACKAGE_NAME: &str = "LiveKitBridge";
9
10#[derive(Debug, Deserialize)]
11#[serde(rename_all = "camelCase")]
12pub struct SwiftTargetInfo {
13 pub triple: String,
14 pub unversioned_triple: String,
15 pub module_triple: String,
16 pub swift_runtime_compatibility_version: String,
17 #[serde(rename = "librariesRequireRPath")]
18 pub libraries_require_rpath: bool,
19}
20
21#[derive(Debug, Deserialize)]
22#[serde(rename_all = "camelCase")]
23pub struct SwiftPaths {
24 pub runtime_library_paths: Vec<String>,
25 pub runtime_library_import_paths: Vec<String>,
26 pub runtime_resource_path: String,
27}
28
29#[derive(Debug, Deserialize)]
30pub struct SwiftTarget {
31 pub target: SwiftTargetInfo,
32 pub paths: SwiftPaths,
33}
34
35const MACOS_TARGET_VERSION: &str = "10.15.7";
36
37fn main() {
38 if cfg!(not(any(test, feature = "test-support"))) {
39 let swift_target = get_swift_target();
40
41 build_bridge(&swift_target);
42 link_swift_stdlib(&swift_target);
43 link_webrtc_framework(&swift_target);
44
45 // Register exported Objective-C selectors, protocols, etc when building example binaries.
46 println!("cargo:rustc-link-arg=-Wl,-ObjC");
47 }
48}
49
50fn build_bridge(swift_target: &SwiftTarget) {
51 println!("cargo:rerun-if-env-changed=MACOSX_DEPLOYMENT_TARGET");
52 println!("cargo:rerun-if-changed={}/Sources", SWIFT_PACKAGE_NAME);
53 println!(
54 "cargo:rerun-if-changed={}/Package.swift",
55 SWIFT_PACKAGE_NAME
56 );
57 println!(
58 "cargo:rerun-if-changed={}/Package.resolved",
59 SWIFT_PACKAGE_NAME
60 );
61 let swift_package_root = swift_package_root();
62 if !Command::new("swift")
63 .arg("build")
64 .args(["--configuration", &env::var("PROFILE").unwrap()])
65 .args(["--triple", &swift_target.target.triple])
66 .current_dir(&swift_package_root)
67 .status()
68 .unwrap()
69 .success()
70 {
71 panic!(
72 "Failed to compile swift package in {}",
73 swift_package_root.display()
74 );
75 }
76
77 println!(
78 "cargo:rustc-link-search=native={}",
79 swift_target.out_dir_path().display()
80 );
81 println!("cargo:rustc-link-lib=static={}", SWIFT_PACKAGE_NAME);
82}
83
84fn link_swift_stdlib(swift_target: &SwiftTarget) {
85 for path in &swift_target.paths.runtime_library_paths {
86 println!("cargo:rustc-link-search=native={}", path);
87 }
88}
89
90fn link_webrtc_framework(swift_target: &SwiftTarget) {
91 let swift_out_dir_path = swift_target.out_dir_path();
92 println!("cargo:rustc-link-lib=framework=WebRTC");
93 println!(
94 "cargo:rustc-link-search=framework={}",
95 swift_out_dir_path.display()
96 );
97 // Find WebRTC.framework as a sibling of the executable when running tests.
98 println!("cargo:rustc-link-arg=-Wl,-rpath,@executable_path");
99 // Find WebRTC.framework in parent directory of the executable when running examples.
100 println!("cargo:rustc-link-arg=-Wl,-rpath,@executable_path/..");
101
102 let source_path = swift_out_dir_path.join("WebRTC.framework");
103 let deps_dir_path =
104 PathBuf::from(env::var("OUT_DIR").unwrap()).join("../../../deps/WebRTC.framework");
105 let target_dir_path =
106 PathBuf::from(env::var("OUT_DIR").unwrap()).join("../../../WebRTC.framework");
107 copy_dir(&source_path, &deps_dir_path);
108 copy_dir(&source_path, &target_dir_path);
109}
110
111fn get_swift_target() -> SwiftTarget {
112 let mut arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
113 if arch == "aarch64" {
114 arch = "arm64".into();
115 }
116 let target = format!("{}-apple-macosx{}", arch, MACOS_TARGET_VERSION);
117
118 let swift_target_info_str = Command::new("swift")
119 .args(["-target", &target, "-print-target-info"])
120 .output()
121 .unwrap()
122 .stdout;
123
124 serde_json::from_slice(&swift_target_info_str).unwrap()
125}
126
127fn swift_package_root() -> PathBuf {
128 env::current_dir().unwrap().join(SWIFT_PACKAGE_NAME)
129}
130
131fn copy_dir(source: &Path, destination: &Path) {
132 assert!(
133 Command::new("rm")
134 .arg("-rf")
135 .arg(destination)
136 .status()
137 .unwrap()
138 .success(),
139 "could not remove {:?} before copying",
140 destination
141 );
142
143 assert!(
144 Command::new("cp")
145 .arg("-R")
146 .args([source, destination])
147 .status()
148 .unwrap()
149 .success(),
150 "could not copy {:?} to {:?}",
151 source,
152 destination
153 );
154}
155
156impl SwiftTarget {
157 fn out_dir_path(&self) -> PathBuf {
158 swift_package_root()
159 .join(".build")
160 .join(&self.target.unversioned_triple)
161 .join(env::var("PROFILE").unwrap())
162 }
163}