Skip to content

Commit 8c229c6

Browse files
committed
stdbuf: fix build.rs to propagate custom profiles to libstdbuf nested build
The PROFILE env var only returns the base profile name ('release' or 'debug'), not the actual profile. For example, when building with --profile=release-small, PROFILE=release, so the old code would pass --release to the nested libstdbuf build, causing it to be compiled with release settings instead of release-small settings (opt-level=z, strip=true, etc.). Fix this by extracting the actual profile name from OUT_DIR, which always contains the real profile name in its path (.../target/{profile}/build/...). This correctly handles all custom profiles like release-small, profiling, etc. Fixes: #12434 Signed-off-by: Etienne Cordonnier <ecordonnier@snap.com>
1 parent 29fdaa6 commit 8c229c6

1 file changed

Lines changed: 19 additions & 10 deletions

File tree

src/uu/stdbuf/build.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,21 @@ fn main() {
8585
cmd.current_dir(libstdbuf_src)
8686
.args(["build", "--target-dir", build_dir.to_str().unwrap()]);
8787

88-
// Get the current profile
89-
let profile = env::var("PROFILE").unwrap_or_else(|_| "debug".to_string());
90-
91-
// Pass the release flag if we're in release mode
92-
if profile == "release" || profile == "bench" {
93-
cmd.arg("--release");
88+
// Determine the actual profile from OUT_DIR since PROFILE env var only gives the base profile
89+
// for inherited profiles (e.g., PROFILE=release for both release and release-small).
90+
// OUT_DIR is always .../target/[triple/]{profile}/build/{pkg-hash}/out, so the profile
91+
// is exactly 3 parent levels up — regardless of whether a target triple is in the path.
92+
let profile = Path::new(&out_dir)
93+
.parent() // .../build/{pkg-hash}
94+
.and_then(|p| p.parent()) // .../build
95+
.and_then(|p| p.parent()) // .../{profile}
96+
.and_then(|p| p.file_name())
97+
.and_then(|s| s.to_str())
98+
.unwrap_or("debug");
99+
100+
// Pass the profile to the nested build (for release, release-small, profiling, bench, etc.)
101+
if profile != "debug" {
102+
cmd.arg("--profile").arg(profile);
94103
}
95104

96105
// Pass the target architecture if we're cross-compiling
@@ -112,17 +121,17 @@ fn main() {
112121
// Check multiple possible locations for the built library
113122
let possible_paths = if !target.is_empty() && target != "unknown" {
114123
vec![
115-
build_dir.join(&target).join(&profile).join(&lib_name),
124+
build_dir.join(&target).join(profile).join(&lib_name),
116125
build_dir
117126
.join(&target)
118-
.join(&profile)
127+
.join(profile)
119128
.join("deps")
120129
.join(&lib_name),
121130
]
122131
} else {
123132
vec![
124-
build_dir.join(&profile).join(&lib_name),
125-
build_dir.join(&profile).join("deps").join(&lib_name),
133+
build_dir.join(profile).join(&lib_name),
134+
build_dir.join(profile).join("deps").join(&lib_name),
126135
]
127136
};
128137

0 commit comments

Comments
 (0)