o removed waywars #endif
[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=${prefix}/python${az_python_version}/site-packages
365     AC_SUBST([PYTHON_EXECDIR])
366     AC_MSG_RESULT([$PYTHON_EXECDIR])
367 ])
368
369 # AZ_PYTHON_RUN( PYTHON_PROGRAM )
370 # -----------------
371 # Run a Python Test Program saving its output
372 # in az_python_output and its condition code
373 # in az_python_cc.
374
375 AC_DEFUN([AZ_PYTHON_RUN],
376 [
377     AC_ARG_VAR( [PYTHON], [Python Executable Path] )
378     if test -z "$PYTHON"
379     then
380         AC_MSG_ERROR([Python Executable not found])
381     else
382         cat >conftest.py <<_ACEOF
383 $1
384 _ACEOF
385         az_python_output=`$PYTHON conftest.py`
386         az_python_cc=$?
387         rm conftest.py
388         if test -f "conftest.pyc"
389         then
390             rm conftest.pyc
391         fi
392     fi
393 ])
394
395
396
397 # AZ_PYTHON_VERSION_CHECK( VERSION, [ACTION-IF-TRUE], [ACTION-IF-FALSE] )
398 # -----------------------------------------------------------------------------
399 # Run ACTION-IF-TRUE if the Python interpreter has version >= VERSION.
400 # Run ACTION-IF-FALSE otherwise.
401 # This test uses sys.hexversion instead of the string equivalant (first
402 # word of sys.version), in order to cope with versions such as 2.2c1.
403 # hexversion has been introduced in Python 1.5.2; it's probably not
404 # worth to support older versions (1.5.1 was released on October 31, 1998).
405
406 AC_DEFUN([AZ_PYTHON_VERSION_CHECK],
407  [
408     AC_ARG_VAR( [PYTHON], [Python Executable Path] )
409     if test -n "$PYTHON"
410     then
411         AC_MSG_CHECKING([whether $PYTHON version >= $1])
412         AZ_PYTHON_RUN([
413 import sys, string
414 # split strings by '.' and convert to numeric.  Append some zeros
415 # because we need at least 4 digits for the hex conversion.
416 minver = map(int, string.split('$1', '.')) + [[0, 0, 0]]
417 minverhex = 0
418 for i in xrange(0, 4): minverhex = (minverhex << 8) + minver[[i]]
419 if sys.hexversion >= minverhex:
420     sys.exit( 0 )
421 else:
422     sys.exit( 1 )
423         ])
424         if test $az_python_cc -eq 0
425         then
426             $2
427         m4_ifvaln(
428             [$3],
429             [else $3]
430         )
431         fi
432     fi
433 ])
434
435
436
437 # AZ_PYTHON_VERSION_ENSURE( VERSION )
438 # -----------------
439 # Insure that the Python Interpreter Version
440 # is greater than or equal to the VERSION
441 # parameter.
442
443 AC_DEFUN([AZ_PYTHON_VERSION_ENSURE],
444 [
445     AZ_PYTHON_VERSION_CHECK(
446         [$1],
447         [AC_MSG_RESULT(yes)],
448         [AC_MSG_ERROR(too old)]
449     )
450 ])
451
452
453
454 # AZ_PYTHON_WITH( [path] )
455 # -----------------------------------------------------------------
456 # Handles the various --with-python commands.
457 # Input:
458 #   $1 is the optional search path for the python executable if needed
459 # Ouput:
460 #   PYTHON_USE (AM_CONDITIONAL) is true if python executable found
461 #   and --with-python was requested; otherwise false.
462 #   $PYTHON contains the full executable path to python if PYTHON_USE
463 #   is true.
464 #
465 # Example:
466 #   AZ_PYTHON_WITH( )
467 #   or
468 #   AZ_PYTHON_WITH("/usr/bin")
469
470 AC_DEFUN([AZ_PYTHON_WITH],
471 [
472     AC_ARG_VAR([PYTHON],[Python Executable Path])
473
474     # unless PYTHON was supplied to us (as a precious variable),
475     # see if --with-python[=PythonExecutablePath], --with-python,
476     # --without-python or --with-python=no was given.
477     if test -z "$PYTHON"
478     then
479         AC_MSG_CHECKING(for --with-python)
480         AC_ARG_WITH(
481             python,
482             AC_HELP_STRING([--with-python@<:@=PYTHON@:>@],
483                 [absolute path name of Python executable]
484             ),
485             [
486                 if test "$withval" = "yes"
487                 then
488                     # "yes" was specified, but we don't have a path
489                     # for the executable.
490                     # So, let's searth the PATH Environment Variable.
491                     AC_MSG_RESULT(yes)
492                     AC_PATH_PROG(
493                         [PYTHON],
494                         python,
495                         [],
496                         $1
497                     )
498                     if test -z "$PYTHON"
499                     then
500                         AC_MSG_ERROR(no path to python found)
501                     fi
502                     az_python_use=true
503                     AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")
504                     AZ_PYTHON_CSPEC()
505                     AZ_PYTHON_LSPEC()
506                     AZ_PYTHON_PREFIX()
507                 elif test "$withval" = "no"
508                 then
509                     AC_MSG_RESULT(no)
510                     az_python_use=false
511                     AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")
512                 else
513                     # $withval must be the executable path then.
514                     AC_SUBST([PYTHON], ["${withval}"])
515                     AC_MSG_RESULT($withval)
516                     az_python_use=true
517                     AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")
518                     AZ_PYTHON_CSPEC()
519                     AZ_PYTHON_LSPEC()
520                     AZ_PYTHON_PREFIX()
521                 fi
522             ],
523             [
524                 # --with-python was not specified.
525                 AC_MSG_RESULT(no)
526                 az_python_use=false
527                 AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")
528             ]
529         )
530     fi
531
532 ])