Skip to content

Commit 493eaf7

Browse files
committed
Merge rust-bitcoin#594: Rewrite revendoring script
2ae7ca9 secp-sys: update README for new vendoring script (Andrew Poelstra) 4b02e9c run new vendor-libsecp.sh; fix upstream CHANGELOG. (Andrew Poelstra) b58a60f rewrite ./vendor-libsecp.sh (Andrew Poelstra) Pull request description: For Nix purposes I need the revendoring script to work without network access and without user interaction. I also realized it would be convenient if the script could figure out what the right version prefix is supposed to be. Then I noticed some shellcheck issues. Anyway I just rewrote the whole thing. I'm now able to run this script within nix and vet that the current contents of the `depend/` directory are consistent with the secp256k1-HEAD-revision.txt, for all commits. ACKs for top commit: tcharding: ACK 2ae7ca9 sanket1729: reACK 2ae7ca9 Tree-SHA512: ea3028e3517b2dbe0f34bcf20685945ecf543fc42e01f10d435432ad290088586b2a2b0f0e94bc3ce59ec38727656eb04eef57c5df6a34da77070e0f288b1d84
2 parents 1ad3107 + 2ae7ca9 commit 493eaf7

File tree

4 files changed

+108
-56
lines changed

4 files changed

+108
-56
lines changed

secp256k1-sys/README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ This prefix ensures that no symbol collision can happen:
2020
To update the vendored sources, use the `vendor-libsecp.sh` script:
2121

2222
```
23-
$ ./vendor-libsecp.sh depend <version-code> <rev>
23+
$ ./vendor-libsecp.sh <rev>
2424
```
2525

26-
- Where `<version-code>` is the secp256k1-sys version number underscored: `0_1_2`.
27-
- Where `<rev>` is the git revision of libsecp256k1 to checkout.
26+
Where `<rev>` is the git revision of libsecp256k1 to checkout. If you do not
27+
specify a revision, the script will simply clone the repo and use whatever
28+
revision the default branch is pointing to.
2829

2930

3031
## Linking to external symbols
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
# This file was automatically created by ./vendor-libsecp.sh
1+
# This file was automatically created by vendor-libsecp.sh
22
21ffe4b22a9683cf24ae0763359e401d1284cc7a

secp256k1-sys/depend/secp256k1/CHANGELOG.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
77
## [0.2.0] - 2022-12-12
88

99
### Added
10-
- Added `rustsecp256k1_v0_8_0_selftest`, to be used in conjunction with `rustsecp256k1_v0_8_0_context_static`.
10+
- Added `secp256k1_selftest`, to be used in conjunction with `secp256k1_context_static`.
1111

1212
### Changed
1313
- Enabled modules schnorrsig, extrakeys and ECDH by default in `./configure`.
1414

1515
### Deprecated
1616
- Deprecated context flags `SECP256K1_CONTEXT_VERIFY` and `SECP256K1_CONTEXT_SIGN`. Use `SECP256K1_CONTEXT_NONE` instead.
17-
- Renamed `rustsecp256k1_v0_8_0_context_no_precomp` to `rustsecp256k1_v0_8_0_context_static`.
17+
- Renamed `secp256k1_context_no_precomp` to `secp256k1_context_static`.
1818

1919
### ABI Compatibility
2020

secp256k1-sys/vendor-libsecp.sh

+101-50
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,96 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22
set -e
33

