Skip to content

Commit 4cec098

Browse files
committedFeb 11, 2020
build: create a standalone build script for libsecp256k1
heavily based on Electron-Cash/Electron-Cash@eda0159
1 parent 1d72585 commit 4cec098

11 files changed

+146
-105
lines changed
 

‎MANIFEST.in

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ graft electrum
1212
prune electrum/tests
1313
graft contrib/udev
1414

15+
exclude electrum/*.so
16+
exclude electrum/*.so.0
17+
1518
global-exclude __pycache__
1619
global-exclude *.py[co~]
1720
global-exclude *.py.orig

‎contrib/build-linux/appimage/build.sh

+4-23
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ BUILDDIR="$CONTRIB_APPIMAGE/build/appimage"
1010
APPDIR="$BUILDDIR/electrum.AppDir"
1111
CACHEDIR="$CONTRIB_APPIMAGE/.cache/appimage"
1212

13+
export GCC_STRIP_BINARIES="1"
14+
1315
# pinned versions
1416
PYTHON_VERSION=3.7.6
1517
PKG2APPIMAGE_COMMIT="eb8f3acdd9f11ab19b78f5cb15daa772367daf15"
16-
LIBSECP_VERSION="b408c6a8b287003d1ade5709e6f7bc3c7f1d5be7"
1718
SQUASHFSKIT_COMMIT="ae0d656efa2d0df2fcac795b6823b44462f19386"
1819

1920

@@ -45,7 +46,6 @@ info "building python."
4546
tar xf "$CACHEDIR/Python-$PYTHON_VERSION.tar.xz" -C "$BUILDDIR"
4647
(
4748
cd "$BUILDDIR/Python-$PYTHON_VERSION"
48-
export SOURCE_DATE_EPOCH=1530212462
4949
LC_ALL=C export BUILD_DATE=$(date -u -d "@$SOURCE_DATE_EPOCH" "+%b %d %Y")
5050
LC_ALL=C export BUILD_TIME=$(date -u -d "@$SOURCE_DATE_EPOCH" "+%H:%M:%S")
5151
# Patch taken from Ubuntu http://archive.ubuntu.com/ubuntu/pool/main/p/python3.7/python3.7_3.7.6-1.debian.tar.xz
@@ -77,26 +77,8 @@ git clone "https://github.com/squashfskit/squashfskit.git" "$BUILDDIR/squashfski
7777
MKSQUASHFS="$BUILDDIR/squashfskit/squashfs-tools/mksquashfs"
7878

7979

80-
info "building libsecp256k1."
81-
(
82-
git clone https://github.com/bitcoin-core/secp256k1 "$CACHEDIR"/secp256k1 \
83-
|| (cd "$CACHEDIR"/secp256k1 && git reset --hard && git pull)
84-
cd "$CACHEDIR"/secp256k1
85-
git reset --hard "$LIBSECP_VERSION"
86-
git clean -f -x -q
87-
export SOURCE_DATE_EPOCH=1530212462
88-
echo "LDFLAGS = -no-undefined" >> Makefile.am
89-
./autogen.sh
90-
./configure \
91-
--prefix="$APPDIR/usr" \
92-
--enable-module-recovery \
93-
--enable-experimental \
94-
--enable-module-ecdh \
95-
--disable-jni \
96-
-q
97-
make -j4 -s || fail "Could not build libsecp"
98-
make -s install > /dev/null || fail "Could not install libsecp"
99-
)
80+
"$CONTRIB"/make_libsecp256k1.sh || fail "Could not build libsecp"
81+
cp -f "$PROJECT_ROOT/electrum/libsecp256k1.so.0" "$APPDIR/usr/lib/libsecp256k1.so.0" || fail "Could not copy libsecp to its destination"
10082

10183

10284
appdir_python() {
@@ -224,7 +206,6 @@ rm -rf "$PYDIR"/site-packages/PyQt5/Qt.so
224206

225207
# these are deleted as they were not deterministic; and are not needed anyway
226208
find "$APPDIR" -path '*/__pycache__*' -delete
227-
rm "$APPDIR"/usr/lib/libsecp256k1.a
228209
# note that jsonschema-*.dist-info is needed by that package as it uses 'pkg_resources.get_distribution'
229210
# also, see https://gitlab.com/python-devs/importlib_metadata/issues/71
230211
for f in "$PYDIR"/site-packages/jsonschema-*.dist-info; do mv "$f" "$(echo "$f" | sed s/\.dist-info/\.dist-info2/)"; done

