setup.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. def fix_broken_aclocal_path():
  19. os.environ['ACLOCAL_PATH'] = '/ucrt64/share/aclocal:/usr/share/aclocal'
  20. import platform
  21. if platform.system() == 'Windows':
  22. fix_broken_libpython_fn()
  23. if os.getenv('MSYSTEM') == 'UCRT64':
  24. fix_broken_aclocal_path()
  25. if not os.path.exists(cache_path):
  26. os.makedirs(cache_path)
  27. if not os.path.exists(ext_path):
  28. print('\nCloning libsecp256k1')
  29. run(['git','clone','https://github.com/bitcoin-core/secp256k1.git'],check=True,cwd=cache_path)
  30. if not os.path.exists(os.path.join(ext_path,'.libs/libsecp256k1.a')):
  31. print('\nBuilding libsecp256k1')
  32. cmds = {
  33. 'Windows': (
  34. ['sh','./autogen.sh'],
  35. ['sh','./configure','CFLAGS=-g -O2 -fPIC','--disable-dependency-tracking'],
  36. ['mingw32-make','MAKE=mingw32-make']
  37. ),
  38. 'Linux': (
  39. ['./autogen.sh'],
  40. ['./configure','CFLAGS=-g -O2 -fPIC'],
  41. ['make'] + ([] if have_arm else ['-j4']),
  42. ),
  43. }[platform.system()]
  44. for cmd in cmds:
  45. print('Executing {}'.format(' '.join(cmd)))
  46. run(cmd,check=True,cwd=ext_path)
  47. uname = {k:run(['uname',f'-{k}'],stdout=PIPE,check=True).stdout.strip().decode() for k in ('m','s')}
  48. have_arm = uname['m'] in ('aarch64','armv7l') # x86_64, aarch64, armv7l
  49. have_msys2 = uname['s'].startswith('MSYS_NT') # Linux, MSYS_NT.*
  50. class my_build_ext(build_ext):
  51. def build_extension(self,ext):
  52. build_libsecp256k1()
  53. build_ext.build_extension(self,ext)
  54. setup(
  55. cmdclass = { 'build_ext': my_build_ext },
  56. ext_modules = [Extension(
  57. name = 'mmgen.proto.secp256k1.secp256k1',
  58. sources = ['extmod/secp256k1mod.c'],
  59. libraries = ([],['gmp'])[have_msys2],
  60. extra_objects = [os.path.join(ext_path,'.libs/libsecp256k1.a')],
  61. include_dirs = [os.path.join(ext_path,'include')],
  62. )]
  63. )