4-
5-
if [ -z "$1" ] | [ -z "$2" ]; then
6-
echo "\$1 parameter must be the rust-secp256k1-sys depend directory"
7-
echo "\$2 parameter must be the rust-secp256k1-sys version code (M_m_p format)"
8-
echo "\$3 parameter (optional) can be the revision to check out"
9-
exit 1
4+
# Set default variables
5+
if [ -z "$SECP_VENDOR_GIT_ROOT" ]; then
6+
SECP_VENDOR_GIT_ROOT="$(git rev-parse --show-toplevel)"
7+
else
8+
SECP_VENDOR_GIT_ROOT="$(realpath "$SECP_VENDOR_GIT_ROOT")"
109
fi
10+
SECP_SYS="$SECP_VENDOR_GIT_ROOT"/secp256k1-sys
11+
DEFAULT_VERSION_CODE=$(grep version "$SECP_SYS/Cargo.toml" | sed 's/\./_/g' | sed 's/.*"\(.*\)".*/\1/')
12+
DEFAULT_DEPEND_DIR="$SECP_SYS/depend"
13+
DEFAULT_SECP_REPO=https://github.com/bitcoin-core/secp256k1.git
14+
15+
: "${SECP_VENDOR_VERSION_CODE:=$DEFAULT_VERSION_CODE}"
16+
: "${SECP_VENDOR_DEPEND_DIR:=$DEFAULT_DEPEND_DIR}"
17+
: "${SECP_VENDOR_SECP_REPO:=$DEFAULT_SECP_REPO}"
18+
# CP_NOT_CLONE lets us just copy a directory rather than git cloning.
19+
# This is usually a bad idea, since it will bring in build artifacts or any other
20+
# junk from the source directory, but may be useful during development or CI.
21+
: "${SECP_VENDOR_CP_NOT_CLONE:=no}"
22+
23+
echo "Using version code $SECP_VENDOR_VERSION_CODE. Set SECP_VENDOR_VERSION_CODE to override."
24+
echo "Using depend directory $SECP_VENDOR_DEPEND_DIR. Set SECP_VENDOR_DEPEND_DIR to override."
25+
echo "Using secp repository $SECP_VENDOR_SECP_REPO. Set SECP_VENDOR_SECP_REPO to override."
1126

12-
PARENT_DIR=$1
13-
VERSIONCODE=$2
14-
REV=$3
15-
DIR=secp256k1
16-
ORIGDIR=$(pwd)
17-
18-
while true; do
19-
read -r -p "$PARENT_DIR/$DIR will be deleted [yn]: " yn
20-
case $yn in
21-
[Yy]* ) break;;
22-
[Nn]* ) exit;;
23-
* ) echo "Please answer yes or no.";;
27+
# Parse command-line options
28+
SECP_REV=""
29+
FORCE=no
30+
while (( "$#" )); do
31+
case "$1" in
32+
-f)
33+
FORCE=yes
34+
;;
35+
*)
36+
if [ -z "$SECP_REV" ]; then
37+
echo "Using secp256k1 revision $SECP_REV."
38+
SECP_REV="$1"
39+
else
40+
echo "WARNING: ignoring unknown command-line argument $1"
41+
fi
42+
;;
2443
esac
44+
shift
2545
done
2646

27-
cd "$PARENT_DIR" || exit 1
28-
rm -rf "$DIR"
29-
git clone https://github.com/bitcoin-core/secp256k1.git "$DIR"
30-
cd "$DIR"
31-
if [ -n "$REV" ]; then
32-
git checkout "$REV"
47+
if [ -z "$SECP_REV" ]; then
48+
echo "WARNING: No secp256k1 revision specified. Will use whatever we find at the git repo."
49+
fi
50+
echo
51+
52+
# Check if we will do anything destructive.
53+
54+
if [ "$FORCE" == "no" ]; then
55+
if ! git diff --quiet -- "*.rs"; then
56+
echo "ERROR: There appear to be modified source files. Check these in or pass -f (some source files will be modified to have symbols renamed)."
57+
exit 2
58+
fi
59+
if ! git diff --quiet -- "$SECP_VENDOR_DEPEND_DIR"; then
60+
echo "ERROR: The depend directory appears to be modified. Check it in or pass -f (this directory will be deleted)."
61+
exit 2
62+
fi
3363
fi
34-
HEAD=$(git rev-parse HEAD)
35-
cd ..
36-
echo "# This file was automatically created by $0" > ./secp256k1-HEAD-revision.txt
37-
echo "$HEAD" >> ./secp256k1-HEAD-revision.txt
3864

