source: git/m4/ax_python_embed.m4 @ 6b44a8

spielwiese
Last change on this file since 6b44a8 was 6039c6, checked in by Hans Schoenemann <hannes@…>, 7 years ago
update: ax_python_embed.m4 to version 15 (port: debian 9)
  • Property mode set to 100644
File size: 16.8 KB
Line 
1# ===========================================================================
2#     https://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 15
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# Output:
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 search 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 equivalent (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
401# split strings by '.' and convert to numeric.  Append some zeros
402# because we need at least 4 digits for the hex conversion.
403# It accepts a string like "X[.Y[.Z]]" with X,Y,Z=digits
404# and [] means optional.
405minver = list(map(int, '$1'.split('.'))) + [[0, 0, 0]]
406minver[3] = 255
407minverhex = 0
408for i in range(0, 4): minverhex = (minverhex << 8) + minver[[i]]
409if sys.hexversion >= minverhex:
410    sys.exit( 0 )
411else:
412    sys.exit( 1 )
413        ])
414        if test $ax_python_cc -eq 0
415        then
416            $2
417        m4_ifvaln(
418            [$3],
419            [else $3]
420        )
421        fi
422    fi
423])
424
425
426
427# AX_PYTHON_VERSION_ENSURE( VERSION )
428# -----------------
429# Insure that the Python Interpreter Version
430# is greater than or equal to the VERSION
431# parameter.
432
433AC_DEFUN([AX_PYTHON_VERSION_ENSURE],
434[
435    AX_PYTHON_VERSION_CHECK(
436        [$1],
437        [AC_MSG_RESULT(yes)],
438        [AC_MSG_ERROR(too old)]
439    )
440])
441
442
443
444# AX_PYTHON_WITH( [path] )
445# -----------------------------------------------------------------
446# Handles the various --with-python commands.
447# Input:
448#   $1 is the optional search path for the python executable if needed
449# Output:
450#   PYTHON_USE (AM_CONDITIONAL) is true if python executable found
451#   and --with-python was requested; otherwise false.
452#   $PYTHON contains the full executable path to python if PYTHON_USE
453#   is true.
454#
455# Example:
456#   AX_PYTHON_WITH( )
457#   or
458#   AX_PYTHON_WITH("/usr/bin")
459
460AC_DEFUN([AX_PYTHON_WITH],
461[
462    AC_ARG_VAR([PYTHON],[Python Executable Path])
463
464    # unless PYTHON was supplied to us (as a precious variable),
465    # see if --with-python[=PythonExecutablePath], --with-python,
466    # --without-python or --with-python=no was given.
467    if test -z "$PYTHON"
468    then
469        AC_MSG_CHECKING(for --with-python)
470        AC_ARG_WITH(
471            python,
472            AS_HELP_STRING([--with-python@<:@=PYTHON@:>@],
473                [absolute path name of Python executable]
474            ),
475            [
476                if test "$withval" = "yes"
477                then
478                    # "yes" was specified, but we don't have a path
479                    # for the executable.
480                    # So, let's search the PATH Environment Variable.
481                    AC_MSG_RESULT(yes)
482                    AC_PATH_PROG(
483                        [PYTHON],
484                        python,
485                        [],
486                        $1
487                    )
488                    if test -z "$PYTHON"
489                    then
490                        AC_MSG_ERROR(no path to python found)
491                    fi
492                    ax_python_use=true
493                    AM_CONDITIONAL(PYTHON_USE, test x"$ax_python_use" = x"true")
494                    AX_PYTHON_PREFIX( )
495                elif test "$withval" = "no"
496                then
497                    AC_MSG_RESULT(no)
498                    ax_python_use=false
499                    AM_CONDITIONAL(PYTHON_USE, test x"$ax_python_use" = x"true")
500                else
501                    # $withval must be the executable path then.
502                    AC_SUBST([PYTHON], ["${withval}"])
503                    AC_MSG_RESULT($withval)
504                    ax_python_use=true
505                    AM_CONDITIONAL(PYTHON_USE, test x"$ax_python_use" = x"true")
506                    AX_PYTHON_PREFIX( )
507                fi
508            ],
509            [
510                # --with-python was not specified.
511                AC_MSG_RESULT(no)
512                ax_python_use=false
513                AM_CONDITIONAL(PYTHON_USE, test x"$ax_python_use" = x"true")
514            ]
515        )
516    fi
517
518])
Note: See TracBrowser for help on using the repository browser.