| 1 |
// This file is part of the uutils coreutils package. |
| 2 |
// |
| 3 |
// For the full copyright and license information, please view the LICENSE |
| 4 |
// file that was distributed with this source code. |
| 5 |
|
| 6 |
// spell-checker:ignore (ToDO) bindeps dylib libstdbuf deps liblibstdbuf |
| 7 |
|
| 8 |
use std::env; |
| 9 |
use std::fs; |
| 10 |
use std::path::Path; |
| 11 |
use std::process::Command; |
| 12 |
|
| 13 |
fn main() { |
| 14 |
// do not compile libstdbuf for windows target. The windows stdbuf.exe loads libstdbuf.dll compiled for the cygwin target. |
| 15 |
if env::var("CARGO_CFG_UNIX").is_err() { |
| 16 |
println!("cargo:rustc-cfg=feature=\"feat_external_libstdbuf\""); |
| 17 |
return; |
| 18 |
} |
| 19 |
println!("cargo:rerun-if-changed=build.rs"); |
| 20 |
println!("cargo:rerun-if-changed=src/libstdbuf/src/libstdbuf.rs"); |
| 21 |
|
| 22 |
let out_dir = env::var("OUT_DIR").expect("OUT_DIR not set"); |
| 23 |
let target = env::var("TARGET").unwrap_or_else(|_| "unknown".to_string()); |
| 24 |
|
| 25 |
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap(); |
| 26 |
let target_vendor = env::var("CARGO_CFG_TARGET_VENDOR").unwrap(); |
| 27 |
|
| 28 |
let dylib_ext = if target_vendor == "apple" { |
| 29 |
".dylib" |
| 30 |
} else if target_os == "cygwin" { |
| 31 |
".dll" |
| 32 |
} else { |
| 33 |
".so" |
| 34 |
}; |
| 35 |
|
| 36 |
// Check if we're building from the repository (where src/libstdbuf exists) |
| 37 |
// or from crates.io (where it doesn't) |
| 38 |
let libstdbuf_src = Path::new("src/libstdbuf"); |
| 39 |
if !libstdbuf_src.exists() { |
| 40 |
// When building from crates.io, libstdbuf is already available as a dependency |
| 41 |
// We can't build it here, so we'll need to handle this differently |
| 42 |
// For now, we'll create a dummy library file to satisfy the include_bytes! macro |
| 43 |
let lib_name = format!("libstdbuf{dylib_ext}"); |
| 44 |
let dest_path = Path::new(&out_dir).join(&lib_name); |
| 45 |
|
| 46 |
// Create an empty file as a placeholder |
| 47 |
// The actual library will be provided by the dependency |
| 48 |
fs::write(&dest_path, []).expect("Failed to create placeholder libstdbuf"); |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
// Create a separate build directory for libstdbuf to avoid conflicts |
| 53 |
let build_dir = Path::new(&out_dir).join("libstdbuf-build"); |
| 54 |
fs::create_dir_all(&build_dir).expect("Failed to create build directory"); |
| 55 |
|
| 56 |
// Get the cargo executable |
| 57 |
let cargo = env::var("CARGO").unwrap_or_else(|_| "cargo".to_string()); |
| 58 |
|
| 59 |
// This manual cargo call ensures that libstdbuf is built before stdbuf.rs is compiled, which is necessary |
| 60 |
// for include_bytes!(..."/libstdbuf.so") to work. |
| 61 |
// In the future, "bindeps" should be used to simplify the code and avoid the manual cargo call, |
| 62 |
// however this is available only in cargo nightly at the moment. |
| 63 |
// See the tracking issue: https://github.com/rust-lang/cargo/issues/9096 |
| 64 |
let mut cmd = Command::new(&cargo); |
| 65 |
cmd.env_clear().envs(env::vars()); |
| 66 |
cmd.current_dir(libstdbuf_src) |
| 67 |
.args(["build", "--target-dir", build_dir.to_str().unwrap()]); |
| 68 |
|
| 69 |
// Get the current profile |
| 70 |
let profile = env::var("PROFILE").unwrap_or_else(|_| "debug".to_string()); |
| 71 |
|
| 72 |
// Pass the release flag if we're in release mode |
| 73 |
if profile == "release" || profile == "bench" { |
| 74 |
cmd.arg("--release"); |
| 75 |
} |
| 76 |
|
| 77 |
// Pass the target architecture if we're cross-compiling |
| 78 |
if !target.is_empty() && target != "unknown" { |
| 79 |
cmd.arg("--target").arg(&target); |
| 80 |
} |
| 81 |
|
| 82 |
let status = cmd.status().expect("Failed to build libstdbuf"); |
| 83 |
assert!(status.success(), "Failed to build libstdbuf"); |
| 84 |
|
| 85 |
// Copy the built library to OUT_DIR for include_bytes! to find |
| 86 |
let lib_name = if target_os == "cygwin" { |
| 87 |
format!("stdbuf{dylib_ext}") |
| 88 |
} else { |
| 89 |
format!("libstdbuf{dylib_ext}") |
| 90 |
}; |
| 91 |
let dest_path = Path::new(&out_dir).join(format!("libstdbuf{dylib_ext}")); |
| 92 |
|
| 93 |
// Check multiple possible locations for the built library |
| 94 |
let possible_paths = if !target.is_empty() && target != "unknown" { |
| 95 |
vec![ |
| 96 |
build_dir.join(&target).join(&profile).join(&lib_name), |
| 97 |
build_dir |
| 98 |
.join(&target) |
| 99 |
.join(&profile) |
| 100 |
.join("deps") |
| 101 |
.join(&lib_name), |
| 102 |
] |
| 103 |
} else { |
| 104 |
vec![ |
| 105 |
build_dir.join(&profile).join(&lib_name), |
| 106 |
build_dir.join(&profile).join("deps").join(&lib_name), |
| 107 |
] |
| 108 |
}; |
| 109 |
|
| 110 |
// Try to find the library in any of the possible locations |
| 111 |
let mut found = false; |
| 112 |
for source_path in &possible_paths { |
| 113 |
if source_path.exists() { |
| 114 |
fs::copy(source_path, &dest_path).expect("Failed to copy libstdbuf library"); |
| 115 |
found = true; |
| 116 |
break; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
assert!( |
| 121 |
found, |
| 122 |
"Could not find built libstdbuf library. Searched in: {possible_paths:?}." |
| 123 |
); |
| 124 |
|
| 125 |
// Create a symlink to libstdbuf in the main target directory for development convenience |
| 126 |
// This allows running stdbuf directly from the build directory (e.g., target/debug/coreutils) |
| 127 |
// without needing to install the library to LIBSTDBUF_DIR. This is particularly useful for |
| 128 |
// running tests and manual testing during development. |
| 129 |
#[cfg(all(unix, feature = "feat_external_libstdbuf"))] |
| 130 |
{ |
| 131 |
use std::path::PathBuf; |
| 132 |
|
| 133 |
// Get the main target directory (e.g., target/debug or target/release) |
| 134 |
// OUT_DIR is something like target/debug/build/uu_stdbuf-<hash>/out |
| 135 |
let out_dir_path = PathBuf::from(&out_dir); |
| 136 |
if let Some(target_dir) = out_dir_path |
| 137 |
.parent() |
| 138 |
.and_then(|p| p.parent()) |
| 139 |
.and_then(|p| p.parent()) |
| 140 |
{ |
| 141 |
let lib_filename = format!("libstdbuf{dylib_ext}"); |
| 142 |
let source = target_dir.join("deps").join(&lib_filename); |
| 143 |
let dest = target_dir.join(&lib_filename); |
| 144 |
|
| 145 |
// Remove old symlink if it exists (in case it points to the wrong place) |
| 146 |
let _ = fs::remove_file(&dest); |
| 147 |
|
| 148 |
// Create symlink if the source exists |
| 149 |
if source.exists() { |
| 150 |
#[cfg(unix)] |
| 151 |
{ |
| 152 |
use std::os::unix::fs::symlink; |
| 153 |
if let Err(e) = symlink(&source, &dest) { |
| 154 |
eprintln!("Warning: Failed to create symlink for libstdbuf: {e}"); |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
} |