Skip to content

Fix wrong SDK being used for the host on macOS #7284

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

Closed
wants to merge 1 commit into from
Closed
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
30 changes: 29 additions & 1 deletion src/cargo/core/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::path::PathBuf;
use semver::Version;

use super::BuildContext;
use crate::core::{Edition, Package, PackageId, Target};
use crate::core::{compiler::Kind, Edition, Package, PackageId, Target};
use crate::util::{self, join_paths, process, CargoResult, CfgExpr, Config, ProcessBuilder};

pub struct Doctest {
Expand Down Expand Up @@ -117,6 +117,7 @@ impl<'cfg> Compilation<'cfg> {
&self,
pkg: &Package,
target: &Target,
kind: Kind,
is_primary: bool,
) -> CargoResult<ProcessBuilder> {
let rustc = if is_primary {
Expand All @@ -128,6 +129,15 @@ impl<'cfg> Compilation<'cfg> {
};

let mut p = self.fill_env(rustc, pkg, true)?;
// Invoking cargo build from an Xcode project targeting iOS or iOS Simulator used to fail to
// build any crate with a custom build script, because the wrong linker was invoked for the
// custom build script. This is because Xcode sets SDKROOT for the target platform (the
// build script must be built for the host platform instead)
if kind == Kind::Host {
#[cfg(target_os = "macos")]
p.env("SDKROOT", get_sdk_root("macosx")?);
}

if target.edition() != Edition::Edition2015 {
p.arg(format!("--edition={}", target.edition()));
}
Expand Down Expand Up @@ -308,3 +318,21 @@ fn target_runner(bcx: &BuildContext<'_, '_>) -> CargoResult<Option<(PathBuf, Vec

Ok(None)
}

#[cfg(target_os = "macos")]
fn get_sdk_root(sdk_name: &'_ str) -> CargoResult<String> {
let res = process("xcrun")
.arg("--show-sdk-path")
.arg("--sdk")
.arg(sdk_name)
.exec_with_output();

match res {
Ok(output) => Ok(String::from_utf8(output.stdout).unwrap().trim().to_string()),
Err(e) => Err(failure::format_err!(
"failed to get {} SDK path: {}",
sdk_name,
e
)),
}
}
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ fn prepare_rustc<'a, 'cfg>(

let mut base = cx
.compilation
.rustc_process(unit.pkg, unit.target, is_primary)?;
.rustc_process(unit.pkg, unit.target, unit.kind, is_primary)?;
base.inherit_jobserver(&cx.jobserver);
build_base_args(cx, &mut base, unit, crate_types)?;
build_deps_args(&mut base, cx, unit)?;
Expand Down