Skip to content

Commit 67bb3da

Browse files
committed
Feature-gate the executable auto-download
This way all the build-deps are not used when auto-download is not required granting a MSRV of 1.41.1
1 parent 5e4a925 commit 67bb3da

File tree

6 files changed

+180
-170
lines changed

6 files changed

+180
-170
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ jobs:
7272
strategy:
7373
fail-fast: false
7474
matrix:
75-
toolchain: [ "1.57.0", "stable", "nightly" ]
75+
toolchain: [ "1.41.1", "stable", "nightly" ]
7676

7777
steps:
7878
- uses: actions/checkout@v2

Cargo.toml

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,26 @@ which = "4.2.5"
1717
anyhow = "1.0.66"
1818

1919
[dev-dependencies]
20-
env_logger = "0.9"
20+
env_logger = "0.9.0"
2121

2222
[build-dependencies]
23-
bitcoin_hashes = "0.11"
24-
filetime = "0.2"
25-
flate2 = "1.0"
26-
tar = "0.4"
27-
ureq = "2.5.0"
28-
zip = "0.6"
29-
time = "=0.3.10" # otherwise rust 1.57 fails to select time "^0.3.7"
23+
bitcoin_hashes = { version = "0.11", optional = true }
24+
filetime = { version = "0.2", optional = true }
25+
flate2 = { version = "1.0", optional = true }
26+
tar = { version = "0.4", optional = true }
27+
ureq = { version = "2.5.0", optional = true }
28+
zip = { version = "0.6", optional = true }
3029

3130
[features]
32-
"23_0" = []
33-
"22_0" = []
34-
"0_21_1" = []
35-
"0_21_0" = []
36-
"0_20_1" = []
37-
"0_20_0" = []
38-
"0_19_1" = []
39-
"0_19_0_1" = []
40-
"0_18_1" = []
41-
"0_18_0" = []
42-
"0_17_1" = []
31+
"download" = [ "bitcoin_hashes", "filetime", "flate2", "tar", "ureq", "zip"]
32+
"23_0" = ["download"]
33+
"22_0" = ["download"]
34+
"0_21_1" = ["download"]
35+
"0_21_0" = ["download"]
36+
"0_20_1" = ["download"]
37+
"0_20_0" = ["download"]
38+
"0_19_1" = ["download"]
39+
"0_19_0_1" = ["download"]
40+
"0_18_1" = ["download"]
41+
"0_18_0" = ["download"]
42+
"0_17_1" = ["download"]

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ let bitcoind = bitcoind::BitcoinD::new(bitcoind::downloaded_exe_path().unwrap())
3535

3636
## MSRV
3737

38-
The MSRV is 1.41 for versions up to 0.27.\*, and 1.57 from 0.28.0.
38+
The MSRV is 1.41.1 for version 0.29.* if no feature is used, otherwise is 1.57
3939

4040
## Issues with traditional approach
4141

build.rs

Lines changed: 141 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -1,148 +1,160 @@
1-
use bitcoin_hashes::{sha256, Hash};
2-
use flate2::read::GzDecoder;
3-
use std::fs::File;
4-
use std::io::{self, BufRead, BufReader, Cursor, Read};
5-
use std::path::Path;
6-
use std::str::FromStr;
7-
use tar::Archive;
8-
9-
include!("src/versions.rs");
10-
11-
#[cfg(all(
12-
target_os = "macos",
13-
any(target_arch = "x86_64", target_arch = "aarch64"),
14-
))]
15-
fn download_filename() -> String {
16-
if cfg!(any(
17-
feature = "22_0",
18-
feature = "0_21_1",
19-
feature = "0_21_0",
20-
feature = "0_20_1",
21-
feature = "0_20_0",
22-
feature = "0_19_1",
23-
feature = "0_19_0_1",
24-
feature = "0_18_1",
25-
feature = "0_18_0",
26-
feature = "0_17_1",
27-
)) {
28-
format!("bitcoin-{}-osx64.tar.gz", &VERSION)
29-
} else {
30-
format!("bitcoin-{}-x86_64-apple-darwin.tar.gz", &VERSION)
31-
}
32-
}
1+
#[cfg(not(feature = "download"))]
2+
mod download {}
333

34-
#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
35-
fn download_filename() -> String {
36-
format!("bitcoin-{}-x86_64-linux-gnu.tar.gz", &VERSION)
37-
}
4+
#[cfg(not(feature = "download"))]
5+
fn main() {}
386

