-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
90ce423
commit 75323ad
Showing
2 changed files
with
69 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,83 @@ | ||
#![cfg(any(target_os = "macos", target_os = "ios"))] | ||
// This script should not run on any platform besides macOS, but making the whole file conditional | ||
// results in `cargo` complaining about there being no `main()` method in build.rs. | ||
#[cfg(not(any(target_os = "macos", target_os = "ios")))] | ||
fn main() {} | ||
|
||
#[cfg(any(target_os = "macos", target_os = "ios"))] | ||
fn main() { | ||
link_swift(); | ||
link_swift_package("cryptokit-bridge", "./cryptokit-bridge/"); | ||
swift::configure(); | ||
swift::link_package("cryptokit-bridge", "./cryptokit-bridge/"); | ||
} | ||
|
||
// Copied from https://github.com/Brendonovich/swift-rs | ||
// With get_swift_target_info changed to print the current target | ||
// rather than always trying for the macosx target. | ||
#[cfg(any(target_os = "macos", target_os = "ios"))] | ||
mod swift { | ||
use serde::Deserialize; | ||
use std::{env, process::Command}; | ||
|
||
use std::{env, process::Command}; | ||
#[derive(Debug, Deserialize)] | ||
struct SwiftTargetInfo { | ||
#[serde(rename = "unversionedTriple")] | ||
pub unversioned_triple: String, | ||
#[serde(rename = "librariesRequireRPath")] | ||
pub libraries_require_rpath: bool, | ||
} | ||
|
||
use serde::Deserialize; | ||
#[derive(Debug, Deserialize)] | ||
struct SwiftPaths { | ||
#[serde(rename = "runtimeLibraryPaths")] | ||
pub runtime_library_paths: Vec<String>, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct SwiftTargetInfo { | ||
pub unversioned_triple: String, | ||
#[serde(rename = "librariesRequireRPath")] | ||
pub libraries_require_rpath: bool, | ||
} | ||
#[derive(Debug, Deserialize)] | ||
struct SwiftTarget { | ||
pub target: SwiftTargetInfo, | ||
pub paths: SwiftPaths, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct SwiftPaths { | ||
pub runtime_library_paths: Vec<String>, | ||
pub runtime_resource_path: String, | ||
} | ||
fn get_target_info() -> SwiftTarget { | ||
let swift_target_info_str = Command::new("swift") | ||
.args(["-print-target-info"]) | ||
.output() | ||
.unwrap() | ||
.stdout; | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct SwiftTarget { | ||
pub target: SwiftTargetInfo, | ||
pub paths: SwiftPaths, | ||
} | ||
|
||
pub fn get_swift_target_info() -> SwiftTarget { | ||
let swift_target_info_str = Command::new("swift") | ||
.args(["-print-target-info"]) | ||
.output() | ||
.unwrap() | ||
.stdout; | ||
serde_json::from_slice(&swift_target_info_str).unwrap() | ||
} | ||
|
||
serde_json::from_slice(&swift_target_info_str).unwrap() | ||
} | ||
pub fn configure() { | ||
let swift_target_info = get_target_info(); | ||
if swift_target_info.target.libraries_require_rpath { | ||
panic!("Libraries require RPath! Change minimum MacOS value to fix.") | ||
} | ||
|
||
pub fn link_swift() { | ||
let swift_target_info = get_swift_target_info(); | ||
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); | ||
}); | ||
} | ||
|
||
swift_target_info | ||
.paths | ||
.runtime_library_paths | ||
.iter() | ||
.for_each(|path| { | ||
println!("cargo:rustc-link-search=native={}", path); | ||
}); | ||
} | ||
pub fn link_package(package_name: &str, package_root: &str) { | ||
let profile = env::var("PROFILE").unwrap(); | ||
|
||
pub fn link_swift_package(package_name: &str, package_root: &str) { | ||
let profile = env::var("PROFILE").unwrap(); | ||
dbg!(&profile); | ||
if !Command::new("swift") | ||
.args(["build", "-c", &profile]) | ||
.current_dir(package_root) | ||
.status() | ||
.unwrap() | ||
.success() | ||
{ | ||
panic!("Failed to compile swift package {}", package_name); | ||
} | ||
|
||
if !Command::new("swift") | ||
.args(["build", "-c", &profile]) | ||
.current_dir(package_root) | ||
.status() | ||
.unwrap() | ||
.success() | ||
{ | ||
panic!("Failed to compile swift package {}", package_name); | ||
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); | ||
let swift_target_info = get_target_info(); | ||
println!( | ||
"cargo:rustc-link-search=native={}/{}.build/{}/{}", | ||
manifest_dir, package_root, swift_target_info.target.unversioned_triple, profile | ||
); | ||
println!("cargo:rustc-link-lib=static={}", package_name); | ||
} | ||
|
||
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); | ||
let swift_target_info = get_swift_target_info(); | ||
println!( | ||
"cargo:rustc-link-search=native={}/{}.build/{}/{}", | ||
manifest_dir, package_root, swift_target_info.target.unversioned_triple, profile | ||
); | ||
println!("cargo:rustc-link-lib=static={}", package_name); | ||
} |