source: git/m4/ax_python_embed.m4 @ 978443

spielwiese
Last change on this file since 978443 was 75f460, checked in by Hans Schoenemann <hannes@…>, 9 years ago
format
  • Property mode set to 100644
File size: 16.8 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}-config --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}-config --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_cspec=`${PYTHON}-config --cflags | sed -e 's@ -mno-fused-madd @ @g'`
239        #   or -Qunused-arguments / clang :(
240#        ax_python_cspec="${ax_python_ccshared} ${ax_python_includespec}"
241        AC_SUBST([PYTHON_CSPEC], [${ax_python_cspec}])
242        AC_MSG_NOTICE([PYTHON_CSPEC=${ax_python_cspec}])
243    fi
244])
245
246
247
248# AX_PYTHON_INSIST( )
249# -----------------
250# Look for Python and set the output variable 'PYTHON'
251# to 'python' if found, empty otherwise.
252
253AC_DEFUN([AX_PYTHON_INSIST],
254[
255    AC_ARG_VAR( [PYTHON], [Python Executable Path] )
256    if test -z "$PYTHON"
257    then
258        AC_MSG_ERROR([Python Executable not found])
259    fi
260])
261
262
263
264# AX_PYTHON_LSPEC( )
265# -----------------
266# Set up the linker options to link Python embedded
267# programs/libraries in $PYTHON_LSPEC if $PYTHON
268# has been defined.
269
270AC_DEFUN([AX_PYTHON_LSPEC],
271[
272
273    AC_ARG_VAR( [PYTHON], [Python Executable Path] )
274    if test -n "$PYTHON"
275    then
276
277        AX_PYTHON_RUN([
278import sys
279import distutils.sysconfig
280strUseFrameWork = "--enable-framework"
281dictConfig = distutils.sysconfig.get_config_vars( )
282strConfigArgs = dictConfig.get("CONFIG_ARGS")
283strLinkSpec =  dictConfig.get('LDFLAGS')
284if -1 ==  strConfigArgs.find(strUseFrameWork):
285    strLibPL = dictConfig.get("LIBPL")
286    if strLibPL and (strLibPL != ""):
287        strLinkSpec += " -L%s" % (strLibPL)
288    strSys = dictConfig.get("SYSLIBS")
289    if strSys and (strSys != ""):
290        strLinkSpec += " %s" % (strSys)
291    strSHL = dictConfig.get("SHLIBS")
292    if strSHL and (strSHL != ""):
293        strLinkSpec += " %s" % (strSHL)
294    # Construct the Python Library Name.
295    strTmplte = " -lpython%d.%d"
296    if (sys.platform == "win32") or (sys.platform == "os2emx"):
297        strTmplte = " -lpython%d%d"
298    strWrk = strTmplte % ( (sys.hexversion >> 24),
299                            ((sys.hexversion >> 16) & 0xff))
300    strLinkSpec += strWrk
301else:
302    # This is not ideal since it changes the search path
303    # for Frameworks which could have side-effects on
304    # other included Frameworks.  However, it is necessary
305    # where someone has installed more than one frameworked
306    # Python.  Frameworks are really only used in Mac OS X.
307    strLibFW = dictConfig.get("PYTHONFRAMEWORKPREFIX")
308    if strLibFW and (strLibFW != ""):
309        strLinkSpec += " -F%s" % (strLibFW)
310strLinkSpec += " %s" % (dictConfig.get('LINKFORSHARED'))
311print strLinkSpec
312        ])
313
314#        AC_SUBST([PYTHON_LSPEC], [${ax_python_output}])
315#        AC_MSG_NOTICE([PYTHON_LSPEC=${ax_python_output}])
316
317        ax_python_lspec=`${PYTHON}-config --ldflags`
318        AC_SUBST([PYTHON_LSPEC], [${ax_python_lspec}])
319        AC_MSG_NOTICE([PYTHON_LSPEC=${ax_python_lspec}])
320
321    fi
322])
323
324
325
326# AX_PYTHON_PATH( )
327# -----------------
328# Look for Python and set the output variable 'PYTHON'
329# to 'python' if found, empty otherwise.
330
331AC_DEFUN([AX_PYTHON_PATH],
332[
333    AC_ARG_VAR( [PYTHON], [Python Executable Path] )
334    AC_PATH_PROG( PYTHON, python, [], $1 )
335    if test -z "$PYTHON"
336    then
337        AC_MSG_ERROR([Python Executable not found])
338    else
339        ax_python_use=true
340    fi
341    AM_CONDITIONAL(PYTHON_USE, test "$ax_python_use" = "true")
342])
343
344
345
346# AX_PYTHON_PREFIX( )
347# -------------------
348# Use the values of $prefix and $exec_prefix for the corresponding
349# values of PYTHON_PREFIX and PYTHON_EXEC_PREFIX.
350
351AC_DEFUN([AX_PYTHON_PREFIX],
352[
353    if test -z "$PYTHON"
354    then
355        AC_MSG_ERROR([Python Executable Path is not known])
356    fi
357    ax_python_prefix=`${PYTHON} -c "import sys; print sys.prefix"`
358    ax_python_execprefix=`${PYTHON} -c "import sys; print sys.exec_prefix"`
359    AC_SUBST([PYTHON_PREFIX], ["${ax_python_prefix}"])
360    AC_SUBST([PYTHON_EXECPREFIX], ["${ax_python_execprefix}"])
361])
362
363
364
365# AX_PYTHON_RUN( PYTHON_PROGRAM )
366# -----------------
367# Run a Python Test Program saving its output
368# in ax_python_output and its condition code
369# in ax_python_cc.
370
371AC_DEFUN([AX_PYTHON_RUN],
372[
373    AC_ARG_VAR( [PYTHON], [Python Executable Path] )
374    if test -z "$PYTHON"
375    then
376        AC_MSG_ERROR([Python Executable not found])
377    else
378        cat >conftest.py <<_ACEOF
379$1
380_ACEOF
381        ax_python_output=`$PYTHON conftest.py`
382        ax_python_cc=$?
383        rm conftest.py
384        if test -f "conftest.pyc"
385        then
386            rm conftest.pyc
387        fi
388    fi
389])
390
391
392
393# AX_PYTHON_VERSION_CHECK( VERSION, [ACTION-IF-TRUE], [ACTION-IF-FALSE] )
394# -----------------------------------------------------------------------------
395# Run ACTION-IF-TRUE if the Python interpreter has version >= VERSION.
396# Run ACTION-IF-FALSE otherwise.
397# This test uses sys.hexversion instead of the string equivalant (first
398# word of sys.version), in order to cope with versions such as 2.2c1.
399# hexversion has been introduced in Python 1.5.2; it's probably not
400# worth to support older versions (1.5.1 was released on October 31, 1998).
401
402AC_DEFUN([AX_PYTHON_VERSION_CHECK],
403 [
404    AC_ARG_VAR( [PYTHON], [Python Executable Path] )
405    if test -n "$PYTHON"
406    then
407        AC_MSG_CHECKING([whether $PYTHON version >= $1])
408        AX_PYTHON_RUN([
409import sys, string
410# split strings by '.' and convert to numeric.  Append some zeros
411# because we need at least 4 digits for the hex conversion.
412minver = map(int, string.split('$1', '.')) + [[0, 0, 0]]
413minverhex = 0
414for i in xrange(0, 4): minverhex = (minverhex << 8) + minver[[i]]
415if sys.hexversion >= minverhex:
416    sys.exit( 0 )
417else:
418    sys.exit( 1 )
419        ])
420        if test $ax_python_cc -eq 0
421        then
422            $2
423        m4_ifvaln(
424            [$3],
425            [else $3]
426        )
427        fi
428    fi
429])
430
431
432
433# AX_PYTHON_VERSION_ENSURE( VERSION )
434# -----------------
435# Insure that the Python Interpreter Version
436# is greater than or equal to the VERSION
437# parameter.
438
439AC_DEFUN([AX_PYTHON_VERSION_ENSURE],
440[
441    AX_PYTHON_VERSION_CHECK(
442        [$1],
443        [AC_MSG_RESULT(yes)],
444        [AC_MSG_ERROR(too old)]
445    )
446])
447
448
449
450# AX_PYTHON_WITH( [path] )
451# -----------------------------------------------------------------
452# Handles the various --with-python commands.
453# Input:
454#   $1 is the optional search path for the python executable if needed
455# Ouput:
456#   PYTHON_USE (AM_CONDITIONAL) is true if python executable found
457#   and --with-python was requested; otherwise false.
458#   $PYTHON contains the full executable path to python if PYTHON_USE
459#   is true.
460#
461# Example:
462#   AX_PYTHON_WITH( )
463#   or
464#   AX_PYTHON_WITH("/usr/bin")
465
466AC_DEFUN([AX_PYTHON_WITH],
467[
468    AC_ARG_VAR([PYTHON],[Python Executable Path])
469
470    # unless PYTHON was supplied to us (as a precious variable),
471    # see if --with-python[=PythonExecutablePath], --with-python,
472    # --without-python or --with-python=no was given.
473    if test -z "$PYTHON"
474    then
475        AC_MSG_CHECKING(for --with-python)
476        AC_ARG_WITH(
477            python,
478            AS_HELP_STRING([--with-python@<:@=PYTHON@:>@],
479                [absolute path name of Python executable]
480            ),
481            [
482                if test "$withval" = "yes"
483                then
484                    # "yes" was specified, but we don't have a path
485                    # for the executable.
486                    # So, let's searth the PATH Environment Variable.
487                    AC_MSG_RESULT(yes)
488                    AC_PATH_PROG(
489                        [PYTHON],
490                        python,
491                        [],
492                        $1
493                    )
494                    if test -z "$PYTHON"
495                    then
496                        AC_MSG_ERROR(no path to python found)
497                    fi
498                    ax_python_use=true
499                    AM_CONDITIONAL(PYTHON_USE, test x"$ax_python_use" = x"true")
500                    AX_PYTHON_PREFIX( )
501                elif test "$withval" = "no"
502                then
503                    AC_MSG_RESULT(no)
504                    ax_python_use=false
505                    AM_CONDITIONAL(PYTHON_USE, test x"$ax_python_use" = x"true")
506                else
507                    # $withval must be the executable path then.
508                    AC_SUBST([PYTHON], ["${withval}"])
509                    AC_MSG_RESULT($withval)
510                    ax_python_use=true
511                    AM_CONDITIONAL(PYTHON_USE, test x"$ax_python_use" = x"true")
512                    AX_PYTHON_PREFIX( )
513                fi
514            ],
515            [
516                # --with-python was not specified.
517                AC_MSG_RESULT(no)
518                ax_python_use=false
519                AM_CONDITIONAL(PYTHON_USE, test x"$ax_python_use" = x"true")
520            ]
521        )
522    fi
523
524])
Note: See TracBrowser for help on using the repository browser.