2022-09-02 21:56:38 +03:00
|
|
|
use serde::Deserialize;
|
2022-09-16 19:09:11 +03:00
|
|
|
use std::{
|
|
|
|
env,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
process::Command,
|
|
|
|
};
|
2022-09-02 21:56:38 +03:00
|
|
|
|
2022-09-16 12:22:17 +03:00
|
|
|
const SWIFT_PACKAGE_NAME: &'static str = "LiveKitBridge";
|
|
|
|
|
2022-09-02 21:56:38 +03:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct SwiftTargetInfo {
|
|
|
|
pub triple: String,
|
|
|
|
pub unversioned_triple: String,
|
|
|
|
pub module_triple: String,
|
|
|
|
pub swift_runtime_compatibility_version: String,
|
|
|
|
#[serde(rename = "librariesRequireRPath")]
|
|
|
|
pub libraries_require_rpath: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct SwiftPaths {
|
|
|
|
pub runtime_library_paths: Vec<String>,
|
|
|
|
pub runtime_library_import_paths: Vec<String>,
|
|
|
|
pub runtime_resource_path: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
pub struct SwiftTarget {
|
|
|
|
pub target: SwiftTargetInfo,
|
|
|
|
pub paths: SwiftPaths,
|
|
|
|
}
|
|
|
|
|
2022-10-20 17:55:39 +03:00
|
|
|
const MACOS_TARGET_VERSION: &str = "10.15.7";
|
2022-09-02 21:56:38 +03:00
|
|
|
|
|
|
|
fn main() {
|
2022-10-19 14:53:40 +03:00
|
|
|
if cfg!(not(any(test, feature = "test-support"))) {
|
|
|
|
let swift_target = get_swift_target();
|
2022-09-06 17:56:49 +03:00
|
|
|
|
2022-10-19 14:53:40 +03:00
|
|
|
build_bridge(&swift_target);
|
|
|
|
link_swift_stdlib(&swift_target);
|
|
|
|
link_webrtc_framework(&swift_target);
|
2022-10-18 08:24:48 +03:00
|
|
|
|
2022-10-19 14:53:40 +03:00
|
|
|
// Register exported Objective-C selectors, protocols, etc when building example binaries.
|
|
|
|
println!("cargo:rustc-link-arg=-Wl,-ObjC");
|
|
|
|
}
|
2022-09-02 21:56:38 +03:00
|
|
|
}
|
|
|
|
|
2022-09-06 17:56:49 +03:00
|
|
|
fn build_bridge(swift_target: &SwiftTarget) {
|
2022-09-07 15:52:11 +03:00
|
|
|
println!("cargo:rerun-if-changed={}/Sources", SWIFT_PACKAGE_NAME);
|
|
|
|
println!(
|
|
|
|
"cargo:rerun-if-changed={}/Package.swift",
|
|
|
|
SWIFT_PACKAGE_NAME
|
|
|
|
);
|
2022-09-16 12:22:17 +03:00
|
|
|
println!(
|
|
|
|
"cargo:rerun-if-changed={}/Package.resolved",
|
|
|
|
SWIFT_PACKAGE_NAME
|
|
|
|
);
|
2022-09-06 17:56:49 +03:00
|
|
|
let swift_package_root = swift_package_root();
|
2022-09-02 21:56:38 +03:00
|
|
|
if !Command::new("swift")
|
2022-09-16 12:22:17 +03:00
|
|
|
.arg("build")
|
|
|
|
.args(&["--configuration", &env::var("PROFILE").unwrap()])
|
2022-09-16 12:47:17 +03:00
|
|
|
.args(&["--triple", &swift_target.target.triple])
|
2022-09-06 17:56:49 +03:00
|
|
|
.current_dir(&swift_package_root)
|
2022-09-02 21:56:38 +03:00
|
|
|
.status()
|
|
|
|
.unwrap()
|
|
|
|
.success()
|
|
|
|
{
|
|
|
|
panic!(
|
|
|
|
"Failed to compile swift package in {}",
|
2022-09-06 17:56:49 +03:00
|
|
|
swift_package_root.display()
|
2022-09-02 21:56:38 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
println!(
|
2022-09-06 17:56:49 +03:00
|
|
|
"cargo:rustc-link-search=native={}",
|
|
|
|
swift_target.out_dir_path().display()
|
2022-09-02 21:56:38 +03:00
|
|
|
);
|
2022-09-06 17:56:49 +03:00
|
|
|
println!("cargo:rustc-link-lib=static={}", SWIFT_PACKAGE_NAME);
|
2022-09-02 21:56:38 +03:00
|
|
|
}
|
|
|
|
|
2022-09-06 17:56:49 +03:00
|
|
|
fn link_swift_stdlib(swift_target: &SwiftTarget) {
|
2022-10-20 17:55:39 +03:00
|
|
|
for path in &swift_target.paths.runtime_library_paths {
|
|
|
|
println!("cargo:rustc-link-search=native={}", path);
|
|
|
|
}
|
2022-09-06 17:56:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn link_webrtc_framework(swift_target: &SwiftTarget) {
|
|
|
|
let swift_out_dir_path = swift_target.out_dir_path();
|
|
|
|
println!("cargo:rustc-link-lib=framework=WebRTC");
|
|
|
|
println!(
|
|
|
|
"cargo:rustc-link-search=framework={}",
|
|
|
|
swift_out_dir_path.display()
|
|
|
|
);
|
2022-09-16 19:09:11 +03:00
|
|
|
// Find WebRTC.framework as a sibling of the executable when running tests.
|
|
|
|
println!("cargo:rustc-link-arg=-Wl,-rpath,@executable_path");
|
2022-10-18 08:24:48 +03:00
|
|
|
// Find WebRTC.framework in parent directory of the executable when running examples.
|
|
|
|
println!("cargo:rustc-link-arg=-Wl,-rpath,@executable_path/..");
|
2022-09-06 17:56:49 +03:00
|
|
|
|
|
|
|
let source_path = swift_out_dir_path.join("WebRTC.framework");
|
2022-09-16 19:09:11 +03:00
|
|
|
let deps_dir_path =
|
|
|
|
PathBuf::from(env::var("OUT_DIR").unwrap()).join("../../../deps/WebRTC.framework");
|
|
|
|
let target_dir_path =
|
|
|
|
PathBuf::from(env::var("OUT_DIR").unwrap()).join("../../../WebRTC.framework");
|
|
|
|
copy_dir(&source_path, &deps_dir_path);
|
|
|
|
copy_dir(&source_path, &target_dir_path);
|
2022-09-02 21:56:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_swift_target() -> SwiftTarget {
|
|
|
|
let mut arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
|
|
|
|
if arch == "aarch64" {
|
|
|
|
arch = "arm64".into();
|
|
|
|
}
|
|
|
|
let target = format!("{}-apple-macosx{}", arch, MACOS_TARGET_VERSION);
|
|
|
|
|
|
|
|
let swift_target_info_str = Command::new("swift")
|
|
|
|
.args(&["-target", &target, "-print-target-info"])
|
|
|
|
.output()
|
|
|
|
.unwrap()
|
|
|
|
.stdout;
|
|
|
|
|
|
|
|
serde_json::from_slice(&swift_target_info_str).unwrap()
|
|
|
|
}
|
2022-09-06 17:56:49 +03:00
|
|
|
|
|
|
|
fn swift_package_root() -> PathBuf {
|
|
|
|
env::current_dir().unwrap().join(SWIFT_PACKAGE_NAME)
|
|
|
|
}
|
|
|
|
|
2022-09-16 19:09:11 +03:00
|
|
|
fn copy_dir(source: &Path, destination: &Path) {
|
|
|
|
assert!(
|
|
|
|
Command::new("cp")
|
|
|
|
.arg("-r")
|
|
|
|
.args(&[source, destination])
|
|
|
|
.status()
|
|
|
|
.unwrap()
|
|
|
|
.success(),
|
|
|
|
"could not copy {:?} to {:?}",
|
|
|
|
source,
|
|
|
|
destination
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-06 17:56:49 +03:00
|
|
|
impl SwiftTarget {
|
|
|
|
fn out_dir_path(&self) -> PathBuf {
|
|
|
|
swift_package_root()
|
|
|
|
.join(".build")
|
|
|
|
.join(&self.target.unversioned_triple)
|
|
|
|
.join(env::var("PROFILE").unwrap())
|
|
|
|
}
|
|
|
|
}
|