Skip to content

Commit 38bbfce

Browse files
authored
Merge pull request #7266 from zcg00/cargo_target_dir
Fixed Bugs When Not Using Default Target Directory
2 parents d31a19f + 43036e2 commit 38bbfce

File tree

3 files changed

+27
-46
lines changed

3 files changed

+27
-46
lines changed

GNUmakefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ PROG_PREFIX ?=
4141
# This won't support any directory with spaces in its name, but you can just
4242
# make a symlink without spaces that points to the directory.
4343
BASEDIR ?= $(shell pwd)
44+
ifdef CARGO_TARGET_DIR
45+
BUILDDIR := $(CARGO_TARGET_DIR)/${PROFILE}
46+
else
4447
BUILDDIR := $(BASEDIR)/target/${PROFILE}
48+
endif
4549
PKG_BUILDDIR := $(BUILDDIR)/deps
4650
DOCSDIR := $(BASEDIR)/docs
4751

src/uu/stdbuf/build.rs

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// spell-checker:ignore (ToDO) dylib libstdbuf deps liblibstdbuf
66

77
use std::env;
8+
use std::env::current_exe;
89
use std::fs;
910
use std::path::Path;
1011

@@ -24,53 +25,25 @@ mod platform {
2425
}
2526

2627
fn main() {
27-
let out_dir = env::var("OUT_DIR").unwrap();
28-
let mut target_dir = Path::new(&out_dir);
28+
let current_exe = current_exe().unwrap();
2929

30-
// Depending on how this is util is built, the directory structure changes.
31-
// This seems to work for now. Here are three cases to test when changing
32-
// this:
33-
//
34-
// - cargo run
35-
// - cross run
36-
// - cargo install --git
37-
// - cargo publish --dry-run
38-
//
39-
// The goal is to find the directory in which we are installing, but that
40-
// depends on the build method, which is annoying. Additionally the env
41-
// var for the profile can only be "debug" or "release", not a custom
42-
// profile name, so we have to use the name of the directory within target
43-
// as the profile name.
44-
//
45-
// Adapted from https://stackoverflow.com/questions/73595435/how-to-get-profile-from-cargo-toml-in-build-rs-or-at-runtime
46-
let profile_name = out_dir
47-
.split(std::path::MAIN_SEPARATOR)
48-
.nth_back(3)
49-
.unwrap();
30+
let out_dir_string = env::var("OUT_DIR").unwrap();
31+
let out_dir = Path::new(&out_dir_string);
5032

51-
let mut name = target_dir.file_name().unwrap().to_string_lossy();
52-
while name != "target" && !name.starts_with("cargo-install") {
53-
target_dir = target_dir.parent().unwrap();
54-
name = target_dir.file_name().unwrap().to_string_lossy();
55-
}
56-
let mut dir = target_dir.to_path_buf();
57-
dir.push(profile_name);
58-
dir.push("deps");
59-
let mut path = None;
33+
let deps_dir = current_exe.ancestors().nth(3).unwrap().join("deps");
34+
dbg!(&deps_dir);
6035

61-
// When running cargo publish, cargo appends hashes to the filenames of the compiled artifacts.
62-
// Therefore, it won't work to just get liblibstdbuf.so. Instead, we look for files with the
63-
// glob pattern "liblibstdbuf*.so" (i.e. starts with liblibstdbuf and ends with the extension).
64-
for entry in fs::read_dir(dir).unwrap().flatten() {
65-
let name = entry.file_name();
66-
let name = name.to_string_lossy();
67-
if name.starts_with("liblibstdbuf") && name.ends_with(platform::DYLIB_EXT) {
68-
path = Some(entry.path());
69-
}
70-
}
71-
fs::copy(
72-
path.expect("liblibstdbuf was not found"),
73-
Path::new(&out_dir).join("libstdbuf.so"),
74-
)
75-
.unwrap();
36+
let libstdbuf = deps_dir
37+
.read_dir()
38+
.unwrap()
39+
.flatten()
40+
.find(|entry| {
41+
let n = entry.file_name();
42+
let name = n.to_string_lossy();
43+
44+
name.starts_with("liblibstdbuf") && name.ends_with(platform::DYLIB_EXT)
45+
})
46+
.expect("unable to find libstdbuf");
47+
48+
fs::copy(libstdbuf.path(), out_dir.join("libstdbuf.so")).unwrap();
7649
}

util/build-gnu.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,11 @@ echo "path_GNU='${path_GNU}'"
8484

8585
###
8686

87+
if [[ ! -z "$CARGO_TARGET_DIR" ]]; then
88+
UU_BUILD_DIR="${CARGO_TARGET_DIR}/${UU_MAKE_PROFILE}"
89+
else
8790
UU_BUILD_DIR="${path_UUTILS}/target/${UU_MAKE_PROFILE}"
91+
fi
8892
echo "UU_BUILD_DIR='${UU_BUILD_DIR}'"
8993

9094
cd "${path_UUTILS}" && echo "[ pwd:'${PWD}' ]"

0 commit comments

Comments
 (0)