AC macro for embedding python?

Brendan Cully brendan at kublai.com
Wed Mar 5 19:03:39 CET 2003


> In spite of what seems to me to be an obvious need for it, it looks like there
> is no standard way of getting all the flags needed for embedding. So, I intend
> to write some kind of AC_PATH_PYTHON macro to set the appropriate
> PYTHON_INCLUDES and PYTHON_LIBS variables. I'm looking for a little guidance

To follow up on my own post, I've written the attached macro, which has worked in
the few environments I've been able to test it in. Others may find it useful?

I'd appreciate constructive criticism too.

-b
-------------- next part --------------
dnl local M4 configure macros
dnl Brendan Cully <brendan at xiph.org>
dnl $Id: acinclude.m4,v 1.3 2003/03/05 16:26:30 brendan Exp $

# XIPH_PATH_PYTHON(PATH)
# Search for python in PATH, or in the path if none is given.
# Defines PYTHON_CPPFLAGS and PYTHON_LIBS if found
AC_DEFUN([XIPH_PATH_PYTHON],
  [
m4_pushdef([xpp_path], [$1])

PYTHON="no"

if test "xpp_path" != "yes"
then
  AC_MSG_CHECKING([python])
  if test -x "xpp_path"
  then
    PYTHON="xpp_path"
  fi
  AC_MSG_RESULT([$PYTHON])
else
  AC_PATH_PROGS([PYTHON], [python python2 python2.3 python2.2])
fi

m4_popdef([xpp_path])

if test "$PYTHON" != "no"
then
  # The library we're linking against
  PYTHON_LIB="_XIPH_PYTHON_CFG([$PYTHON], [LIBRARY])"
  
  # if LIBRARY is nonsensical, bail out
  if test $? -ne 0 -o -z "$PYTHON_LIB"
  then
    AC_MSG_WARN([Could not find library for $PYTHON])
    break
  fi
  # make library linker friendly. This is a hack, but I don't know what's better
  PYTHON_LIB=`echo "$PYTHON_LIB" | sed 's/lib//;s/\.a.*//;s/\.so.*//;s/\.dylib.*//'`

  # LDFLAGS
  PYTHON_LDFLAGS="-L[]_XIPH_PYTHON_CFG([$PYTHON], [LIBPL])"

  # Extra libraries required by python
  PYTHON_EXTRA_LIBS="_XIPH_PYTHON_CFG([$PYTHON], [LIBS])"
  PYTHON_EXTRA_LIBS="$PYTHON_EXTRA_LIBS _XIPH_PYTHON_CFG([$PYTHON], [SYSLIBS])"
  PYTHON_EXTRA_LIBS="$PYTHON_EXTRA_LIBS _XIPH_PYTHON_CFG([$PYTHON], [SHLIBS])"

  PYTHON_CPPFLAGS="-I[]_XIPH_PYTHON_CFG([$PYTHON], [INCLUDEPY])"

  # test header and library functionality
  saved_CPPFLAGS="$CPPFLAGS"
  CPPFLAGS="$CPPFLAGS $PYTHON_CPPFLAGS"
  AC_CHECK_HEADER([Python.h],
    [
    saved_LDFLAGS="$LDFLAGS"
    LDFLAGS="$LDFLAGS $PYTHON_LDFLAGS"
    AC_CHECK_LIB([$PYTHON_LIB], [Py_Initialize],
      [PYTHON_LIBS="-l$PYTHON_LIB $PYTHON_EXTRA_LIBS"],
      [AC_MSG_WARN([Could not link to the python library])],
      [$PYTHON_EXTRA_LIBS])
    LDFLAGS="$saved_LDFLAGS"
    ],
    [AC_MSG_WARN([Python.h doesn't appear to be usable])])
  CPPFLAGS="$saved_CPPFLAGS"
fi
  ])dnl

# _XIPH_PYTHON_CFG(PYTHONPATH, CFGVAR)
# Ask python in PYTHONPATH for the definition of CFGVAR
m4_define([_XIPH_PYTHON_CFG],
  [`$1 -c 'from distutils.sysconfig import get_config_var; print get_config_var("$2")' | sed 's/None//'`])


More information about the Python-list mailing list