Skip to content

Commit 68f7b82

Browse files
committed
Use CARGO_ENCODED_RUSTFLAGS to support paths with spaces
Fixes #1391
1 parent 85a99b3 commit 68f7b82

File tree

7 files changed

+94
-64
lines changed

7 files changed

+94
-64
lines changed

build_system/build_backend.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use std::env;
21
use std::path::PathBuf;
32

43
use crate::path::{Dirs, RelPath};
54
use crate::rustc_info::get_file_name;
5+
use crate::shared_utils::{rustflags_from_env, rustflags_to_cmd_env};
66
use crate::utils::{is_ci, is_ci_opt, maybe_incremental, CargoProject, Compiler, LogGroup};
77

88
pub(crate) static CG_CLIF: CargoProject = CargoProject::new(&RelPath::SOURCE, "cg_clif");
@@ -18,11 +18,11 @@ pub(crate) fn build_backend(
1818
let mut cmd = CG_CLIF.build(&bootstrap_host_compiler, dirs);
1919
maybe_incremental(&mut cmd);
2020

21-
let mut rustflags = env::var("RUSTFLAGS").unwrap_or_default();
21+
let mut rustflags = rustflags_from_env("RUSTFLAGS");
2222

2323
if is_ci() {
2424
// Deny warnings on CI
25-
rustflags += " -Dwarnings";
25+
rustflags.push("-Dwarnings".to_owned());
2626

2727
if !is_ci_opt() {
2828
cmd.env("CARGO_PROFILE_RELEASE_DEBUG_ASSERTIONS", "true");
@@ -42,7 +42,7 @@ pub(crate) fn build_backend(
4242
_ => unreachable!(),
4343
}
4444

45-
cmd.env("RUSTFLAGS", rustflags);
45+
rustflags_to_cmd_env(&mut cmd, "RUSTFLAGS", &rustflags);
4646

4747
eprintln!("[BUILD] rustc_codegen_cranelift");
4848
crate::utils::spawn_and_wait(cmd);

build_system/build_sysroot.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ pub(crate) fn build_sysroot(
128128
cargo: bootstrap_host_compiler.cargo.clone(),
129129
rustc: rustc_clif.clone(),
130130
rustdoc: rustdoc_clif.clone(),
131-
rustflags: String::new(),
132-
rustdocflags: String::new(),
131+
rustflags: vec![],
132+
rustdocflags: vec![],
133133
triple: target_triple,
134134
runner: vec![],
135135
}
@@ -241,25 +241,25 @@ fn build_clif_sysroot_for_triple(
241241
}
242242

243243
// Build sysroot
244-
let mut rustflags = " -Zforce-unstable-if-unmarked -Cpanic=abort".to_string();
244+
let mut rustflags = vec!["-Zforce-unstable-if-unmarked".to_owned(), "-Cpanic=abort".to_owned()];
245245
match cg_clif_dylib_path {
246246
CodegenBackend::Local(path) => {
247-
rustflags.push_str(&format!(" -Zcodegen-backend={}", path.to_str().unwrap()));
247+
rustflags.push(format!("-Zcodegen-backend={}", path.to_str().unwrap()));
248248
}
249249
CodegenBackend::Builtin(name) => {
250-
rustflags.push_str(&format!(" -Zcodegen-backend={name}"));
250+
rustflags.push(format!("-Zcodegen-backend={name}"));
251251
}
252252
};
253253
// Necessary for MinGW to find rsbegin.o and rsend.o
254-
rustflags
255-
.push_str(&format!(" --sysroot {}", RTSTARTUP_SYSROOT.to_path(dirs).to_str().unwrap()));
254+
rustflags.push("--sysroot".to_owned());
255+
rustflags.push(RTSTARTUP_SYSROOT.to_path(dirs).to_str().unwrap().to_owned());
256256
if channel == "release" {
257257
// Incremental compilation by default disables mir inlining. This leads to both a decent
258258
// compile perf and a significant runtime perf regression. As such forcefully enable mir
259259
// inlining.
260-
rustflags.push_str(" -Zinline-mir");
260+
rustflags.push("-Zinline-mir".to_owned());
261261
}
262-
compiler.rustflags += &rustflags;
262+
compiler.rustflags.extend(rustflags);
263263
let mut build_cmd = STANDARD_LIBRARY.build(&compiler, dirs);
264264
maybe_incremental(&mut build_cmd);
265265
if channel == "release" {

build_system/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ mod config;
1616
mod path;
1717
mod prepare;
1818
mod rustc_info;
19+
mod shared_utils;
1920
mod tests;
2021
mod utils;
2122

@@ -169,8 +170,8 @@ fn main() {
169170
cargo,
170171
rustc,
171172
rustdoc,
172-
rustflags: String::new(),
173-
rustdocflags: String::new(),
173+
rustflags: vec![],
174+
rustdocflags: vec![],
174175
triple,
175176
runner: vec![],
176177
}

build_system/shared_utils.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// This file is used by both the build system as well as cargo-clif.rs
2+
3+
// Adapted from https://github.com/rust-lang/cargo/blob/6dc1deaddf62c7748c9097c7ea88e9ec77ff1a1a/src/cargo/core/compiler/build_context/target_info.rs#L750-L77
4+
pub(crate) fn rustflags_from_env(kind: &str) -> Vec<String> {
5+
// First try CARGO_ENCODED_RUSTFLAGS from the environment.
6+
// Prefer this over RUSTFLAGS since it's less prone to encoding errors.
7+
if let Ok(a) = std::env::var(format!("CARGO_ENCODED_{}", kind)) {
8+
if a.is_empty() {
9+
return Vec::new();
10+
}
11+
return a.split('\x1f').map(str::to_string).collect();
12+
}
13+
14+
// Then try RUSTFLAGS from the environment
15+
if let Ok(a) = std::env::var(kind) {
16+
let args = a.split(' ').map(str::trim).filter(|s| !s.is_empty()).map(str::to_string);
17+
return args.collect();
18+
}
19+
20+
// No rustflags to be collected from the environment
21+
Vec::new()
22+
}
23+
24+
pub(crate) fn rustflags_to_cmd_env(cmd: &mut std::process::Command, kind: &str, flags: &[String]) {
25+
cmd.env(format!("CARGO_ENCODED_{}", kind), flags.join("\x1f"));
26+
}

build_system/tests.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::env;
21
use std::ffi::OsStr;
32
use std::fs;
43
use std::path::PathBuf;
@@ -9,6 +8,7 @@ use crate::config;
98
use crate::path::{Dirs, RelPath};
109
use crate::prepare::{apply_patches, GitRepo};
1110
use crate::rustc_info::get_default_sysroot;
11+
use crate::shared_utils::rustflags_from_env;
1212
use crate::utils::{spawn_and_wait, spawn_and_wait_with_input, CargoProject, Compiler, LogGroup};
1313
use crate::{CodegenBackend, SysrootKind};
1414

@@ -307,7 +307,7 @@ pub(crate) fn run_tests(
307307
);
308308
// Rust's build system denies a couple of lints that trigger on several of the test
309309
// projects. Changing the code to fix them is not worth it, so just silence all lints.
310-
target_compiler.rustflags += " --cap-lints=allow";
310+
target_compiler.rustflags.push("--cap-lints=allow".to_owned());
311311

312312
let runner = TestRunner::new(
313313
dirs.clone(),
@@ -351,18 +351,15 @@ impl<'a> TestRunner<'a> {
351351
is_native: bool,
352352
stdlib_source: PathBuf,
353353
) -> Self {
354-
if let Ok(rustflags) = env::var("RUSTFLAGS") {
355-
target_compiler.rustflags.push(' ');
356-
target_compiler.rustflags.push_str(&rustflags);
357-
}
358-
if let Ok(rustdocflags) = env::var("RUSTDOCFLAGS") {
359-
target_compiler.rustdocflags.push(' ');
360-
target_compiler.rustdocflags.push_str(&rustdocflags);
361-
}
354+
target_compiler.rustflags.extend(rustflags_from_env("RUSTFLAGS"));
355+
target_compiler.rustdocflags.extend(rustflags_from_env("RUSTDOCFLAGS"));
362356

363357
// FIXME fix `#[linkage = "extern_weak"]` without this
364358
if target_compiler.triple.contains("darwin") {
365-
target_compiler.rustflags.push_str(" -Clink-arg=-undefined -Clink-arg=dynamic_lookup");
359+
target_compiler.rustflags.extend([
360+
"-Clink-arg=-undefined".to_owned(),
361+
"-Clink-arg=dynamic_lookup".to_owned(),
362+
]);
366363
}
367364

368365
let jit_supported = use_unstable_features
@@ -471,7 +468,7 @@ impl<'a> TestRunner<'a> {
471468
S: AsRef<OsStr>,
472469
{
473470
let mut cmd = Command::new(&self.target_compiler.rustc);
474-
cmd.args(self.target_compiler.rustflags.split_whitespace());
471+
cmd.args(&self.target_compiler.rustflags);
475472
cmd.arg("-L");
476473
cmd.arg(format!("crate={}", BUILD_EXAMPLE_OUT_DIR.to_path(&self.dirs).display()));
477474
cmd.arg("--out-dir");

build_system/utils.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ use std::process::{self, Command, Stdio};
66
use std::sync::atomic::{AtomicBool, Ordering};
77

88
use crate::path::{Dirs, RelPath};
9+
use crate::shared_utils::rustflags_to_cmd_env;
910

1011
#[derive(Clone, Debug)]
1112
pub(crate) struct Compiler {
1213
pub(crate) cargo: PathBuf,
1314
pub(crate) rustc: PathBuf,
1415
pub(crate) rustdoc: PathBuf,
15-
pub(crate) rustflags: String,
16-
pub(crate) rustdocflags: String,
16+
pub(crate) rustflags: Vec<String>,
17+
pub(crate) rustdocflags: Vec<String>,
1718
pub(crate) triple: String,
1819
pub(crate) runner: Vec<String>,
1920
}
@@ -23,8 +24,8 @@ impl Compiler {
2324
match self.triple.as_str() {
2425
"aarch64-unknown-linux-gnu" => {
2526
// We are cross-compiling for aarch64. Use the correct linker and run tests in qemu.
26-
self.rustflags += " -Clinker=aarch64-linux-gnu-gcc";
27-
self.rustdocflags += " -Clinker=aarch64-linux-gnu-gcc";
27+
self.rustflags.push("-Clinker=aarch64-linux-gnu-gcc".to_owned());
28+
self.rustdocflags.push("-Clinker=aarch64-linux-gnu-gcc".to_owned());
2829
self.runner = vec![
2930
"qemu-aarch64".to_owned(),
3031
"-L".to_owned(),
@@ -33,8 +34,8 @@ impl Compiler {
3334
}
3435
"s390x-unknown-linux-gnu" => {
3536
// We are cross-compiling for s390x. Use the correct linker and run tests in qemu.
36-
self.rustflags += " -Clinker=s390x-linux-gnu-gcc";
37-
self.rustdocflags += " -Clinker=s390x-linux-gnu-gcc";
37+
self.rustflags.push("-Clinker=s390x-linux-gnu-gcc".to_owned());
38+
self.rustdocflags.push("-Clinker=s390x-linux-gnu-gcc".to_owned());
3839
self.runner = vec![
3940
"qemu-s390x".to_owned(),
4041
"-L".to_owned(),
@@ -100,8 +101,8 @@ impl CargoProject {
100101

101102
cmd.env("RUSTC", &compiler.rustc);
102103
cmd.env("RUSTDOC", &compiler.rustdoc);
103-
cmd.env("RUSTFLAGS", &compiler.rustflags);
104-
cmd.env("RUSTDOCFLAGS", &compiler.rustdocflags);
104+
rustflags_to_cmd_env(&mut cmd, "RUSTFLAGS", &compiler.rustflags);
105+
rustflags_to_cmd_env(&mut cmd, "RUSTDOCFLAGS", &compiler.rustdocflags);
105106
if !compiler.runner.is_empty() {
106107
cmd.env(
107108
format!("CARGO_TARGET_{}_RUNNER", compiler.triple.to_uppercase().replace('-', "_")),

scripts/cargo-clif.rs

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,28 @@ use std::env;
33
use std::os::unix::process::CommandExt;
44
use std::process::Command;
55

6+
include!("../build_system/shared_utils.rs");
7+
68
fn main() {
79
let current_exe = env::current_exe().unwrap();
810
let mut sysroot = current_exe.parent().unwrap();
911
if sysroot.file_name().unwrap().to_str().unwrap() == "bin" {
1012
sysroot = sysroot.parent().unwrap();
1113
}
1214

13-
let mut rustflags = String::new();
14-
rustflags.push_str(" -Cpanic=abort -Zpanic-abort-tests -Zcodegen-backend=");
15+
let mut rustflags = vec!["-Cpanic=abort".to_owned(), "-Zpanic-abort-tests".to_owned()];
1516
if let Some(name) = option_env!("BUILTIN_BACKEND") {
16-
rustflags.push_str(name);
17+
rustflags.push(format!("-Zcodegen-backend={name}"));
1718
} else {
18-
rustflags.push_str(
19-
sysroot
20-
.join(if cfg!(windows) { "bin" } else { "lib" })
21-
.join(
22-
env::consts::DLL_PREFIX.to_string()
23-
+ "rustc_codegen_cranelift"
24-
+ env::consts::DLL_SUFFIX,
25-
)
26-
.to_str()
27-
.unwrap(),
19+
let dylib = sysroot.join(if cfg!(windows) { "bin" } else { "lib" }).join(
20+
env::consts::DLL_PREFIX.to_string()
21+
+ "rustc_codegen_cranelift"
22+
+ env::consts::DLL_SUFFIX,
2823
);
24+
rustflags.push(format!("-Zcodegen-backend={}", dylib.to_str().unwrap()));
2925
}
30-
rustflags.push_str(" --sysroot ");
31-
rustflags.push_str(sysroot.to_str().unwrap());
32-
env::set_var("RUSTFLAGS", env::var("RUSTFLAGS").unwrap_or(String::new()) + &rustflags);
33-
env::set_var("RUSTDOCFLAGS", env::var("RUSTDOCFLAGS").unwrap_or(String::new()) + &rustflags);
26+
rustflags.push("--sysroot".to_owned());
27+
rustflags.push(sysroot.to_str().unwrap().to_owned());
3428

3529
let cargo = if let Some(cargo) = option_env!("CARGO") {
3630
cargo
@@ -49,10 +43,7 @@ fn main() {
4943

5044
let args: Vec<_> = match args.get(0).map(|arg| &**arg) {
5145
Some("jit") => {
52-
env::set_var(
53-
"RUSTFLAGS",
54-
env::var("RUSTFLAGS").unwrap_or(String::new()) + " -Cprefer-dynamic",
55-
);
46+
rustflags.push("-Cprefer-dynamic".to_owned());
5647
args.remove(0);
5748
IntoIterator::into_iter(["rustc".to_string()])
5849
.chain(args)
@@ -64,10 +55,7 @@ fn main() {
6455
.collect()
6556
}
6657
Some("lazy-jit") => {
67-
env::set_var(
68-
"RUSTFLAGS",
69-
env::var("RUSTFLAGS").unwrap_or(String::new()) + " -Cprefer-dynamic",
70-
);
58+
rustflags.push("-Cprefer-dynamic".to_owned());
7159
args.remove(0);
7260
IntoIterator::into_iter(["rustc".to_string()])
7361
.chain(args)
@@ -81,11 +69,28 @@ fn main() {
8169
_ => args,
8270
};
8371

72+
let mut cmd = Command::new(cargo);
73+
cmd.args(args);
74+
rustflags_to_cmd_env(
75+
&mut cmd,
76+
"RUSTFLAGS",
77+
&rustflags_from_env("RUSTFLAGS")
78+
.into_iter()
79+
.chain(rustflags.iter().map(|flag| flag.clone()))
80+
.collect::<Vec<_>>(),
81+
);
82+
rustflags_to_cmd_env(
83+
&mut cmd,
84+
"RUSTDOCFLAGS",
85+
&rustflags_from_env("RUSTDOCFLAGS")
86+
.into_iter()
87+
.chain(rustflags.iter().map(|flag| flag.clone()))
88+
.collect::<Vec<_>>(),
89+
);
90+
8491
#[cfg(unix)]
85-
panic!("Failed to spawn cargo: {}", Command::new(cargo).args(args).exec());
92+
panic!("Failed to spawn cargo: {}", cmd.exec());
8693

8794
#[cfg(not(unix))]
88-
std::process::exit(
89-
Command::new(cargo).args(args).spawn().unwrap().wait().unwrap().code().unwrap_or(1),
90-
);
95+
std::process::exit(cmd.spawn().unwrap().wait().unwrap().code().unwrap_or(1));
9196
}

0 commit comments

Comments
 (0)