Skip to content

Commit 0982f96

Browse files
committed
contrib: add bump-bitcoin.sh script
This facilitates bumping the bitcoin dependency and re-generate the clang2py language bindings.
1 parent 3cbc8b5 commit 0982f96

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

contrib/bump-bitcoin.sh

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
# Get the directory where the script is located
6+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
7+
8+
# Function to display usage information
9+
usage() {
10+
echo "Usage: $0 <commit-sha>"
11+
echo "Example: $0 abcdef1234567890abcdef1234567890abcdef12"
12+
exit 1
13+
}
14+
15+
# Function to print error messages
16+
error_echo() {
17+
echo "Error: $1"
18+
}
19+
20+
# Check if a commit SHA is provided
21+
if [ -z "${1:-}" ]; then
22+
error_echo "No commit SHA provided."
23+
usage
24+
fi
25+
26+
COMMIT_SHA="$1"
27+
28+
# Validate commit SHA format
29+
if ! [[ "$COMMIT_SHA" =~ ^[a-fA-F0-9]{40}$ ]]; then
30+
error_echo "Invalid commit SHA format."
31+
usage
32+
fi
33+
34+
# Ensure required commands are available
35+
required_commands=("git" "cmake" "ninja" "clang2py")
36+
for cmd in "${required_commands[@]}"; do
37+
if ! command -v "$cmd" &> /dev/null; then
38+
error_echo "$cmd is not installed."
39+
exit 1
40+
fi
41+
done
42+
43+
# Pull the subtree with the provided commit SHA
44+
echo "Pulling subtree with commit SHA: $COMMIT_SHA"
45+
git subtree pull --prefix=depend/bitcoin https://github.com/bitcoin/bitcoin "$COMMIT_SHA" --squash
46+
47+
echo "Subtree pulled successfully."
48+
49+
pushd depend/bitcoin
50+
51+
mkdir -p build
52+
cmake -B build -G Ninja -DBUILD_KERNEL_LIB=ON -DBUILD_UTIL_CHAINSTATE=ON
53+
cmake --build build
54+
55+
# Install and capture library path
56+
INSTALL_OUTPUT=$(sudo cmake --install build 2>&1)
57+
LIB_PATH=$(echo "$INSTALL_OUTPUT" | grep -Eo '/[^ ]+\.(so|dylib|dll)') || true
58+
59+
NM_OPTION=""
60+
if [[ "${OSTYPE}" == "darwin"* ]]; then
61+
NM_OPTION="--nm $SCRIPT_DIR/nm_patch.py"
62+
fi
63+
64+
if [ -z "$LIB_PATH" ]; then
65+
echo "Error: Library path not found."
66+
exit 1
67+
fi
68+
69+
OUTPUT_FILE="bindings.py.new"
70+
71+
if [ -f "$OUTPUT_FILE" ]; then
72+
echo "Warning: $OUTPUT_FILE already exists and will be overwritten."
73+
fi
74+
75+
popd
76+
77+
clang2py "$SCRIPT_DIR/../depend/bitcoin/src/kernel/bitcoinkernel.h" -l "$LIB_PATH" $NM_OPTION > "$OUTPUT_FILE"
78+
79+
echo "Bindings generated successfully at $OUTPUT_FILE."

contrib/nm_patch.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/opt/homebrew/bin/python3
2+
3+
# Copied from https://github.com/trolldbois/ctypeslib/issues/125#issuecomment-1552170404
4+
import sys
5+
import os
6+
7+
args = sys.argv[1:]
8+
args.remove("--dynamic")
9+
# print(args, file=sys.stderr)
10+
os.execvp("nm", args)

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ packages = { find = { where = ["src"] } }
3030
dev = [
3131
"pytest>=7.0",
3232
"ctypeslib2", # to generate the language bindings
33+
"clang==15.*",
3334
]

0 commit comments

Comments
 (0)