Update OpenBSD hack.
[asdcplib.git] / m4 / az_python.m4
1 ##### http://autoconf-archive.cryp.to/az_python.html
2 #
3 # SYNOPSIS
4 #
5 #   AZ_PYTHON_DEFAULT
6 #   AZ_PYTHON_ENABLE
7 #   AZ_PYTHON_WITH
8 #   AZ_PYTHON_PATH
9 #   AZ_PYTHON_VERSION_ENSURE( [2.2] )
10 #   AZ_PYTHON_CSPEC
11 #   AZ_PYTHON_LSPEC
12 #
13 # DESCRIPTION
14 #
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.
26 #
27 #   At this time, it does NOT support linking with Python statically.
28 #   It does support dynamic linking.
29 #
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
35 #   time.
36 #
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.
43 #
44 #   The following is an example of how to set up for python usage
45 #   within your application in your configure.in:
46 #
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] )
53 #     AZ_PYTHON_CSPEC
54 #     AZ_PYTHON_LSPEC
55 #
56 #   The AZ_PYTHON_DEFAULT sets the $PYTHON_USE to false. Thereby,
57 #   excluding it if it was optional.
58 #
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.
62 #
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.
66 #
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.
73 #
74 #   The AZ_PYTHON_VERSION_ENSURE issues an error if the Python
75 #   previously found is not of version 2.2 or greater.
76 #
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
79 #   as:
80 #
81 #   Makefile.am example showing optional inclusion of directories:
82 #
83 #    if PYTHON_USE
84 #    plugins = plugins
85 #    src = src
86 #    else
87 #    plugins =
88 #    src =
89 #    endif
90 #
91 #    SUBDIRS = . $(plugins) $(src)
92 #
93 #   Makefile.am example showing optional shared library build:
94 #
95 #    if PYTHON_USE
96 #    lib_LTLIBRARIES        = libElemList.la
97 #    libElemList_la_SOURCES = libElemList.c
98 #    libElemList_la_CFLAGS  = @PYTHON_CSPEC@
99 #    libElemList_la_LDFLAGS = @PYTHON_LSPEC@
100 #    endif
101 #
102 #   Makefile.am example showing optional program build:
103 #
104 #    if PYTHON_USE
105 #    bin_PROGRAMS    = runFunc
106 #    runFunc_SOURCES = runFunc.c
107 #    runFunc_CFLAGS  = @PYTHON_CSPEC@
108 #    runFunc_LDFLAGS = @PYTHON_LSPEC@
109 #    endif
110 #
111 #   The above compiles the modules only if PYTHON_USE was specified as
112 #   true. Also, the else portion of the if was optional.
113 #
114 # LAST MODIFICATION
115 #
116 #   2007-08-04
117 #
118 # COPYLEFT
119 #
120 #   Copyright (c) 2007 Robert White <kranki@mac.com>
121 #   Copyright (c) 2007 Dustin J. Mitchell <dustin@cs.uchicago.edu>
122 #
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.
126
127 # AZ_PYTHON_DEFAULT( )
128 # -----------------
129 # Sets the default to not include Python support.
130
131 AC_DEFUN([AZ_PYTHON_DEFAULT],
132 [
133     az_python_use=false
134     AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")
135 ])
136
137
138
139 # AZ_PYTHON_ENABLE( [path] )
140 # -----------------------------------------------------------------
141 # Handles the various --enable-python commands.
142 # Input:
143 #   $1 is the optional search path for the python executable if needed
144 # Ouput:
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
148 #   is true.
149 #
150 # Example:
151 #   AZ_PYTHON_ENABLE( )
152 #   or
153 #   AZ_PYTHON_ENABLE( "/usr/bin" )
154
155 AC_DEFUN([AZ_PYTHON_ENABLE],
156 [
157     AC_ARG_VAR([PYTHON],[Python Executable Path])
158
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.
162     if test -z "$PYTHON"
163     then
164         AC_MSG_CHECKING(for --enable-python)
165         AC_ARG_ENABLE(
166             python,
167             AC_HELP_STRING([--enable-python@<:@=PYTHON@:>@],
168                 [absolute path name of Python executable]
169             ),
170             [
171                 if test "$enableval" = "yes"
172                 then
173                     # "yes" was specified, but we don't have a path
174                     # for the executable.
175                     # So, let's searth the PATH Environment Variable.
176                     AC_MSG_RESULT(yes)
177                     AC_PATH_PROG(
178                         [PYTHON],
179                         python,
180                         [],
181                         $1
182                     )
183                     if test -z "$PYTHON"
184                     then
185                         AC_MSG_ERROR(no path to python found)
186                     fi
187                     az_python_use=true
188                     AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")
189                     AZ_PYTHON_PREFIX( )
190                 elif test "$enableval" = "no"
191                 then
192                     AC_MSG_RESULT(no)
193                     az_python_use=false
194                     AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")
195                 else
196                     # $enableval must be the executable path then.
197                     AC_SUBST([PYTHON], ["${enableval}"])
198                     AC_MSG_RESULT($withval)
199                     az_python_use=true
200                     AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")
201                     AZ_PYTHON_PREFIX( )
202                 fi
203             ],
204             [
205                 # --with-python was not specified.
206                 AC_MSG_RESULT(no)
207                 az_python_use=false
208                 AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")
209             ]
210         )
211     fi
212
213 ])
214
215
216
217 # AZ_PYTHON_CSPEC( )
218 # -----------------
219 # Set up the c compiler options to compile Python
220 # embedded programs/libraries in $PYTHON_CSPEC if
221 # $PYTHON has been defined.
222
223 AC_DEFUN([AZ_PYTHON_CSPEC],
224 [
225     AC_ARG_VAR( [PYTHON], [Python Executable Path] )
226     if test -n "$PYTHON"
227     then
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"
231         then
232             AC_MSG_ERROR([Python Prefix is not known])
233         fi
234         python_path=
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"
241         fi
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])
248     fi
249 ])
250
251
252
253 # AZ_PYTHON_INSIST( )
254 # -----------------
255 # Look for Python and set the output variable 'PYTHON'
256 # to 'python' if found, empty otherwise.
257
258 AC_DEFUN([AZ_PYTHON_PATH],
259 [
260     AC_ARG_VAR( [PYTHON], [Python Executable Path] )
261     if test -z "$PYTHON"
262     then
263         AC_MSG_ERROR([Python Executable not found])
264     fi
265 ])
266
267
268
269 # AZ_PYTHON_LSPEC( )
270 # -----------------
271 # Set up the linker options to link Python embedded
272 # programs/libraries in $PYTHON_LSPEC if $PYTHON
273 # has been defined.
274
275 AC_DEFUN([AZ_PYTHON_LSPEC],
276 [
277     AC_ARG_VAR( [PYTHON], [Python Executable Path] )
278     if test -n "$PYTHON"
279     then
280         AC_MSG_CHECKING([for python linker flags])
281         AZ_PYTHON_RUN([
282 import sys
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
305 else:
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'))
315 print strLinkSpec
316         ])
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])
323     fi
324 ])
325
326
327
328 # AZ_PYTHON_PATH( )
329 # -----------------
330 # Look for Python and set the output variable 'PYTHON'
331 # to 'python' if found, empty otherwise.
332
333 AC_DEFUN([AZ_PYTHON_PATH],
334 [
335     AC_ARG_VAR( [PYTHON], [Python Executable Path] )
336     AC_PATH_PROG( PYTHON, python, [], $1 )
337     if test -z "$PYTHON"
338     then
339         AC_MSG_ERROR([Python Executable not found])
340     else
341         az_python_use=true
342     fi
343     AM_CONDITIONAL(PYTHON_USE, test "$az_python_use" = "true")
344 ])
345
346
347
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.
352
353 AC_DEFUN([AZ_PYTHON_PREFIX],
354 [
355     if test -z "$PYTHON"
356     then
357         AC_MSG_ERROR([Python Executable Path is not known])
358     fi
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])
368 ])
369
370 # AZ_PYTHON_RUN( PYTHON_PROGRAM )
371 # -----------------
372 # Run a Python Test Program saving its output
373 # in az_python_output and its condition code
374 # in az_python_cc.
375
376 AC_DEFUN([AZ_PYTHON_RUN],
377 [
378     AC_ARG_VAR( [PYTHON], [Python Executable Path] )
379     if test -z "$PYTHON"
380     then
381         AC_MSG_ERROR([Python Executable not found])
382     else
383         cat >conftest.py <<_ACEOF
384 $1
385 _ACEOF
386         az_python_output=`$PYTHON conftest.py`
387         az_python_cc=$?
388         rm conftest.py
389         if test -f "conftest.pyc"
390         then
391             rm conftest.pyc
392         fi
393     fi
394 ])
395
396
397
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).
406
407 AC_DEFUN([AZ_PYTHON_VERSION_CHECK],
408  [
409     AC_ARG_VAR( [PYTHON], [Python Executable Path] )
410     if test -n "$PYTHON"
411     then
412         AC_MSG_CHECKING([whether $PYTHON version >= $1])
413         AZ_PYTHON_RUN([
414 import sys, string
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]]
418 minverhex = 0
419 for i in xrange(0, 4): minverhex = (minverhex << 8) + minver[[i]]
420 if sys.hexversion >= minverhex:
421     sys.exit( 0 )
422 else:
423     sys.exit( 1 )
424         ])
425         if test $az_python_cc -eq 0
426         then
427             $2
428         m4_ifvaln(
429             [$3],
430             [else $3]
431         )
432         fi
433     fi
434 ])
435
436
437
438 # AZ_PYTHON_VERSION_ENSURE( VERSION )
439 # -----------------
440 # Insure that the Python Interpreter Version
441 # is greater than or equal to the VERSION
442 # parameter.
443
444 AC_DEFUN([AZ_PYTHON_VERSION_ENSURE],
445 [
446     AZ_PYTHON_VERSION_CHECK(
447         [$1],
448         [AC_MSG_RESULT(yes)],
449         [AC_MSG_ERROR(too old)]
450     )
451 ])
452
453
454
455 # AZ_PYTHON_WITH( [path] )
456 # -----------------------------------------------------------------
457 # Handles the various --with-python commands.
458 # Input:
459 #   $1 is the optional search path for the python executable if needed
460 # Ouput:
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
464 #   is true.
465 #
466 # Example:
467 #   AZ_PYTHON_WITH( )
468 #   or
469 #   AZ_PYTHON_WITH("/usr/bin")
470
471 AC_DEFUN([AZ_PYTHON_WITH],
472 [
473     AC_ARG_VAR([PYTHON],[Python Executable Path])
474
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.
478     if test -z "$PYTHON"
479     then
480         AC_MSG_CHECKING(for --with-python)
481         AC_ARG_WITH(
482             python,
483             AC_HELP_STRING([--with-python@<:@=PYTHON@:>@],
484                 [absolute path name of Python executable]
485             ),
486             [
487                 if test "$withval" = "yes"
488                 then
489                     # "yes" was specified, but we don't have a path
490                     # for the executable.
491                     # So, let's searth the PATH Environment Variable.
492                     AC_MSG_RESULT(yes)
493                     AC_PATH_PROG(
494                         [PYTHON],
495                         python,
496                         [],
497                         $1
498                     )
499                     if test -z "$PYTHON"
500                     then
501                         AC_MSG_ERROR(no path to python found)
502                     fi
503                     az_python_use=true
504                     AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")
505                     AZ_PYTHON_CSPEC()
506                     AZ_PYTHON_LSPEC()
507                     AZ_PYTHON_PREFIX()
508                 elif test "$withval" = "no"
509                 then
510                     AC_MSG_RESULT(no)
511                     az_python_use=false
512                     AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")
513                 else
514                     # $withval must be the executable path then.
515                     AC_SUBST([PYTHON], ["${withval}"])
516                     AC_MSG_RESULT($withval)
517                     az_python_use=true
518                     AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")
519                     AZ_PYTHON_CSPEC()
520                     AZ_PYTHON_LSPEC()
521                     AZ_PYTHON_PREFIX()
522                 fi
523             ],
524             [
525                 # --with-python was not specified.
526                 AC_MSG_RESULT(no)
527                 az_python_use=false
528                 AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")
529             ]
530         )
531     fi
532
533 ])