‎contrib/build-wine/build-electrum-git.sh

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ NAME_ROOT=electrum
66
export WINEPREFIX=/opt/wine64
77
export WINEDEBUG=-all
88
export PYTHONDONTWRITEBYTECODE=1
9-
export PYTHONHASHSEED=22
109

1110
PYHOME=c:/python3
1211
PYTHON="wine $PYHOME/python.exe -OO -B"

‎contrib/build-wine/build-secp256k1.sh

-56
This file was deleted.

‎contrib/build-wine/build.sh

+11-4
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22

33
set -e
44

5-
# Lucky number
6-
export PYTHONHASHSEED=22
7-
85
here="$(dirname "$(readlink -e "$0")")"
96
test -n "$here" -a -d "$here" || exit
107

118
export CONTRIB="$here/.."
9+
export PROJECT_ROOT="$CONTRIB/.."
1210
export CACHEDIR="$here/.cache"
1311
export PIP_CACHE_DIR="$CACHEDIR/pip_cache"
1412

13+
export BUILD_TYPE="wine"
14+
export GCC_TRIPLET_HOST="i686-w64-mingw32"
15+
export GCC_TRIPLET_BUILD="x86_64-pc-linux-gnu"
16+
export GCC_STRIP_BINARIES="1"
17+
1518
. "$CONTRIB"/build_tools_util.sh
1619

