vendor-libsecp.sh (4096B)
1 #!/usr/bin/env bash 2 set -e 3 4 # NB adapted from rust-secp256k1/secp256k1-sys/vendor-libsecp.sh 5 # 6 # https://github.com/rust-bitcoin/rust-secp256k1 7 8 # Set default variables 9 if [ -z "$SECP_VENDOR_GIT_ROOT" ]; then 10 SECP_VENDOR_GIT_ROOT="$(git rev-parse --show-toplevel)" 11 else 12 SECP_VENDOR_GIT_ROOT="$(realpath "$SECP_VENDOR_GIT_ROOT")" 13 fi 14 SECP_SYS="$SECP_VENDOR_GIT_ROOT"/secp256k1-sys 15 DEFAULT_VERSION_CODE=$(grep "^version" ../ppad-csecp256k1.cabal | sed 's/\./_/g' | sed 's/version: *\([0-9]\+.*\)/\1/') 16 DEFAULT_DEPEND_DIR="$SECP_SYS/depend" 17 DEFAULT_SECP_REPO=https://github.com/bitcoin-core/secp256k1.git 18 19 : "${SECP_VENDOR_VERSION_CODE:=$DEFAULT_VERSION_CODE}" 20 : "${SECP_VENDOR_DEPEND_DIR:=$DEFAULT_DEPEND_DIR}" 21 : "${SECP_VENDOR_SECP_REPO:=$DEFAULT_SECP_REPO}" 22 # CP_NOT_CLONE lets us just copy a directory rather than git cloning. 23 # This is usually a bad idea, since it will bring in build artifacts or any other 24 # junk from the source directory, but may be useful during development or CI. 25 : "${SECP_VENDOR_CP_NOT_CLONE:=no}" 26 27 echo "Using version code $SECP_VENDOR_VERSION_CODE. Set SECP_VENDOR_VERSION_CODE to override." 28 echo "Using depend directory $SECP_VENDOR_DEPEND_DIR. Set SECP_VENDOR_DEPEND_DIR to override." 29 echo "Using secp repository $SECP_VENDOR_SECP_REPO. Set SECP_VENDOR_SECP_REPO to override." 30 31 # Parse command-line options 32 SECP_REV="" 33 FORCE=no 34 while (( "$#" )); do 35 case "$1" in 36 -f) 37 FORCE=yes 38 ;; 39 *) 40 if [ -z "$SECP_REV" ]; then 41 echo "Using secp256k1 revision $SECP_REV." 42 SECP_REV="$1" 43 else 44 echo "WARNING: ignoring unknown command-line argument $1" 45 fi 46 ;; 47 esac 48 shift 49 done 50 51 if [ -z "$SECP_REV" ]; then 52 echo "WARNING: No secp256k1 revision specified. Will use whatever we find at the git repo." 53 fi 54 echo 55 56 # Check if we will do anything destructive. 57 58 if [ "$FORCE" == "no" ]; then 59 if ! git diff --quiet -- "*.hs"; then 60 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)." 61 exit 2 62 fi 63 if ! git diff --quiet -- "$SECP_VENDOR_DEPEND_DIR"; then 64 echo "ERROR: The depend directory appears to be modified. Check it in or pass -f (this directory will be deleted)." 65 exit 2 66 fi 67 fi 68 69 DIR=./secp256k1 70 71 pushd "$SECP_VENDOR_DEPEND_DIR" > /dev/null 72 rm -rf "$DIR" || true 73 74 # Clone the repo. As a special case, if the repo is a local path and we have 75 # not specified a revision, just copy the directory rather than using 'git clone'. 76 # This lets us use non-git repos or dirty source trees as secp sources. 77 if [ "$SECP_VENDOR_CP_NOT_CLONE" == "yes" ]; then 78 cp -r "$SECP_VENDOR_SECP_REPO" "$DIR" 79 chmod -R +w "$DIR" # cp preserves write perms, which if missing will cause patch to fail 80 else 81 git clone "$SECP_VENDOR_SECP_REPO" "$DIR" 82 fi 83 84 # Check out specified revision 85 pushd "$DIR" > /dev/null 86 if [ -n "$SECP_REV" ]; then 87 git checkout "$SECP_REV" 88 fi 89 SOURCE_REV=$(git rev-parse HEAD || echo "[unknown revision from $SECP_VENDOR_SECP_REPO]") 90 rm -rf .git/ || true 91 popd 92 93 # Record revision 94 echo "# This file was automatically created by $(basename "$0")" > ./secp256k1-HEAD-revision.txt 95 echo "$SOURCE_REV" >> ./secp256k1-HEAD-revision.txt 96 97 # Prefix all methods with haskellsecp and a version prefix 98 find "$DIR" \ 99 -not -path '*/\.*' \ 100 -not -name "CHANGELOG.md" \ 101 -type f \ 102 -print0 | xargs -0 sed -i "/^#include/! s/secp256k1_/haskellsecp256k1_v${SECP_VENDOR_VERSION_CODE}_/g" 103 # special rule for a method that is not prefixed in libsecp 104 find "$DIR" \ 105 -not -path '*/\.*' \ 106 -type f \ 107 -print0 | xargs -0 sed -i "/^#include/! s/ecdsa_signature_parse_der_lax/haskellsecp256k1_v${SECP_VENDOR_VERSION_CODE}_ecdsa_signature_parse_der_lax/g" 108 109 cd "$SECP_SYS" 110 111 # Update the extern references in the Haskell FFI source files. 112 find "./lib/" \ 113 -name "*.hs" \ 114 -type f \ 115 -print0 | xargs -0 sed -i -r "s/haskellsecp256k1_v[0-9]+_[0-9]+_[0-9]+_(.*)([\"\(])/haskellsecp256k1_v${SECP_VENDOR_VERSION_CODE}_\1\2/g" 116 117 popd > /dev/null 118