source: git/m4/ax_python_embed.m4 @ 84b294

jengelh-datetimespielwiese
Last change on this file since 84b294 was 84b294, checked in by Oleksandr Motsak <motsak@…>, 10 years ago
python-related autoconf macro for pyobjects
  • Property mode set to 100644
File size: 16.7 KB
Line 
1# ===========================================================================
2#      http://www.gnu.org/software/autoconf-archive/ax_python_embed.html
3# ===========================================================================
4#
5# SYNOPSIS
6#
7#   AX_PYTHON_DEFAULT
8#   AX_PYTHON_ENABLE
9#   AX_PYTHON_WITH
10#   AX_PYTHON_PATH
11#   AX_PYTHON_VERSION_ENSURE( [2.2] )
12#   AX_PYTHON_CSPEC
13#   AX_PYTHON_LSPEC
14#
15# DESCRIPTION
16#
17#   This file provides autoconf support for those applications that want to
18#   embed python. It supports all pythons >= 2.2 which is the first official
19#   release containing distutils. Version 2.2 of python was released
20#   December 21, 2001. Since it actually executes the python, cross platform
21#   configuration will probably not work. Also, most of the platforms
22#   supported are consistent until you look into Mac OS X. The python
23#   included with it is installed as a framework which is a very different
24#   environment to set up the normal tools such as gcc and libtool to deal
25#   with. Therefore, once we establish which python that we are going to
26#   use, we use its distutils to actually compile and link our modules or
27#   applications.
28#
29#   At this time, it does NOT support linking with Python statically. It
30#   does support dynamic linking.
31#
32#   This set of macros help define $PYTHON, $PYTHON_USE, $PYTHON_CSPEC and
33#   $PYTHON_LSPEC. $PYTHON defines the full executable path for the Python
34#   being linked to and is used within these macros to determine if that has
35#   been specified or found. These macros do execute this python version so
36#   it must be present on the system at configure time.
37#
38#   $PYTHON_USE is an automake variable that defines whether Python support
39#   should be included or not in your application. $PYTHON_CSPEC is a
40#   variable that supplies additional CFLAGS for the compilation of the
41#   application/shared library. $PYTHON_LSPEC is a variable that supplies
42#   additional LDFLAGS for linking the application/shared library.
43#
44#   The following is an example of how to set up for python usage within
45#   your application in your configure.in:
46#
47#     AX_PYTHON_DEFAULT( )
48#     AX_PYTHON_ENABLE( )             # Optional
49#     AX_PYTHON_WITH( )               # Optional
50#     AX_PYTHON_PATH( )               # or AX_PYTHON_INSIST( )
51#     # if $PYTHON is not defined, then the following do nothing.
52#     AX_PYTHON_VERSION_ENSURE( [2.2] )
53#     AX_PYTHON_CSPEC
54#     AX_PYTHON_LSPEC
55#
56#   The AX_PYTHON_DEFAULT sets the $PYTHON_USE to false. Thereby, excluding
57#   it if it was optional.
58#
59#   The AX_PYTHON_ENABLE looks for the optional configure parameters of
60#   --enable-python/--disable-python and establishes the $PYTHON and
61#   $PYTHON_USE variables accordingly.
62#
63#   The AX_PYTHON_WITH looks for the optional configure parameters of
64#   --with-python/--without-python and establishes the $PYTHON and
65#   $PYTHON_USE variables accordingly.
66#
67#   The AX_PYTHON_PATH looks for python assuming that none has been
68#   previously found or defined and issues an error if it does not find it.
69#   If it does find it, it establishes the $PYTHON and $PYTHON_USE variables
70#   accordingly. AX_PYTHON_INSIST could be used here instead if you want to
71#   insist that Python support be included using the --enable-python or
72#   --with-python checks previously done.
73#
74#   The AX_PYTHON_VERSION_ENSURE issues an error if the Python previously
75#   found is not of version 2.2 or greater.
76#
77#   Once that these macros have be run, we can use PYTHON_USE within the
78#   makefile.am file to conditionally add the Python support such as:
79#
80#   Makefile.am example showing optional inclusion of directories:
81#
82#    if PYTHON_USE
83#    plugins = plugins
84#    src = src
85#    else
86#    plugins =
87#    src =
88#    endif
89#
90#    SUBDIRS = . $(plugins) $(src)
91#
92#   Makefile.am example showing optional shared library build:
93#
94#    if PYTHON_USE
95#    lib_LTLIBRARIES        = libElemList.la
96#    libElemList_la_SOURCES = libElemList.c
97#    libElemList_la_CFLAGS  = @PYTHON_CSPEC@
98#    libElemList_la_LDFLAGS = @PYTHON_LSPEC@
99#    endif
100#
101#   Makefile.am example showing optional program build:
102#
103#    if PYTHON_USE
104#    bin_PROGRAMS    = runFunc
105#    runFunc_SOURCES = runFunc.c
106#    runFunc_CFLAGS  = @PYTHON_CSPEC@
107#    runFunc_LDFLAGS = @PYTHON_LSPEC@
108#    endif
109#
110#   The above compiles the modules only if PYTHON_USE was specified as true.
111#   Also, the else portion of the if was optional.
112#
113# LICENSE
114#
115#   Copyright (c) 2008 Robert White <kranki@mac.com>
116#   Copyright (c) 2008 Dustin J. Mitchell <dustin@cs.uchicago.edu>
117#
118#   Copying and distribution of this file, with or without modification, are
119#   permitted in any medium without royalty provided the copyright notice
120#   and this notice are preserved. This file is offered as-is, without any
121#   warranty.
122
123#serial 8
124
125# AX_PYTHON_DEFAULT( )
126# -----------------
127# Sets the default to not include Python support.
128
129AC_DEFUN([AX_PYTHON_DEFAULT],
130[
131    ax_python_use=false
132    AM_CONDITIONAL(PYTHON_USE, test x"$ax_python_use" = x"true")
133])
134
135
136
137# AX_PYTHON_ENABLE( [path] )
138# -----------------------------------------------------------------
139# Handles the various --enable-python commands.
140# Input:
141#   $1 is the optional search path for the python executable if needed
142# Ouput:
143#   PYTHON_USE (AM_CONDITIONAL) is true if python executable found
144#   and --enable-python was requested; otherwise false.
145#   $PYTHON contains the full executable path to python if PYTHON_ENABLE_USE
146#   is true.
147#
148# Example:
149#   AX_PYTHON_ENABLE( )
150#   or
151#   AX_PYTHON_ENABLE( "/usr/bin" )
152
153AC_DEFUN([AX_PYTHON_ENABLE],
154[
155    AC_ARG_VAR([PYTHON],[Python Executable Path])
156
157    # unless PYTHON was supplied to us (as a precious variable),
158    # see if --enable-python[=PythonExecutablePath], --enable-python,
159    # --disable-python or --enable-python=no was given.
160    if test -z "$PYTHON"
161    then
162        AC_MSG_CHECKING(for --enable-python)
163        AC_ARG_ENABLE(
164            python,
165            AS_HELP_STRING([--enable-python@<:@=PYTHON@:>@],
166                [absolute path name of Python executable]
167            ),
168            [
169                if test "$enableval" = "yes"
170                then
171                    # "yes" was specified, but we don't have a path
172                    # for the executable.
173                    # So, let's searth the PATH Environment Variable.
174                    AC_MSG_RESULT(yes)
175                    AC_PATH_PROG(
176                        [PYTHON],
177                        python,
178                        [],
179                        $1
180                    )
181                    if test -z "$PYTHON"
182                    then
183                        AC_MSG_ERROR(no path to python found)
184                    fi
185                    ax_python_use=true
186                    AM_CONDITIONAL(PYTHON_USE, test x"$ax_python_use" = x"true")
187                    AX_PYTHON_PREFIX( )
188                elif test "$enableval" = "no"
189                then
190                    AC_MSG_RESULT(no)
191                    ax_python_use=false
192                    AM_CONDITIONAL(PYTHON_USE, test x"$ax_python_use" = x"true")
193                else
194                    # $enableval must be the executable path then.
195                    AC_SUBST([PYTHON], ["${enableval}"])
196                    AC_MSG_RESULT($withval)
197                    ax_python_use=true
198                    AM_CONDITIONAL(PYTHON_USE, test x"$ax_python_use" = x"true")
199                    AX_PYTHON_PREFIX( )
200                fi
201            ],
202            [
203                # --with-python was not specified.
204                AC_MSG_RESULT(no)
205                ax_python_use=false
206                AM_CONDITIONAL(PYTHON_USE, test x"$ax_python_use" = x"true")
207            ]
208        )
209    fi
210
211])
212
213
214
215# AX_PYTHON_CSPEC( )
216# -----------------
217# Set up the c compiler options to compile Python
218# embedded programs/libraries in $PYTHON_CSPEC if
219# $PYTHON has been defined.
220
221AC_DEFUN([AX_PYTHON_CSPEC],
222[
223    AC_ARG_VAR( [PYTHON], [Python Executable Path] )
224    if test -n "$PYTHON"
225    then
226        ax_python_prefix=`${PYTHON} -c "import sys; print sys.prefix"`
227        if test -z "$ax_python_prefix"
228        then
229            AC_MSG_ERROR([Python Prefix is not known])
230        fi
231        ax_python_execprefix=`${PYTHON} -c "import sys; print sys.exec_prefix"`
232        ax_python_version=`$PYTHON -c "import sys; print sys.version[[:3]]"`
233        ax_python_includespec="-I${ax_python_prefix}/include/python${ax_python_version}"
234        if test x"$python_prefix" != x"$python_execprefix"; then
235            ax_python_execspec="-I${ax_python_execprefix}/include/python${ax_python_version}"
236            ax_python_includespec="${ax_python_includespec} $ax_python_execspec"
237        fi
238        ax_python_ccshared=`${PYTHON} -c "import distutils.sysconfig; print distutils.sysconfig.get_config_var('CFLAGSFORSHARED')"`
239        ax_python_cspec="${ax_python_ccshared} ${ax_python_includespec}"
240        AC_SUBST([PYTHON_CSPEC], [${ax_python_cspec}])
241        AC_MSG_NOTICE([PYTHON_CSPEC=${ax_python_cspec}])
242    fi
243])
244
245
246
247# AX_PYTHON_INSIST( )
248# -----------------
249# Look for Python and set the output variable 'PYTHON'
250# to 'python' if found, empty otherwise.
251
252AC_DEFUN([AX_PYTHON_INSIST],
253[
254    AC_ARG_VAR( [PYTHON], [Python Executable Path] )
255    if test -z "$PYTHON"
256    then
257        AC_MSG_ERROR([Python Executable not found])
258    fi
259])
260
261
262
263# AX_PYTHON_LSPEC( )
264# -----------------
265# Set up the linker options to link Python embedded
266# programs/libraries in $PYTHON_LSPEC if $PYTHON
267# has been defined.
268
269AC_DEFUN([AX_PYTHON_LSPEC],
270[
271    AC_ARG_VAR( [PYTHON], [Python Executable Path] )
272    if test -n "$PYTHON"
273    then
274        AX_PYTHON_RUN([
275import sys
276import distutils.sysconfig
277strUseFrameWork = "--enable-framework"
278dictConfig = distutils.sysconfig.get_config_vars( )
279strConfigArgs = dictConfig.get("CONFIG_ARGS")
280strLinkSpec =  dictConfig.get('LDFLAGS')
281if -1 ==  strConfigArgs.find(strUseFrameWork):
282    strLibPL = dictConfig.get("LIBPL")
283    if strLibPL and (strLibPL != ""):
284        strLinkSpec += " -L%s" % (strLibPL)
285    strSys = dictConfig.get("SYSLIBS")
286    if strSys and (strSys != ""):
287        strLinkSpec += " %s" % (strSys)
288    strSHL = dictConfig.get("SHLIBS")
289    if strSHL and (strSHL != ""):
290        strLinkSpec += " %s" % (strSHL)
291    # Construct the Python Library Name.
292    strTmplte = " -lpython%d.%d"
293    if (sys.platform == "win32") or (sys.platform == "os2emx"):
294        strTmplte = " -lpython%d%d"
295    strWrk = strTmplte % ( (sys.hexversion >> 24),
296                            ((sys.hexversion >> 16) & 0xff))
297    strLinkSpec += strWrk
298else:
299    # This is not ideal since it changes the search path
300    # for Frameworks which could have side-effects on
301    # other included Frameworks.  However, it is necessary
302    # where someone has installed more than one frameworked
303    # Python.  Frameworks are really only used in MacOSX.
304    strLibFW = dictConfig.get("PYTHONFRAMEWORKPREFIX")
305    if strLibFW and (strLibFW != ""):
306        strLinkSpec += " -F%s" % (strLibFW)
307strLinkSpec += " %s" % (dictConfig.get('LINKFORSHARED'))
308print strLinkSpec
309        ])
310        AC_SUBST([PYTHON_LSPEC], [${ax_python_output}])
311        AC_MSG_NOTICE([PYTHON_LSPEC=${ax_python_output}])
312    fi
313])
314
315
316
317# AX_PYTHON_PATH( )
318# -----------------
319# Look for Python and set the output variable 'PYTHON'
320# to 'python' if found, empty otherwise.
321
322AC_DEFUN([AX_PYTHON_PATH],
323[
324    AC_ARG_VAR( [PYTHON], [Python Executable Path] )
325    AC_PATH_PROG( PYTHON, python, [], $1 )
326    if test -z "$PYTHON"
327    then
328        AC_MSG_ERROR([Python Executable not found])
329    else
330        ax_python_use=true
331    fi
332    AM_CONDITIONAL(PYTHON_USE, test "$ax_python_use" = "true")
333])
334
335
336
337# AX_PYTHON_PREFIX( )
338# -------------------
339# Use the values of $prefix and $exec_prefix for the corresponding
340# values of PYTHON_PREFIX and PYTHON_EXEC_PREFIX.
341
342AC_DEFUN([AX_PYTHON_PREFIX],
343[
344    if test -z "$PYTHON"
345    then
346        AC_MSG_ERROR([Python Executable Path is not known])
347    fi
348    ax_python_prefix=`${PYTHON} -c "import sys; print sys.prefix"`
349    ax_python_execprefix=`${PYTHON} -c "import sys; print sys.exec_prefix"`
350    AC_SUBST([PYTHON_PREFIX], ["${ax_python_prefix}"])
351    AC_SUBST([PYTHON_EXECPREFIX], ["${ax_python_execprefix}"])
352])
353
354
355
356# AX_PYTHON_RUN( PYTHON_PROGRAM )
357# -----------------
358# Run a Python Test Program saving its output
359# in ax_python_output and its condition code
360# in ax_python_cc.
361
362AC_DEFUN([AX_PYTHON_RUN],
363[
364    AC_ARG_VAR( [PYTHON], [Python Executable Path] )
365    if test -z "$PYTHON"
366    then
367        AC_MSG_ERROR([Python Executable not found])
368    else
369        cat >conftest.py <<_ACEOF
370$1
371_ACEOF
372        ax_python_output=`$PYTHON conftest.py`
373        ax_python_cc=$?
374        rm conftest.py
375        if test -f "conftest.pyc"
376        then
377            rm conftest.pyc
378        fi
379    fi
380])
381
382
383
384# AX_PYTHON_VERSION_CHECK( VERSION, [ACTION-IF-TRUE], [ACTION-IF-FALSE] )
385# -----------------------------------------------------------------------------
386# Run ACTION-IF-TRUE if the Python interpreter has version >= VERSION.
387# Run ACTION-IF-FALSE otherwise.
388# This test uses sys.hexversion instead of the string equivalant (first
389# word of sys.version), in order to cope with versions such as 2.2c1.
390# hexversion has been introduced in Python 1.5.2; it's probably not
391# worth to support older versions (1.5.1 was released on October 31, 1998).
392
393AC_DEFUN([AX_PYTHON_VERSION_CHECK],
394 [
395    AC_ARG_VAR( [PYTHON], [Python Executable Path] )
396    if test -n "$PYTHON"
397    then
398        AC_MSG_CHECKING([whether $PYTHON version >= $1])
399        AX_PYTHON_RUN([
400import sys, string
401# split strings by '.' and convert to numeric.  Append some zeros
402# because we need at least 4 digits for the hex conversion.
403minver = map(int, string.split('$1', '.')) + [[0, 0, 0]]
404minverhex = 0
405for i in xrange(0, 4): minverhex = (minverhex << 8) + minver[[i]]
406if sys.hexversion >= minverhex:
407    sys.exit( 0 )
408else:
409    sys.exit( 1 )
410        ])
411        if test $ax_python_cc -eq 0
412        then
413            $2
414        m4_ifvaln(
415            [$3],
416            [else $3]
417        )
418        fi
419    fi
420])
421
422
423
424# AX_PYTHON_VERSION_ENSURE( VERSION )
425# -----------------
426# Insure that the Python Interpreter Version
427# is greater than or equal to the VERSION
428# parameter.
429
430AC_DEFUN([AX_PYTHON_VERSION_ENSURE],
431[
432    AX_PYTHON_VERSION_CHECK(
433        [$1],
434        [AC_MSG_RESULT(yes)],
435        [AC_MSG_ERROR(too old)]
436    )
437])
438
439
440
441# AX_PYTHON_WITH( [path] )
442# -----------------------------------------------------------------
443# Handles the various --with-python commands.
444# Input:
445#   $1 is the optional search path for the python executable if needed
446# Ouput:
447#   PYTHON_USE (AM_CONDITIONAL) is true if python executable found
448#   and --with-python was requested; otherwise false.
449#   $PYTHON contains the full executable path to python if PYTHON_USE
450#   is true.
451#
452# Example:
453#   AX_PYTHON_WITH( )
454#   or
455#   AX_PYTHON_WITH("/usr/bin")
456
457AC_DEFUN([AX_PYTHON_WITH],
458[
459    AC_ARG_VAR([PYTHON],[Python Executable Path])
460
461    # unless PYTHON was supplied to us (as a precious variable),
462    # see if --with-python[=PythonExecutablePath], --with-python,
463    # --without-python or --with-python=no was given.
464    if test -z "$PYTHON"
465    then
466        AC_MSG_CHECKING(for --with-python)
467        AC_ARG_WITH(
468            python,
469            AS_HELP_STRING([--with-python@<:@=PYTHON@:>@],
470                [absolute path name of Python executable]
471            ),
472            [
473                if test "$withval" = "yes"
474                then
475                    # "yes" was specified, but we don't have a path
476                    # for the executable.
477                    # So, let's searth the PATH Environment Variable.
478                    AC_MSG_RESULT(yes)
479                    AC_PATH_PROG(
480                        [PYTHON],
481                        python,
482                        [],
483                        $1
484                    )
485                    if test -z "$PYTHON"
486                    then
487                        AC_MSG_ERROR(no path to python found)
488                    fi
489                    ax_python_use=true
490                    AM_CONDITIONAL(PYTHON_USE, test x"$ax_python_use" = x"true")
491                    AX_PYTHON_PREFIX( )
492                elif test "$withval" = "no"
493                then
494                    AC_MSG_RESULT(no)
495                    ax_python_use=false
496                    AM_CONDITIONAL(PYTHON_USE, test x"$ax_python_use" = x"true")
497                else
498                    # $withval must be the executable path then.
499                    AC_SUBST([PYTHON], ["${withval}"])
500                    AC_MSG_RESULT($withval)
501                    ax_python_use=true
502                    AM_CONDITIONAL(PYTHON_USE, test x"$ax_python_use" = x"true")
503                    AX_PYTHON_PREFIX( )
504                fi
505            ],
506            [
507                # --with-python was not specified.
508                AC_MSG_RESULT(no)
509                ax_python_use=false
510                AM_CONDITIONAL(PYTHON_USE, test x"$ax_python_use" = x"true")
511            ]
512        )
513    fi
514
515])
Note: See TracBrowser for help on using the repository browser.