setup.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. run(['git','clone','https://github.com/bitcoin-core/secp256k1.git'],check=True,cwd=cache_path)
  13. if not os.path.exists(os.path.join(ext_path,'.libs/libsecp256k1.a')):
  14. import platform
  15. cmds = {
  16. 'Windows': (
  17. ['sh','./autogen.sh'],
  18. ['sh','./configure','CFLAGS=-g -O2 -fPIC','--disable-dependency-tracking'],
  19. ['mingw32-make']
  20. ),
  21. 'Linux': (
  22. ['./autogen.sh'],
  23. ['./configure','CFLAGS=-g -O2 -fPIC'],
  24. ['make','-j4']
  25. ),
  26. }[platform.system()]
  27. for cmd in cmds:
  28. print('Executing {}'.format(' '.join(cmd)))
  29. run(cmd,check=True,cwd=ext_path)
  30. have_msys2 = run(['uname','-s'],stdout=PIPE,check=True).stdout.startswith(b'MSYS_NT')
  31. if have_msys2:
  32. print('MSYS2 system detected')
  33. class my_build_ext(build_ext):
  34. def build_extension(self,ext):
  35. build_libsecp256k1()
  36. build_ext.build_extension(self,ext)
  37. setup(
  38. cmdclass = { 'build_ext': my_build_ext },
  39. ext_modules = [Extension(
  40. name = 'mmgen.secp256k1',
  41. sources = ['extmod/secp256k1mod.c'],
  42. libraries = ([],['gmp'])[have_msys2],
  43. extra_objects = [os.path.join(ext_path,'.libs/libsecp256k1.a')],
  44. include_dirs = [os.path.join(ext_path,'include')],
  45. )]
  46. )