diff --git a/README.md b/README.md index fde8596ec..8ae08f108 100644 --- a/README.md +++ b/README.md @@ -73,9 +73,6 @@ $ brew install swig - `--enable-swig-python`. Enable the [SWIG](http://www.swig.org/) Python interface. The resulting shared library can be imported from Python using the generated interface file `src/swig_python/wallycore/__init__.py`. (default: no). -- `--enable-python-manylinux`. Enable [manylinux](https://github.com/pypa/manylinux) - support for building [PyPI](https://pypi.org/) compatible python wheels. Using - the resulting library in non-python programs requires linking with `libpython.so`. - `--enable-swig-java`. Enable the [SWIG](http://www.swig.org/) Java (JNI) interface. After building, see `src/swig_java/src/com/blockstream/libwally/Wally.java` for the Java interface definition (default: no). diff --git a/configure.ac b/configure.ac index c57cc75a3..9af51d86d 100644 --- a/configure.ac +++ b/configure.ac @@ -311,12 +311,11 @@ AC_SUBST([libsecp256k1_LIBS]) # # Python facilities # -AC_ARG_ENABLE(python-manylinux, - AS_HELP_STRING([--enable-python-manylinux],[enable manylinux Python compatibility (default: no)]), - [python_manylinux=$enableval], [python_manylinux=no]) -AM_CONDITIONAL([PYTHON_MANYLINUX], [test "x$python_manylinux" = "xyes"]) - -AX_PYTHON_DEVEL([>= '2.7.0']) +dnl We set the 'optional' flag here because manylinux cibuildwheel containers +dnl don't include libpython*.so, and thus AX_PYTHON_DEVEL fails its final sanity +dnl check. We actually don't link against libpython*.so anyway, so all we care +dnl about is that we get a good value of $PYTHON_CPPFLAGS to pass to SWIG. +AX_PYTHON_DEVEL([>= '2.7.0'], [true]) AM_CONDITIONAL([HAVE_PYTHON], [test "$PYTHON" != ""]) @@ -338,10 +337,10 @@ AC_ARG_ENABLE(swig-python, [swig_python=$enableval], [swig_python=no]) AM_CONDITIONAL([USE_SWIG_PYTHON], [test "x$swig_python" = "xyes"]) -AM_CONDITIONAL([RUN_PYTHON_TESTS], [test "$PYTHON" != "" -a "x$pythonexists" = "xyes"]) +AM_CONDITIONAL([RUN_PYTHON_TESTS], [test -n "$PYTHON"]) if test "x$swig_python" = "xyes"; then - if test "x$pythonexists" != "xyes"; then + if test -z "$PYTHON_CPPFLAGS"; then AC_MSG_FAILURE([ERROR: No usable Python was found for swig-python]) fi if test "x$elements_abi" = "xno"; then diff --git a/setup.py b/setup.py index 98807bc84..1bcb7eb2f 100644 --- a/setup.py +++ b/setup.py @@ -62,7 +62,7 @@ def _call(args, cwd=ABS_PATH): _call(['git', 'submodule', 'update', '--init', '--recursive']) CONFIGURE_ARGS = [ - '--with-pic', '--enable-swig-python', '--enable-python-manylinux', + '--with-pic', '--enable-swig-python', '--disable-swig-java', '--disable-tests', '--disable-dependency-tracking' ] diff --git a/src/Makefile.am b/src/Makefile.am index 56154796d..b374a06cd 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -38,15 +38,7 @@ TOOLS_EXTRA_ARGS := endif if USE_SWIG_PYTHON -noinst_LTLIBRARIES += libswig_python.la -libswig_python_la_SOURCES = swig_python/swig_python_wrap.c - -libswig_python_la_CFLAGS = -I$(top_srcdir) $(libsecp256k1_CFLAGS) $(AM_CFLAGS) $(SWIG_PYTHON_CPPFLAGS) $(SWIG_WARN_CFLAGS) $(NOALIAS_CFLAGS) -if PYTHON_MANYLINUX -else -libswig_python_la_LIBADD = $(PYTHON_LIBS) -endif # PYTHON_MANYLINUX - +BUILT_SOURCES = swig_python/swig_python_wrap.c # Append our extra wrapper code to the package swig_python/swig_python_wrap.c : swig_python/swig.i swig_python/python_extra.py_in $(AM_V_at)$(SWIG) $(SWIG_PYTHON_OPT) $(SWIG_GEN_FLAGS) -outdir swig_python -o $@ $< && \ @@ -60,20 +52,6 @@ clean-local: clean-swig-python endif # USE_SWIG_PYTHON if RUN_PYTHON_TESTS -# Python requires the shared library to be named _wallycore.so -# for 'import' to work. -if IS_OSX -platform_dso_ext = dylib -else -if IS_MINGW -platform_dso_ext = dll -else -platform_dso_ext = so -endif # IS_MINGW -endif # IS_OSX -.libs/_wallycore.so: .libs/libwallycore.$(platform_dso_ext) - $(AM_V_at)ln -sfn libwallycore.$(platform_dso_ext) $@ -PYTHON_TEST_DEPS = .libs/_wallycore.so PYTHON_TEST = PYTHONDONTWRITEBYTECODE=1 $(PYTHON) endif @@ -224,31 +202,26 @@ endif TESTS = noinst_PROGRAMS = + if RUN_TESTS TESTS += test_bech32 noinst_PROGRAMS += test_bech32 test_bech32_SOURCES = ctest/test_bech32.c test_bech32_CFLAGS = -I$(top_srcdir)/include $(AM_CFLAGS) test_bech32_LDADD = $(lib_LTLIBRARIES) @CTEST_EXTRA_STATIC@ -if PYTHON_MANYLINUX -test_bech32_LDADD += $(PYTHON_LIBS) -endif + TESTS += test_psbt noinst_PROGRAMS += test_psbt test_psbt_SOURCES = ctest/test_psbt.c ccan/ccan/str/hex/hex.c test_psbt_CFLAGS = -I$(top_srcdir)/include $(AM_CFLAGS) -I$(srcdir)/ccan test_psbt_LDADD = $(lib_LTLIBRARIES) @CTEST_EXTRA_STATIC@ -if PYTHON_MANYLINUX -test_psbt_LDADD += $(PYTHON_LIBS) -endif + TESTS += test_psbt_limits noinst_PROGRAMS += test_psbt_limits test_psbt_limits_SOURCES = ctest/test_psbt_limits.c ccan/ccan/str/hex/hex.c test_psbt_limits_CFLAGS = -I$(top_srcdir)/include $(AM_CFLAGS) -I$(srcdir)/ccan test_psbt_limits_LDADD = $(lib_LTLIBRARIES) @CTEST_EXTRA_STATIC@ -if PYTHON_MANYLINUX -test_psbt_limits_LDADD += $(PYTHON_LIBS) -endif + if USE_PTHREAD TESTS += test_clear noinst_PROGRAMS += test_clear @@ -256,43 +229,32 @@ test_clear_SOURCES = ctest/test_clear.c test_clear_CFLAGS = -I$(top_srcdir)/include $(PTHREAD_CFLAGS) $(AM_CFLAGS) $(NOOPT_CFLAGS) $(NOBUILTIN_CFLAGS) test_clear_LIBS = $(PTHREAD_LIBS) test_clear_LDADD = $(lib_LTLIBRARIES) @CTEST_EXTRA_STATIC@ -if PYTHON_MANYLINUX -test_clear_LDADD += $(PYTHON_LIBS) -endif endif + TESTS += test_coinselection noinst_PROGRAMS += test_coinselection test_coinselection_SOURCES = ctest/test_coinselection.c test_coinselection_CFLAGS = -I$(top_srcdir)/include $(AM_CFLAGS) test_coinselection_LDADD = $(lib_LTLIBRARIES) @CTEST_EXTRA_STATIC@ -if PYTHON_MANYLINUX -test_coinselection_LDADD += $(PYTHON_LIBS) -endif + TESTS += test_tx noinst_PROGRAMS += test_tx test_tx_SOURCES = ctest/test_tx.c test_tx_CFLAGS = -I$(top_srcdir)/include $(AM_CFLAGS) test_tx_LDADD = $(lib_LTLIBRARIES) @CTEST_EXTRA_STATIC@ -if PYTHON_MANYLINUX -test_tx_LDADD += $(PYTHON_LIBS) -endif + TESTS += test_descriptor noinst_PROGRAMS += test_descriptor test_descriptor_SOURCES = ctest/test_descriptor.c test_descriptor_CFLAGS = -I$(top_srcdir)/include $(AM_CFLAGS) test_descriptor_LDADD = $(lib_LTLIBRARIES) @CTEST_EXTRA_STATIC@ -if PYTHON_MANYLINUX -test_descriptor_LDADD += $(PYTHON_LIBS) -endif + if BUILD_ELEMENTS TESTS += test_elements_tx noinst_PROGRAMS += test_elements_tx test_elements_tx_SOURCES = ctest/test_elements_tx.c test_elements_tx_CFLAGS = -I$(top_srcdir)/include $(AM_CFLAGS) test_elements_tx_LDADD = $(lib_LTLIBRARIES) @CTEST_EXTRA_STATIC@ -if PYTHON_MANYLINUX -test_elements_tx_LDADD += $(PYTHON_LIBS) -endif endif check-local: check-libwallycore check-swig-python check-swig-java @@ -300,7 +262,7 @@ check-local: check-libwallycore check-swig-python check-swig-java if SHARED_BUILD_ENABLED if RUN_PYTHON_TESTS -check-libwallycore: $(PYTHON_TEST_DEPS) +check-libwallycore: $(AM_V_at)$(PYTHON_TEST) test/test_address.py $(AM_V_at)$(PYTHON_TEST) test/test_aes.py $(AM_V_at)$(PYTHON_TEST) test/test_anti_exfil.py diff --git a/tools/build-aux/m4/ax_python_devel.m4 b/tools/build-aux/m4/ax_python_devel.m4 index 8269c547e..935056cc4 100644 --- a/tools/build-aux/m4/ax_python_devel.m4 +++ b/tools/build-aux/m4/ax_python_devel.m4 @@ -4,7 +4,7 @@ # # SYNOPSIS # -# AX_PYTHON_DEVEL([version]) +# AX_PYTHON_DEVEL([version[,optional]]) # # DESCRIPTION # @@ -23,6 +23,11 @@ # version number. Don't use "PYTHON_VERSION" for this: that environment # variable is declared as precious and thus reserved for the end-user. # +# By default this will fail if it does not detect a development version of +# python. If you want it to continue, set optional to true, like +# AX_PYTHON_DEVEL([], [true]). The ax_python_devel_found variable will be +# "no" if it fails. +# # This macro should work for all versions of Python >= 2.1.0. As an end # user, you can disable the check for the python version by setting the # PYTHON_NOVERSIONCHECK environment variable to something else than the @@ -67,10 +72,18 @@ # modified version of the Autoconf Macro, you may extend this special # exception to the GPL to apply to your modified version as well. -#serial 21 +#serial 37 AU_ALIAS([AC_PYTHON_DEVEL], [AX_PYTHON_DEVEL]) AC_DEFUN([AX_PYTHON_DEVEL],[ + # Get whether it's optional + if test -z "$2"; then + ax_python_devel_optional=false + else + ax_python_devel_optional=$2 + fi + ax_python_devel_found=yes + # # Allow the use of a (user set) custom python version # @@ -81,21 +94,26 @@ AC_DEFUN([AX_PYTHON_DEVEL],[ AC_PATH_PROG([PYTHON],[python[$PYTHON_VERSION]]) if test -z "$PYTHON"; then - AC_MSG_ERROR([Cannot find python$PYTHON_VERSION in your system path]) + AC_MSG_WARN([Cannot find python$PYTHON_VERSION in your system path]) + if ! $ax_python_devel_optional; then + AC_MSG_ERROR([Giving up, python development not available]) + fi + ax_python_devel_found=no PYTHON_VERSION="" fi - # - # Check for a version of Python >= 2.1.0 - # - AC_MSG_CHECKING([for a version of Python >= '2.1.0']) - ac_supports_python_ver=`$PYTHON -c "import sys; \ + if test $ax_python_devel_found = yes; then + # + # Check for a version of Python >= 2.1.0 + # + AC_MSG_CHECKING([for a version of Python >= '2.1.0']) + ac_supports_python_ver=`$PYTHON -c "import sys; \ ver = sys.version.split ()[[0]]; \ print (ver >= '2.1.0')"` - if test "$ac_supports_python_ver" != "True"; then + if test "$ac_supports_python_ver" != "True"; then if test -z "$PYTHON_NOVERSIONCHECK"; then AC_MSG_RESULT([no]) - AC_MSG_FAILURE([ + AC_MSG_WARN([ This version of the AC@&t@_PYTHON_DEVEL macro doesn't work properly with versions of Python before 2.1.0. You may need to re-run configure, setting the @@ -104,58 +122,119 @@ PYTHON_EXTRA_LIBS and PYTHON_EXTRA_LDFLAGS by hand. Moreover, to disable this check, set PYTHON_NOVERSIONCHECK to something else than an empty string. ]) + if ! $ax_python_devel_optional; then + AC_MSG_FAILURE([Giving up]) + fi + ax_python_devel_found=no + PYTHON_VERSION="" else AC_MSG_RESULT([skip at user request]) fi - else + else AC_MSG_RESULT([yes]) + fi fi - # - # if the macro parameter ``version'' is set, honour it - # - if test -n "$1"; then + if test $ax_python_devel_found = yes; then + # + # If the macro parameter ``version'' is set, honour it. + # A Python shim class, VPy, is used to implement correct version comparisons via + # string expressions, since e.g. a naive textual ">= 2.7.3" won't work for + # Python 2.7.10 (the ".1" being evaluated as less than ".3"). + # + if test -n "$1"; then AC_MSG_CHECKING([for a version of Python $1]) - ac_supports_python_ver=`$PYTHON -c "import sys; \ - ver = sys.version.split ()[[0]]; \ + cat << EOF > ax_python_devel_vpy.py +class VPy: + def vtup(self, s): + return tuple(map(int, s.strip().replace("rc", ".").split("."))) + def __init__(self): + import sys + self.vpy = tuple(sys.version_info)[[:3]] + def __eq__(self, s): + return self.vpy == self.vtup(s) + def __ne__(self, s): + return self.vpy != self.vtup(s) + def __lt__(self, s): + return self.vpy < self.vtup(s) + def __gt__(self, s): + return self.vpy > self.vtup(s) + def __le__(self, s): + return self.vpy <= self.vtup(s) + def __ge__(self, s): + return self.vpy >= self.vtup(s) +EOF + ac_supports_python_ver=`$PYTHON -c "import ax_python_devel_vpy; \ + ver = ax_python_devel_vpy.VPy(); \ print (ver $1)"` + rm -rf ax_python_devel_vpy*.py* __pycache__/ax_python_devel_vpy*.py* if test "$ac_supports_python_ver" = "True"; then - AC_MSG_RESULT([yes]) + AC_MSG_RESULT([yes]) else AC_MSG_RESULT([no]) - AC_MSG_ERROR([this package requires Python $1. + AC_MSG_WARN([this package requires Python $1. If you have it installed, but it isn't the default Python interpreter in your system path, please pass the PYTHON_VERSION variable to configure. See ``configure --help'' for reference. ]) + if ! $ax_python_devel_optional; then + AC_MSG_ERROR([Giving up]) + fi + ax_python_devel_found=no PYTHON_VERSION="" fi + fi fi - # - # Check if you have distutils, else fail - # - AC_MSG_CHECKING([for the distutils Python package]) - ac_distutils_result=`$PYTHON -c "import distutils" 2>&1` - if test $? -eq 0; then + if test $ax_python_devel_found = yes; then + # + # Check if you have distutils, else fail + # + AC_MSG_CHECKING([for the sysconfig Python package]) + ac_sysconfig_result=`$PYTHON -c "import sysconfig" 2>&1` + if test $? -eq 0; then AC_MSG_RESULT([yes]) - else + IMPORT_SYSCONFIG="import sysconfig" + else AC_MSG_RESULT([no]) - AC_MSG_ERROR([cannot import Python module "distutils". + + AC_MSG_CHECKING([for the distutils Python package]) + ac_sysconfig_result=`$PYTHON -c "from distutils import sysconfig" 2>&1` + if test $? -eq 0; then + AC_MSG_RESULT([yes]) + IMPORT_SYSCONFIG="from distutils import sysconfig" + else + AC_MSG_WARN([cannot import Python module "distutils". Please check your Python installation. The error was: -$ac_distutils_result]) - PYTHON_VERSION="" +$ac_sysconfig_result]) + if ! $ax_python_devel_optional; then + AC_MSG_ERROR([Giving up]) + fi + ax_python_devel_found=no + PYTHON_VERSION="" + fi + fi fi - # - # Check for Python include path - # - AC_MSG_CHECKING([for Python include path]) - if test -z "$PYTHON_CPPFLAGS"; then - python_path=`$PYTHON -c "import distutils.sysconfig; \ - print (distutils.sysconfig.get_python_inc ());"` - plat_python_path=`$PYTHON -c "import distutils.sysconfig; \ - print (distutils.sysconfig.get_python_inc (plat_specific=1));"` + if test $ax_python_devel_found = yes; then + # + # Check for Python include path + # + AC_MSG_CHECKING([for Python include path]) + if test -z "$PYTHON_CPPFLAGS"; then + if test "$IMPORT_SYSCONFIG" = "import sysconfig"; then + # sysconfig module has different functions + python_path=`$PYTHON -c "$IMPORT_SYSCONFIG; \ + print (sysconfig.get_path ('include'));"` + plat_python_path=`$PYTHON -c "$IMPORT_SYSCONFIG; \ + print (sysconfig.get_path ('platinclude'));"` + else + # old distutils way + python_path=`$PYTHON -c "$IMPORT_SYSCONFIG; \ + print (sysconfig.get_python_inc ());"` + plat_python_path=`$PYTHON -c "$IMPORT_SYSCONFIG; \ + print (sysconfig.get_python_inc (plat_specific=1));"` + fi if test -n "${python_path}"; then if test "${plat_python_path}" != "${python_path}"; then python_path="-I$python_path -I$plat_python_path" @@ -164,21 +243,22 @@ $ac_distutils_result]) fi fi PYTHON_CPPFLAGS=$python_path - fi - AC_MSG_RESULT([$PYTHON_CPPFLAGS]) + fi + AC_MSG_RESULT([$PYTHON_CPPFLAGS]) + AC_SUBST([PYTHON_CPPFLAGS]) - # - # Check for Python library path - # - AC_MSG_CHECKING([for Python library path]) - if test -z "$PYTHON_LIBS"; then + # + # Check for Python library path + # + AC_MSG_CHECKING([for Python library path]) + if test -z "$PYTHON_LIBS"; then # (makes two attempts to ensure we've got a version number # from the interpreter) ac_python_version=`cat</dev/null; then - # On OSX we can hit https://bugs.python.org/issue3588 - PYTHON_EXTRA_LIBS=`$PYTHON-config --ldflags` - fi - fi - AC_MSG_RESULT([$PYTHON_EXTRA_LDFLAGS]) + # Hack for macos, it sticks this in here. + PYTHON_EXTRA_LDFLAGS=`echo $PYTHON_EXTRA_LDFLAGS | sed 's/CoreFoundation.*$/CoreFoundation/'` + fi + AC_MSG_RESULT([$PYTHON_EXTRA_LDFLAGS]) + AC_SUBST(PYTHON_EXTRA_LDFLAGS) - # - # final check to see if everything compiles alright - # - AC_MSG_CHECKING([consistency of all components of python development environment]) - # save current global flags - ac_save_LIBS="$LIBS" - ac_save_LDFLAGS="$LDFLAGS" - ac_save_CPPFLAGS="$CPPFLAGS" - LIBS="$ac_save_LIBS $PYTHON_LIBS $PYTHON_EXTRA_LIBS $PYTHON_EXTRA_LIBS" - LDFLAGS="$ac_save_LDFLAGS $PYTHON_EXTRA_LDFLAGS" - CPPFLAGS="$ac_save_CPPFLAGS $PYTHON_CPPFLAGS" - AC_LANG_PUSH([C]) - AC_LINK_IFELSE([ + # + # final check to see if everything compiles alright + # + AC_MSG_CHECKING([consistency of all components of python development environment]) + # save current global flags + ac_save_LIBS="$LIBS" + ac_save_LDFLAGS="$LDFLAGS" + ac_save_CPPFLAGS="$CPPFLAGS" + LIBS="$ac_save_LIBS $PYTHON_LIBS $PYTHON_EXTRA_LIBS" + LDFLAGS="$ac_save_LDFLAGS $PYTHON_EXTRA_LDFLAGS" + CPPFLAGS="$ac_save_CPPFLAGS $PYTHON_CPPFLAGS" + AC_LANG_PUSH([C]) + AC_LINK_IFELSE([ AC_LANG_PROGRAM([[#include ]], [[Py_Initialize();]]) ],[pythonexists=yes],[pythonexists=no]) - AC_LANG_POP([C]) - # turn back to default flags - CPPFLAGS="$ac_save_CPPFLAGS" - LIBS="$ac_save_LIBS" - LDFLAGS="$ac_save_LDFLAGS" + AC_LANG_POP([C]) + # turn back to default flags + CPPFLAGS="$ac_save_CPPFLAGS" + LIBS="$ac_save_LIBS" + LDFLAGS="$ac_save_LDFLAGS" - AC_MSG_RESULT([$pythonexists]) + AC_MSG_RESULT([$pythonexists]) - if test ! "x$pythonexists" = "xyes"; then - if test "x$python_manylinux" = "xyes"; then - # Ignore linking errors for manylinux builds (no libpython.so) - pythonexists="yes" - else - AC_MSG_RESULT([ + if test ! "x$pythonexists" = "xyes"; then + AC_MSG_WARN([ Could not link test program to Python. Maybe the main Python library has been installed in some non-standard library path. If so, pass it to configure, via the LIBS environment variable. @@ -320,20 +453,15 @@ EOD` You probably have to install the development version of the Python package for your distribution. The exact name of this package varies among them. ============================================================================ - ]) - PYTHON_VERSION="" - PYTHON_CPPFLAGS="" - PYTHON_LIBS="" - PYTHON_SITE_PKG="" - PYTHON_EXTRA_LDFLAGS="" - PYTHON_EXTRA_LIBS="" - fi + ]) + if ! $ax_python_devel_optional; then + AC_MSG_ERROR([Giving up]) + fi + ax_python_devel_found=no + PYTHON_VERSION="" + fi fi - AC_SUBST([PYTHON_CPPFLAGS]) - AC_SUBST([PYTHON_LIBS]) - AC_SUBST([PYTHON_SITE_PKG]) - AC_SUBST(PYTHON_EXTRA_LDFLAGS) - AC_SUBST(PYTHON_EXTRA_LIBS) + # # all done! #