1- #! /bin/bash
1+ #! /usr/ bin/env bash
22set -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 " ) "
109fi
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
2545done
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
3363fi
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"
46100patch " $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