39-
#[cfg(all(target_os = "linux", target_arch = "aarch64"))]
40-
fn download_filename() -> String {
41-
format!("bitcoin-{}-aarch64-linux-gnu.tar.gz", &VERSION)
7+
#[cfg(feature = "download")]
8+
fn main() {
9+
download::start();
4210
}
4311

44-
#[cfg(all(target_os = "windows", target_arch = "x86_64"))]
45-
fn download_filename() -> String {
46-
format!("bitcoin-{}-win64.zip", &VERSION)
47-
}
12+
#[cfg(feature = "download")]
13+
mod download {
14+
15+
use bitcoin_hashes::{sha256, Hash};
16+
use flate2::read::GzDecoder;
17+
use std::fs::File;
18+
use std::io::{self, BufRead, BufReader, Cursor, Read};
19+
use std::path::Path;
20+
use std::str::FromStr;
21+
use tar::Archive;
22+
23+
include!("src/versions.rs");
4824

49-
fn get_expected_sha256(filename: &str) -> sha256::Hash {
50-
let sha256sums_filename = format!("sha256/bitcoin-core-{}-SHA256SUMS", &VERSION);
51-
#[cfg(any(
52-
feature = "0_21_1",
53-
feature = "0_21_0",
54-
feature = "0_20_1",
55-
feature = "0_20_0",
56-
feature = "0_19_1",
57-
feature = "0_19_0_1",
58-
feature = "0_18_1",
59-
feature = "0_18_0",
60-
feature = "0_17_1",
25+
#[cfg(all(
26+
target_os = "macos",
27+
any(target_arch = "x86_64", target_arch = "aarch64"),
6128
))]
62-
let sha256sums_filename = format!("{}.asc", sha256sums_filename);
63-
let file = File::open(&sha256sums_filename).unwrap();
64-
for line in BufReader::new(file).lines().flatten() {
65-
let tokens: Vec<_> = line.split(" ").collect();
66-
if tokens.len() == 2 && filename == tokens[1] {
67-
return sha256::Hash::from_str(tokens[0]).unwrap();
29+
fn download_filename() -> String {
30+
if cfg!(any(
31+
feature = "22_0",
32+
feature = "0_21_1",
33+
feature = "0_21_0",
34+
feature = "0_20_1",
35+
feature = "0_20_0",
36+
feature = "0_19_1",
37+
feature = "0_19_0_1",
38+
feature = "0_18_1",
39+
feature = "0_18_0",
40+
feature = "0_17_1",
41+
)) {
42+
format!("bitcoin-{}-osx64.tar.gz", &VERSION)
43+
} else {
44+
format!("bitcoin-{}-x86_64-apple-darwin.tar.gz", &VERSION)
6845
}
6946
}
70-
panic!(
71-
"Couldn't find hash for `{}` in `{}`:\n{}",
72-
filename,
73-
sha256sums_filename,
74-
std::fs::read_to_string(&sha256sums_filename).unwrap()
75-
);
76-
}
7747

78-
fn main() {
79-
if !HAS_FEATURE {
80-
return;
48+
#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
49+
fn download_filename() -> String {
50+
format!("bitcoin-{}-x86_64-linux-gnu.tar.gz", &VERSION)
51+
}
52+
53+
#[cfg(all(target_os = "linux", target_arch = "aarch64"))]
54+
fn download_filename() -> String {
55+
format!("bitcoin-{}-aarch64-linux-gnu.tar.gz", &VERSION)
8156
}
82-
let download_filename = download_filename();
83-
let expected_hash = get_expected_sha256(&download_filename);
84-
let out_dir = std::env::var_os("OUT_DIR").unwrap();
85-
let mut bitcoin_exe_home = Path::new(&out_dir).join("bitcoin");
86-
if !bitcoin_exe_home.exists() {
87-
std::fs::create_dir(&bitcoin_exe_home).unwrap();
57+
58+
#[cfg(all(target_os = "windows", target_arch = "x86_64"))]
59+
fn download_filename() -> String {
60+
format!("bitcoin-{}-win64.zip", &VERSION)
8861
}
89-
let existing_filename = bitcoin_exe_home
90-
.join(format!("bitcoin-{}", VERSION))
91-
.join("bin")
92-
.join("bitcoind");
93-
94-
if !existing_filename.exists() {
95-
println!(
96-
"filename:{} version:{} hash:{}",
97-
download_filename, VERSION, expected_hash
98-
);
9962

100-
let url = format!(
101-
"https://bitcoincore.org/bin/bitcoin-core-{}/{}",
102-
VERSION, download_filename
63+
fn get_expected_sha256(filename: &str) -> sha256::Hash {
64+
let sha256sums_filename = format!("sha256/bitcoin-core-{}-SHA256SUMS", &VERSION);
65+
#[cfg(any(
66+
feature = "0_21_1",
67+
feature = "0_21_0",
68+
feature = "0_20_1",
69+
feature = "0_20_0",
70+
feature = "0_19_1",
71+
feature = "0_19_0_1",
72+
feature = "0_18_1",
73+
feature = "0_18_0",
74+
feature = "0_17_1",
75+
))]
76+
let sha256sums_filename = format!("{}.asc", sha256sums_filename);
77+
let file = File::open(&sha256sums_filename).unwrap();
78+
for line in BufReader::new(file).lines().flatten() {
79+
let tokens: Vec<_> = line.split(" ").collect();
80+
if tokens.len() == 2 && filename == tokens[1] {
81+
return sha256::Hash::from_str(tokens[0]).unwrap();
82+
}
83+
}
84+
panic!(
85+
"Couldn't find hash for `{}` in `{}`:\n{}",
86+
filename,
87+
sha256sums_filename,
88+
std::fs::read_to_string(&sha256sums_filename).unwrap()
10389
);
104-
println!("url:{}", url);
105-
let mut downloaded_bytes = Vec::new();
106-
let resp = ureq::get(&url).call().unwrap();
107-
assert_eq!(resp.status(), 200, "url {} didn't return 200", url);
108-
109-
let _size = resp
110-
.into_reader()
111-
.read_to_end(&mut downloaded_bytes)
112-
.unwrap();
113-
let downloaded_hash = sha256::Hash::hash(&downloaded_bytes);
114-
assert_eq!(expected_hash, downloaded_hash);
115-
116-
if download_filename.ends_with(".tar.gz") {
117-
let d = GzDecoder::new(&downloaded_bytes[..]);
118-
119-
let mut archive = Archive::new(d);
120-
for mut entry in archive.entries().unwrap().flatten() {
121-
if let Ok(file) = entry.path() {
122-
if file.ends_with("bitcoind") {
123-
entry.unpack_in(&bitcoin_exe_home).unwrap();
90+
}
91+
92+
pub(crate) fn start() {
93+
let download_filename = download_filename();
94+
let expected_hash = get_expected_sha256(&download_filename);
95+
let out_dir = std::env::var_os("OUT_DIR").unwrap();
96+
let mut bitcoin_exe_home = Path::new(&out_dir).join("bitcoin");
97+
if !bitcoin_exe_home.exists() {
98+
std::fs::create_dir(&bitcoin_exe_home).unwrap();
99+
}
100+
let existing_filename = bitcoin_exe_home
101+
.join(format!("bitcoin-{}", VERSION))
102+
.join("bin")
103+
.join("bitcoind");
104+
105+
if !existing_filename.exists() {
106+
println!(
107+
"filename:{} version:{} hash:{}",
108+
download_filename, VERSION, expected_hash
109+
);
110+
111+
let url = format!(
112+
"https://bitcoincore.org/bin/bitcoin-core-{}/{}",
113+
VERSION, download_filename
114+
);
115+
println!("url:{}", url);
116+
let mut downloaded_bytes = Vec::new();
117+
let resp = ureq::get(&url).call().unwrap();
118+
assert_eq!(resp.status(), 200, "url {} didn't return 200", url);
119+
120+
let _size = resp
121+
.into_reader()
122+
.read_to_end(&mut downloaded_bytes)
123+
.unwrap();
124+
let downloaded_hash = sha256::Hash::hash(&downloaded_bytes);
125+
assert_eq!(expected_hash, downloaded_hash);
126+
127+
if download_filename.ends_with(".tar.gz") {
128+
let d = GzDecoder::new(&downloaded_bytes[..]);
129+
130+
let mut archive = Archive::new(d);
131+
for mut entry in archive.entries().unwrap().flatten() {
132+
if let Ok(file) = entry.path() {
133+
if file.ends_with("bitcoind") {
134+
entry.unpack_in(&bitcoin_exe_home).unwrap();
135+
}
124136
}
125137
}
126-
}
127-
} else if download_filename.ends_with(".zip") {
128-
let cursor = Cursor::new(downloaded_bytes);
129-
let mut archive = zip::ZipArchive::new(cursor).unwrap();
130-
for i in 0..zip::ZipArchive::len(&archive) {
131-
let mut file = archive.by_index(i).unwrap();
132-
let outpath = match file.enclosed_name() {
133-
Some(path) => path.to_owned(),
134-
None => continue,
135-
};
136-
137-
if outpath.file_name().map(|s| s.to_str()) == Some(Some("bitcoind.exe")) {
138-
for d in outpath.iter() {
139-
bitcoin_exe_home.push(d);
138+
} else if download_filename.ends_with(".zip") {
139+
let cursor = Cursor::new(downloaded_bytes);
140+
let mut archive = zip::ZipArchive::new(cursor).unwrap();
141+
for i in 0..zip::ZipArchive::len(&archive) {
142+
let mut file = archive.by_index(i).unwrap();
143+
let outpath = match file.enclosed_name() {
144+
Some(path) => path.to_owned(),
145+
None => continue,
146+
};
147+
148+
if outpath.file_name().map(|s| s.to_str()) == Some(Some("bitcoind.exe")) {
149+
for d in outpath.iter() {
150+
bitcoin_exe_home.push(d);
151+
}
152+
std::fs::create_dir_all(&bitcoin_exe_home.parent().unwrap()).unwrap();
153+
println!("{:?}", bitcoin_exe_home);
154+
let mut outfile = std::fs::File::create(&bitcoin_exe_home).unwrap();
155+
io::copy(&mut file, &mut outfile).unwrap();
156+
break;
140157
}
141-
std::fs::create_dir_all(&bitcoin_exe_home.parent().unwrap()).unwrap();
142-
println!("{:?}", bitcoin_exe_home);
143-
let mut outfile = std::fs::File::create(&bitcoin_exe_home).unwrap();
144-
io::copy(&mut file, &mut outfile).unwrap();
145-
break;
146158
}
147159
}
148160
}

0 commit comments

Comments
 (0)