Skip to content

Commit b86defa

Browse files
committed
fix: supports cross-platform building and 32-bit
1 parent 62594c0 commit b86defa

2 files changed

Lines changed: 19 additions & 29 deletions

File tree

src/uu/stdbuf/build.rs

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,6 @@ use std::fs;
99
use std::path::Path;
1010
use std::process::Command;
1111

12-
#[cfg(any(
13-
target_os = "linux",
14-
target_os = "android",
15-
target_os = "freebsd",
16-
target_os = "netbsd",
17-
target_os = "openbsd",
18-
target_os = "dragonfly"
19-
))]
20-
mod platform {
21-
pub const DYLIB_EXT: &str = ".so";
22-
}
23-
24-
#[cfg(target_vendor = "apple")]
25-
mod platform {
26-
pub const DYLIB_EXT: &str = ".dylib";
27-
}
28-
29-
#[cfg(target_os = "cygwin")]
30-
mod platform {
31-
pub const DYLIB_EXT: &str = ".dll";
32-
}
33-
3412
fn main() {
3513
println!("cargo:rerun-if-changed=build.rs");
3614
println!("cargo:rerun-if-changed=src/libstdbuf/src/libstdbuf.rs");
@@ -58,14 +36,25 @@ fn main() {
5836
let out_dir = env::var("OUT_DIR").expect("OUT_DIR not set");
5937
let target = env::var("TARGET").unwrap_or_else(|_| "unknown".to_string());
6038

39+
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
40+
let target_vendor = env::var("CARGO_CFG_TARGET_VENDOR").unwrap();
41+
42+
let dylib_ext = if target_vendor == "apple" {
43+
".dylib"
44+
} else if target_os == "windows" || target_os == "cygwin" {
45+
".dll"
46+
} else {
47+
".so"
48+
};
49+
6150
// Check if we're building from the repository (where src/libstdbuf exists)
6251
// or from crates.io (where it doesn't)
6352
let libstdbuf_src = Path::new("src/libstdbuf");
6453
if !libstdbuf_src.exists() {
6554
// When building from crates.io, libstdbuf is already available as a dependency
6655
// We can't build it here, so we'll need to handle this differently
6756
// For now, we'll create a dummy library file to satisfy the include_bytes! macro
68-
let lib_name = format!("libstdbuf{}", platform::DYLIB_EXT);
57+
let lib_name = format!("libstdbuf{dylib_ext}");
6958
let dest_path = Path::new(&out_dir).join(&lib_name);
7059

7160
// Create an empty file as a placeholder
@@ -108,11 +97,12 @@ fn main() {
10897
assert!(status.success(), "Failed to build libstdbuf");
10998

11099
// Copy the built library to OUT_DIR for include_bytes! to find
111-
#[cfg(target_os = "cygwin")]
112-
let lib_name = format!("stdbuf{}", platform::DYLIB_EXT);
113-
#[cfg(not(target_os = "cygwin"))]
114-
let lib_name = format!("libstdbuf{}", platform::DYLIB_EXT);
115-
let dest_path = Path::new(&out_dir).join(format!("libstdbuf{}", platform::DYLIB_EXT));
100+
let lib_name = if target_os == "cygwin" {
101+
format!("stdbuf{dylib_ext}")
102+
} else {
103+
format!("libstdbuf{dylib_ext}")
104+
};
105+
let dest_path = Path::new(&out_dir).join(format!("libstdbuf{dylib_ext}"));
116106

117107
// Check multiple possible locations for the built library
118108
let possible_paths = if !target.is_empty() && target != "unknown" {

src/uu/wc/src/wc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ fn is_stdin_small_file() -> bool {
302302

303303
matches!(
304304
stat::fstat(io::stdin().as_fd()),
305-
Ok(meta) if meta.st_mode & libc::S_IFMT == libc::S_IFREG && meta.st_size <= (10 << 20)
305+
Ok(meta) if meta.st_mode as libc::mode_t & libc::S_IFMT == libc::S_IFREG && meta.st_size <= (10 << 20)
306306
)
307307
}
308308

0 commit comments

Comments
 (0)