39-
# We need to make some source changes to the files.
65+
DIR=./secp256k1
66+
67+
pushd "$SECP_VENDOR_DEPEND_DIR" > /dev/null
68+
rm -rf "$DIR" || true
69+
70+
# Clone the repo. As a special case, if the repo is a local path and we have
71+
# not specified a revision, just copy the directory rather than using 'git clone'.
72+
# This lets us use non-git repos or dirty source trees as secp sources.
73+
if [ "$SECP_VENDOR_CP_NOT_CLONE" == "yes" ]; then
74+
cp -r "$SECP_VENDOR_SECP_REPO" "$DIR"
75+
chmod -R +w "$DIR" # cp preserves write perms, which if missing will cause patch to fail
76+
else
77+
git clone "$SECP_VENDOR_SECP_REPO" "$DIR"
78+
fi
79+
80+
# Check out specified revision
81+
pushd "$DIR" > /dev/null
82+
if [ -n "$SECP_REV" ]; then
83+
git checkout "$SECP_REV"
84+
fi
85+
SOURCE_REV=$(git rev-parse HEAD || echo "[unknown revision from $SECP_VENDOR_SECP_REPO]")
86+
rm -rf .git/ || true
87+
popd
88+
89+
# Record revision
90+
echo "# This file was automatically created by $(basename "$0")" > ./secp256k1-HEAD-revision.txt
91+
echo "$SOURCE_REV" >> ./secp256k1-HEAD-revision.txt
92+
93+
# Patch source files
4094

4195
# To support compiling for WASM, we need to remove all methods that use malloc.
4296
# To compensate, the secp_context_create and _destroy methods are redefined in Rust.
@@ -46,28 +100,25 @@ patch "$DIR/src/scratch_impl.h" "./scratch_impl.h.patch"
46100
patch "$DIR/src/util.h" "./util.h.patch"
47101

48102
# Prefix all methods with rustsecp and a version prefix
49-
find "$DIR" -not -path '*/\.*' -type f -print0 | xargs -0 sed -i "/^#include/! s/secp256k1_/rustsecp256k1_v${VERSIONCODE}_/g"
50-
103+
find "$DIR" \
104+
-not -path '*/\.*' \
105+
-not -name "CHANGELOG.md" \
106+
-type f \
107+
-print0 | xargs -0 sed -i "/^#include/! s/secp256k1_/rustsecp256k1_v${SECP_VENDOR_VERSION_CODE}_/g"
51108
# special rule for a method that is not prefixed in libsecp
52-
find "$DIR" -not -path '*/\.*' -type f -print0 | xargs -0 sed -i "/^#include/! s/ecdsa_signature_parse_der_lax/rustsecp256k1_v${VERSIONCODE}_ecdsa_signature_parse_der_lax/g"
53-
54-
# TODO: can be removed once 496c5b43b lands in secp-zkp
55-
find "$DIR" -not -path '*/\.*' -type f -print0 | xargs -0 sed -i 's/^const int CURVE_B/static const int CURVE_B/g'
56-
57-
while true; do
58-
read -r -p "Update Rust extern references and Cargo.toml as well? [yn]: " yn
59-
case $yn in
60-
[Yy]* ) break;;
61-
[Nn]* ) exit;;
62-
* ) echo "Please answer yes or no.";;
63-
esac
64-
done
65-
66-
cd "$ORIGDIR"
109+
find "$DIR" \
110+
-not -path '*/\.*' \
111+
-type f \
112+
-print0 | xargs -0 sed -i "/^#include/! s/ecdsa_signature_parse_der_lax/rustsecp256k1_v${SECP_VENDOR_VERSION_CODE}_ecdsa_signature_parse_der_lax/g"
67113

114+
cd "$SECP_SYS"
68115
# Update the `links = ` in the manifest file.
69-
sed -i -r "s/^links = \".*\"$/links = \"rustsecp256k1_v${VERSIONCODE}\"/" Cargo.toml
70-
116+
sed -i -r "s/^links = \".*\"$/links = \"rustsecp256k1_v${SECP_VENDOR_VERSION_CODE}\"/" Cargo.toml
71117
# Update the extern references in the Rust FFI source files.
72-
find "./src/" -name "*.rs" -type f -print0 | xargs -0 sed -i -r "s/rustsecp256k1_v[0-9]+_[0-9]+_[0-9]+_(.*)([\"\(])/rustsecp256k1_v${VERSIONCODE}_\1\2/g"
118+
find "./src/" \
119+
-name "*.rs" \
120+
-type f \
121+
-print0 | xargs -0 sed -i -r "s/rustsecp256k1_v[0-9]+_[0-9]+_[0-9]+_(.*)([\"\(])/rustsecp256k1_v${SECP_VENDOR_VERSION_CODE}_\1\2/g"
122+
123+
popd > /dev/null
73124

0 commit comments

Comments
 (0)