setup.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, a command-line cryptocurrency wallet
  4. # Copyright (C)2013-2023 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
  9. # https://gitlab.com/mmgen/mmgen
  10. import os,platform
  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. root = Path().resolve().anchor
  17. installed_lib_file = Path(root, 'msys64', 'ucrt64', 'lib', 'libsecp256k1.dll.a')
  18. if installed_lib_file.exists():
  19. return
  20. # fix broken aclocal path:
  21. os.environ['ACLOCAL_PATH'] = '/C/msys64/ucrt64/share/aclocal:/C/msys64/usr/share/aclocal'
  22. if not src_path.exists():
  23. cache_path.mkdir(parents=True)
  24. print('\nCloning libsecp256k1')
  25. run(['git','clone','https://github.com/bitcoin-core/secp256k1.git'],check=True,cwd=cache_path)
  26. if not lib_file.exists():
  27. print(f'\nBuilding libsecp256k1 (cwd={str(src_path)})')
  28. cmds = (
  29. ['sh','./autogen.sh'],
  30. ['sh','./configure','CFLAGS=-g -O2 -fPIC','--disable-dependency-tracking'],
  31. ['mingw32-make','MAKE=mingw32-make'])
  32. for cmd in cmds:
  33. print('Executing {}'.format(' '.join(cmd)))
  34. run(cmd,check=True,cwd=src_path)
  35. if not installed_lib_file.exists():
  36. run(['mingw32-make','install','MAKE=mingw32-make'],check=True,cwd=src_path)
  37. class my_build_ext(build_ext):
  38. def build_extension(self,ext):
  39. if platform.system() == 'Windows':
  40. build_libsecp256k1()
  41. build_ext.build_extension(self,ext)
  42. if platform.system() == 'Windows':
  43. home = Path(os.environ['HOME'])
  44. cache_path = home.joinpath('.cache','mmgen')
  45. src_path = home.joinpath('.cache','mmgen','secp256k1')
  46. lib_file = src_path.joinpath('.libs', 'libsecp256k1.dll.a')
  47. libraries = ['gmp']
  48. include_dirs = [str(src_path.joinpath('include'))]
  49. extra_objects = [str(lib_file)]
  50. else:
  51. libraries = []
  52. include_dirs = []
  53. out = run(['/sbin/ldconfig','-p'],stdout=PIPE,text=True,check=True).stdout.splitlines()
  54. import sys,re
  55. extra_objects = [s.split()[-1] for s in out if re.search(r'libsecp256k1.*\.so$',s)]
  56. if not extra_objects:
  57. print('setup.py: unable to find shared libsecp256k1 library. Is it installed on your system?')
  58. sys.exit(1)
  59. setup(
  60. cmdclass = { 'build_ext': my_build_ext },
  61. ext_modules = [Extension(
  62. name = 'mmgen.proto.secp256k1.secp256k1',
  63. sources = ['extmod/secp256k1mod.c'],
  64. libraries = libraries,
  65. include_dirs = include_dirs,
  66. extra_objects = extra_objects,
  67. )]
  68. )