setup.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/env python3
  2. #
  3. # MMGen Wallet, a terminal-based cryptocurrency wallet
  4. # Copyright (C)2013-2025 The MMGen Project <mmgen@tuta.io>
  5. # Licensed under the GNU General Public License, Version 3:
  6. # https://www.gnu.org/licenses
  7. # Public project repositories:
  8. # https://github.com/mmgen/mmgen-wallet
  9. # https://gitlab.com/mmgen/mmgen-wallet
  10. import sys, os
  11. from pathlib import Path
  12. from subprocess import run
  13. from setuptools import setup, Extension
  14. from setuptools.command.build_ext import build_ext
  15. def build_libsecp256k1():
  16. home = Path(os.environ['HOME'])
  17. cache_path = home.joinpath('.cache', 'mmgen')
  18. src_path = home.joinpath('.cache', 'mmgen', 'secp256k1')
  19. lib_file = src_path.joinpath('.libs', 'libsecp256k1.dll.a')
  20. root = Path().resolve().anchor
  21. installed_lib_file = Path(root, 'msys64', 'ucrt64', 'lib', 'libsecp256k1.dll.a')
  22. if installed_lib_file.exists():
  23. return
  24. # fix broken aclocal path:
  25. os.environ['ACLOCAL_PATH'] = '/C/msys64/ucrt64/share/aclocal:/C/msys64/usr/share/aclocal'
  26. if not src_path.exists():
  27. cache_path.mkdir(parents=True)
  28. print('\nCloning libsecp256k1')
  29. run(['git', 'clone', 'https://github.com/bitcoin-core/secp256k1.git'], check=True, cwd=cache_path)
  30. if not lib_file.exists():
  31. print(f'\nBuilding libsecp256k1 (cwd={str(src_path)})')
  32. cmds = (
  33. ['sh', './autogen.sh'],
  34. ['sh', './configure', 'CFLAGS=-g -O2 -fPIC', '--disable-dependency-tracking',
  35. '--enable-module-recovery', 'MAKE=mingw32-make'],
  36. ['mingw32-make', 'MAKE=mingw32-make'])
  37. for cmd in cmds:
  38. print('Executing {}'.format(' '.join(cmd)))
  39. run(cmd, check=True, cwd=src_path)
  40. if not installed_lib_file.exists():
  41. run(['mingw32-make', 'install', 'MAKE=mingw32-make'], check=True, cwd=src_path)
  42. class my_build_ext(build_ext):
  43. def build_extension(self, ext):
  44. if sys.platform == 'win32':
  45. build_libsecp256k1()
  46. build_ext.build_extension(self, ext)
  47. setup(
  48. cmdclass = {'build_ext': my_build_ext},
  49. ext_modules = [Extension(
  50. name = 'mmgen.proto.secp256k1.secp256k1',
  51. sources = ['extmod/secp256k1mod.c'],
  52. depends = ['extmod/random.h'],
  53. libraries = ['gmp', 'secp256k1', 'bcrypt'] if sys.platform == 'win32' else ['secp256k1'],
  54. include_dirs = ['/usr/local/include'] if sys.platform == 'darwin' else [],
  55. library_dirs = ['/usr/local/lib'] if sys.platform == 'darwin' else [],
  56. )]
  57. )