setup.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. def fix_broken_libpython_fn():
  10. from pathlib import Path
  11. from_path = Path('C:/msys64/mingw64/lib/libpython3.10.dll.a')
  12. to_path = Path('C:/msys64/mingw64/lib/libpython310.dll.a')
  13. if from_path.exists() and not to_path.exists():
  14. import shutil
  15. print(f'Fixing broken library filename: {from_path.name!r} -> {to_path.name!r}')
  16. shutil.copy2(from_path,to_path)
  17. import platform
  18. if platform.system() == 'Windows':
  19. fix_broken_libpython_fn()
  20. if not os.path.exists(cache_path):
  21. os.makedirs(cache_path)
  22. if not os.path.exists(ext_path):
  23. print('\nCloning libsecp256k1')
  24. run(['git','clone','https://github.com/bitcoin-core/secp256k1.git'],check=True,cwd=cache_path)
  25. if not os.path.exists(os.path.join(ext_path,'.libs/libsecp256k1.a')):
  26. print('\nBuilding libsecp256k1')
  27. cmds = {
  28. 'Windows': (
  29. ['sh','./autogen.sh'],
  30. ['sh','./configure','CFLAGS=-g -O2 -fPIC','--disable-dependency-tracking'],
  31. ['mingw32-make','MAKE=mingw32-make']
  32. ),
  33. 'Linux': (
  34. ['./autogen.sh'],
  35. ['./configure','CFLAGS=-g -O2 -fPIC'],
  36. ['make'] + ([] if have_arm else ['-j4']),
  37. ),
  38. }[platform.system()]
  39. for cmd in cmds:
  40. print('Executing {}'.format(' '.join(cmd)))
  41. run(cmd,check=True,cwd=ext_path)
  42. uname = {k:run(['uname',f'-{k}'],stdout=PIPE,check=True).stdout.strip().decode() for k in ('m','s')}
  43. have_arm = uname['m'] in ('aarch64','armv7l') # x86_64, aarch64, armv7l
  44. have_msys2 = uname['s'].startswith('MSYS_NT') # Linux, MSYS_NT.*
  45. class my_build_ext(build_ext):
  46. def build_extension(self,ext):
  47. build_libsecp256k1()
  48. build_ext.build_extension(self,ext)
  49. setup(
  50. cmdclass = { 'build_ext': my_build_ext },
  51. ext_modules = [Extension(
  52. name = 'mmgen.proto.secp256k1.secp256k1',
  53. sources = ['extmod/secp256k1mod.c'],
  54. libraries = ([],['gmp'])[have_msys2],
  55. extra_objects = [os.path.join(ext_path,'.libs/libsecp256k1.a')],
  56. include_dirs = [os.path.join(ext_path,'include')],
  57. )]
  58. )