1 ##### http://autoconf-archive.cryp.to/az_python.html
9 # AZ_PYTHON_VERSION_ENSURE( [2.2] )
15 # This file provides autoconf support for those applications that
16 # want to embed python. It supports all pythons >= 2.2 which is the
17 # first official release containing distutils. Version 2.2 of python
18 # was released December 21, 2001. Since it actually executes the
19 # python, cross platform configuration will probably not work. Also,
20 # most of the platforms supported are consistent until you look into
21 # MacOSX. The python included with it is installed as a framework
22 # which is a very different environment to set up the normal tools
23 # such as gcc and libtool to deal with. Therefore, once we establish
24 # which python that we are going to use, we use its distutils to
25 # actually compile and link our modules or applications.
27 # At this time, it does NOT support linking with Python statically.
28 # It does support dynamic linking.
30 # This set of macros help define $PYTHON, $ENABLE_PYTHON, $PYTHON_CSPEC
31 # and $PYTHON_LSPEC. $PYTHON defines the full executable path for the
32 # Python being linked to and is used within these macros to determine
33 # if that has been specified or found. These macros do execute this
34 # python version so it must be present on the system at configure
37 # $ENABLE_PYTHON is an automake variable that defines whether Python
38 # support should be included or not in your application.
39 # $PYTHON_CSPEC is a variable that supplies additional CFLAGS for the
40 # compilation of the application/shared library. $PYTHON_LSPEC is a
41 # variable that supplies additional LDFLAGS for linking the
42 # application/shared library.
44 # The following is an example of how to set up for python usage
45 # within your application in your configure.in:
47 # AZ_PYTHON_DEFAULT( )
48 # AZ_PYTHON_ENABLE( ) # Optional
49 # AZ_PYTHON_WITH( ) # Optional
50 # AZ_PYTHON_PATH( ) # or AZ_PYTHON_INSIST( )
51 # # if $PYTHON is not defined, then the following do nothing.
52 # AZ_PYTHON_VERSION_ENSURE( [2.2] )
56 # The AZ_PYTHON_DEFAULT sets the $ENABLE_PYTHON to false. Thereby,
57 # excluding it if it was optional.
59 # The AZ_PYTHON_ENABLE looks for the optional configure parameters of
60 # --enable-python/--disable-python and establishes the $PYTHON and
61 # $ENABLE_PYTHON variables accordingly.
63 # The AZ_PYTHON_WITH looks for the optional configure parameters of
64 # --with-python/--without-python and establishes the $PYTHON and
65 # $ENABLE_PYTHON variables accordingly.
67 # The AZ_PYTHON_PATH looks for python assuming that none has been
68 # previously found or defined and issues an error if it does not find
69 # it. If it does find it, it establishes the $PYTHON and $ENABLE_PYTHON
70 # variables accordingly. AZ_PYTHON_INSIST could be used here instead
71 # if you want to insist that Python support be included using the
72 # --enable-python or --with-python checks previously done.
74 # The AZ_PYTHON_VERSION_ENSURE issues an error if the Python
75 # previously found is not of version 2.2 or greater.
77 # Once that these macros have be run, we can use ENABLE_PYTHON within
78 # the makefile.am file to conditionally add the Python support such
81 # Makefile.am example showing optional inclusion of directories:
91 # SUBDIRS = . $(plugins) $(src)
93 # Makefile.am example showing optional shared library build:
96 # lib_LTLIBRARIES = libElemList.la
97 # libElemList_la_SOURCES = libElemList.c
98 # libElemList_la_CFLAGS = @PYTHON_CSPEC@
99 # libElemList_la_LDFLAGS = @PYTHON_LSPEC@
102 # Makefile.am example showing optional program build:
105 # bin_PROGRAMS = runFunc
106 # runFunc_SOURCES = runFunc.c
107 # runFunc_CFLAGS = @PYTHON_CSPEC@
108 # runFunc_LDFLAGS = @PYTHON_LSPEC@
111 # The above compiles the modules only if ENABLE_PYTHON was specified as
112 # true. Also, the else portion of the if was optional.
120 # Copyright (c) 2007 Robert White <kranki@mac.com>
121 # Copyright (c) 2007 Dustin J. Mitchell <dustin@cs.uchicago.edu>
123 # Copying and distribution of this file, with or without
124 # modification, are permitted in any medium without royalty provided
125 # the copyright notice and this notice are preserved.
127 # AZ_PYTHON_DEFAULT( )
129 # Sets the default to not include Python support.
131 AC_DEFUN([AZ_PYTHON_DEFAULT],
134 AM_CONDITIONAL(ENABLE_PYTHON, test x"$az_python_use" = x"true")
139 # AZ_PYTHON_ENABLE( [path] )
140 # -----------------------------------------------------------------
141 # Handles the various --enable-python commands.
143 # $1 is the optional search path for the python executable if needed
145 # ENABLE_PYTHON (AM_CONDITIONAL) is true if python executable found
146 # and --enable-python was requested; otherwise false.
147 # $PYTHON contains the full executable path to python if PYTHON_ENABLE_USE
151 # AZ_PYTHON_ENABLE( )
153 # AZ_PYTHON_ENABLE( "/usr/bin" )
155 AC_DEFUN([AZ_PYTHON_ENABLE],
157 AC_ARG_VAR([PYTHON],[Python Executable Path])
159 # unless PYTHON was supplied to us (as a precious variable),
160 # see if --enable-python[=PythonExecutablePath], --enable-python,
161 # --disable-python or --enable-python=no was given.
164 AC_MSG_CHECKING(for --enable-python)
167 AC_HELP_STRING([--enable-python@<:@=PYTHON@:>@],
168 [absolute path name of Python executable]
171 if test "$enableval" = "yes"
173 # "yes" was specified, but we don't have a path
174 # for the executable.
175 # So, let's searth the PATH Environment Variable.
185 AC_MSG_ERROR(no path to python found)
188 AM_CONDITIONAL(ENABLE_PYTHON, test x"$az_python_use" = x"true")
190 elif test "$enableval" = "no"
194 AM_CONDITIONAL(ENABLE_PYTHON, test x"$az_python_use" = x"true")
196 # $enableval must be the executable path then.
197 AC_SUBST([PYTHON], ["${enableval}"])
198 AC_MSG_RESULT($withval)
200 AM_CONDITIONAL(ENABLE_PYTHON, test x"$az_python_use" = x"true")
205 # --with-python was not specified.
208 AM_CONDITIONAL(ENABLE_PYTHON, test x"$az_python_use" = x"true")
218 # Set up the c compiler options to compile Python
219 # embedded programs/libraries in $PYTHON_CSPEC if
220 # $PYTHON has been defined.
222 AC_DEFUN([AZ_PYTHON_CSPEC],
224 AC_ARG_VAR( [PYTHON], [Python Executable Path] )
227 az_python_prefix=`${PYTHON} -c "import sys; print sys.prefix"`
228 if test -z "$az_python_prefix"
230 AC_MSG_ERROR([Python Prefix is not known])
232 az_python_execprefix=`${PYTHON} -c "import sys; print sys.exec_prefix"`
233 az_python_version=`$PYTHON -c "import sys; print sys.version[[:3]]"`
234 az_python_includespec="-I${az_python_prefix}/include/python${az_python_version}"
235 if test x"$python_prefix" != x"$python_execprefix"; then
236 az_python_execspec="-I${az_python_execprefix}/include/python${az_python_version}"
237 az_python_includespec="${az_python_includespec} $az_python_execspec"
239 az_python_ccshared=`${PYTHON} -c "import distutils.sysconfig; print distutils.sysconfig.get_config_var('CFLAGSFORSHARED')"`
240 az_python_cspec="${az_python_ccshared} ${az_python_includespec}"
241 AC_SUBST([PYTHON_CSPEC], [${az_python_cspec}])
242 AC_MSG_NOTICE([PYTHON_CSPEC=${az_python_cspec}])
243 AC_DEFINE(ENABLE_PYTHON, 1, [enabled python pass])
249 # AZ_PYTHON_INSIST( )
251 # Look for Python and set the output variable 'PYTHON'
252 # to 'python' if found, empty otherwise.
254 AC_DEFUN([AZ_PYTHON_PATH],
256 AC_ARG_VAR( [PYTHON], [Python Executable Path] )
259 AC_MSG_ERROR([Python Executable not found])
267 # Set up the linker options to link Python embedded
268 # programs/libraries in $PYTHON_LSPEC if $PYTHON
271 AC_DEFUN([AZ_PYTHON_LSPEC],
273 AC_ARG_VAR( [PYTHON], [Python Executable Path] )
278 import distutils.sysconfig
279 strUseFrameWork = "--enable-framework"
280 dictConfig = distutils.sysconfig.get_config_vars( )
281 strConfigArgs = dictConfig.get("CONFIG_ARGS")
282 strLinkSpec = dictConfig.get('LDFLAGS')
283 if -1 == strConfigArgs.find(strUseFrameWork):
284 strLibPL = dictConfig.get("LIBPL")
285 if strLibPL and (strLibPL != ""):
286 strLinkSpec += " -L%s" % (strLibPL)
287 strSys = dictConfig.get("SYSLIBS")
288 if strSys and (strSys != ""):
289 strLinkSpec += " %s" % (strSys)
290 strSHL = dictConfig.get("SHLIBS")
291 if strSHL and (strSHL != ""):
292 strLinkSpec += " %s" % (strSHL)
293 # Construct the Python Library Name.
294 strTmplte = " -lpython%d.%d"
295 if (sys.platform == "win32") or (sys.platform == "os2emx"):
296 strTmplte = " -lpython%d%d"
297 strWrk = strTmplte % ( (sys.hexversion >> 24),
298 ((sys.hexversion >> 16) & 0xff))
299 strLinkSpec += strWrk
301 # This is not ideal since it changes the search path
302 # for Frameworks which could have side-effects on
303 # other included Frameworks. However, it is necessary
304 # where someone has installed more than one frameworked
305 # Python. Frameworks are really only used in MacOSX.
306 strLibFW = dictConfig.get("PYTHONFRAMEWORKPREFIX")
307 if strLibFW and (strLibFW != ""):
308 strLinkSpec += " -F%s" % (strLibFW)
309 strLinkSpec += " %s" % (dictConfig.get('LINKFORSHARED'))
312 AC_SUBST([PYTHON_LSPEC], [${az_python_output}])
313 AC_MSG_NOTICE([PYTHON_LSPEC=${az_python_output}])
321 # Look for Python and set the output variable 'PYTHON'
322 # to 'python' if found, empty otherwise.
324 AC_DEFUN([AZ_PYTHON_PATH],
326 AC_ARG_VAR( [PYTHON], [Python Executable Path] )
327 AC_PATH_PROG( PYTHON, python, [], $1 )
330 AC_MSG_ERROR([Python Executable not found])
334 AM_CONDITIONAL(ENABLE_PYTHON, test "$az_python_use" = "true")
339 # AZ_PYTHON_PREFIX( )
340 # -------------------
341 # Use the values of $prefix and $exec_prefix for the corresponding
342 # values of PYTHON_PREFIX and PYTHON_EXEC_PREFIX.
344 AC_DEFUN([AZ_PYTHON_PREFIX],
348 AC_MSG_ERROR([Python Executable Path is not known])
350 ax_python_prefix=`${PYTHON} -c "import sys; print sys.prefix"`
351 ax_python_execprefix=`${PYTHON} -c "import sys; print sys.exec_prefix"`
352 AC_SUBST([PYTHON_PREFIX], ["${ax_python_prefix}"])
353 AC_SUBST([PYTHON_EXECPREFIX], ["${ax_python_execprefix}"])
358 # AZ_PYTHON_RUN( PYTHON_PROGRAM )
360 # Run a Python Test Program saving its output
361 # in az_python_output and its condition code
364 AC_DEFUN([AZ_PYTHON_RUN],
366 AC_ARG_VAR( [PYTHON], [Python Executable Path] )
369 AC_MSG_ERROR([Python Executable not found])
371 cat >conftest.py <<_ACEOF
374 az_python_output=`$PYTHON conftest.py`
377 if test -f "conftest.pyc"
386 # AZ_PYTHON_VERSION_CHECK( VERSION, [ACTION-IF-TRUE], [ACTION-IF-FALSE] )
387 # -----------------------------------------------------------------------------
388 # Run ACTION-IF-TRUE if the Python interpreter has version >= VERSION.
389 # Run ACTION-IF-FALSE otherwise.
390 # This test uses sys.hexversion instead of the string equivalant (first
391 # word of sys.version), in order to cope with versions such as 2.2c1.
392 # hexversion has been introduced in Python 1.5.2; it's probably not
393 # worth to support older versions (1.5.1 was released on October 31, 1998).
395 AC_DEFUN([AZ_PYTHON_VERSION_CHECK],
397 AC_ARG_VAR( [PYTHON], [Python Executable Path] )
400 AC_MSG_CHECKING([whether $PYTHON version >= $1])
403 # split strings by '.' and convert to numeric. Append some zeros
404 # because we need at least 4 digits for the hex conversion.
405 minver = map(int, string.split('$1', '.')) + [[0, 0, 0]]
407 for i in xrange(0, 4): minverhex = (minverhex << 8) + minver[[i]]
408 if sys.hexversion >= minverhex:
413 if test $az_python_cc -eq 0
426 # AZ_PYTHON_VERSION_ENSURE( VERSION )
428 # Insure that the Python Interpreter Version
429 # is greater than or equal to the VERSION
432 AC_DEFUN([AZ_PYTHON_VERSION_ENSURE],
434 AZ_PYTHON_VERSION_CHECK(
436 [AC_MSG_RESULT(yes)],
437 [AC_MSG_ERROR(too old)]
443 # AZ_PYTHON_WITH( [path] )
444 # -----------------------------------------------------------------
445 # Handles the various --with-python commands.
447 # $1 is the optional search path for the python executable if needed
449 # ENABLE_PYTHON (AM_CONDITIONAL) is true if python executable found
450 # and --with-python was requested; otherwise false.
451 # $PYTHON contains the full executable path to python if ENABLE_PYTHON
457 # AZ_PYTHON_WITH("/usr/bin")
459 AC_DEFUN([AZ_PYTHON_WITH],
461 AC_ARG_VAR([PYTHON],[Python Executable Path])
463 # unless PYTHON was supplied to us (as a precious variable),
464 # see if --with-python[=PythonExecutablePath], --with-python,
465 # --without-python or --with-python=no was given.
468 AC_MSG_CHECKING(for --with-python)
471 AC_HELP_STRING([--with-python@<:@=PYTHON@:>@],
472 [absolute path name of Python executable]
475 if test "$withval" = "yes"
477 # "yes" was specified, but we don't have a path
478 # for the executable.
479 # So, let's searth the PATH Environment Variable.
489 AC_MSG_ERROR(no path to python found)
492 AM_CONDITIONAL(ENABLE_PYTHON, test x"$az_python_use" = x"true")
494 elif test "$withval" = "no"
498 AM_CONDITIONAL(ENABLE_PYTHON, test x"$az_python_use" = x"true")
500 # $withval must be the executable path then.
501 AC_SUBST([PYTHON], ["${withval}"])
502 AC_MSG_RESULT($withval)
504 AM_CONDITIONAL(ENABLE_PYTHON, test x"$az_python_use" = x"true")
509 # --with-python was not specified.
512 AM_CONDITIONAL(ENABLE_PYTHON, test x"$az_python_use" = x"true")