From dd0aea350cb529eafbccc7ee65e98a72cfa09839 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Tue, 31 Oct 2023 00:42:37 -0400 Subject: [PATCH 1/2] feat(trim-paths): set env `CARGO_TRIM_PATHS` for build scripts --- src/cargo/core/compiler/custom_build.rs | 4 ++ src/cargo/core/profiles.rs | 1 + tests/testsuite/profile_trim_paths.rs | 66 +++++++++++++++++++++++++ 3 files changed, 71 insertions(+) diff --git a/src/cargo/core/compiler/custom_build.rs b/src/cargo/core/compiler/custom_build.rs index 022ad87613a..c921986a838 100644 --- a/src/cargo/core/compiler/custom_build.rs +++ b/src/cargo/core/compiler/custom_build.rs @@ -307,6 +307,10 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult { cmd.env("CARGO_MANIFEST_LINKS", links); } + if let Some(trim_paths) = unit.profile.trim_paths.as_ref() { + cmd.env("CARGO_TRIM_PATHS", trim_paths.to_string()); + } + // Be sure to pass along all enabled features for this package, this is the // last piece of statically known information that we have. for feat in &unit.features { diff --git a/src/cargo/core/profiles.rs b/src/cargo/core/profiles.rs index 06a5f3b5784..95354a3f6ad 100644 --- a/src/cargo/core/profiles.rs +++ b/src/cargo/core/profiles.rs @@ -327,6 +327,7 @@ impl Profiles { result.root = for_unit_profile.root; result.debuginfo = for_unit_profile.debuginfo; result.opt_level = for_unit_profile.opt_level; + result.trim_paths = for_unit_profile.trim_paths.clone(); result } diff --git a/tests/testsuite/profile_trim_paths.rs b/tests/testsuite/profile_trim_paths.rs index b0b8d9577a4..f264253c936 100644 --- a/tests/testsuite/profile_trim_paths.rs +++ b/tests/testsuite/profile_trim_paths.rs @@ -511,3 +511,69 @@ fn object_works() { assert!(memchr::memmem::find(&stdout, registry_src_bytes).is_none()); assert!(memchr::memmem::find(&stdout, pkg_root).is_none()); } + +// TODO: might want to move to test/testsuite/build_script.rs once stabilized. +#[cargo_test(nightly, reason = "-Zremap-path-scope is unstable")] +fn custom_build_env_var_trim_paths() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + "#, + ) + .file("src/lib.rs", "") + .file("build.rs", "") + .build(); + + let test_cases = [ + ("[]", "none"), + ("\"all\"", "all"), + ("\"diagnostics\"", "diagnostics"), + ("\"macro\"", "macro"), + ("\"none\"", "none"), + ("\"object\"", "object"), + ("false", "none"), + ("true", "all"), + ( + r#"["diagnostics", "macro", "object"]"#, + "diagnostics,macro,object", + ), + ]; + + for (opts, expected) in test_cases { + p.change_file( + "Cargo.toml", + &format!( + r#" + [package] + name = "foo" + version = "0.0.1" + + [profile.dev] + trim-paths = {opts} + "# + ), + ); + + p.change_file( + "build.rs", + &format!( + r#" + fn main() {{ + assert_eq!( + std::env::var("CARGO_TRIM_PATHS").unwrap().as_str(), + "{expected}", + ); + }} + "# + ), + ); + + p.cargo("build -Ztrim-paths") + .masquerade_as_nightly_cargo(&["-Ztrim-paths"]) + .run(); + } +} From 29157296386be0db340683ad546a7b9d60e083d7 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Tue, 31 Oct 2023 08:59:26 -0400 Subject: [PATCH 2/2] docs(trim-paths): env `CARGO_TRIM_PATHS` for build scripts --- src/doc/src/reference/unstable.md | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/doc/src/reference/unstable.md b/src/doc/src/reference/unstable.md index 67144807fe2..0683daa3c59 100644 --- a/src/doc/src/reference/unstable.md +++ b/src/doc/src/reference/unstable.md @@ -94,7 +94,7 @@ For the latest nightly, see the [nightly version] of this page. * [per-package-target](#per-package-target) --- Sets the `--target` to use for each individual package. * [artifact dependencies](#artifact-dependencies) --- Allow build artifacts to be included into other build artifacts and build them for different targets. * [Edition 2024](#edition-2024) — Adds support for the 2024 Edition. - * [Profile `trim-paths` option](#profile-trim-paths-option) --- Control the sanitisation of file paths in build outputs. + * [Profile `trim-paths` option](#profile-trim-paths-option) --- Control the sanitization of file paths in build outputs. * Information and metadata * [Build-plan](#build-plan) --- Emits JSON information on which commands will be run. * [unit-graph](#unit-graph) --- Emits JSON for Cargo's internal graph structure. @@ -1292,7 +1292,7 @@ edition that may break your build. * Tracking Issue: [rust-lang/cargo#12137](https://github.com/rust-lang/cargo/issues/12137) * Tracking Rustc Issue: [rust-lang/rust#111540](https://github.com/rust-lang/rust/issues/111540) -This adds a new profile setting to control how paths are sanitised in the resulting binary. +This adds a new profile setting to control how paths are sanitized in the resulting binary. This can be enabled like so: ```toml @@ -1370,6 +1370,19 @@ Paths to all other source files will not be affected. This will not affect any hard-coded paths in the source code, such as in strings. +#### Environment variable + +*as a new entry of ["Environment variables Cargo sets for build scripts"](./environment-variables.md#environment-variables-cargo-sets-for-crates)* + +* `CARGO_TRIM_PATHS` --- The value of `trim-paths` profile option. + `false`, `"none"`, and empty arrays would be converted to `none`. + `true` and `"all"` become `all`. + Values in a non-empty array would be joined into a comma-separated list. + If the build script introduces absolute paths to built artifacts (such as by invoking a compiler), + the user may request them to be sanitized in different types of artifacts. + Common paths requiring sanitization include `OUT_DIR` and `CARGO_MANIFEST_DIR`, + plus any other introduced by the build script, such as include directories. + # Stabilized and removed features ## Compile progress