Skip to content

Commit eaaa12b

Browse files
committed
fix: Set environment variables in Tool from try_get_compiler
1 parent bf4f709 commit eaaa12b

File tree

1 file changed

+37
-18
lines changed

1 file changed

+37
-18
lines changed

src/lib.rs

+37-18
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ pub struct Tool {
196196
cc_wrapper_args: Vec<OsString>,
197197
args: Vec<OsString>,
198198
env: Vec<(OsString, OsString)>,
199+
env_remove: Vec<OsString>,
199200
family: ToolFamily,
200201
cuda: bool,
201202
removed_args: Vec<OsString>,
@@ -1482,12 +1483,8 @@ impl Build {
14821483
let (mut cmd, name) = if msvc && asm_ext == Some(AsmFileExt::DotAsm) {
14831484
self.msvc_macro_assembler()?
14841485
} else {
1485-
let mut cmd = compiler.to_command();
1486-
for &(ref a, ref b) in self.env.iter() {
1487-
cmd.env(a, b);
1488-
}
14891486
(
1490-
cmd,
1487+
compiler.to_command(),
14911488
compiler
14921489
.path
14931490
.file_name()
@@ -1518,9 +1515,6 @@ impl Build {
15181515
cmd.arg("--");
15191516
}
15201517
cmd.arg(&obj.src);
1521-
if cfg!(target_os = "macos") {
1522-
self.fix_env_for_apple_os(&mut cmd)?;
1523-
}
15241518

15251519
Ok((cmd, name))
15261520
}
@@ -1529,9 +1523,7 @@ impl Build {
15291523
pub fn try_expand(&self) -> Result<Vec<u8>, Error> {
15301524
let compiler = self.try_get_compiler()?;
15311525
let mut cmd = compiler.to_command();
1532-
for &(ref a, ref b) in self.env.iter() {
1533-
cmd.env(a, b);
1534-
}
1526+
15351527
cmd.arg("-E");
15361528

15371529
assert!(
@@ -1669,6 +1661,19 @@ impl Build {
16691661
cmd.push_cc_arg(warnings_to_errors_flag);
16701662
}
16711663

1664+
for (k, v) in self.env.iter() {
1665+
cmd.env.push((k.to_os_string(), v.to_os_string()));
1666+
}
1667+
1668+
if cfg!(target_os = "macos") {
1669+
for (k, v) in self.fixed_env_for_apple_os()? {
1670+
match v {
1671+
Some(v) => cmd.env.push((k, v)),
1672+
None => cmd.env_remove.push(k),
1673+
}
1674+
}
1675+
}
1676+
16721677
Ok(cmd)
16731678
}
16741679

@@ -3301,9 +3306,11 @@ impl Build {
33013306
}
33023307
}
33033308

3304-
fn fix_env_for_apple_os(&self, cmd: &mut Command) -> Result<(), Error> {
3309+
fn fixed_env_for_apple_os(&self) -> Result<HashMap<OsString, Option<OsString>>, Error> {
33053310
let target = self.get_target()?;
33063311
let host = self.get_host()?;
3312+
let mut env = HashMap::new();
3313+
33073314
if host.contains("apple-darwin") && target.contains("apple-darwin") {
33083315
// If, for example, `cargo` runs during the build of an XCode project, then `SDKROOT` environment variable
33093316
// would represent the current target, and this is the problem for us, if we want to compile something
@@ -3315,15 +3322,16 @@ impl Build {
33153322
if let Ok(sdkroot) = env::var("SDKROOT") {
33163323
if !sdkroot.contains("MacOSX") {
33173324
let macos_sdk = self.apple_sdk_root("macosx")?;
3318-
cmd.env("SDKROOT", macos_sdk);
3325+
env.insert("SDKROOT".into(), Some(macos_sdk));
33193326
}
33203327
}
33213328
// Additionally, `IPHONEOS_DEPLOYMENT_TARGET` must not be set when using the Xcode linker at
33223329
// "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld",
33233330
// although this is apparently ignored when using the linker at "/usr/bin/ld".
3324-
cmd.env_remove("IPHONEOS_DEPLOYMENT_TARGET");
3331+
env.insert("IPHONEOS_DEPLOYMENT_TARGET".into(), None);
33253332
}
3326-
Ok(())
3333+
3334+
Ok(env)
33273335
}
33283336

33293337
fn apple_sdk_root(&self, sdk: &str) -> Result<OsString, Error> {
@@ -3389,6 +3397,7 @@ impl Tool {
33893397
cc_wrapper_args: Vec::new(),
33903398
args: Vec::new(),
33913399
env: Vec::new(),
3400+
env_remove: Vec::new(),
33923401
family: family,
33933402
cuda: false,
33943403
removed_args: Vec::new(),
@@ -3420,6 +3429,7 @@ impl Tool {
34203429
cc_wrapper_args: Vec::new(),
34213430
args: Vec::new(),
34223431
env: Vec::new(),
3432+
env_remove: Vec::new(),
34233433
family: family,
34243434
cuda: cuda,
34253435
removed_args: Vec::new(),
@@ -3508,9 +3518,14 @@ impl Tool {
35083518
.collect::<Vec<_>>();
35093519
cmd.args(&value);
35103520

3511-
for &(ref k, ref v) in self.env.iter() {
3521+
for (k, v) in self.env.iter() {
35123522
cmd.env(k, v);
35133523
}
3524+
3525+
for k in self.env_remove.iter() {
3526+
cmd.env_remove(k);
3527+
}
3528+
35143529
cmd
35153530
}
35163531

@@ -3530,12 +3545,16 @@ impl Tool {
35303545

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

3552+
/// Returns the set of environment variables needed to be removed for this
3553+
/// compiler to operate.
3554+
pub fn env_remove(&self) -> &[OsString] {
3555+
&self.env_remove
3556+
}
3557+
35393558
/// Returns the compiler command in format of CC environment variable.
35403559
/// Or empty string if CC env was not present
35413560
///

0 commit comments

Comments
 (0)