commit e6ed2372887ac0106db8854f70e9b2fdcadec74f Author: Dave Mielke Date: Thu Apr 15 08:07:07 2021 -0400 Python: We need to migrate from distutils to sysconfig. (dm) diff --git a/Bindings/Python/Makefile.in b/Bindings/Python/Makefile.in index 01d60d616..e03073de8 100644 --- a/Bindings/Python/Makefile.in +++ b/Bindings/Python/Makefile.in @@ -21,7 +21,6 @@ include $(SRC_TOP)bindings.mk PYTHON_OK = @PYTHON_OK@ PYTHON = @PYTHON@ -PYTHON_PROLOGUE = @PYTHON_PROLOGUE@ PYTHON_VERSION = @PYTHON_VERSION@ PYTHON_CPPFLAGS = @PYTHON_CPPFLAGS@ PYTHON_LIBS = @PYTHON_LIBS@ diff --git a/Bindings/Python/bindings.m4 b/Bindings/Python/bindings.m4 index 784418f0c..b85668991 100644 --- a/Bindings/Python/bindings.m4 +++ b/Bindings/Python/bindings.m4 @@ -31,100 +31,79 @@ PYTHON_OK=true export PYTHONCOERCECLOCALE=0 AC_PATH_PROGS([PYTHON], [python3 python python2], [python]) -if test -z "${PYTHON}" +if test -n "${PYTHON}" then - AC_MSG_WARN([Python interpreter not found]) - PYTHON_OK=false -else - PYTHON_PROLOGUE="" - for python_module in sys distutils.sysconfig - do - python_error="`"${PYTHON}" -c "import ${python_module};" 2>&1`" - if test -n "${python_error}" - then - AC_MSG_WARN([Python module not found: ${python_module}: ${python_error}]) + test -n "${PYTHON_VERSION}" || { + BRLTTY_PYTHON_QUERY([PYTHON_VERSION], [version]) + + test -n "${PYTHON_VERSION}" || { + AC_MSG_WARN([Python version not defined]) PYTHON_OK=false - else - PYTHON_PROLOGUE="${PYTHON_PROLOGUE}import ${python_module}; " - fi - done - AC_SUBST([PYTHON_PROLOGUE]) + } + } + AC_SUBST([PYTHON_VERSION]) - if "${PYTHON_OK}" - then - if test -z "${PYTHON_VERSION}" - then - [PYTHON_VERSION="`"${PYTHON}" -c "${PYTHON_PROLOGUE} print(sys.version_info[0]);"`"] - if test -z "${PYTHON_VERSION}" - then - AC_MSG_WARN([Python version not defined]) - fi - fi - AC_SUBST([PYTHON_VERSION]) + test -n "${PYTHON_CPPFLAGS}" || { + BRLTTY_PYTHON_QUERY([python_include_directory], [incdir]) - if test -z "${PYTHON_CPPFLAGS}" + if test -n "${python_include_directory}" then - [python_include_directory="`"${PYTHON}" -c "${PYTHON_PROLOGUE} print(distutils.sysconfig.get_python_inc());"`"] - if test -z "${python_include_directory}" - then - AC_MSG_WARN([Python include directory not found]) + PYTHON_CPPFLAGS="-I${python_include_directory}" + + test -f "${python_include_directory}/Python.h" || { + AC_MSG_WARN([Python developer environment not installed]) PYTHON_OK=false - else - PYTHON_CPPFLAGS="-I${python_include_directory}" - - if test ! -f "${python_include_directory}/Python.h" - then - AC_MSG_WARN([Python developer environment not installed]) - PYTHON_OK=false - fi - fi + } + else + AC_MSG_WARN([Python include directory not found]) + PYTHON_OK=false fi - AC_SUBST([PYTHON_CPPFLAGS]) + } + AC_SUBST([PYTHON_CPPFLAGS]) - if test -z "${PYTHON_LIBS}" - then - PYTHON_LIBS="-lpython${PYTHON_VERSION}" - - [python_library_directory="`"${PYTHON}" -c "${PYTHON_PROLOGUE} print(distutils.sysconfig.get_python_lib(0,1));"`"] - if test -z "${python_library_directory}" - then - AC_MSG_WARN([Python library directory not found]) - else - PYTHON_LIBS="-L${python_library_directory}/config ${PYTHON_LIBS}" - fi - fi - AC_SUBST([PYTHON_LIBS]) + test -n "${PYTHON_LIBS}" || { + PYTHON_LIBS="-lpython${PYTHON_VERSION}" + BRLTTY_PYTHON_QUERY([python_library_directory], [libdir]) - if test -z "${PYTHON_EXTRA_LIBS}" + if test -n "${python_library_directory}" then - [PYTHON_EXTRA_LIBS="`"${PYTHON}" -c "${PYTHON_PROLOGUE} print(distutils.sysconfig.get_config_var('LOCALMODLIBS'), distutils.sysconfig.get_config_var('LIBS'));"`"] + PYTHON_LIBS="-L${python_library_directory}/config ${PYTHON_LIBS}" + else + AC_MSG_WARN([Python library directory not found]) + PYTHON_OK=false fi - AC_SUBST([PYTHON_EXTRA_LIBS]) + } + AC_SUBST([PYTHON_LIBS]) - if test -z "${PYTHON_EXTRA_LDFLAGS}" - then - [PYTHON_EXTRA_LDFLAGS="`"${PYTHON}" -c "${PYTHON_PROLOGUE} print(distutils.sysconfig.get_config_var('LINKFORSHARED'));"`"] - fi - AC_SUBST([PYTHON_EXTRA_LDFLAGS]) + test -n "${PYTHON_EXTRA_LIBS}" || { + BRLTTY_PYTHON_QUERY([PYTHON_EXTRA_LIBS], [libopts]) + } + AC_SUBST([PYTHON_EXTRA_LIBS]) - if test -z "${PYTHON_SITE_PKG}" - then - [PYTHON_SITE_PKG="`"${PYTHON}" -c "${PYTHON_PROLOGUE} print(distutils.sysconfig.get_python_lib(1,0));"`"] - if test -z "${PYTHON_SITE_PKG}" - then - AC_MSG_WARN([Python package directory not found]) - fi - fi - AC_SUBST([PYTHON_SITE_PKG]) - fi + test -n "${PYTHON_EXTRA_LDFLAGS}" || { + BRLTTY_PYTHON_QUERY([PYTHON_EXTRA_LDFLAGS], [linkopts]) + } + AC_SUBST([PYTHON_EXTRA_LDFLAGS]) + + test -n "${PYTHON_SITE_PKG}" || { + BRLTTY_PYTHON_QUERY([PYTHON_SITE_PKG], [pkgdir]) + + test -n "${PYTHON_SITE_PKG}" || { + AC_MSG_WARN([Python packages directory not found]) + PYTHON_OK=false + } + } + AC_SUBST([PYTHON_SITE_PKG]) +else + AC_MSG_WARN([Python interpreter not found]) + PYTHON_OK=false fi -AC_PATH_PROGS([CYTHON], [cython cython3]) -if test -z "${CYTHON}" -then +AC_PATH_PROGS([CYTHON], [cython3 cython]) +test -n "${CYTHON}" || { AC_MSG_WARN([Cython compiler not found]) PYTHON_OK=false -fi +} if test "${GCC}" = "yes" then @@ -141,3 +120,7 @@ AC_SUBST([CYTHON_CFLAGS]) AC_SUBST([PYTHON_OK]) ]) + +AC_DEFUN([BRLTTY_PYTHON_QUERY], [dnl + $1="`${PYTHON} Tools/pythoncmd $2`" +]) diff --git a/Tools/pythoncmd b/Tools/pythoncmd new file mode 100755 index 000000000..14994dd6e --- /dev/null +++ b/Tools/pythoncmd @@ -0,0 +1,107 @@ +#!/usr/bin/env python +############################################################################### +# libbrlapi - A library providing access to braille terminals for applications. +# +# Copyright (C) 2005-2021 by +# Alexis Robert +# Samuel Thibault +# +# libbrlapi comes with ABSOLUTELY NO WARRANTY. +# +# This is free software, placed under the terms of the +# GNU Lesser General Public License, as published by the Free Software +# Foundation; either version 2.1 of the License, or (at your option) any +# later version. Please see the file LICENSE-LGPL for details. +# +# Web Page: http://brltty.app/ +# +# This software is maintained by Dave Mielke . +############################################################################### + +import sys +import os + +def nextProgramArgument(): + return sys.argv.pop(0) + +def putProgramMessage(message): + print("%s: %s" % (programName, message)) + +def syntaxError(message): + putProgramMessage(message) + sys.exit(2) + +def verifyNoMoreProgramArguments(): + if (len(sys.argv) > 0): + syntaxError("too many parameters") + +try: + import sysconfig +# print("sysconfig") + + def getIncludeDirectory(): + return sysconfig.get_path("include") + + def getLibraryDirectory(): + return sysconfig.get_path("stdlib") + + def getPackageDirectory(): + return sysconfig.get_path("platlib") + + def getConfigurationVariable(name): + return sysconfig.get_config_var(name) +except ModuleNotFoundError: + import distutils.sysconfig +# print("distutils") + + def getIncludeDirectory(): + return distutils.sysconfig.get_python_inc() + + def getLibraryDirectory(): + return distutils.sysconfig.get_python_lib(0, 1) + + def getPackageDirectory(): + return distutils.sysconfig.get_python_lib(True, False) + + def getConfigurationVariable(name): + return distutils.sysconfig.get_config_var(name) + +programPath = nextProgramArgument() +programName = os.path.basename(programPath) + +class ProgramActions: + def version(): + verifyNoMoreProgramArguments() + print(sys.version_info[0]) + + def incdir(): + verifyNoMoreProgramArguments() + print(getIncludeDirectory()) + + def libdir(): + verifyNoMoreProgramArguments() + print(getLibraryDirectory()) + + def pkgdir(): + verifyNoMoreProgramArguments() + print(getPackageDirectory()) + + def libopts(): + verifyNoMoreProgramArguments() + print(getConfigurationVariable("LIBS")) + + def linkopts(): + verifyNoMoreProgramArguments() + print(getConfigurationVariable("LINKFORSHARED")) + +try: + programAction = nextProgramArgument() + + try: + getattr(globals()["ProgramActions"], programAction)() + except AttributeError: + syntaxError("unknown action: %s" % programAction) +except IndexError: + syntaxError("action not specified") + +sys.exit(0)