Move the sources to trunk
[opencv] / autotools / aclocal / az_python.m4
1 dnl @synopsis AZ_PYTHON_DEFAULT
2 dnl @synopsis AZ_PYTHON_ENABLE
3 dnl @synopsis AZ_PYTHON_WITH
4 dnl @synopsis AZ_PYTHON_PATH
5 dnl @synopsis AZ_PYTHON_VERSION_ENSURE( [2.2] )
6 dnl @synopsis AZ_PYTHON_CSPEC
7 dnl @synopsis AZ_PYTHON_LSPEC
8 dnl
9 dnl @summary New and revised Python support.
10 dnl
11 dnl This file provides autoconf support for those applications that
12 dnl want to embed python. It supports all pythons >= 2.2 which is the
13 dnl first official release containing distutils. Version 2.2 of python
14 dnl was released December 21, 2001. Since it actually executes the
15 dnl python, cross platform configuration will probably not work. Also,
16 dnl most of the platforms supported are consistent until you look into
17 dnl MacOSX. The python included with it is installed as a framework
18 dnl which is a very different environment to set up the normal tools
19 dnl such as gcc and libtool to deal with. Therefore, once we establish
20 dnl which python that we are going to use, we use its distutils to
21 dnl actually compile and link our modules or applications.
22 dnl
23 dnl At this time, it does NOT support linking with Python statically.
24 dnl It does support dynamic linking.
25 dnl
26 dnl This set of macros help define $PYTHON, $PYTHON_USE, $PYTHON_CSPEC
27 dnl and $PYTHON_LSPEC. $PYTHON defines the full executable path for the
28 dnl Python being linked to and is used within these macros to determine
29 dnl if that has been specified or found. These macros do execute this
30 dnl python version so it must be present on the system at configure
31 dnl time.
32 dnl
33 dnl $PYTHON_USE is an automake variable that defines whether Python
34 dnl support should be included or not in your application.
35 dnl $PYTHON_CSPEC is a variable that supplies additional CFLAGS for the
36 dnl compilation of the application/shared library. $PYTHON_LSPEC is a
37 dnl variable that supplies additional LDFLAGS for linking the
38 dnl application/shared library.
39 dnl
40 dnl The following is an example of how to set up for python usage
41 dnl within your application in your configure.in:
42 dnl
43 dnl   AZ_PYTHON_DEFAULT( )
44 dnl   AZ_PYTHON_ENABLE( )             # Optional
45 dnl   AZ_PYTHON_WITH( )               # Optional
46 dnl   AZ_PYTHON_PATH( )               # or AZ_PYTHON_INSIST( )
47 dnl   # if $PYTHON is not defined, then the following do nothing.
48 dnl   AZ_PYTHON_VERSION_ENSURE( [2.2] )
49 dnl   AZ_PYTHON_CSPEC
50 dnl   AZ_PYTHON_LSPEC
51 dnl
52 dnl The AZ_PYTHON_DEFAULT sets the $PYTHON_USE to false. Thereby,
53 dnl excluding it if it was optional.
54 dnl
55 dnl The AZ_PYTHON_ENABLE looks for the optional configure parameters of
56 dnl --enable-python/--disable-python and establishes the $PYTHON and
57 dnl $PYTHON_USE variables accordingly.
58 dnl
59 dnl The AZ_PYTHON_WITH looks for the optional configure parameters of
60 dnl --with-python/--without-python and establishes the $PYTHON and
61 dnl $PYTHON_USE variables accordingly.
62 dnl
63 dnl The AZ_PYTHON_PATH looks for python assuming that none has been
64 dnl previously found or defined and issues an error if it does not find
65 dnl it. If it does find it, it establishes the $PYTHON and $PYTHON_USE
66 dnl variables accordingly. AZ_PYTHON_INSIST could be used here instead
67 dnl if you want to insist that Python support be included using the
68 dnl --enable-python or --with-python checks previously done.
69 dnl
70 dnl The AZ_PYTHON_VERSION_ENSURE issues an error if the Python
71 dnl previously found is not of version 2.2 or greater.
72 dnl
73 dnl Once that these macros have be run, we can use PYTHON_USE within
74 dnl the makefile.am file to conditionally add the Python support such
75 dnl as:
76 dnl
77 dnl Makefile.am example showing optional inclusion of directories:
78 dnl
79 dnl  if PYTHON_USE
80 dnl  plugins = plugins
81 dnl  src = src
82 dnl  else
83 dnl  plugins =
84 dnl  src =
85 dnl  endif
86 dnl
87 dnl  SUBDIRS = . $(plugins) $(src)
88 dnl
89 dnl Makefile.am example showing optional shared library build:
90 dnl
91 dnl  if PYTHON_USE
92 dnl  lib_LTLIBRARIES        = libElemList.la
93 dnl  libElemList_la_SOURCES = libElemList.c
94 dnl  libElemList_la_CFLAGS  = @PYTHON_CSPEC@
95 dnl  libElemList_la_LDFLAGS = @PYTHON_LSPEC@
96 dnl  endif
97 dnl
98 dnl Makefile.am example showing optional program build:
99 dnl
100 dnl  if PYTHON_USE
101 dnl  bin_PROGRAMS    = runFunc
102 dnl  runFunc_SOURCES = runFunc.c
103 dnl  runFunc_CFLAGS  = @PYTHON_CSPEC@
104 dnl  runFunc_LDFLAGS = @PYTHON_LSPEC@
105 dnl  endif
106 dnl
107 dnl The above compiles the modules only if PYTHON_USE was specified as
108 dnl true. Also, the else portion of the if was optional.
109 dnl
110 dnl @category InstalledPackages
111 dnl @author Robert White <kranki@mac.com>
112 dnl @author Dustin Mitchell <dustin@cs.uchicago.edu>
113 dnl @version 2005-01-14
114 dnl @license GPLWithACException
115
116 # AZ_PYTHON_DEFAULT( )
117 # -----------------
118 # Sets the default to not include Python support.
119
120 AC_DEFUN([AZ_PYTHON_DEFAULT],
121 [
122     az_python_use=false
123     AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")
124 ])
125
126
127
128 # AZ_PYTHON_ENABLE( [path] )
129 # -----------------------------------------------------------------
130 # Handles the various --enable-python commands.
131 # Input:
132 #   $1 is the optional search path for the python executable if needed
133 # Ouput:
134 #   PYTHON_USE (AM_CONDITIONAL) is true if python executable found
135 #   and --enable-python was requested; otherwise false.
136 #   $PYTHON contains the full executable path to python if PYTHON_ENABLE_USE
137 #   is true.
138 #
139 # Example:
140 #   AZ_PYTHON_ENABLE( )
141 #   or
142 #   AZ_PYTHON_ENABLE( "/usr/bin" )
143
144 AC_DEFUN([AZ_PYTHON_ENABLE],
145 [
146     AC_ARG_VAR([PYTHON],[Python Executable Path])
147
148     # unless PYTHON was supplied to us (as a precious variable),
149     # see if --enable-python[=PythonExecutablePath], --enable-python,
150     # --disable-python or --enable-python=no was given.
151     if test -z "$PYTHON"
152     then
153         AC_MSG_CHECKING(for --enable-python)
154         AC_ARG_ENABLE(
155             python,
156             AC_HELP_STRING([--enable-python@<:@=PYTHON@:>@],
157                 [absolute path name of Python executable]
158             ),
159             [
160                 if test "$enableval" = "yes"
161                 then
162                     # "yes" was specified, but we don't have a path
163                     # for the executable.
164                     # So, let's searth the PATH Environment Variable.
165                     AC_MSG_RESULT(yes)
166                     AC_PATH_PROG(
167                         [PYTHON],
168                         python,
169                         [],
170                         $1
171                     )
172                     if test -z "$PYTHON"
173                     then
174                         AC_MSG_ERROR(no path to python found)
175                     fi
176                     az_python_use=true
177                     AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")
178                     AZ_PYTHON_PREFIX( )
179                 elif test "$enableval" = "no"
180                 then
181                     AC_MSG_RESULT(no)
182                     az_python_use=false
183                     AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")
184                 else
185                     # $enableval must be the executable path then.
186                     AC_SUBST([PYTHON], ["${enableval}"])
187                     AC_MSG_RESULT($withval)
188                     az_python_use=true
189                     AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")
190                     AZ_PYTHON_PREFIX( )
191                 fi
192             ],
193             [
194                 # --with-python was not specified.
195                 AC_MSG_RESULT(no)
196                 az_python_use=false
197                 AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")
198             ]
199         )
200     fi
201
202 ])
203
204
205
206 # AZ_PYTHON_CSPEC( )
207 # -----------------
208 # Set up the c compiler options to compile Python
209 # embedded programs/libraries in $PYTHON_CSPEC if
210 # $PYTHON has been defined.
211
212 AC_DEFUN([AZ_PYTHON_CSPEC],
213 [
214     AC_ARG_VAR( [PYTHON], [Python Executable Path] )
215     if test -n "$PYTHON"
216     then
217         az_python_prefix=`${PYTHON} -c "import sys; print sys.prefix"`
218         if test -z "$az_python_prefix"
219         then
220             AC_MSG_ERROR([Python Prefix is not known])
221         fi
222         az_python_execprefix=`${PYTHON} -c "import sys; print sys.exec_prefix"`
223         az_python_version=`$PYTHON -c "import sys; print sys.version[[:3]]"`
224         az_python_includespec="-I${az_python_prefix}/include/python${az_python_version}"
225         if test x"$python_prefix" != x"$python_execprefix"; then
226             az_python_execspec="-I${az_python_execprefix}/include/python${az_python_version}"
227             az_python_includespec="${az_python_includespec} $az_python_execspec"
228         fi
229         az_python_ccshared=`${PYTHON} -c "import distutils.sysconfig; print distutils.sysconfig.get_config_var('CFLAGSFORSHARED')"`
230         az_python_cspec="${az_python_ccshared} ${az_python_includespec}"
231         AC_SUBST([PYTHON_CSPEC], [${az_python_cspec}])
232         AC_MSG_NOTICE([PYTHON_CSPEC=${az_python_cspec}])
233     fi
234 ])
235
236
237
238 # AZ_PYTHON_INSIST( )
239 # -----------------
240 # Look for Python and set the output variable 'PYTHON'
241 # to 'python' if found, empty otherwise.
242
243 AC_DEFUN([AZ_PYTHON_PATH],
244 [
245     AC_ARG_VAR( [PYTHON], [Python Executable Path] )
246     if test -z "$PYTHON"
247     then
248         AC_MSG_ERROR([Python Executable not found])
249     fi
250 ])
251
252
253
254 # AZ_PYTHON_LSPEC( )
255 # -----------------
256 # Set up the linker options to link Python embedded
257 # programs/libraries in $PYTHON_LSPEC if $PYTHON
258 # has been defined.
259
260 AC_DEFUN([AZ_PYTHON_LSPEC],
261 [
262     AC_ARG_VAR( [PYTHON], [Python Executable Path] )
263     if test -n "$PYTHON"
264     then
265         AZ_PYTHON_RUN([
266 import sys
267 import distutils.sysconfig
268 strUseFrameWork = "--enable-framework"
269 dictConfig = distutils.sysconfig.get_config_vars( )
270 strConfigArgs = dictConfig.get("CONFIG_ARGS")
271 strLinkSpec =  dictConfig.get('LDFLAGS')
272 if -1 ==  strConfigArgs.find(strUseFrameWork):
273     strLibPL = dictConfig.get("LIBPL")
274     if strLibPL and (strLibPL != ""):
275         strLinkSpec += " -L%s" % (strLibPL)
276     strSys = dictConfig.get("SYSLIBS")
277     if strSys and (strSys != ""):
278         strLinkSpec += " %s" % (strSys)
279     strSHL = dictConfig.get("SHLIBS")
280     if strSHL and (strSHL != ""):
281         strLinkSpec += " %s" % (strSHL)
282     # Construct the Python Library Name.
283     strTmplte = " -lpython%d.%d"
284     if (sys.platform == "win32") or (sys.platform == "os2emx"):
285         strTmplte = " -lpython%d%d"
286     strWrk = strTmplte % ( (sys.hexversion >> 24),
287                             ((sys.hexversion >> 16) & 0xff))
288     strLinkSpec += strWrk
289 else:
290     # This is not ideal since it changes the search path
291     # for Frameworks which could have side-effects on
292     # other included Frameworks.  However, it is necessary
293     # where someone has installed more than one frameworked
294     # Python.  Frameworks are really only used in MacOSX.
295     strLibFW = dictConfig.get("PYTHONFRAMEWORKPREFIX")
296     if strLibFW and (strLibFW != ""):
297         strLinkSpec += " -F%s" % (strLibFW)
298     strLibPL = dictConfig.get("LIBPL")
299     if strLibPL and (strLibPL != ""):
300         strLinkSpec += " -L%s" % (strLibPL)
301     # Construct the Python Library Name.
302     strTmplte = " -lpython%d.%d"
303     if (sys.platform == "win32") or (sys.platform == "os2emx"):
304         strTmplte = " -lpython%d%d"
305     strWrk = strTmplte % ( (sys.hexversion >> 24),
306                             ((sys.hexversion >> 16) & 0xff))
307     strLinkSpec += strWrk
308 strLinkSpec += " %s" % (dictConfig.get('LINKFORSHARED'))
309 print strLinkSpec
310         ])
311         AC_SUBST([PYTHON_LSPEC], [${az_python_output}])
312         AC_MSG_NOTICE([PYTHON_LSPEC=${az_python_output}])
313     fi
314 ])
315
316
317
318 # AZ_PYTHON_PATH( )
319 # -----------------
320 # Look for Python and set the output variable 'PYTHON'
321 # to 'python' if found, empty otherwise.
322
323 AC_DEFUN([AZ_PYTHON_PATH],
324 [
325     AC_ARG_VAR( [PYTHON], [Python Executable Path] )
326     AC_PATH_PROG( PYTHON, python, [], $1 )
327     if test -z "$PYTHON"
328     then
329         AC_MSG_ERROR([Python Executable not found])
330     else
331         az_python_use=true
332     fi
333     AM_CONDITIONAL(PYTHON_USE, test "$az_python_use" = "true")
334 ])
335
336
337
338 # AZ_PYTHON_PREFIX( )
339 # -------------------
340 # Use the values of $prefix and $exec_prefix for the corresponding
341 # values of PYTHON_PREFIX and PYTHON_EXEC_PREFIX.
342
343 AC_DEFUN([AZ_PYTHON_PREFIX],
344 [
345     if test -z "$PYTHON"
346     then
347         AC_MSG_ERROR([Python Executable Path is not known])
348     fi
349     ax_python_prefix=`${PYTHON} -c "import sys; print sys.prefix"`
350     ax_python_execprefix=`${PYTHON} -c "import sys; print sys.exec_prefix"`
351     AC_SUBST([PYTHON_PREFIX], ["${ax_python_prefix}"])
352     AC_SUBST([PYTHON_EXECPREFIX], ["${ax_python_execprefix}"])
353 ])
354
355
356
357 # AZ_PYTHON_RUN( PYTHON_PROGRAM )
358 # -----------------
359 # Run a Python Test Program saving its output
360 # in az_python_output and its condition code
361 # in az_python_cc.
362
363 AC_DEFUN([AZ_PYTHON_RUN],
364 [
365     AC_ARG_VAR( [PYTHON], [Python Executable Path] )
366     if test -z "$PYTHON"
367     then
368         AC_MSG_ERROR([Python Executable not found])
369     else
370         cat >conftest.py <<_ACEOF
371 $1
372 _ACEOF
373         az_python_output=`$PYTHON conftest.py`
374         az_python_cc=$?
375         rm conftest.py
376         if test -f "conftest.pyc"
377         then
378             rm conftest.pyc
379         fi
380     fi
381 ])
382
383
384
385 # AZ_PYTHON_VERSION_CHECK( VERSION, [ACTION-IF-TRUE], [ACTION-IF-FALSE] )
386 # -----------------------------------------------------------------------------
387 # Run ACTION-IF-TRUE if the Python interpreter has version >= VERSION.
388 # Run ACTION-IF-FALSE otherwise.
389 # This test uses sys.hexversion instead of the string equivalant (first
390 # word of sys.version), in order to cope with versions such as 2.2c1.
391 # hexversion has been introduced in Python 1.5.2; it's probably not
392 # worth to support older versions (1.5.1 was released on October 31, 1998).
393
394 AC_DEFUN([AZ_PYTHON_VERSION_CHECK],
395  [
396     AC_ARG_VAR( [PYTHON], [Python Executable Path] )
397     if test -n "$PYTHON"
398     then
399         AC_MSG_CHECKING([whether $PYTHON version >= $1])
400         AZ_PYTHON_RUN([
401 import sys, string
402 # split strings by '.' and convert to numeric.  Append some zeros
403 # because we need at least 4 digits for the hex conversion.
404 minver = map(int, string.split('$1', '.')) + [[0, 0, 0]]
405 minverhex = 0
406 for i in xrange(0, 4): minverhex = (minverhex << 8) + minver[[i]]
407 if sys.hexversion >= minverhex:
408     sys.exit( 0 )
409 else:
410     sys.exit( 1 )
411         ])
412         if test $az_python_cc -eq 0
413         then
414             $2
415         m4_ifvaln(
416             [$3],
417             [else $3]
418         )
419         fi
420     fi
421 ])
422
423
424
425 # AZ_PYTHON_VERSION_ENSURE( VERSION )
426 # -----------------
427 # Insure that the Python Interpreter Version
428 # is greater than or equal to the VERSION
429 # parameter.
430
431 AC_DEFUN([AZ_PYTHON_VERSION_ENSURE],
432 [
433     AZ_PYTHON_VERSION_CHECK(
434         [$1],
435         [AC_MSG_RESULT(yes)],
436         [AC_MSG_ERROR(too old)]
437     )
438 ])
439
440
441
442 # AZ_PYTHON_WITH( [path] )
443 # -----------------------------------------------------------------
444 # Handles the various --with-python commands.
445 # Input:
446 #   $1 is the optional search path for the python executable if needed
447 # Ouput:
448 #   PYTHON_USE (AM_CONDITIONAL) is true if python executable found
449 #   and --with-python was requested; otherwise false.
450 #   $PYTHON contains the full executable path to python if PYTHON_USE
451 #   is true.
452 #
453 # Example:
454 #   AZ_PYTHON_WITH( )
455 #   or
456 #   AZ_PYTHON_WITH("/usr/bin")
457
458 AC_DEFUN([AZ_PYTHON_WITH],
459 [
460     AC_ARG_VAR([PYTHON],[Python Executable Path])
461
462     # unless PYTHON was supplied to us (as a precious variable),
463     # see if --with-python[=PythonExecutablePath], --with-python,
464     # --without-python or --with-python=no was given.
465     if test -z "$PYTHON"
466     then
467         AC_MSG_CHECKING(for --with-python)
468         AC_ARG_WITH(
469             python,
470             AC_HELP_STRING([--with-python@<:@=PYTHON@:>@],
471                 [absolute path name of Python executable]
472             ),
473             [
474                 if test "$withval" = "yes"
475                 then
476                     # "yes" was specified, but we don't have a path
477                     # for the executable.
478                     # So, let's searth the PATH Environment Variable.
479                     AC_MSG_RESULT(yes)
480                     AC_PATH_PROG(
481                         [PYTHON],
482                         python,
483                         [],
484                         $1
485                     )
486                     if test -z "$PYTHON"
487                     then
488                         AC_MSG_ERROR(no path to python found)
489                     fi
490                     az_python_use=true
491                     AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")
492                     AZ_PYTHON_PREFIX( )
493                 elif test "$withval" = "no"
494                 then
495                     AC_MSG_RESULT(no)
496                     az_python_use=false
497                     AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")
498                 else
499                     # $withval must be the executable path then.
500                     AC_SUBST([PYTHON], ["${withval}"])
501                     AC_MSG_RESULT($withval)
502                     az_python_use=true
503                     AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")
504                     AZ_PYTHON_PREFIX( )
505                 fi
506             ],
507             [
508                 # --with-python was not specified.
509                 AC_MSG_RESULT(no)
510                 az_python_use=false
511                 AM_CONDITIONAL(PYTHON_USE, test x"$az_python_use" = x"true")
512             ]
513         )
514     fi
515
516 ])