Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: locate sdkroot required since big sur #362

Merged
merged 1 commit into from
Jan 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/mun_codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ inkwell = { version = "=0.1.0-beta.2", features = ["llvm11-0", "no-libffi-linkin
by_address = "1.0.4"
paths = { version="=0.1.0", path="../mun_paths", package="mun_paths"}
smallvec = "1.6.1"
once_cell = "1.4.0"

[dev-dependencies]
abi = { path = "../mun_abi", package = "mun_abi" }
Expand Down
39 changes: 39 additions & 0 deletions crates/mun_codegen/src/apple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use once_cell::sync::OnceCell;
use std::{
env, io,
path::{Path, PathBuf},
process::Command,
};

/// Finds the Apple SDK root directory by checking the `SDKROOT` environment variable or running
/// `xcrun --show-sdk-path`. The result is cached so multiple calls to this function should be
/// fast.
pub fn get_apple_sdk_root() -> Result<&'static Path, String> {
static SDK_PATH: OnceCell<PathBuf> = OnceCell::new();

SDK_PATH
.get_or_try_init(|| {
if let Ok(sdkroot) = env::var("SDKROOT") {
return Ok(PathBuf::from(sdkroot));
}

let res = Command::new("xcrun")
.arg("--show-sdk-path")
.output()
.and_then(|output| {
if output.status.success() {
Ok(String::from_utf8(output.stdout).unwrap())
} else {
let error = String::from_utf8(output.stderr);
let error = format!("process exit with error: {}", error.unwrap());
Err(io::Error::new(io::ErrorKind::Other, &error[..]))
}
});

match res {
Ok(output) => Ok(PathBuf::from(output.trim())),
Err(e) => Err(format!("failed to get SDK path: {}", e)),
}
})
.map(AsRef::as_ref)
}
1 change: 1 addition & 0 deletions crates/mun_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ mod test;

pub mod value;

mod apple;
pub(crate) mod intrinsics;
mod linker;
mod module_group;
Expand Down
10 changes: 10 additions & 0 deletions crates/mun_codegen/src/linker.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::apple::get_apple_sdk_root;
use mun_target::spec;
use mun_target::spec::LinkerFlavor;
use std::fmt;
Expand All @@ -11,6 +12,9 @@ pub enum LinkerError {

/// Error in path conversion
PathError(PathBuf),

/// Could not locate platform SDK
PlatformSdkMissing(String),
}

impl fmt::Display for LinkerError {
Expand All @@ -22,6 +26,9 @@ impl fmt::Display for LinkerError {
"path contains invalid UTF-8 characters: {}",
path.display()
),
LinkerError::PlatformSdkMissing(err) => {
write!(f, "could not find platform sdk: {}", err)
}
}
}
}
Expand Down Expand Up @@ -119,6 +126,9 @@ impl Linker for Ld64Linker {

// Link as dynamic library
self.args.push("-dylib".to_owned());

let sdk_root = get_apple_sdk_root().map_err(LinkerError::PlatformSdkMissing)?;
self.args.push(format!("-L{}/usr/lib", sdk_root.display()));
self.args.push("-lsystem".to_owned());

// Specify output path
Expand Down
5 changes: 1 addition & 4 deletions crates/mun_target/src/spec/apple_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ fn macos_deployment_target() -> (u32, u32) {
let deployment_target = env::var("MACOSX_DEPLOYMENT_TARGET").ok();
let version = deployment_target
.as_ref()
.and_then(|s| {
let mut i = s.splitn(2, '.');
i.next().and_then(|a| i.next().map(|b| (a, b)))
})
.and_then(|s| s.split_once('.'))
.and_then(|(a, b)| {
a.parse::<u32>()
.and_then(|a| b.parse::<u32>().map(|b| (a, b)))
Expand Down