m4/python.m4: Using distutils.sysconfig module at configure time

Julien ÉLIE julien at trigofacile.com
Sat Jun 6 13:50:03 UTC 2015


Hi all,

For INN 2.6.0, I suggest to improve the detection of the compiler
and linker flags to use to embed Python.  As a matter of fact,
using the distutils.sysconfig module fixes a few issues.
It implies that configuring INN will then require Python 2.2.0
as that module was introduced in Python 2.2.0, released
in December 2001!  So I bet it will not be a problem
to use that module, now that we are in 2015...

For the record, Python 1.5.2 is currently enough to build INN.


The problem I encounter when building INN on Debian Jessie is that
the /usr/lib/python2.7/config directory does not exist.
It is /usr/lib/python2.7/config-x86_64-linux-gnu on my system, so the
build fails because "config" is hard-coded in the current way the
detection is coded.


With the patch below, the flags are correctly computed and are
look like:

PYTHON_CPPFLAGS = -I/usr/include/python2.7
PYTHON_LIBS     = -L/usr/lib/python2.7/config-x86_64-linux-gnu -lpython2.7 -lpthread -ldl -lutil -lm -L/usr/lib -lz -Xlinker -export-dynamic -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-z,relro



If someone has a reason for not going ahead with distutils.sysconfig
for INN 2.6.0, please tell.





--- m4/python.m4	(révision 9891)
+++ m4/python.m4	(copie de travail)
@@ -4,44 +4,40 @@
 dnl Defines INN_ARG_PYTHON, which sets up the --with-python command line
 dnl argument and also sets various flags needed for embedded Python if it is
 dnl requested.
+dnl
+dnl We use the distutils.sysconfig module shipped with Python 2.2 and later
+dnl to find the compiler and linker flags to use to embed Python.
 
 AC_DEFUN([INN_ARG_PYTHON],
 [AC_ARG_VAR([PYTHON], [Location of Python interpreter])
-AC_ARG_WITH([python],
+ AC_ARG_WITH([python],
     [AS_HELP_STRING([--with-python], [Embedded Python module support [no]])],
-    [case $withval in
-    yes) DO_PYTHON=DO
-         AC_DEFINE(DO_PYTHON, 1,
-             [Define to compile in Python module support.])
-         ;;
-    no)  DO_PYTHON=DONT ;;
-    *)   AC_MSG_ERROR([invalid argument to --with-python]) ;;
-    esac],
-    DO_PYTHON=DONT)
-
-dnl A better way of doing this rather than grepping through the Makefile would
-dnl be to use distutils.sysconfig, but this module isn't available in older
-dnl versions of Python.
-if test x"$DO_PYTHON" = xDO ; then
-    INN_PATH_PROG_ENSURE([PYTHON], [python])
-    AC_MSG_CHECKING([for Python linkage])
-    py_prefix=`$PYTHON -c 'import sys; print (sys.prefix)'`
-    py_ver=`$PYTHON -c 'import sys; print (sys.version[[:3]])'`
-    py_libdir="$py_prefix/lib/python$py_ver"
-    PYTHON_CPPFLAGS="-I$py_prefix/include/python$py_ver"
-    py_linkage=""
-    for py_linkpart in LIBS LIBC LIBM LOCALMODLIBS BASEMODLIBS \
-                       LINKFORSHARED LDFLAGS ; do
-        py_linkage="$py_linkage "`grep "^${py_linkpart}=" \
-                                       $py_libdir/config/Makefile \
-                                  | sed -e 's/^.*=//'`
-    done
-    PYTHON_LIBS="-L$py_libdir/config -lpython$py_ver $py_linkage"
-    PYTHON_LIBS=`echo $PYTHON_LIBS | sed -e 's/[ \\t]*/ /g'`
-    AC_MSG_RESULT([$py_libdir])
-else
-    PYTHON_CPPFLAGS=
-    PYTHON_LIBS=
-fi
-AC_SUBST([PYTHON_CPPFLAGS])
-AC_SUBST([PYTHON_LIBS])])
+    [AS_CASE([$withval],
+     [yes], [DO_PYTHON=DO
+             AC_DEFINE([DO_PYTHON], [1],
+                [Define to compile in Python module support.])],
+     [no], [DO_PYTHON=DONT],
+     [AC_MSG_ERROR([invalid argument to --with-python])])],
+    [DO_PYTHON=DONT])
+ AS_IF([test x"$DO_PYTHON" = xDO],
+    [INN_PATH_PROG_ENSURE([PYTHON], [python])
+     AC_MSG_CHECKING([for Python linkage])
+     py_include=`$PYTHON -c 'import distutils.sysconfig; \
+         print(distutils.sysconfig.get_python_inc())'`
+     PYTHON_CPPFLAGS="-I$py_include"
+     py_ver=`$PYTHON -c 'import sys; print(sys.version[[:3]])'`
+     py_libdir=`$PYTHON -c 'import distutils.sysconfig; \
+         print(distutils.sysconfig.get_python_lib(False, True))'`
+     py_linkage=`$PYTHON -c 'import distutils.sysconfig; \
+         print(" ".join(distutils.sysconfig.get_config_vars("LIBS", \
+             "LIBC", "LIBM", "LOCALMODLIBS", "BASEMODLIBS", \
+             "LINKFORSHARED", "LDFLAGS")))'`
+     py_configdir=`$PYTHON -c 'import distutils.sysconfig; \
+         print(distutils.sysconfig.get_config_var("LIBPL"))'`
+     PYTHON_LIBS="-L$py_configdir -lpython$py_ver $py_linkage"
+     PYTHON_LIBS=`echo $PYTHON_LIBS | sed -e 's/[ \\t]*/ /g'`
+     AC_MSG_RESULT([$py_libdir])],
+    [PYTHON_CPPFLAGS=
+     PYTHON_LIBS=])
+ AC_SUBST([PYTHON_CPPFLAGS])
+ AC_SUBST([PYTHON_LIBS])])



-- 
Julien ÉLIE

« Ce n'est pas en tournant le dos aux choses qu'on leur fait face. »
  (Pierre Dac)


More information about the inn-workers mailing list