Skip to content

Commit

Permalink
fix: Set environment variables in Tool from try_get_compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
oblique committed Aug 9, 2023
1 parent bf4f709 commit bbfda2c
Showing 1 changed file with 37 additions and 18 deletions.
55 changes: 37 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ pub struct Tool {
cc_wrapper_args: Vec<OsString>,
args: Vec<OsString>,
env: Vec<(OsString, OsString)>,
env_remove: Vec<OsString>,
family: ToolFamily,
cuda: bool,
removed_args: Vec<OsString>,
Expand Down Expand Up @@ -1482,12 +1483,8 @@ impl Build {
let (mut cmd, name) = if msvc && asm_ext == Some(AsmFileExt::DotAsm) {
self.msvc_macro_assembler()?
} else {
let mut cmd = compiler.to_command();
for &(ref a, ref b) in self.env.iter() {
cmd.env(a, b);
}
(
cmd,
compiler.to_command(),
compiler
.path
.file_name()
Expand Down Expand Up @@ -1518,9 +1515,6 @@ impl Build {
cmd.arg("--");
}
cmd.arg(&obj.src);
if cfg!(target_os = "macos") {
self.fix_env_for_apple_os(&mut cmd)?;
}

Ok((cmd, name))
}
Expand All @@ -1529,9 +1523,7 @@ impl Build {
pub fn try_expand(&self) -> Result<Vec<u8>, Error> {
let compiler = self.try_get_compiler()?;
let mut cmd = compiler.to_command();
for &(ref a, ref b) in self.env.iter() {
cmd.env(a, b);
}

cmd.arg("-E");

assert!(
Expand Down Expand Up @@ -1669,6 +1661,19 @@ impl Build {
cmd.push_cc_arg(warnings_to_errors_flag);
}

for (k, v) in self.env.iter() {
cmd.env.push((k.to_os_string(), v.to_os_string()));
}

if cfg!(target_os = "macos") {
for (k, v) in self.fixed_env_for_apple_os()?.into_iter() {
match v {
Some(v) => cmd.env.push((k, v)),
None => cmd.env_remove.push(k),
}
}
}

Ok(cmd)
}

Expand Down Expand Up @@ -3301,9 +3306,11 @@ impl Build {
}
}

fn fix_env_for_apple_os(&self, cmd: &mut Command) -> Result<(), Error> {
fn fixed_env_for_apple_os(&self) -> Result<HashMap<OsString, Option<OsString>>, Error> {
let target = self.get_target()?;
let host = self.get_host()?;
let mut env = HashMap::new();

if host.contains("apple-darwin") && target.contains("apple-darwin") {
// If, for example, `cargo` runs during the build of an XCode project, then `SDKROOT` environment variable
// would represent the current target, and this is the problem for us, if we want to compile something
Expand All @@ -3315,15 +3322,16 @@ impl Build {
if let Ok(sdkroot) = env::var("SDKROOT") {
if !sdkroot.contains("MacOSX") {
let macos_sdk = self.apple_sdk_root("macosx")?;
cmd.env("SDKROOT", macos_sdk);
env.insert("SDKROOT".into(), Some(macos_sdk));
}
}
// Additionally, `IPHONEOS_DEPLOYMENT_TARGET` must not be set when using the Xcode linker at
// "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld",
// although this is apparently ignored when using the linker at "/usr/bin/ld".
cmd.env_remove("IPHONEOS_DEPLOYMENT_TARGET");
env.insert("IPHONEOS_DEPLOYMENT_TARGET".into(), None);
}
Ok(())

Ok(env)
}

fn apple_sdk_root(&self, sdk: &str) -> Result<OsString, Error> {
Expand Down Expand Up @@ -3389,6 +3397,7 @@ impl Tool {
cc_wrapper_args: Vec::new(),
args: Vec::new(),
env: Vec::new(),
env_remove: Vec::new(),
family: family,
cuda: false,
removed_args: Vec::new(),
Expand Down Expand Up @@ -3420,6 +3429,7 @@ impl Tool {
cc_wrapper_args: Vec::new(),
args: Vec::new(),
env: Vec::new(),
env_remove: Vec::new(),
family: family,
cuda: cuda,
removed_args: Vec::new(),
Expand Down Expand Up @@ -3508,9 +3518,14 @@ impl Tool {
.collect::<Vec<_>>();
cmd.args(&value);

for &(ref k, ref v) in self.env.iter() {
for (k, v) in self.env.iter() {
cmd.env(k, v);
}

for k in self.env_remove.iter() {
cmd.env_remove(k);
}

cmd
}

Expand All @@ -3530,12 +3545,16 @@ impl Tool {

/// Returns the set of environment variables needed for this compiler to
/// operate.
///
/// This is typically only used for MSVC compilers currently.
pub fn env(&self) -> &[(OsString, OsString)] {
&self.env
}

/// Returns the set of environment variables needed to be removed for this
/// compiler to operate.
pub fn env_remove(&self) -> &[OsString] {
&self.env_remove
}

/// Returns the compiler command in format of CC environment variable.
/// Or empty string if CC env was not present
///
Expand Down

0 comments on commit bbfda2c

Please sign in to comment.