Skip to content

Commit fb0d41f

Browse files
author
Jon Gjengset
committed
Adopt CARGO_ENCODED_RUSTFLAGS
1 parent c16b9d4 commit fb0d41f

File tree

4 files changed

+57
-9
lines changed

4 files changed

+57
-9
lines changed

src/cargo/core/compiler/build_context/target_info.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,7 @@ fn output_err_info(cmd: &ProcessBuilder, stdout: &str, stderr: &str) -> String {
548548
///
549549
/// The locations are:
550550
///
551+
/// - the `CARGO_ENCODED_RUSTFLAGS` environment variable
551552
/// - the `RUSTFLAGS` environment variable
552553
///
553554
/// then if this was not found
@@ -595,7 +596,14 @@ fn env_args(
595596
return Ok(Vec::new());
596597
}
597598

598-
// First try RUSTFLAGS from the environment
599+
// First try CARG_ENCODED_RUSTFLAGS from the environment.
600+
// Prefer this over RUSTFLAGS since it's less prone to encoding errors.
601+
if let Ok(a) = env::var(format!("CARGO_ENCODED_{}", name)) {
602+
let args = a.split('\x1f').map(str::to_string);
603+
return Ok(args.collect());
604+
}
605+
606+
// Then try RUSTFLAGS from the environment
599607
if let Ok(a) = env::var(name) {
600608
let args = a
601609
.split(' ')

src/cargo/core/compiler/custom_build.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,11 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
255255
} else {
256256
cmd.env_remove("RUSTC_WRAPPER");
257257
}
258-
cmd.env("RUSTFLAGS", bcx.rustflags_args(unit).join(" "));
258+
cmd.env(
259+
"CARGO_ENCODED_RUSTFLAGS",
260+
bcx.rustflags_args(unit).join("\x1f"),
261+
);
262+
cmd.env_remove("RUSTFLAGS");
259263
let version = &bcx.rustc().version;
260264
cmd.env(
261265
"RUSTC_VERSION",

src/doc/src/reference/environment-variables.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,17 @@ system:
3535
* `RUSTDOCFLAGS` — A space-separated list of custom flags to pass to all `rustdoc`
3636
invocations that Cargo performs. In contrast with [`cargo rustdoc`], this is
3737
useful for passing a flag to *all* `rustdoc` instances. See
38-
[`build.rustdocflags`] for some more ways to set flags.
38+
[`build.rustdocflags`] for some more ways to set flags. This string is
39+
split by whitespace; for a more robust encoding of multiple arguments,
40+
set `CARGO_ENCODED_RUSTDOCFLAGS` instead with arguments separated by
41+
`0x1f` (ASCII Unit Separator).
3942
* `RUSTFLAGS` — A space-separated list of custom flags to pass to all compiler
4043
invocations that Cargo performs. In contrast with [`cargo rustc`], this is
4144
useful for passing a flag to *all* compiler instances. See
42-
[`build.rustflags`] for some more ways to set flags.
45+
[`build.rustflags`] for some more ways to set flags. This string is
46+
split by whitespace; for a more robust encoding of multiple arguments,
47+
set `CARGO_ENCODED_RUSTFLAGS` instead with arguments separated by
48+
`0x1f` (ASCII Unit Separator).
4349
* `CARGO_INCREMENTAL` — If this is set to 1 then Cargo will force [incremental
4450
compilation] to be enabled for the current compilation, and when set to 0 it
4551
will force disabling it. If this env var isn't present then cargo's defaults
@@ -341,8 +347,10 @@ let out_dir = env::var("OUT_DIR").unwrap();
341347
changed by editing `.cargo/config.toml`; see the documentation
342348
about [cargo configuration][cargo-config] for more
343349
information.
344-
* `RUSTFLAGS` — the `RUSTFLAGS` that Cargo invokes `rustc` with.
345-
See [`build.rustflags`].
350+
* `CARGO_ENCODED_RUSTFLAGS` — extra flags that Cargo invokes `rustc`
351+
with, separated by a `0x1f` character
352+
(ASCII Unit Separator). See
353+
[`build.rustflags`].
346354
* `CARGO_PKG_<var>` - The package information variables, with the same names and values as are [provided during crate building][variables set for crates].
347355
* `RUSTC_VERSION` - The version of rustc used by the cargo that invokes
348356
the build script. Its constituent parts are also

tests/testsuite/build_script.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ fn custom_build_env_vars() {
122122
123123
assert!(env::var("RUSTC_LINKER").is_err());
124124
125-
let rustflags = env::var("RUSTFLAGS").unwrap();
125+
assert!(env::var("RUSTFLAGS").is_err());
126+
let rustflags = env::var("CARGO_ENCODED_RUSTFLAGS").unwrap();
126127
assert_eq!(rustflags, "");
127128
128129
let version = env::var("RUSTC_VERSION").unwrap();
@@ -173,11 +174,12 @@ fn custom_build_env_var_rustflags() {
173174
174175
fn main() {{
175176
// Static assertion that exactly one of the cfg paths is always taken.
177+
assert!(env::var("RUSTFLAGS").is_err());
176178
let x;
177179
#[cfg(special)]
178-
{{ assert_eq!(env::var("RUSTFLAGS").unwrap(), "{}"); x = String::new(); }}
180+
{{ assert_eq!(env::var("CARGO_ENCODED_RUSTFLAGS").unwrap(), "{}"); x = String::new(); }}
179181
#[cfg(notspecial)]
180-
{{ assert_eq!(env::var("RUSTFLAGS").unwrap(), "{}"); x = String::new(); }}
182+
{{ assert_eq!(env::var("CARGO_ENCODED_RUSTFLAGS").unwrap(), "{}"); x = String::new(); }}
181183
let _ = x;
182184
}}
183185
"#,
@@ -193,6 +195,32 @@ fn custom_build_env_var_rustflags() {
193195
p.cargo("check").env("RUSTFLAGS", rustflags_alt).run();
194196
}
195197

198+
#[cargo_test]
199+
fn custom_build_env_var_rustflags_escape() {
200+
let p = project()
201+
.file(
202+
".cargo/config",
203+
r#"
204+
[build]
205+
rustflags = ["-Clink-arg=-add_empty_section text foobar", "--cfg=foo"]
206+
"#,
207+
)
208+
.file(
209+
"build.rs",
210+
r#"
211+
use std::env;
212+
213+
fn main() {{
214+
assert_eq!(env::var("CARGO_ENCODED_RUSTFLAGS").unwrap(), "-Clink-arg=-add_empty_section text foobar\x1f--cfg=foo");
215+
}}
216+
"#,
217+
)
218+
.file("src/lib.rs", "")
219+
.build();
220+
221+
p.cargo("check").run();
222+
}
223+
196224
#[cargo_test]
197225
fn custom_build_env_var_rustc_wrapper() {
198226
let wrapper = tools::echo_wrapper();

0 commit comments

Comments
 (0)