Skip to content

Commit 16edfb2

Browse files
committed
Change CARGO_TRIM_PATHS to the profile option
1 parent 3f59b7b commit 16edfb2

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

text/3127-trim-paths.md

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,17 @@ If `trim-paths` is not `none` or `false`, then the following paths are sanitised
137137
2. Path to the working directory will be stripped. E.g. `/home/username/crate/src/lib.rs` -> `src/lib.rs`.
138138
3. Path to packages outside of the working directory will be replaced with `[package name]-[version]`. E.g. `/home/username/deps/foo/src/lib.rs` -> `foo-0.1.0/src/lib.rs`
139139

140-
Paths requiring sanitisation can be retrieved by build scripts at their execution time from the environment variable `CARGO_TRIM_PATHS`, in comma-separated format.
141-
If a build script does anything that may result in these, or any other absolute paths, to be included in compilation outputs, such as by invoking a C compiler, then the build script should make sure they are trimmed.
142-
Cargo's mapping scheme (what Cargo will map these paths to) is not provided in `CARGO_TRIM_PATHS`, and build scripts are free to decide as long as they are reproducible and privacy preserving.
143-
144140
When a path to the source files of the standard and core library is *not* in scope for sanitisation, the emitted path will depend on if `rust-src` component
145141
is present. If it is, then some paths will point to the copy of the source files on your file system; if it isn't, then they will
146142
show up as `/rustc/[rustc commit hash]/library/...` (just like when it is selected for sanitisation). Paths to all other source files will not be affected.
147143

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

146+
### Environment variables Cargo sets for build scripts
147+
* `CARGO_TRIM_PATHS` - The value of `trim-paths` profile option. If the build script introduces absolute paths to built artefacts (such as
148+
by invoking a compiler), the user may request them to be sanitised in different types of artefacts. Common paths requiring sanitisation
149+
include `OUT_DIR` and `CARGO_MANIFEST_DIR`, plus any other introduced by the build script, such as include directories.
150+
150151
# Reference-level explanation
151152
[reference-level-explanation]: #reference-level-explanation
152153

@@ -159,9 +160,6 @@ If `trim-paths` is anything else, then its value is supplied directly to `rustc`
159160
- If the compilation unit is under the working directory, from the the working directory absolute path to empty string.
160161
If it's outside the working directory, from the absolute path of the package root to `[package name]-[package version]`.
161162

162-
If a package in the dependency tree has build scripts, then the absolute path to the package root is supplied by
163-
the environment variable `CARGO_TRIM_PATHS` when executing build scripts.
164-
165163
The default value of `trim-paths` is `object` for release profile. As a result, panic messages (which are always embedded) are sanitised. If debug information is embedded, then they are sanitised; if they are split then they are kept untouched, but the paths to these split files are sanitised.
166164

167165
Some interactions with compiler-intrinsic macros need to be considered:
@@ -239,20 +237,28 @@ She can ship her binary like Alice, without worrying about leaking usernames.
239237

240238
## Hana needs to compile a C program in their build script
241239

242-
They can consult `CARGO_TRIM_PATHS` in their build script to find out which paths need to be sanitised
240+
They can consult `CARGO_TRIM_PATHS` in their build script to find out paths in what places the user wants sanitised
243241

244242
```rust
245243
// in build.rs
244+
use std::env;
245+
use std::process::Command;
246246

247247
let mut gcc = Command::new("gcc");
248-
249-
if let Ok(paths) = std::env::var("CARGO_TRIM_PATHS") {
250-
for to_trim in paths.split(','){
251-
gcc.arg(format!("-ffile-prefix-map={to_trim}=redacted"));
252-
}
248+
let out_dir = env::var("OUT_DIR").unwrap();
249+
let scope = env::var("CARGO_TRIM_PATHS").unwrap();
250+
251+
if scope != "none" && scope != "false" {
252+
// Runtime working directory of the build script
253+
let cwd = env::var("CARGO_MANIFEST_DIR").unwrap();
254+
let gcc_scope = match scope.as_str() {
255+
"macro" => "-fmacro-prefix-map",
256+
_ => "-ffile-prefix-map",
257+
};
258+
gcc.args([&format!("{gcc_scope}={cwd}=redacted"), &format!("{gcc_scope}={out_dir}=redacted")]);
253259
}
254260

255-
gcc.args(["-std=c11", "-O2", "-o=lib.o", "lib.c"]);
261+
gcc.args(["-std=c11", &format!("-o={out_dir}/lib.o"), "lib.c"]);
256262

257263
let output = gcc.output();
258264

0 commit comments

Comments
 (0)