From ebd5e965e95ac544a146a728b43b494edecd2ebe Mon Sep 17 00:00:00 2001 From: Terts Diepraam Date: Sat, 1 Jan 2022 20:44:02 +0100 Subject: [PATCH] stdbuf: fix cargo --git build (#1276) --- src/uu/stdbuf/build.rs | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/uu/stdbuf/build.rs b/src/uu/stdbuf/build.rs index b14d503cf..b03bce849 100644 --- a/src/uu/stdbuf/build.rs +++ b/src/uu/stdbuf/build.rs @@ -20,17 +20,23 @@ mod platform { } fn main() { - let manifest_dir = env::var("CARGO_MANIFEST_DIR").expect("Could not find manifest dir"); - let profile = env::var("PROFILE").expect("Could not determine profile"); - let out_dir = env::var("OUT_DIR").unwrap(); - let libstdbuf = format!( - "{}/../../../{}/{}/deps/liblibstdbuf{}", - manifest_dir, - env::var("CARGO_TARGET_DIR").unwrap_or_else(|_| "target".to_string()), - profile, - platform::DYLIB_EXT - ); + let mut target_dir = Path::new(&out_dir); + + // Depending on how this is util is built, the directory structure. This seems to work for now. + // Here are three cases to test when changing this: + // - cargo run + // - cross run + // - cargo install --git + let mut name = target_dir.file_name().unwrap().to_string_lossy(); + while name != "target" && !name.starts_with("cargo-install") { + target_dir = target_dir.parent().unwrap(); + name = target_dir.file_name().unwrap().to_string_lossy(); + } + let mut libstdbuf = target_dir.to_path_buf(); + libstdbuf.push(env::var("PROFILE").unwrap()); + libstdbuf.push("deps"); + libstdbuf.push(format!("liblibstdbuf{}", platform::DYLIB_EXT)); fs::copy(libstdbuf, Path::new(&out_dir).join("libstdbuf.so")).unwrap(); }