Skip to content

Commit e715d03

Browse files
committed
Remove env vars instead of setting them to an empty string
1 parent 2e9eba7 commit e715d03

File tree

6 files changed

+33
-9
lines changed

6 files changed

+33
-9
lines changed

src/librustc_codegen_ssa/back/command.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub struct Command {
1414
program: Program,
1515
args: Vec<OsString>,
1616
env: Vec<(OsString, OsString)>,
17+
env_remove: Vec<OsString>,
1718
}
1819

1920
#[derive(Clone)]
@@ -41,6 +42,7 @@ impl Command {
4142
program,
4243
args: Vec::new(),
4344
env: Vec::new(),
45+
env_remove: Vec::new(),
4446
}
4547
}
4648

@@ -75,6 +77,17 @@ impl Command {
7577
self.env.push((key.to_owned(), value.to_owned()));
7678
}
7779

80+
pub fn env_remove<K>(&mut self, key: K) -> &mut Command
81+
where K: AsRef<OsStr>,
82+
{
83+
self._env_remove(key.as_ref());
84+
self
85+
}
86+
87+
fn _env_remove(&mut self, key: &OsStr) {
88+
self.env_remove.push(key.to_owned());
89+
}
90+
7891
pub fn output(&mut self) -> io::Result<Output> {
7992
self.command().output()
8093
}
@@ -100,6 +113,9 @@ impl Command {
100113
};
101114
ret.args(&self.args);
102115
ret.envs(self.env.clone());
116+
for k in &self.env_remove {
117+
ret.env_remove(k);
118+
}
103119
return ret
104120
}
105121

src/librustc_codegen_ssa/back/link.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,9 @@ fn link_natively<'a, B: ArchiveBuilder<'a>>(sess: &'a Session,
532532
for &(ref k, ref v) in &sess.target.target.options.link_env {
533533
cmd.env(k, v);
534534
}
535+
for k in &sess.target.target.options.link_env_remove {
536+
cmd.env_remove(k);
537+
}
535538

536539
if sess.opts.debugging_opts.print_link_args {
537540
println!("{:?}", &cmd);

src/librustc_target/spec/apple_base.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,18 @@ pub fn macos_llvm_target(arch: &str) -> String {
5252
format!("{}-apple-macosx{}.{}.0", arch, major, minor)
5353
}
5454

55-
pub fn macos_link_env() -> Vec<(String, String)> {
56-
let mut env = Vec::with_capacity(2);
57-
// Ignore the `SDKROOT` environment variable if it's clearly set for the wrong platform, which
55+
pub fn macos_link_env_remove() -> Vec<String> {
56+
let mut env_remove = Vec::with_capacity(2);
57+
// Remove the `SDKROOT` environment variable if it's clearly set for the wrong platform, which
5858
// may occur when we're linking a custom build script while targeting iOS for example.
5959
if let Some(sdkroot) = env::var("SDKROOT").ok() {
6060
if sdkroot.contains("iPhoneOS.platform") || sdkroot.contains("iPhoneSimulator.platform") {
61-
env.push(("SDKROOT".to_string(), String::new()))
61+
env_remove.push("SDKROOT".to_string())
6262
}
6363
}
6464
// Additionally, `IPHONEOS_DEPLOYMENT_TARGET` must not be set when using the Xcode linker at
6565
// "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld",
6666
// although this is apparently ignored when using the linker at "/usr/bin/ld".
67-
env.push(("IPHONEOS_DEPLOYMENT_TARGET".to_string(), String::new()));
68-
env
67+
env_remove.push("IPHONEOS_DEPLOYMENT_TARGET".to_string());
68+
env_remove
6969
}

src/librustc_target/spec/i686_apple_darwin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub fn target() -> TargetResult {
55
base.cpu = "yonah".to_string();
66
base.max_atomic_width = Some(64);
77
base.pre_link_args.insert(LinkerFlavor::Gcc, vec!["-m32".to_string()]);
8-
base.link_env.extend(super::apple_base::macos_link_env());
8+
base.link_env_remove.extend(super::apple_base::macos_link_env_remove());
99
base.stack_probes = true;
1010
base.eliminate_frame_pointer = false;
1111

src/librustc_target/spec/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,8 +580,10 @@ pub struct TargetOptions {
580580
/// user-defined libraries.
581581
pub post_link_args: LinkArgs,
582582

583-
/// Environment variables to be set before invoking the linker.
583+
/// Environment variables to be set for the linker invocation.
584584
pub link_env: Vec<(String, String)>,
585+
/// Environment variables to be removed for the linker invocation.
586+
pub link_env_remove: Vec<String>,
585587

586588
/// Extra arguments to pass to the external assembler (when used)
587589
pub asm_args: Vec<String>,
@@ -843,6 +845,7 @@ impl Default for TargetOptions {
843845
post_link_objects_crt: Vec::new(),
844846
late_link_args: LinkArgs::new(),
845847
link_env: Vec::new(),
848+
link_env_remove: Vec::new(),
846849
archive_format: "gnu".to_string(),
847850
custom_unwind_resume: false,
848851
allow_asm: true,
@@ -1118,6 +1121,7 @@ impl Target {
11181121
key!(post_link_objects_crt, list);
11191122
key!(post_link_args, link_args);
11201123
key!(link_env, env);
1124+
key!(link_env_remove, list);
11211125
key!(asm_args, list);
11221126
key!(cpu);
11231127
key!(features);
@@ -1334,6 +1338,7 @@ impl ToJson for Target {
13341338
target_option_val!(post_link_objects_crt);
13351339
target_option_val!(link_args - post_link_args);
13361340
target_option_val!(env - link_env);
1341+
target_option_val!(link_env_remove);
13371342
target_option_val!(asm_args);
13381343
target_option_val!(cpu);
13391344
target_option_val!(features);

src/librustc_target/spec/x86_64_apple_darwin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub fn target() -> TargetResult {
66
base.max_atomic_width = Some(128); // core2 support cmpxchg16b
77
base.eliminate_frame_pointer = false;
88
base.pre_link_args.insert(LinkerFlavor::Gcc, vec!["-m64".to_string()]);
9-
base.link_env.extend(super::apple_base::macos_link_env());
9+
base.link_env_remove.extend(super::apple_base::macos_link_env_remove());
1010
base.stack_probes = true;
1111

1212
// Clang automatically chooses a more specific target based on

0 commit comments

Comments
 (0)