autobuild: Simplify find_git_root.
[sfrench/samba-autobuild/.git] / m4 / check_python.m4
1 dnl Autoconf macros for finding a Python development environment
2 dnl
3 dnl Copyright (C) 2007-2008 Jelmer Vernooij <jelmer@samba.org>
4 dnl Published under the GNU GPL, v3 or later
5 dnl
6 AC_ARG_VAR([PYTHON_VER],[The installed Python
7         version to use, for example '2.3'. This string 
8         will be appended to the Python interpreter
9         canonical name.])
10
11 AC_DEFUN([TRY_LINK_PYTHON],
12 [
13         if test $working_python = no; then
14                 ac_save_LIBS="$LIBS"
15                 ac_save_CFLAGS="$CFLAGS"
16                 LIBS="$LIBS $1"
17                 CFLAGS="$CFLAGS $2"
18
19                 AC_TRY_LINK([
20                                 #undef HAVE_UINTPTR_T
21                                 /* we have our own configure tests */
22                                 #include <Python.h>
23                         ],[
24                                 Py_InitModule(NULL, NULL);
25                         ],[
26                                 PYTHON_LDFLAGS="$1"
27                                 PYTHON_CFLAGS="$2"
28                                 working_python=yes
29                         ])
30                 LIBS="$ac_save_LIBS"
31                 CFLAGS="$ac_save_CFLAGS"
32         fi
33 ])
34
35 dnl Try to find a Python implementation including header files
36 dnl AC_SAMBA_PYTHON_DEVEL(RUN-IF-FOUND, RUN-IF-NOT-FOUND)
37 dnl
38 dnl Will set the following variables:
39 dnl $PYTHON
40 dnl $PYTHON_CONFIG (if found)
41 dnl $PYTHON_CFLAGS
42 dnl $PYTHON_LDFLAGS
43 AC_DEFUN([AC_SAMBA_PYTHON_DEVEL],
44 [
45         if test -z "$PYTHON_VER"; then
46                 AC_PATH_PROGS([PYTHON], [python2.6 python2.5 python2.4 python])
47         else
48                 AC_PATH_PROG([PYTHON],[python[$PYTHON_VER]])
49         fi
50         if test -z "$PYTHON"; then
51                 working_python=no
52                 AC_MSG_WARN([No python found])
53         fi
54
55         dnl assume no working python
56         working_python=no
57
58         if test -z "$PYTHON_VER"; then 
59                 AC_PATH_PROGS([PYTHON_CONFIG], [python2.6-config python2.5-config python2.4-config python-config])
60         else 
61                 AC_PATH_PROG([PYTHON_CONFIG], [python[$PYTHON_VER]-config])
62         fi
63
64         if test -z "$PYTHON_CONFIG"; then
65                 AC_MSG_WARN([No python-config found])
66         else
67                 TRY_LINK_PYTHON([`$PYTHON_CONFIG --ldflags`], [`$PYTHON_CONFIG --includes`])
68                 TRY_LINK_PYTHON([`$PYTHON_CONFIG --ldflags`], [`$PYTHON_CONFIG --cflags`])
69                 if test x$working_python = xno; then
70                         # It seems the library path isn't included on some systems
71                         base=`$PYTHON_CONFIG --prefix`
72                         TRY_LINK_PYTHON([`echo -n -L${base}/lib " "; $PYTHON_CONFIG --ldflags`], [`$PYTHON_CONFIG --includes`])
73                         TRY_LINK_PYTHON([`echo -n -L${base}/lib " "; $PYTHON_CONFIG --ldflags`], [`$PYTHON_CONFIG --cflags`])
74                 fi
75         fi
76
77         if test x$PYTHON != x
78         then
79                 if `$PYTHON -c "import sys; sys.exit(sys.version_info.__getslice__(0, 2) >= (2, 4))"`
80                 then
81                         AC_MSG_ERROR([Python ($PYTHON) is too old. At least version 2.4 is required])
82                 fi
83
84                 DISTUTILS_CFLAGS=`$PYTHON -c "from distutils import sysconfig; \
85                                               print '-I%s -I%s %s' % ( \
86                                                         sysconfig.get_python_inc(), \
87                                                         sysconfig.get_python_inc(plat_specific=1), \
88                                                         sysconfig.get_config_var('CFLAGS'))"`
89                 DISTUTILS_LDFLAGS=`$PYTHON -c "from distutils import sysconfig; \
90                                                print '%s %s -lpython%s -L%s %s -L%s' % ( \
91                                                         sysconfig.get_config_var('LIBS'), \
92                                                         sysconfig.get_config_var('SYSLIBS'), \
93                                                         sysconfig.get_config_var('VERSION'), \
94                                                         sysconfig.get_config_var('LIBDIR'), \
95                                                         sysconfig.get_config_var('LDFLAGS'), \
96                                                         sysconfig.get_config_var('LIBPL'))"`
97                 TRY_LINK_PYTHON($DISTUTILS_LDFLAGS, $DISTUTILS_CFLAGS)
98         fi
99
100         AC_MSG_CHECKING(working python module support)
101         if test $working_python = yes; then
102                 AC_MSG_RESULT([yes])
103                 $1
104         else
105                 AC_MSG_RESULT([no])
106                 $2
107         fi
108 ])
109
110