8000 Fixed Bugs When Not Using Default Target Directory by zcg00 · Pull Request #7266 · uutils/coreutils · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fixed Bugs When Not Using Default Target Directory #7266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ PROG_PREFIX ?=
# This won't support any directory with spaces in its name, but you can just
# make a symlink without spaces that points to the directory.
BASEDIR ?= $(shell pwd)
ifdef CARGO_TARGET_DIR
BUILDDIR := $(CARGO_TARGET_DIR)/${PROFILE}
else
BUILDDIR := $(BASEDIR)/target/${PROFILE}
endif
PKG_BUILDDIR := $(BUILDDIR)/deps
DOCSDIR := $(BASEDIR)/docs

Expand Down
65 changes: 19 additions & 46 deletions src/uu/stdbuf/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// spell-checker:ignore (ToDO) dylib libstdbuf deps liblibstdbuf

use std::env;
use std::env::current_exe;
use std::fs;
use std::path::Path;

Expand All @@ -24,53 +25,25 @@ mod platform {
}

fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
let mut target_dir = Path::new(&out_dir);
let current_exe = current_exe().unwrap();

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

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 dir = target_dir.to_path_buf();
dir.push(profile_name);
dir.push("deps");
let mut path = None;
let deps_dir = current_exe.ancestors().nth(3).unwrap().join("deps");
dbg!(&deps_dir);

// When running cargo publish, cargo appends hashes to the filenames of the compiled artifacts.
// Therefore, it won't work to just get liblibstdbuf.so. Instead, we look for files with the
// glob pattern "liblibstdbuf*.so" (i.e. starts with liblibstdbuf and ends with the extension).
for entry in fs::read_dir(dir).unwrap().flatten() {
let name = entry.file_name();
let name = name.to_string_lossy();
if name.starts_with("liblibstdbuf") && name.ends_with(platform::DYLIB_EXT) {
path = Some(entry.path());
}
}
fs::copy(
path.expect("liblibstdbuf was not found"),
Path::new(&out_dir).join("libstdbuf.so"),
)
.unwrap();
let libstdbuf = deps_dir
.read_dir()
.unwrap()
.flatten()
.find(|entry| {
let n = entry.file_name();
< 8000 /td> let name = n.to_string_lossy();

name.starts_with("liblibstdbuf") && name.ends_with(platform::DYLIB_EXT)
})
.expect("unable to find libstdbuf");

fs::copy(libstdbuf.path(), out_dir.join("libstdbuf.so")).unwrap();
}
4 changes: 4 additions & 0 deletions util/build-gnu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ echo "path_GNU='${path_GNU}'"

###

if [[ ! -z "$CARGO_TARGET_DIR" ]]; then
UU_BUILD_DIR="${CARGO_TARGET_DIR}/${UU_MAKE_PROFILE}"
else
UU_BUILD_DIR="${path_UUTILS}/target/${UU_MAKE_PROFILE}"
fi
echo "UU_BUILD_DIR='${UU_BUILD_DIR}'"

cd "${path_UUTILS}" && echo "[ pwd:'${PWD}' ]"
Expand Down
Loading
0