setup.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env python3
  2. import sys,os
  3. from setuptools import setup,Extension
  4. from setuptools.command.build_ext import build_ext
  5. from subprocess import run,PIPE
  6. cache_path = os.path.join(os.environ['HOME'],'.cache','mmgen')
  7. ext_path = os.path.join(cache_path,'secp256k1')
  8. def build_libsecp256k1():
  9. if not os.path.exists(cache_path):
  10. os.makedirs(cache_path)
  11. if not os.path.exists(ext_path):
  12. print('\nCloning libsecp256k1')
  13. run(['git','clone','https://github.com/bitcoin-core/secp256k1.git'],check=True,cwd=cache_path)
  14. if not os.path.exists(os.path.join(ext_path,'.libs/libsecp256k1.a')):
  15. import platform
  16. print('\nBuilding libsecp256k1')
  17. cmds = {
  18. 'Windows': (
  19. ['sh','./autogen.sh'],
  20. ['sh','./configure','CFLAGS=-g -O2 -fPIC','--disable-dependency-tracking'],
  21. ['mingw32-make','MAKE=mingw32-make']
  22. ),
  23. 'Linux': (
  24. ['./autogen.sh'],
  25. ['./configure','CFLAGS=-g -O2 -fPIC'],
  26. ['make'] + ([] if have_arm else ['-j4']),
  27. ),
  28. }[platform.system()]
  29. for cmd in cmds:
  30. print('Executing {}'.format(' '.join(cmd)))
  31. run(cmd,check=True,cwd=ext_path)
  32. uname = {k:run(['uname',f'-{k}'],stdout=PIPE,check=True).stdout.strip().decode() for k in ('m','s')}
  33. have_arm = uname['m'] in ('aarch64','armv7l') # x86_64, aarch64, armv7l
  34. have_msys2 = uname['s'].startswith('MSYS_NT') # Linux, MSYS_NT.*
  35. class my_build_ext(build_ext):
  36. def build_extension(self,ext):
  37. build_libsecp256k1()
  38. build_ext.build_extension(self,ext)
  39. setup(
  40. cmdclass = { 'build_ext': my_build_ext },
  41. ext_modules = [Extension(
  42. name = 'mmgen.secp256k1',
  43. sources = ['extmod/secp256k1mod.c'],
  44. libraries = ([],['gmp'])[have_msys2],
  45. extra_objects = [os.path.join(ext_path,'.libs/libsecp256k1.a')],
  46. include_dirs = [os.path.join(ext_path,'include')],
  47. )]
  48. )