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