Skip to content
Open
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
8 changes: 4 additions & 4 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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!(
Expand Down
40 changes: 40 additions & 0 deletions src/download_path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::path::{Path, PathBuf};

pub(crate) fn downloaded_exe_path(out_dir: impl AsRef<Path>, 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)
);
}
}
16 changes: 5 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -503,16 +506,7 @@ pub fn downloaded_exe_path() -> anyhow::Result<String> {
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()))
}
Expand All @@ -521,7 +515,7 @@ pub fn downloaded_exe_path() -> anyhow::Result<String> {
///
/// 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<String> {
if let Ok(path) = std::env::var("BITCOIND_EXE") {
Expand Down