Skip to content

Commit

Permalink
setup.py: support pkgconfig to use system-installed libsecp256k1_zkp
Browse files Browse the repository at this point in the history
If the environment contains WALLY_ABI_PY_WHEEL_USE_PKG_SECP256K1=1 (and
the amalgamated objects are not being built), then the pkgconfig Python
module will be called to find the include path for the system-installed
libsecp256k1_zkp, and the secp256k1 submodule will not be checked out.

WALLY_ABI_PY_WHEEL_USE_PKG_SECP256K1 can alternatively be set to a
specific package name if the pkg-config name of the system-installed
libsecp256k1-zkp is something other than 'libsecp256k1_zkp'.
  • Loading branch information
whitslack committed Nov 16, 2023
1 parent e8cd9a1 commit 8139509
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,44 @@
'\nuse it. Set WALLY_ABI_PY_WHEEL_USE_LIB=static to link with the static library.\n',
file=sys.stderr)

def str_to_bool(str, if_none=ValueError, if_invalid=ValueError):
if str is None:
default = if_none
else:
lstr = str.lower()
if lstr in ('0', 'n', 'no', 'f', 'false', 'off'):
return False
elif lstr in ('1', 'y', 'yes', 't', 'true', 'on'):
return True
default = if_invalid
if isinstance(default, BaseException):
raise default('Invalid flag value: {}'.format(str))
return default

def pkg_default(pkg, if_true, if_false=None):
b = str_to_bool(pkg, None, None)
return if_true if b == True else if_false if b == False else pkg

PKG_SECP256K1 = pkg_default(
os.environ.get('WALLY_ABI_PY_WHEEL_USE_PKG_SECP256K1'), 'libsecp256k1_zkp')

if PKG_SECP256K1 is not None:
try:
import pkgconfig
except ImportError:
print('Error: You specified WALLY_ABI_PY_WHEEL_USE_PKG_SECP256K1, but you do not have'
'\nthe pkgconfig Python module installed.',
file=sys.stderr)
sys.exit(127)

def call(args, cwd=ABS_PATH):
subprocess.check_call(args, cwd=cwd, env=CONFIGURE_ENV)

if not os.path.exists('src/secp256k1/Makefile.am'):
# Sync libsecp-zkp
if (USE_LIB == 'no' or PKG_SECP256K1 is None) and not os.path.exists(
'src/.libs/libwallycore.so' if USE_LIB == 'shared' else
'src/secp256k1/.libs/libsecp256k1.a' if USE_LIB == 'static' else
'src/secp256k1/Makefile.am'):
# Sync libsecp256k1-zkp (only if needed)
call(['git','submodule','init'])
call(['git','submodule','sync','--recursive'])
call(['git','submodule','update','--init','--recursive'])
Expand Down Expand Up @@ -90,7 +123,6 @@ def call(args, cwd=ABS_PATH):
include_dirs=[
'./',
'./src',
'./src/secp256k1/include',
]
library_dirs = [
]
Expand All @@ -103,13 +135,18 @@ def call(args, cwd=ABS_PATH):
if USE_LIB == 'no':
include_dirs += [
'./src/ccan',
'./src/secp256k1/include',
]
sources += [
'src/amalgamation/combined.c',
'src/amalgamation/combined_ccan.c',
'src/amalgamation/combined_ccan2.c',
]
else:
if PKG_SECP256K1 is None:
include_dirs += ['./src/secp256k1/include']
else:
include_dirs += pkgconfig.parse(PKG_SECP256K1)['include_dirs']
library_dirs += ['src/.libs']
libraries += ['wallycore']
if USE_LIB == 'static':
Expand Down

0 comments on commit 8139509

Please sign in to comment.