setup.py 2.2 KB

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