1720
info "Clearing $here/build and $here/dist..."
@@ -20,7 +23,11 @@ rm "$here"/dist/* -rf
2023

2124
mkdir -p "$CACHEDIR" "$PIP_CACHE_DIR"
2225

23-
$here/build-secp256k1.sh || fail "build-secp256k1 failed"
26+
if [ -f "$PROJECT_ROOT/electrum/libsecp256k1-0.dll" ]; then
27+
info "libsecp256k1 already built, skipping"
28+
else
29+
"$CONTRIB"/make_libsecp256k1.sh || fail "Could not build libsecp"
30+
fi
2431

2532
$here/prepare-wine.sh || fail "prepare-wine failed"
2633

‎contrib/build-wine/deterministic.spec

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ binaries = []
3434
# Workaround for "Retro Look":
3535
binaries += [b for b in collect_dynamic_libs('PyQt5') if 'qwindowsvista' in b[0]]
3636

37-
binaries += [('C:/tmp/libsecp256k1.dll', '.')]
37+
binaries += [('C:/tmp/libsecp256k1-0.dll', '.')]
3838
binaries += [('C:/tmp/libusb-1.0.dll', '.')]
3939

4040
datas = [

‎contrib/build-wine/prepare-wine.sh

+2-3
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ info "Compiling libusb..."
8989
git remote add origin $LIBUSB_REPO
9090
git fetch --depth 1 origin $LIBUSB_COMMIT
9191
git checkout -b pinned FETCH_HEAD
92-
export SOURCE_DATE_EPOCH=1530212462
9392
echo "libusb_1_0_la_LDFLAGS += -Wc,-static" >> libusb/Makefile.am
9493
./bootstrap.sh || fail "Could not bootstrap libusb"
9594
host="i686-w64-mingw32"
@@ -102,8 +101,8 @@ info "Compiling libusb..."
102101
cp "$CACHEDIR/libusb/libusb/.libs/libusb-1.0.dll" $WINEPREFIX/drive_c/tmp/ || fail "Could not copy libusb to its destination"
103102

104103

105-
# copy libsecp dll (already built by build-secp256k1.sh)
106-
cp "$CACHEDIR/secp256k1/libsecp256k1.dll" $WINEPREFIX/drive_c/tmp/ || fail "Could not copy libsecp to its destination"
104+
# copy libsecp dll (already built)
105+
cp "$PROJECT_ROOT/electrum/libsecp256k1-0.dll" $WINEPREFIX/drive_c/tmp/ || fail "Could not copy libsecp to its destination"
107106

108107

109108
info "Building PyInstaller."

‎contrib/build_tools_util.sh

+55
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,58 @@ function retry() {
7070

7171
return $result
7272
}
73+
74+
function gcc_with_triplet()
75+
{
76+
TRIPLET="$1"
77+
CMD="$2"
78+
shift 2
79+
if [ -n "$TRIPLET" ] ; then
80+
"$TRIPLET-$CMD" "$@"
81+
else
82+
"$CMD" "$@"
83+
fi
84+
}
85+
86+
function gcc_host()
87+
{
88+
gcc_with_triplet "$GCC_TRIPLET_HOST" "$@"
89+
}
90+
91+
function gcc_build()
92+
{
93+
gcc_with_triplet "$GCC_TRIPLET_BUILD" "$@"
94+
}
95+
96+
function host_strip()
97+
{
98+
if [ "$GCC_STRIP_BINARIES" -ne "0" ] ; then
99+
case "$BUILD_TYPE" in
100+
linux|wine)
101+
gcc_host strip "$@"
102+
;;
103+
darwin)
104+
# TODO: Strip on macOS?
105+
;;
106+
esac
107+
fi
108+
}
109+
110+
111+
112+
export SOURCE_DATE_EPOCH=1530212462
113+
export PYTHONHASHSEED=22
114+
# Set the build type, overridden by wine build
115+
export BUILD_TYPE="${BUILD_TYPE:-$(uname | tr '[:upper:]' '[:lower:]')}"
116+
# No additional autoconf flags by default
117+
export AUTOCONF_FLAGS=""
118+
# Add host / build flags if the triplets are set
119+
if [ -n "$GCC_TRIPLET_HOST" ] ; then
120+
export AUTOCONF_FLAGS="$AUTOCONF_FLAGS --host=$GCC_TRIPLET_HOST"
121+
fi
122+
if [ -n "$GCC_TRIPLET_BUILD" ] ; then
123+
export AUTOCONF_FLAGS="$AUTOCONF_FLAGS --build=$GCC_TRIPLET_BUILD"
124+
fi
125+
126+
export GCC_STRIP_BINARIES="${GCC_STRIP_BINARIES:-0}"
127+

‎contrib/make_libsecp256k1.sh

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/bash
2+
3+
LIBSECP_VERSION="b408c6a8b287003d1ade5709e6f7bc3c7f1d5be7"
4+
5+
set -e
6+
7+
here=$(dirname $(realpath "$0" 2> /dev/null || grealpath "$0"))
8+
CONTRIB="$here"
9+
PROJECT_ROOT="$CONTRIB/.."
10+
11+
. "$here"/build_tools_util.sh || (echo "Could not source build_tools_util.sh" && exit 1)
12+
13+
pkgname="secp256k1"
14+
info "Building $pkgname..."
15+
16+
(
17+
cd $CONTRIB
18+
if [ ! -d secp256k1 ]; then
19+
git clone https://github.com/bitcoin-core/secp256k1.git
20+
fi
21+
cd secp256k1
22+
git reset --hard
23+
git clean -f -x -q
24+
git checkout $LIBSECP_VERSION
25+
26+
if ! [ -x configure ] ; then
27+
echo "libsecp256k1_la_LDFLAGS = -no-undefined" >> Makefile.am
28+
echo "LDFLAGS = -no-undefined" >> Makefile.am
29+
./autogen.sh || fail "Could not run autogen for $pkgname. Please make sure you have automake and libtool installed, and try again."
30+
fi
31+
if ! [ -r config.status ] ; then
32+
./configure \
33+
$AUTOCONF_FLAGS \
34+
--prefix="$here/$pkgname/dist" \
35+
--enable-module-recovery \
36+
--enable-experimental \
37+
--enable-module-ecdh \
38+
--disable-jni \
39+
--disable-tests \
40+
--disable-static \
41+
--enable-shared || fail "Could not configure $pkgname. Please make sure you have a C compiler installed and try again."
42+
fi
43+
make -j4 || fail "Could not build $pkgname"
44+
make install || fail "Could not install $pkgname"
45+
. "$here/$pkgname/dist/lib/libsecp256k1.la"
46+
host_strip "$here/$pkgname/dist/lib/$dlname"
47+
cp -fpv "$here/$pkgname/dist/lib/$dlname" "$PROJECT_ROOT/electrum" || fail "Could not copy the $pkgname binary to its destination"
48+
info "$dlname has been placed in the inner 'electrum' folder."
49+
)

‎contrib/osx/make_osx

+5-11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ PACKAGE=Electrum
77
GIT_REPO=https://github.com/spesmilo/electrum
88
LIBSECP_VERSION="b408c6a8b287003d1ade5709e6f7bc3c7f1d5be7"
99

10+
export GCC_STRIP_BINARIES="1"
11+
1012
. $(dirname "$0")/base.sh
1113

1214
CONTRIB_OSX="$(dirname "$(realpath "$0")")"
@@ -16,7 +18,6 @@ ROOT_FOLDER="$CONTRIB/.."
1618
src_dir=$(dirname "$0")
1719
cd $src_dir/../..
1820

19-
export PYTHONHASHSEED=22
2021
VERSION=`git describe --tags --dirty --always`
2122

2223
which brew > /dev/null 2>&1 || fail "Please install brew from https://brew.sh/ to continue"
@@ -96,17 +97,10 @@ cp $BUILDDIR/libusb/1.0.22/lib/libusb-1.0.dylib contrib/osx
9697
echo "82c368dfd4da017ceb32b12ca885576f325503428a4966cc09302cbd62702493 contrib/osx/libusb-1.0.dylib" | \
9798
shasum -a 256 -c || fail "libusb checksum mismatched"
9899

99-
info "Building libsecp256k1"
100+
info "Preparing for building libsecp256k1"
100101
brew install autoconf automake libtool
101-
git clone https://github.com/bitcoin-core/secp256k1 $BUILDDIR/secp256k1
102-
pushd $BUILDDIR/secp256k1
103-
git reset --hard $LIBSECP_VERSION
104-
git clean -f -x -q
105-
./autogen.sh
106-
./configure --enable-module-recovery --enable-experimental --enable-module-ecdh --disable-jni
107-
make -j4
108-
popd
109-
cp $BUILDDIR/secp256k1/.libs/libsecp256k1.0.dylib contrib/osx
102+
"$CONTRIB"/make_libsecp256k1.sh || fail "Could not build libsecp"
103+
cp "$ROOT_FOLDER"/electrum/libsecp256k1.0.dylib contrib/osx
110104

111105
info "Building CalinsQRReader..."
112106
d=contrib/osx/CalinsQRReader

‎electrum/ecc_fast.py

+16-6
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,25 @@ class LibModuleMissing(Exception): pass
3838

3939
def load_library():
4040
if sys.platform == 'darwin':
41-
library_path = 'libsecp256k1.0.dylib'
41+
library_paths = (os.path.join(os.path.dirname(__file__), 'libsecp256k1.0.dylib'),
42+
'libsecp256k1.0.dylib')
4243
elif sys.platform in ('windows', 'win32'):
43-
library_path = 'libsecp256k1.dll'
44+
library_paths = (os.path.join(os.path.dirname(__file__), 'libsecp256k1-0.dll'),
45+
'libsecp256k1-0.dll')
4446
elif 'ANDROID_DATA' in os.environ:
45-
library_path = 'libsecp256k1.so'
46-
else:
47-
library_path = 'libsecp256k1.so.0'
47+
library_paths = ('libsecp256k1.so',)
48+
else: # desktop Linux and similar
49+
library_paths = (os.path.join(os.path.dirname(__file__), 'libsecp256k1.so.0'),
50+
'libsecp256k1.so.0')
4851

49-
secp256k1 = ctypes.cdll.LoadLibrary(library_path)
52+
secp256k1 = None
53+
for libpath in library_paths:
54+
try:
55+
secp256k1 = ctypes.cdll.LoadLibrary(libpath)
56+
except:
57+
pass
58+
else:
59+
break
5060
if not secp256k1:
5161
_logger.error('libsecp256k1 library failed to load')
5262
return None

0 commit comments

Comments
 (0)
Please sign in to comment.