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
62 let swift_package_root = swift_package_root();
63 let swift_target_folder = swift_target_folder();
64 if !Command::new("swift")
65 .arg("build")
66 .args(["--configuration", &env::var("PROFILE").unwrap()])
67 .args(["--triple", &swift_target.target.triple])
68 .args(["--build-path".into(), swift_target_folder])
69 .current_dir(&swift_package_root)
70 .status()
71 .unwrap()
72 .success()
73 {
74 panic!(
75 "Failed to compile swift package in {}",
76 swift_package_root.display()
77 );
78 }
79
80 println!(
81 "cargo:rustc-link-search=native={}",
82 swift_target.out_dir_path().display()
83 );
84 println!("cargo:rustc-link-lib=static={}", SWIFT_PACKAGE_NAME);
85}
86
87fn link_swift_stdlib(swift_target: &SwiftTarget) {
88 for path in &swift_target.paths.runtime_library_paths {
89 println!("cargo:rustc-link-search=native={}", path);
90 }
91}
92
93fn link_webrtc_framework(swift_target: &SwiftTarget) {
94 let swift_out_dir_path = swift_target.out_dir_path();
95 println!("cargo:rustc-link-lib=framework=WebRTC");
96 println!(
97 "cargo:rustc-link-search=framework={}",
98 swift_out_dir_path.display()
99 );
100 // Find WebRTC.framework as a sibling of the executable when running tests.
101 println!("cargo:rustc-link-arg=-Wl,-rpath,@executable_path");
102 // Find WebRTC.framework in parent directory of the executable when running examples.
103 println!("cargo:rustc-link-arg=-Wl,-rpath,@executable_path/..");
104
105 let source_path = swift_out_dir_path.join("WebRTC.framework");
106 let deps_dir_path =
107 PathBuf::from(env::var("OUT_DIR").unwrap()).join("../../../deps/WebRTC.framework");
108 let target_dir_path =
109 PathBuf::from(env::var("OUT_DIR").unwrap()).join("../../../WebRTC.framework");
110 copy_dir(&source_path, &deps_dir_path);
111 copy_dir(&source_path, &target_dir_path);
112}
113
114fn get_swift_target() -> SwiftTarget {
115 let mut arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
116 if arch == "aarch64" {
117 arch = "arm64".into();
118 }
119 let target = format!("{}-apple-macosx{}", arch, MACOS_TARGET_VERSION);
120
121 let swift_target_info_str = Command::new("swift")
122 .args(["-target", &target, "-print-target-info"])
123 .output()
124 .unwrap()
125 .stdout;
126
127 serde_json::from_slice(&swift_target_info_str).unwrap()
128}
129
130fn swift_package_root() -> PathBuf {
131 env::current_dir().unwrap().join(SWIFT_PACKAGE_NAME)
132}
133
134fn swift_target_folder() -> PathBuf {
135 env::current_dir()
136 .unwrap()
137 .join(format!("../../target/{SWIFT_PACKAGE_NAME}"))
138}
139
140fn copy_dir(source: &Path, destination: &Path) {
141 assert!(
142 Command::new("rm")
143 .arg("-rf")
144 .arg(destination)
145 .status()
146 .unwrap()
147 .success(),
148 "could not remove {:?} before copying",
149 destination
150 );
151
152 assert!(
153 Command::new("cp")
154 .arg("-R")
155 .args([source, destination])
156 .status()
157 .unwrap()
158 .success(),
159 "could not copy {:?} to {:?}",
160 source,
161 destination
162 );
163}
164
165impl SwiftTarget {
166 fn out_dir_path(&self) -> PathBuf {
167 swift_target_folder()
168 .join(&self.target.unversioned_triple)
169 .join(env::var("PROFILE").unwrap())
170 }
171}