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, $PYTHON_USE, $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 # $PYTHON_USE 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 $PYTHON_USE 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 # $PYTHON_USE 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 # $PYTHON_USE 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 $PYTHON_USE
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 PYTHON_USE 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 PYTHON_USE 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(PYTHON_USE, 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 # PYTHON_USE (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(PYTHON_USE, test x"$az_python_use" = x"true")
190 elif test "$enableval" = "no"
194 AM_CONDITIONAL(PYTHON_USE, 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(PYTHON_USE, test x"$az_python_use" = x"true")
205 # --with-python was not specified.
208 AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")
219 # Set up the c compiler options to compile Python
220 # embedded programs/libraries in $PYTHON_CSPEC if
221 # $PYTHON has been defined.
223 AC_DEFUN([AZ_PYTHON_CSPEC],
225 AC_ARG_VAR( [PYTHON], [Python Executable Path] )
228 AC_MSG_CHECKING([for python include directory])
229 az_python_prefix=`${PYTHON} -c "import sys; print sys.prefix"`
230 if test -z "$az_python_prefix"
232 AC_MSG_ERROR([Python Prefix is not known])
235 az_python_execprefix=`${PYTHON} -c "import sys; print sys.exec_prefix"`
236 az_python_version=`$PYTHON -c "import sys; print sys.version[[:3]]"`
237 az_python_includespec="-I`$PYTHON -c 'import distutils.sysconfig; print distutils.sysconfig.get_python_inc();'`"
238 if test x"$python_prefix" != x"$python_execprefix"; then
239 az_python_execspec="-I${az_python_execprefix}/include/python${az_python_version}"
240 az_python_includespec="${az_python_includespec} $az_python_execspec"
242 az_python_ccshared=`${PYTHON} -c "import distutils.sysconfig; print distutils.sysconfig.get_config_var('CFLAGSFORSHARED')"`
243 az_python_cspec="${az_python_ccshared} ${az_python_includespec}"
244 AC_SUBST([PYTHON_CSPEC], [${az_python_cspec}])
245 AC_SUBST([PYTHON_CPPFLAGS], [${az_python_includespec}])
246 AC_SUBST([PYTHON_SHORTVERSION], ["${az_python_version}"])
247 AC_MSG_RESULT([$PYTHON_CPPFLAGS])
253 # AZ_PYTHON_INSIST( )
255 # Look for Python and set the output variable 'PYTHON'
256 # to 'python' if found, empty otherwise.
258 AC_DEFUN([AZ_PYTHON_PATH],
260 AC_ARG_VAR( [PYTHON], [Python Executable Path] )
263 AC_MSG_ERROR([Python Executable not found])
271 # Set up the linker options to link Python embedded
272 # programs/libraries in $PYTHON_LSPEC if $PYTHON
275 AC_DEFUN([AZ_PYTHON_LSPEC],
277 AC_ARG_VAR( [PYTHON], [Python Executable Path] )
280 AC_MSG_CHECKING([for python linker flags])
283 import distutils.sysconfig
284 strUseFrameWork = "--enable-framework"
285 dictConfig = distutils.sysconfig.get_config_vars( )
286 strConfigArgs = dictConfig.get("CONFIG_ARGS")
287 strLinkSpec = dictConfig.get('LDFLAGS')
288 if -1 == strConfigArgs.find(strUseFrameWork):
289 strLibPL = dictConfig.get("LIBPL")
290 if strLibPL and (strLibPL != ""):
291 strLinkSpec += " -L%s" % (strLibPL)
292 strSys = dictConfig.get("SYSLIBS")
293 if strSys and (strSys != ""):
294 strLinkSpec += " %s" % (strSys)
295 strSHL = dictConfig.get("SHLIBS")
296 if strSHL and (strSHL != ""):
297 strLinkSpec += " %s" % (strSHL)
298 # Construct the Python Library Name.
299 strTmplte = " -lpython%d.%d"
300 if (sys.platform == "win32") or (sys.platform == "os2emx"):
301 strTmplte = " -lpython%d%d"
302 strWrk = strTmplte % ( (sys.hexversion >> 24),
303 ((sys.hexversion >> 16) & 0xff))
304 strLinkSpec += strWrk
306 # This is not ideal since it changes the search path
307 # for Frameworks which could have side-effects on
308 # other included Frameworks. However, it is necessary
309 # where someone has installed more than one frameworked
310 # Python. Frameworks are really only used in MacOSX.
311 strLibFW = dictConfig.get("PYTHONFRAMEWORKPREFIX")
312 if strLibFW and (strLibFW != ""):
313 strLinkSpec += " -F%s" % (strLibFW)
314 strLinkSpec += " %s" % (dictConfig.get('LINKFORSHARED'))
317 AC_SUBST([PYTHON_LSPEC], [${az_python_output}])
318 az_python_ldflags=`${PYTHON} -c "import distutils.sysconfig; \
319 print '-L' + distutils.sysconfig.get_python_lib(), '-lpython' + \
320 distutils.sysconfig.get_config_var('VERSION');"`
321 AC_SUBST([PYTHON_LDFLAGS], [${az_python_ldflags}])
322 AC_MSG_RESULT([$PYTHON_LDFLAGS])
330 # Look for Python and set the output variable 'PYTHON'
331 # to 'python' if found, empty otherwise.
333 AC_DEFUN([AZ_PYTHON_PATH],
335 AC_ARG_VAR( [PYTHON], [Python Executable Path] )
336 AC_PATH_PROG( PYTHON, python, [], $1 )
339 AC_MSG_ERROR([Python Executable not found])
343 AM_CONDITIONAL(PYTHON_USE, test "$az_python_use" = "true")
348 # AZ_PYTHON_PREFIX( )
349 # -------------------
350 # Use the values of $prefix and $exec_prefix for the corresponding
351 # values of PYTHON_PREFIX and PYTHON_EXEC_PREFIX.
353 AC_DEFUN([AZ_PYTHON_PREFIX],
357 AC_MSG_ERROR([Python Executable Path is not known])
359 AC_MSG_CHECKING([for python extension install directory])
360 ax_python_prefix=`${PYTHON} -c "import sys; print sys.prefix"`
361 ax_python_execprefix=`${PYTHON} -c "import sys; print sys.exec_prefix"`
362 AC_SUBST([PYTHON_PREFIX], ["${ax_python_prefix}"])
363 AC_SUBST([PYTHON_EXECPREFIX], ["${ax_python_execprefix}"])
364 PYTHON_EXECDIR=`$PYTHON -c "import distutils.sysconfig; \
365 print distutils.sysconfig.get_python_lib();"`
366 AC_SUBST([PYTHON_EXECDIR])
367 AC_MSG_RESULT([$PYTHON_EXECDIR])
370 # AZ_PYTHON_RUN( PYTHON_PROGRAM )
372 # Run a Python Test Program saving its output
373 # in az_python_output and its condition code
376 AC_DEFUN([AZ_PYTHON_RUN],
378 AC_ARG_VAR( [PYTHON], [Python Executable Path] )
381 AC_MSG_ERROR([Python Executable not found])
383 cat >conftest.py <<_ACEOF
386 az_python_output=`$PYTHON conftest.py`
389 if test -f "conftest.pyc"
398 # AZ_PYTHON_VERSION_CHECK( VERSION, [ACTION-IF-TRUE], [ACTION-IF-FALSE] )
399 # -----------------------------------------------------------------------------
400 # Run ACTION-IF-TRUE if the Python interpreter has version >= VERSION.
401 # Run ACTION-IF-FALSE otherwise.
402 # This test uses sys.hexversion instead of the string equivalant (first
403 # word of sys.version), in order to cope with versions such as 2.2c1.
404 # hexversion has been introduced in Python 1.5.2; it's probably not
405 # worth to support older versions (1.5.1 was released on October 31, 1998).
407 AC_DEFUN([AZ_PYTHON_VERSION_CHECK],
409 AC_ARG_VAR( [PYTHON], [Python Executable Path] )
412 AC_MSG_CHECKING([whether $PYTHON version >= $1])
415 # split strings by '.' and convert to numeric. Append some zeros
416 # because we need at least 4 digits for the hex conversion.
417 minver = map(int, string.split('$1', '.')) + [[0, 0, 0]]
419 for i in xrange(0, 4): minverhex = (minverhex << 8) + minver[[i]]
420 if sys.hexversion >= minverhex:
425 if test $az_python_cc -eq 0
438 # AZ_PYTHON_VERSION_ENSURE( VERSION )
440 # Insure that the Python Interpreter Version
441 # is greater than or equal to the VERSION
444 AC_DEFUN([AZ_PYTHON_VERSION_ENSURE],
446 AZ_PYTHON_VERSION_CHECK(
448 [AC_MSG_RESULT(yes)],
449 [AC_MSG_ERROR(too old)]
455 # AZ_PYTHON_WITH( [path] )
456 # -----------------------------------------------------------------
457 # Handles the various --with-python commands.
459 # $1 is the optional search path for the python executable if needed
461 # PYTHON_USE (AM_CONDITIONAL) is true if python executable found
462 # and --with-python was requested; otherwise false.
463 # $PYTHON contains the full executable path to python if PYTHON_USE
469 # AZ_PYTHON_WITH("/usr/bin")
471 AC_DEFUN([AZ_PYTHON_WITH],
473 AC_ARG_VAR([PYTHON],[Python Executable Path])
475 # unless PYTHON was supplied to us (as a precious variable),
476 # see if --with-python[=PythonExecutablePath], --with-python,
477 # --without-python or --with-python=no was given.
480 AC_MSG_CHECKING(for --with-python)
483 AC_HELP_STRING([--with-python@<:@=PYTHON@:>@],
484 [absolute path name of Python executable]
487 if test "$withval" = "yes"
489 # "yes" was specified, but we don't have a path
490 # for the executable.
491 # So, let's searth the PATH Environment Variable.
501 AC_MSG_ERROR(no path to python found)
504 AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")
508 elif test "$withval" = "no"
512 AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")
514 # $withval must be the executable path then.
515 AC_SUBST([PYTHON], ["${withval}"])
516 AC_MSG_RESULT($withval)
518 AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")
525 # --with-python was not specified.
528 AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")