From 7426ab1122f82d7a9393c894f98b610e87b9dddd Mon Sep 17 00:00:00 2001 From: LingX Date: Thu, 18 Jun 2026 16:06:33 -0400 Subject: [PATCH] Share downloaded bitcoind path with build script --- build.rs | 8 ++++---- src/download_path.rs | 40 ++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 16 +++++----------- 3 files changed, 49 insertions(+), 15 deletions(-) create mode 100644 src/download_path.rs diff --git a/build.rs b/build.rs index 7db3cb5..1aa67f1 100644 --- a/build.rs +++ b/build.rs @@ -22,6 +22,9 @@ mod download { use tar::Archive; include!("src/versions.rs"); + mod download_path { + include!("src/download_path.rs"); + } #[cfg(all( target_os = "macos", @@ -83,10 +86,7 @@ mod download { std::fs::create_dir(&bitcoin_exe_home) .with_context(|| format!("cannot create dir {:?}", bitcoin_exe_home))?; } - let existing_filename = bitcoin_exe_home - .join(format!("bitcoin-{}", VERSION)) - .join("bin") - .join("bitcoind"); + let existing_filename = download_path::downloaded_exe_path(Path::new(&out_dir), VERSION); if !existing_filename.exists() { println!( diff --git a/src/download_path.rs b/src/download_path.rs new file mode 100644 index 0000000..a75f63a --- /dev/null +++ b/src/download_path.rs @@ -0,0 +1,40 @@ +use std::path::{Path, PathBuf}; + +pub(crate) fn downloaded_exe_path(out_dir: impl AsRef, version: &str) -> PathBuf { + let mut path = out_dir.as_ref().to_path_buf(); + path.push("bitcoin"); + path.push(format!("bitcoin-{}", version)); + path.push("bin"); + + if cfg!(target_os = "windows") { + path.push("bitcoind.exe"); + } else { + path.push("bitcoind"); + } + + path +} + +#[cfg(test)] +mod tests { + use super::downloaded_exe_path; + use std::path::Path; + + #[test] + fn downloaded_exe_path_uses_bitcoin_layout() { + let executable = if cfg!(target_os = "windows") { + "bitcoind.exe" + } else { + "bitcoind" + }; + + assert_eq!( + downloaded_exe_path("/tmp/out", "26.0"), + Path::new("/tmp/out") + .join("bitcoin") + .join("bitcoin-26.0") + .join("bin") + .join(executable) + ); + } +} diff --git a/src/lib.rs b/src/lib.rs index fe075cb..e3b5e32 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,9 @@ mod versions; +#[cfg(feature = "download")] +mod download_path; + use crate::bitcoincore_rpc::jsonrpc::serde_json::Value; use anyhow::Context; use bitcoincore_rpc::{Auth, Client, RpcApi}; @@ -503,16 +506,7 @@ pub fn downloaded_exe_path() -> anyhow::Result { return Err(Error::SkipDownload.into()); } - let mut path: PathBuf = env!("OUT_DIR").into(); - path.push("bitcoin"); - path.push(format!("bitcoin-{}", versions::VERSION)); - path.push("bin"); - - if cfg!(target_os = "windows") { - path.push("bitcoind.exe"); - } else { - path.push("bitcoind"); - } + let path = download_path::downloaded_exe_path(env!("OUT_DIR"), versions::VERSION); Ok(format!("{}", path.display())) } @@ -521,7 +515,7 @@ pub fn downloaded_exe_path() -> anyhow::Result { /// /// 1) If it's specified in the `BITCOIND_EXE` env var /// 2) If there is no env var but an auto-download feature such as `23_1` is enabled, returns the -/// path of the downloaded executabled +/// path of the downloaded executabled /// 3) If neither of the precedent are available, the `bitcoind` executable is searched in the `PATH` pub fn exe_path() -> anyhow::Result { if let Ok(path) = std::env::var("BITCOIND_EXE") {