Skip to content

Commit

Permalink
Make some crates optional behind cli/extra feature flags (#61)
Browse files Browse the repository at this point in the history
structopt, dirs, dotenv, pretty_env_logger and signal-hook.
  • Loading branch information
shesek committed Nov 9, 2020
1 parent 76b732e commit 9db2c34
Show file tree
Hide file tree
Showing 9 changed files with 244 additions and 167 deletions.
28 changes: 17 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ edition = "2018"
include = [ "src", "LICENSE" ]

[features]
default = [ "electrum", "http", "track-spends" ]
default = [ "cli", "electrum", "http", "track-spends" ]
cli = [ "extra", "structopt", "dotenv" ]
extra = [ "dirs", "pretty_env_logger", "signal-hook" ]
electrum = []
http = [ "warp", "tokio" ]
webhooks = [ "reqwest" ]
Expand All @@ -22,38 +24,42 @@ ffi = []
[lib]
crate-type = [ "lib", "cdylib" ]

[[bin]]
name = "bwt"
path = "src/main.rs"
required-features = [ "cli" ]

[dependencies]
bitcoin = { version = "0.25.2", features = [ "use-serde" ] }
bitcoin_hashes = { version = "0.9.0", features = [ "serde" ] }
bitcoincore-rpc = "0.12.0"
miniscript = { version = "3.0.0", features = [ "serde" ] }
chrono = { version = "0.4.19", default-features = false }
dotenv = "0.15.0"
dirs = "3.0.1"
hex = "0.4.2"
serde = { version = "1.0.117", features = [ "derive" ] }
serde_json = "1.0.59"
lazy_static = "1.4.0"
anyhow = "1.0.34"
log = "0.4.11"
pretty_env_logger = "0.4.0"
structopt = "0.3.20"
thiserror = "1.0.22"

# http deps
tokio = { version = "0.2.22", features = ["macros"], optional = true }
warp = { version = "0.2.5", optional = true }

# webhooks deps
reqwest = { version = "0.10.8", optional = true, features = ["json", "blocking"] }

# cli/extra deps
structopt = { version = "0.3.20", optional = true }
dotenv = { version = "0.15.0", optional = true }
dirs = { version = "3.0.1", optional = true }
pretty_env_logger = { version = "0.4.0", optional = true }
[target.'cfg(unix)'.dependencies]
signal-hook = "0.1.16"

#[patch.crates-io]
#bitcoincore-rpc = { path = "../rust-bitcoincore-rpc/client" }
#warp = { path = "/tmp/warp" }
signal-hook = { version = "0.1.16", optional = true }

# Statically link OpenSSL when cross-compiling to ARM
# OpenSSL is currently disabled on ARM, see https://github.com/shesek/bwt/issues/52

# [target.'cfg(any(target_arch = "arm", target_arch = "aarch64"))'.dependencies]
# openssl = { version = "0.10", features = ["vendored"], optional = true }
# webhooks = [ "reqwest", "openssl" ]
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ FROM rust:1.46-slim as builder
RUN apt-get update && apt-get install -y pkg-config libssl-dev
WORKDIR /usr/src/bwt
COPY . .
ARG FEATURES="electrum http webhooks track-spends"
ARG FEATURES="cli electrum http webhooks track-spends"
RUN cargo install --locked --path . --no-default-features --features "$FEATURES"

FROM debian:buster-slim
ARG FEATURES="electrum http webhooks track-spends"
ARG FEATURES="cli electrum http webhooks track-spends"
RUN echo $FEATURES | grep -v webhooks > /dev/null || (apt-get update && apt-get install -y libssl-dev)
COPY --from=builder /usr/local/cargo/bin/bwt /usr/local/bin/
ENTRYPOINT [ "bwt", "--bitcoind-dir", "/bitcoin" ]
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1258,13 +1258,13 @@ If you have [`cargo watch`](https://github.com/passcod/cargo-watch) installed, i

### Features

bwt has 4 optional features: `http`, `electrum`, `webhooks` and `track-spends`.
bwt has 7 optional features: `cli`, `http`, `electrum`, `webhooks`, `track-spends`, `ffi` and `extra`.

All are enabled by default except for `webhooks`.
All are enabled by default except for `webhooks` and `ffi`.

If you're working on code that is unrelated to the HTTP API, it is faster to build with just the `electrum track-spends` features.
If you're working on code that is unrelated to the HTTP API, it is faster to build without the `http` feature.

You can use `scripts/check.sh` to run `cargo check` for all (sensible) feature combos. This is important to ensure no errors were introduced for feature combos that you didn't use.
You can use `scripts/check.sh` to run `cargo check` for some feature combos. This is important to ensure no errors were introduced for feature combos that you didn't use.

### Tests

Expand Down
10 changes: 5 additions & 5 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ for cfg in x86_64-linux,x86_64-unknown-linux-gnu \
IFS=',' read platform target <<< $cfg
if [[ $TARGETS != *"$platform"* ]]; then continue; fi

# The OpenSSL dependency enabled by the webhooks feature causes the following error on ARM targets:
# /lib/aarch64-linux-gnu/libc.so.6: version `GLIBC_2.25' not found (required by bwt-0.1.4-arm64v8/bwt)
# The OpenSSL dependency enabled by the webhooks feature causes an error on ARM targets.
# Disable it for now on ARM, follow up at https://github.com/shesek/bwt/issues/52
features=http,electrum,track-spends$([[ $platform == "arm"* ]] || echo ',webhooks')
complete_feat=cli,http,electrum,track-spends$([[ $platform == "arm"* ]] || echo ',webhooks')
electrum_feat=cli,electrum

build bwt-$version-$platform $target $features
build bwt-$version-electrum_only-$platform $target electrum
build bwt-$version-$platform $target $complete_feat
build bwt-$version-electrum_only-$platform $target $electrum_feat
done

echo Building electrum plugin
Expand Down
10 changes: 7 additions & 3 deletions scripts/check.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#!/bin/bash

# All combos that include at least HTTP or Electrum
feature_combos='H E EH ET HT HW EW EHT EHW ETW HTW EHTW'
# All combos that include at least `cli` and one pf `http`/`electrum`
feature_combos="CH CE CEH CET CHT CHW CEW CEHT CEHW CETW CHTW CEHTW"

# Some simple combos with `ffi` and no `cli`
feature_combos="$feature_combos FE FHT"
# TODO test more `ffi` and `extra` combos

for features in $feature_combos; do
features=`echo $features | sed 's/H/http /; s/E/electrum /; s/W/webhooks /; s/T/track-spends /;'`
features=`echo $features | sed 's/H/http /; s/E/electrum /; s/W/webhooks /; s/T/track-spends /; s/C/cli /; s/F/ffi /;'`
echo "Checking $features"
cargo check --no-default-features --features "$features"
done
2 changes: 1 addition & 1 deletion scripts/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ if [ -z "$SKIP_DOCKER" ]; then

docker_tag=$docker_name:$version
docker build -t $docker_tag .
docker build -t $docker_tag-electrum --build-arg FEATURES=electrum .
docker build -t $docker_tag-electrum --build-arg FEATURES=cli,electrum .
docker tag $docker_tag $docker_name:latest
docker tag $docker_tag-electrum $docker_name:electrum
docker push $docker_name
Expand Down
2 changes: 1 addition & 1 deletion scripts/setup-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ shopt -s expand_aliases
(command -v electrum && command -v bitcoind && command -v bitcoin-cli) > /dev/null \
|| { echo >&2 "bitcoind, bitcoin-cli and electrum must be installed in PATH"; exit 1; }

export FEATURES=${FEATURES:-http electrum webhooks track-spends}
export FEATURES=${FEATURES:-cli http electrum webhooks track-spends}

if [ -z "$DIR" ]; then
DIR=`mktemp -d --suffix -bwt-env`
Expand Down
4 changes: 2 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl App {
c_rx
}

#[cfg(unix)]
#[cfg(all(unix, feature = "signal_hook"))]
fn default_shutdown_signal(&self) -> Option<mpsc::Receiver<()>> {
use signal_hook::iterator::Signals;

Expand All @@ -196,7 +196,7 @@ impl App {
Some(shutdown_rx)
}

#[cfg(not(unix))]
#[cfg(not(all(unix, feature = "signal_hook")))]
fn default_shutdown_signal(&self) -> Option<mpsc::Receiver<()>> {
None
}
Expand Down
Loading

0 comments on commit 9db2c34

Please sign in to comment.