From 5fec784580e6d43689cbfc7046f96149c71e499b Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Fri, 2 Sep 2022 11:42:39 +0200 Subject: [PATCH] Link Swift stdlib --- Cargo.lock | 2 + .../LiveKitObjC.xcodeproj/project.pbxproj | 8 --- crates/capture/Cargo.toml | 2 + crates/capture/build.rs | 56 +++++++++++++++++++ 4 files changed, 60 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 60e54aeb9f..bf60a08367 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -770,6 +770,8 @@ dependencies = [ "objc", "parking_lot 0.11.2", "postage", + "serde", + "serde_json", "simplelog", ] diff --git a/LiveKitObjC/LiveKitObjC.xcodeproj/project.pbxproj b/LiveKitObjC/LiveKitObjC.xcodeproj/project.pbxproj index 9c7a6136b3..db7445a28f 100644 --- a/LiveKitObjC/LiveKitObjC.xcodeproj/project.pbxproj +++ b/LiveKitObjC/LiveKitObjC.xcodeproj/project.pbxproj @@ -243,11 +243,8 @@ AFA4DBD228C0F7F5001AD7BE /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { - ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; - BUILD_LIBRARY_FOR_DISTRIBUTION = NO; CLANG_ENABLE_MODULES = YES; CODE_SIGN_STYLE = Automatic; - DEFINES_MODULE = NO; EXECUTABLE_PREFIX = lib; KEEP_PRIVATE_EXTERNS = NO; LD_RUNPATH_SEARCH_PATHS = ( @@ -256,7 +253,6 @@ "@loader_path/../Frameworks", ); PRODUCT_NAME = "$(TARGET_NAME)"; - SKIP_INSTALL = YES; SWIFT_OBJC_BRIDGING_HEADER = "LiveKitObjC-Bridging-Header.h"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; SWIFT_VERSION = 5.0; @@ -266,11 +262,8 @@ AFA4DBD328C0F7F5001AD7BE /* Release */ = { isa = XCBuildConfiguration; buildSettings = { - ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; - BUILD_LIBRARY_FOR_DISTRIBUTION = NO; CLANG_ENABLE_MODULES = YES; CODE_SIGN_STYLE = Automatic; - DEFINES_MODULE = NO; EXECUTABLE_PREFIX = lib; KEEP_PRIVATE_EXTERNS = NO; LD_RUNPATH_SEARCH_PATHS = ( @@ -279,7 +272,6 @@ "@loader_path/../Frameworks", ); PRODUCT_NAME = "$(TARGET_NAME)"; - SKIP_INSTALL = YES; SWIFT_OBJC_BRIDGING_HEADER = "LiveKitObjC-Bridging-Header.h"; SWIFT_VERSION = 5.0; }; diff --git a/crates/capture/Cargo.toml b/crates/capture/Cargo.toml index cdae75fc2b..339198f764 100644 --- a/crates/capture/Cargo.toml +++ b/crates/capture/Cargo.toml @@ -25,3 +25,5 @@ simplelog = "0.9" [build-dependencies] bindgen = "0.59.2" +serde = { version = "1.0", features = ["derive", "rc"] } +serde_json = { version = "1.0", features = ["preserve_order"] } \ No newline at end of file diff --git a/crates/capture/build.rs b/crates/capture/build.rs index 630cf1ed6b..b16ed18960 100644 --- a/crates/capture/build.rs +++ b/crates/capture/build.rs @@ -1,6 +1,62 @@ +use serde::Deserialize; use std::{env, path::PathBuf, process::Command}; +#[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, + pub runtime_library_import_paths: Vec, + pub runtime_resource_path: String, +} + +#[derive(Debug, Deserialize)] +pub struct SwiftTarget { + pub target: SwiftTargetInfo, + pub paths: SwiftPaths, +} + +const MACOS_TARGET_VERSION: &str = "12"; + +pub fn link_swift_libs() { + 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; + + let swift_target_info: SwiftTarget = serde_json::from_slice(&swift_target_info_str).unwrap(); + if swift_target_info.target.libraries_require_rpath { + panic!("Libraries require RPath! Change minimum MacOS value to fix.") + } + + swift_target_info + .paths + .runtime_library_paths + .iter() + .for_each(|path| { + println!("cargo:rustc-link-search=native={}", path); + }); +} + fn main() { + link_swift_libs(); println!("cargo:rerun-if-changed=/Users/as-cii/Library/Developer/Xcode/DerivedData/LiveKitObjC-ftgpxknhsgkrocbhhgjkyyvkgkbj/Build/Products/Debug/libLiveKitObjC.a"); println!("cargo:rustc-link-search=/Users/as-cii/Library/Developer/Xcode/DerivedData/LiveKitObjC-ftgpxknhsgkrocbhhgjkyyvkgkbj/Build/Products/Debug"); println!("cargo:rustc-link-lib=static=LiveKitObjC");