1 dnl @synopsis AZ_PYTHON_DEFAULT
2 dnl @synopsis AZ_PYTHON_ENABLE
3 dnl @synopsis AZ_PYTHON_WITH
4 dnl @synopsis AZ_PYTHON_PATH
5 dnl @synopsis AZ_PYTHON_VERSION_ENSURE( [2.2] )
6 dnl @synopsis AZ_PYTHON_CSPEC
7 dnl @synopsis AZ_PYTHON_LSPEC
9 dnl @summary New and revised Python support.
11 dnl This file provides autoconf support for those applications that
12 dnl want to embed python. It supports all pythons >= 2.2 which is the
13 dnl first official release containing distutils. Version 2.2 of python
14 dnl was released December 21, 2001. Since it actually executes the
15 dnl python, cross platform configuration will probably not work. Also,
16 dnl most of the platforms supported are consistent until you look into
17 dnl MacOSX. The python included with it is installed as a framework
18 dnl which is a very different environment to set up the normal tools
19 dnl such as gcc and libtool to deal with. Therefore, once we establish
20 dnl which python that we are going to use, we use its distutils to
21 dnl actually compile and link our modules or applications.
23 dnl At this time, it does NOT support linking with Python statically.
24 dnl It does support dynamic linking.
26 dnl This set of macros help define $PYTHON, $PYTHON_USE, $PYTHON_CSPEC
27 dnl and $PYTHON_LSPEC. $PYTHON defines the full executable path for the
28 dnl Python being linked to and is used within these macros to determine
29 dnl if that has been specified or found. These macros do execute this
30 dnl python version so it must be present on the system at configure
33 dnl $PYTHON_USE is an automake variable that defines whether Python
34 dnl support should be included or not in your application.
35 dnl $PYTHON_CSPEC is a variable that supplies additional CFLAGS for the
36 dnl compilation of the application/shared library. $PYTHON_LSPEC is a
37 dnl variable that supplies additional LDFLAGS for linking the
38 dnl application/shared library.
40 dnl The following is an example of how to set up for python usage
41 dnl within your application in your configure.in:
43 dnl AZ_PYTHON_DEFAULT( )
44 dnl AZ_PYTHON_ENABLE( ) # Optional
45 dnl AZ_PYTHON_WITH( ) # Optional
46 dnl AZ_PYTHON_PATH( ) # or AZ_PYTHON_INSIST( )
47 dnl # if $PYTHON is not defined, then the following do nothing.
48 dnl AZ_PYTHON_VERSION_ENSURE( [2.2] )
52 dnl The AZ_PYTHON_DEFAULT sets the $PYTHON_USE to false. Thereby,
53 dnl excluding it if it was optional.
55 dnl The AZ_PYTHON_ENABLE looks for the optional configure parameters of
56 dnl --enable-python/--disable-python and establishes the $PYTHON and
57 dnl $PYTHON_USE variables accordingly.
59 dnl The AZ_PYTHON_WITH looks for the optional configure parameters of
60 dnl --with-python/--without-python and establishes the $PYTHON and
61 dnl $PYTHON_USE variables accordingly.
63 dnl The AZ_PYTHON_PATH looks for python assuming that none has been
64 dnl previously found or defined and issues an error if it does not find
65 dnl it. If it does find it, it establishes the $PYTHON and $PYTHON_USE
66 dnl variables accordingly. AZ_PYTHON_INSIST could be used here instead
67 dnl if you want to insist that Python support be included using the
68 dnl --enable-python or --with-python checks previously done.
70 dnl The AZ_PYTHON_VERSION_ENSURE issues an error if the Python
71 dnl previously found is not of version 2.2 or greater.
73 dnl Once that these macros have be run, we can use PYTHON_USE within
74 dnl the makefile.am file to conditionally add the Python support such
77 dnl Makefile.am example showing optional inclusion of directories:
87 dnl SUBDIRS = . $(plugins) $(src)
89 dnl Makefile.am example showing optional shared library build:
92 dnl lib_LTLIBRARIES = libElemList.la
93 dnl libElemList_la_SOURCES = libElemList.c
94 dnl libElemList_la_CFLAGS = @PYTHON_CSPEC@
95 dnl libElemList_la_LDFLAGS = @PYTHON_LSPEC@
98 dnl Makefile.am example showing optional program build:
101 dnl bin_PROGRAMS = runFunc
102 dnl runFunc_SOURCES = runFunc.c
103 dnl runFunc_CFLAGS = @PYTHON_CSPEC@
104 dnl runFunc_LDFLAGS = @PYTHON_LSPEC@
107 dnl The above compiles the modules only if PYTHON_USE was specified as
108 dnl true. Also, the else portion of the if was optional.
110 dnl @category InstalledPackages
111 dnl @author Robert White <kranki@mac.com>
112 dnl @author Dustin Mitchell <dustin@cs.uchicago.edu>
113 dnl @version 2005-01-14
114 dnl @license GPLWithACException
116 # AZ_PYTHON_DEFAULT( )
118 # Sets the default to not include Python support.
120 AC_DEFUN([AZ_PYTHON_DEFAULT],
123 AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")
128 # AZ_PYTHON_ENABLE( [path] )
129 # -----------------------------------------------------------------
130 # Handles the various --enable-python commands.
132 # $1 is the optional search path for the python executable if needed
134 # PYTHON_USE (AM_CONDITIONAL) is true if python executable found
135 # and --enable-python was requested; otherwise false.
136 # $PYTHON contains the full executable path to python if PYTHON_ENABLE_USE
140 # AZ_PYTHON_ENABLE( )
142 # AZ_PYTHON_ENABLE( "/usr/bin" )
144 AC_DEFUN([AZ_PYTHON_ENABLE],
146 AC_ARG_VAR([PYTHON],[Python Executable Path])
148 # unless PYTHON was supplied to us (as a precious variable),
149 # see if --enable-python[=PythonExecutablePath], --enable-python,
150 # --disable-python or --enable-python=no was given.
153 AC_MSG_CHECKING(for --enable-python)
156 AC_HELP_STRING([--enable-python@<:@=PYTHON@:>@],
157 [absolute path name of Python executable]
160 if test "$enableval" = "yes"
162 # "yes" was specified, but we don't have a path
163 # for the executable.
164 # So, let's searth the PATH Environment Variable.
174 AC_MSG_ERROR(no path to python found)
177 AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")
179 elif test "$enableval" = "no"
183 AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")
185 # $enableval must be the executable path then.
186 AC_SUBST([PYTHON], ["${enableval}"])
187 AC_MSG_RESULT($withval)
189 AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")
194 # --with-python was not specified.
197 AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")
208 # Set up the c compiler options to compile Python
209 # embedded programs/libraries in $PYTHON_CSPEC if
210 # $PYTHON has been defined.
212 AC_DEFUN([AZ_PYTHON_CSPEC],
214 AC_ARG_VAR( [PYTHON], [Python Executable Path] )
217 az_python_prefix=`${PYTHON} -c "import sys; print sys.prefix"`
218 if test -z "$az_python_prefix"
220 AC_MSG_ERROR([Python Prefix is not known])
222 az_python_execprefix=`${PYTHON} -c "import sys; print sys.exec_prefix"`
223 az_python_version=`$PYTHON -c "import sys; print sys.version[[:3]]"`
224 az_python_includespec="-I${az_python_prefix}/include/python${az_python_version}"
225 if test x"$python_prefix" != x"$python_execprefix"; then
226 az_python_execspec="-I${az_python_execprefix}/include/python${az_python_version}"
227 az_python_includespec="${az_python_includespec} $az_python_execspec"
229 az_python_ccshared=`${PYTHON} -c "import distutils.sysconfig; print distutils.sysconfig.get_config_var('CFLAGSFORSHARED')"`
230 az_python_cspec="${az_python_ccshared} ${az_python_includespec}"
231 AC_SUBST([PYTHON_CSPEC], [${az_python_cspec}])
232 AC_MSG_NOTICE([PYTHON_CSPEC=${az_python_cspec}])
238 # AZ_PYTHON_INSIST( )
240 # Look for Python and set the output variable 'PYTHON'
241 # to 'python' if found, empty otherwise.
243 AC_DEFUN([AZ_PYTHON_PATH],
245 AC_ARG_VAR( [PYTHON], [Python Executable Path] )
248 AC_MSG_ERROR([Python Executable not found])
256 # Set up the linker options to link Python embedded
257 # programs/libraries in $PYTHON_LSPEC if $PYTHON
260 AC_DEFUN([AZ_PYTHON_LSPEC],
262 AC_ARG_VAR( [PYTHON], [Python Executable Path] )
267 import distutils.sysconfig
268 strUseFrameWork = "--enable-framework"
269 dictConfig = distutils.sysconfig.get_config_vars( )
270 strConfigArgs = dictConfig.get("CONFIG_ARGS")
271 strLinkSpec = dictConfig.get('LDFLAGS')
272 if -1 == strConfigArgs.find(strUseFrameWork):
273 strLibPL = dictConfig.get("LIBPL")
274 if strLibPL and (strLibPL != ""):
275 strLinkSpec += " -L%s" % (strLibPL)
276 strSys = dictConfig.get("SYSLIBS")
277 if strSys and (strSys != ""):
278 strLinkSpec += " %s" % (strSys)
279 strSHL = dictConfig.get("SHLIBS")
280 if strSHL and (strSHL != ""):
281 strLinkSpec += " %s" % (strSHL)
282 # Construct the Python Library Name.
283 strTmplte = " -lpython%d.%d"
284 if (sys.platform == "win32") or (sys.platform == "os2emx"):
285 strTmplte = " -lpython%d%d"
286 strWrk = strTmplte % ( (sys.hexversion >> 24),
287 ((sys.hexversion >> 16) & 0xff))
288 strLinkSpec += strWrk
290 # This is not ideal since it changes the search path
291 # for Frameworks which could have side-effects on
292 # other included Frameworks. However, it is necessary
293 # where someone has installed more than one frameworked
294 # Python. Frameworks are really only used in MacOSX.
295 strLibFW = dictConfig.get("PYTHONFRAMEWORKPREFIX")
296 if strLibFW and (strLibFW != ""):
297 strLinkSpec += " -F%s" % (strLibFW)
298 strLibPL = dictConfig.get("LIBPL")
299 if strLibPL and (strLibPL != ""):
300 strLinkSpec += " -L%s" % (strLibPL)
301 # Construct the Python Library Name.
302 strTmplte = " -lpython%d.%d"
303 if (sys.platform == "win32") or (sys.platform == "os2emx"):
304 strTmplte = " -lpython%d%d"
305 strWrk = strTmplte % ( (sys.hexversion >> 24),
306 ((sys.hexversion >> 16) & 0xff))
307 strLinkSpec += strWrk
308 strLinkSpec += " %s" % (dictConfig.get('LINKFORSHARED'))
311 AC_SUBST([PYTHON_LSPEC], [${az_python_output}])
312 AC_MSG_NOTICE([PYTHON_LSPEC=${az_python_output}])
320 # Look for Python and set the output variable 'PYTHON'
321 # to 'python' if found, empty otherwise.
323 AC_DEFUN([AZ_PYTHON_PATH],
325 AC_ARG_VAR( [PYTHON], [Python Executable Path] )
326 AC_PATH_PROG( PYTHON, python, [], $1 )
329 AC_MSG_ERROR([Python Executable not found])
333 AM_CONDITIONAL(PYTHON_USE, test "$az_python_use" = "true")
338 # AZ_PYTHON_PREFIX( )
339 # -------------------
340 # Use the values of $prefix and $exec_prefix for the corresponding
341 # values of PYTHON_PREFIX and PYTHON_EXEC_PREFIX.
343 AC_DEFUN([AZ_PYTHON_PREFIX],
347 AC_MSG_ERROR([Python Executable Path is not known])
349 ax_python_prefix=`${PYTHON} -c "import sys; print sys.prefix"`
350 ax_python_execprefix=`${PYTHON} -c "import sys; print sys.exec_prefix"`
351 AC_SUBST([PYTHON_PREFIX], ["${ax_python_prefix}"])
352 AC_SUBST([PYTHON_EXECPREFIX], ["${ax_python_execprefix}"])
357 # AZ_PYTHON_RUN( PYTHON_PROGRAM )
359 # Run a Python Test Program saving its output
360 # in az_python_output and its condition code
363 AC_DEFUN([AZ_PYTHON_RUN],
365 AC_ARG_VAR( [PYTHON], [Python Executable Path] )
368 AC_MSG_ERROR([Python Executable not found])
370 cat >conftest.py <<_ACEOF
373 az_python_output=`$PYTHON conftest.py`
376 if test -f "conftest.pyc"
385 # AZ_PYTHON_VERSION_CHECK( VERSION, [ACTION-IF-TRUE], [ACTION-IF-FALSE] )
386 # -----------------------------------------------------------------------------
387 # Run ACTION-IF-TRUE if the Python interpreter has version >= VERSION.
388 # Run ACTION-IF-FALSE otherwise.
389 # This test uses sys.hexversion instead of the string equivalant (first
390 # word of sys.version), in order to cope with versions such as 2.2c1.
391 # hexversion has been introduced in Python 1.5.2; it's probably not
392 # worth to support older versions (1.5.1 was released on October 31, 1998).
394 AC_DEFUN([AZ_PYTHON_VERSION_CHECK],
396 AC_ARG_VAR( [PYTHON], [Python Executable Path] )
399 AC_MSG_CHECKING([whether $PYTHON version >= $1])
402 # split strings by '.' and convert to numeric. Append some zeros
403 # because we need at least 4 digits for the hex conversion.
404 minver = map(int, string.split('$1', '.')) + [[0, 0, 0]]
406 for i in xrange(0, 4): minverhex = (minverhex << 8) + minver[[i]]
407 if sys.hexversion >= minverhex:
412 if test $az_python_cc -eq 0
425 # AZ_PYTHON_VERSION_ENSURE( VERSION )
427 # Insure that the Python Interpreter Version
428 # is greater than or equal to the VERSION
431 AC_DEFUN([AZ_PYTHON_VERSION_ENSURE],
433 AZ_PYTHON_VERSION_CHECK(
435 [AC_MSG_RESULT(yes)],
436 [AC_MSG_ERROR(too old)]
442 # AZ_PYTHON_WITH( [path] )
443 # -----------------------------------------------------------------
444 # Handles the various --with-python commands.
446 # $1 is the optional search path for the python executable if needed
448 # PYTHON_USE (AM_CONDITIONAL) is true if python executable found
449 # and --with-python was requested; otherwise false.
450 # $PYTHON contains the full executable path to python if PYTHON_USE
456 # AZ_PYTHON_WITH("/usr/bin")
458 AC_DEFUN([AZ_PYTHON_WITH],
460 AC_ARG_VAR([PYTHON],[Python Executable Path])
462 # unless PYTHON was supplied to us (as a precious variable),
463 # see if --with-python[=PythonExecutablePath], --with-python,
464 # --without-python or --with-python=no was given.
467 AC_MSG_CHECKING(for --with-python)
470 AC_HELP_STRING([--with-python@<:@=PYTHON@:>@],
471 [absolute path name of Python executable]
474 if test "$withval" = "yes"
476 # "yes" was specified, but we don't have a path
477 # for the executable.
478 # So, let's searth the PATH Environment Variable.
488 AC_MSG_ERROR(no path to python found)
491 AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")
493 elif test "$withval" = "no"
497 AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")
499 # $withval must be the executable path then.
500 AC_SUBST([PYTHON], ["${withval}"])
501 AC_MSG_RESULT($withval)
503 AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")
508 # --with-python was not specified.
511 AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")