From 25fb8623908f9b5b3675e9625a5b630b796a618b Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Fri, 24 Sep 2021 20:07:06 +0000 Subject: [PATCH] setup.py: fully automate secp256k1 extension module build MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The secp256k1 library is now cloned from Github and built automatically. It’s linked statically to the extension mod, so that the resulting wheel can run on a system without libsecp256k1 installed. --- setup.py | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/setup.py b/setup.py index 091f4122..9c75eb29 100755 --- a/setup.py +++ b/setup.py @@ -1,16 +1,39 @@ #!/usr/bin/env python3 +import sys,os from setuptools import setup,Extension +from setuptools.command.build_ext import build_ext from subprocess import run,PIPE +cache_path = os.path.join(os.environ['HOME'],'.cache','mmgen') +ext_path = os.path.join(cache_path,'secp256k1') + +def build_libsecp256k1(): + if not os.path.exists(cache_path): + os.makedirs(cache_path) + if not os.path.exists(ext_path): + run(['git','clone','https://github.com/bitcoin-core/secp256k1.git'],check=True,cwd=cache_path) + if not os.path.exists(os.path.join(ext_path,'.libs/libsecp256k1.a')): + run(['./autogen.sh'],check=True,cwd=ext_path) + run(['./configure','CFLAGS=-g -O2 -fPIC'],check=True,cwd=ext_path) + run(['make','-j4'],check=True,cwd=ext_path) + have_msys2 = run(['uname','-s'],stdout=PIPE,check=True).stdout.startswith(b'MSYS_NT') if have_msys2: print('MSYS2 system detected') -setup(ext_modules=[Extension( - name = 'mmgen.secp256k1', - sources = ['extmod/secp256k1mod.c'], - libraries = ['secp256k1'] + ([],['gmp'])[have_msys2], - library_dirs = ['/usr/local/lib',r'C:\msys64\mingw64\lib',r'C:\msys64\usr\lib'], - include_dirs = ['/usr/local/include',r'C:\msys64\mingw64\include',r'C:\msys64\usr\include'], - )]) +class my_build_ext(build_ext): + def build_extension(self,ext): + build_libsecp256k1() + build_ext.build_extension(self,ext) + +setup( + cmdclass = { 'build_ext': my_build_ext }, + ext_modules = [Extension( + name = 'mmgen.secp256k1', + sources = ['extmod/secp256k1mod.c'], + libraries = ([],['gmp'])[have_msys2], + extra_objects = [os.path.join(ext_path,'.libs/libsecp256k1.a')], + include_dirs = [os.path.join(ext_path,'include')], + )] +)