Skip to content

Commit 05cce73

Browse files
committed
Auto merge of #12900 - weihanglo:trim-paths-build-script, r=epage
feat(trim-paths): set env `CARGO_TRIM_PATHS` for build scripts
2 parents 4aee12c + 2915729 commit 05cce73

File tree

4 files changed

+86
-2
lines changed

4 files changed

+86
-2
lines changed

src/cargo/core/compiler/custom_build.rs

+4
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,10 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
307307
cmd.env("CARGO_MANIFEST_LINKS", links);
308308
}
309309

310+
if let Some(trim_paths) = unit.profile.trim_paths.as_ref() {
311+
cmd.env("CARGO_TRIM_PATHS", trim_paths.to_string());
312+
}
313+
310314
// Be sure to pass along all enabled features for this package, this is the
311315
// last piece of statically known information that we have.
312316
for feat in &unit.features {

src/cargo/core/profiles.rs

+1
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ impl Profiles {
327327
result.root = for_unit_profile.root;
328328
result.debuginfo = for_unit_profile.debuginfo;
329329
result.opt_level = for_unit_profile.opt_level;
330+
result.trim_paths = for_unit_profile.trim_paths.clone();
330331
result
331332
}
332333

src/doc/src/reference/unstable.md

+15-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ For the latest nightly, see the [nightly version] of this page.
9494
* [per-package-target](#per-package-target) --- Sets the `--target` to use for each individual package.
9595
* [artifact dependencies](#artifact-dependencies) --- Allow build artifacts to be included into other build artifacts and build them for different targets.
9696
* [Edition 2024](#edition-2024) — Adds support for the 2024 Edition.
97-
* [Profile `trim-paths` option](#profile-trim-paths-option) --- Control the sanitisation of file paths in build outputs.
97+
* [Profile `trim-paths` option](#profile-trim-paths-option) --- Control the sanitization of file paths in build outputs.
9898
* Information and metadata
9999
* [Build-plan](#build-plan) --- Emits JSON information on which commands will be run.
100100
* [unit-graph](#unit-graph) --- Emits JSON for Cargo's internal graph structure.
@@ -1292,7 +1292,7 @@ edition that may break your build.
12921292
* Tracking Issue: [rust-lang/cargo#12137](https://github.com/rust-lang/cargo/issues/12137)
12931293
* Tracking Rustc Issue: [rust-lang/rust#111540](https://github.com/rust-lang/rust/issues/111540)
12941294

1295-
This adds a new profile setting to control how paths are sanitised in the resulting binary.
1295+
This adds a new profile setting to control how paths are sanitized in the resulting binary.
12961296
This can be enabled like so:
12971297

12981298
```toml
@@ -1370,6 +1370,19 @@ Paths to all other source files will not be affected.
13701370

13711371
This will not affect any hard-coded paths in the source code, such as in strings.
13721372

1373+
#### Environment variable
1374+
1375+
*as a new entry of ["Environment variables Cargo sets for build scripts"](./environment-variables.md#environment-variables-cargo-sets-for-crates)*
1376+
1377+
* `CARGO_TRIM_PATHS` --- The value of `trim-paths` profile option.
1378+
`false`, `"none"`, and empty arrays would be converted to `none`.
1379+
`true` and `"all"` become `all`.
1380+
Values in a non-empty array would be joined into a comma-separated list.
1381+
If the build script introduces absolute paths to built artifacts (such as by invoking a compiler),
1382+
the user may request them to be sanitized in different types of artifacts.
1383+
Common paths requiring sanitization include `OUT_DIR` and `CARGO_MANIFEST_DIR`,
1384+
plus any other introduced by the build script, such as include directories.
1385+
13731386
# Stabilized and removed features
13741387

13751388
## Compile progress

tests/testsuite/profile_trim_paths.rs

+66
Original file line numberDiff line numberDiff line change
@@ -511,3 +511,69 @@ fn object_works() {
511511
assert!(memchr::memmem::find(&stdout, registry_src_bytes).is_none());
512512
assert!(memchr::memmem::find(&stdout, pkg_root).is_none());
513513
}
514+
515+
// TODO: might want to move to test/testsuite/build_script.rs once stabilized.
516+
#[cargo_test(nightly, reason = "-Zremap-path-scope is unstable")]
517+
fn custom_build_env_var_trim_paths() {
518+
let p = project()
519+
.file(
520+
"Cargo.toml",
521+
r#"
522+
[package]
523+
name = "foo"
524+
version = "0.0.1"
525+
"#,
526+
)
527+
.file("src/lib.rs", "")
528+
.file("build.rs", "")
529+
.build();
530+
531+
let test_cases = [
532+
("[]", "none"),
533+
("\"all\"", "all"),
534+
("\"diagnostics\"", "diagnostics"),
535+
("\"macro\"", "macro"),
536+
("\"none\"", "none"),
537+
("\"object\"", "object"),
538+
("false", "none"),
539+
("true", "all"),
540+
(
541+
r#"["diagnostics", "macro", "object"]"#,
542+
"diagnostics,macro,object",
543+
),
544+
];
545+
546+
for (opts, expected) in test_cases {
547+
p.change_file(
548+
"Cargo.toml",
549+
&format!(
550+
r#"
551+
[package]
552+
name = "foo"
553+
version = "0.0.1"
554+
555+
[profile.dev]
556+
trim-paths = {opts}
557+
"#
558+
),
559+
);
560+
561+
p.change_file(
562+
"build.rs",
563+
&format!(
564+
r#"
565+
fn main() {{
566+
assert_eq!(
567+
std::env::var("CARGO_TRIM_PATHS").unwrap().as_str(),
568+
"{expected}",
569+
);
570+
}}
571+
"#
572+
),
573+
);
574+
575+
p.cargo("build -Ztrim-paths")
576+
.masquerade_as_nightly_cargo(&["-Ztrim-paths"])
577+
.run();
578+
}
579+
}

0 commit comments

Comments
 (0)