setup.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env python3
  2. #
  3. # MMGen Wallet, a terminal-based cryptocurrency wallet
  4. # Copyright (C)2013-2024 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, PIPE
  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. ['mingw32-make', 'MAKE=mingw32-make'])
  36. for cmd in cmds:
  37. print('Executing {}'.format(' '.join(cmd)))
  38. run(cmd, check=True, cwd=src_path)
  39. if not installed_lib_file.exists():
  40. run(['mingw32-make', 'install', 'MAKE=mingw32-make'], check=True, cwd=src_path)
  41. class my_build_ext(build_ext):
  42. def build_extension(self, ext):
  43. if sys.platform == 'win32':
  44. build_libsecp256k1()
  45. build_ext.build_extension(self, ext)
  46. setup(
  47. cmdclass = {'build_ext': my_build_ext},
  48. ext_modules = [Extension(
  49. name = 'mmgen.proto.secp256k1.secp256k1',
  50. sources = ['extmod/secp256k1mod.c'],
  51. libraries = ['gmp', 'secp256k1'] if sys.platform == 'win32' else ['secp256k1'],
  52. include_dirs = ['/usr/local/include'] if sys.platform == 'darwin' else [],
  53. library_dirs = ['/usr/local/lib'] if sys.platform == 'darwin' else [],
  54. )]
  55. )