fix crashes reported by Debian Cylab Mayhem Team
[swftools.git] / m4 / python.m4
blob457f2a04d626f4d855dfe0ad3789095973d50b09
1 AC_DEFUN([RFX_CHECK_PYTHON],
3 AC_MSG_CHECKING([for Python version])
5 if test "x$PYTHON_LIB" '!=' "x" -a "x$PYTHON_INCLUDES" '!=' "x";then
6     # you can override the python detection by putting PYTHON_LIB
7     # and PYTHON_INCLUDES into the environment
8     case "$PYTHON_INCLUDES" in
9         *python2.4*) PY_VERSION=2.4
10                      ;;
11         *python2.5*) PY_VERSION=2.5
12                      ;;
13         *python2.6*) PY_VERSION=2.6
14                      ;;
15         *python2.7*) PY_VERSION=2.7
16                      ;;
17         *python3.0*) PY_VERSION=3.0
18                      ;;
19         *python3.1*) PY_VERSION=3.1
20                      ;;
21         *python3.2*) PY_VERSION=3.2
22                      ;;
23         *python3.3*) PY_VERSION=3.3
24                      ;;
25         *)           PY_VERSION=unknown
26                      ;;
27     esac
28 else
29     if test "x$PYTHON_LIB" '!=' "x" -o "x$PYTHON_INCLUDES" '!=' "x";then
30         echo "Set both PYTHON_LIB and PYTHON_INCLUDES, or none at all"
31     fi
32     # iterate through version 2.4 to 3.3
33     VERSIONS="2.4 2.5 2.6 2.7 3.0 3.1 3.2 3.3"
34     case "$PYTHON" in
35         2*) VERSIONS="2.4 2.5 2.6 2.7 $PYTHON"
36             ;;
37         3*) VERSIONS="3.0 3.1 3.2 3.3 $PYTHON"
38             ;;
39     esac
40     for v in $VERSIONS; do
41         # Linux
42         if test -f "/usr/include/python$v/Python.h";then
43             PY_VERSION=$v
44             PYTHON_LIB="-lpython$PY_VERSION"
45             if test -f "/usr/lib/python$v/site-packages/PIL/_imaging.so";then 
46                 PYTHON_LIB2="$PYTHON_LIB /usr/lib/python$v/site-packages/PIL/_imaging.so"
47                 HAVE_PYTHON_IMAGING_LIB=1
48             else
49                 PYTHON_LIB2="$PYTHON_LIB"
50             fi
51             PYTHON_INCLUDES="-I/usr/include/python$PY_VERSION"
52         # Mac OS X
53         elif test -f "/Library/Frameworks/Python.framework/Versions/$v/include/python$v/Python.h";then
54             PY_VERSION=$v
55             PYTHON_LIB="-framework Python" 
56             if test -f "/Library/Frameworks/Python.framework/Versions/$v/site-packages/PIL/_imaging.so";then
57                 PYTHON_LIB2="$PYTHON_LIB /Library/Python/$v/PIL/_imaging.so"
58                 HAVE_PYTHON_IMAGING_LIB=1
59             else
60                 PYTHON_LIB2="$PYTHON_LIB"
61             fi
62             PYTHON_INCLUDES="-I/Library/Frameworks/Python.framework/Versions/$v/include/python$v/"
63         # Mac OS X [Fink]:
64         elif test "(" -f "/sw/lib/python$v/config/libpython$v.dylib" \
65                    -o -f "/sw/lib/python$v/config/libpython$v.a" \
66                    -o -f "/sw/lib/python$v/config/libpython$v.so" \
67                   ")" \
68                -a -f "/sw/include/python$v/Python.h"; then
69             PY_VERSION=$v
70             PYTHON_LIB="-L /sw/lib/python$v/config/ -lpython$PY_VERSION /sw/lib/python$v/site-packages/PIL/_imaging.so"
71             if test -f "/sw/lib/python$v/site-packages/PIL/_imaging.so";then
72                 PYTHON_LIB2="$PYTHON_LIB /sw/lib/python$v/site-packages/PIL/_imaging.so"
73                 HAVE_PYTHON_IMAGING_LIB=1
74             else
75                 PYTHON_LIB2="$PYTHON_LIB"
76             fi
77             PYTHON_INCLUDES="-I /sw/include/python$v/"
78         fi
79     done
81 AC_MSG_RESULT($PY_VERSION)
82     
83 if test "x$PY_VERSION" "!=" "x"; then
84     AC_MSG_CHECKING([for Python executable])
85     if python$PY_VERSION -V 2>&5;then
86         PYTHON_EXECUTABLE=python$PY_VERSION
87         AC_SUBST(PYTHON_EXECUTABLE)
88         AC_MSG_RESULT([$PYTHON_EXECUTABLE])
89     elif python -V >&5 2>&5;then
90         if python -V 2>&1 | grep -q "\b$PY_VERSION"; then
91           PYTHON_EXECUTABLE=python
92           AC_SUBST(PYTHON_EXECUTABLE)
93           AC_MSG_RESULT([$PYTHON_EXECUTABLE])
94         else
95           AC_MSG_RESULT(["'python' executable is not version $PY_VERSION"])
96         fi
97     else
98         AC_MSG_RESULT([failed])
99     fi
102 if test "x$PY_VERSION" "!=" "x" -a "x$PYTHON_EXECUTABLE" "!=" "x"; then
103     export PYTHON_INCLUDES PYTHON_LIB
104     AC_SUBST(PYTHON_LIB)
105     AC_SUBST(PYTHON_INCLUDES)
106     AC_MSG_CHECKING([whether we can compile the Python test program])
107     
108     cat > conftest.c << EOF
109 #   include <Python.h>
111     int main()
112     {
113         int ret;
114         ret = Py_Main(0, 0);
115         int x; // check also for gcc 2.95.x incompatibilities
116         return ret;
117     }
119     
120     ac_link='$CC $CPPFLAGS $CFLAGS $PYTHON_INCLUDES conftest.c $LDFLAGS $PYTHON_LIB $LIBS -o conftest${ac_exeext}'
121     if { (eval echo python.m4: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
122         AC_MSG_RESULT(yes)
123         PYTHON_OK=yes
124     else
125         echo "configure: failed program was:" >&5
126         cat conftest.c >&5
127         AC_MSG_RESULT(no)
128     fi
129     rm -f conftest*
130     
131     AC_MSG_CHECKING([for Python install path])
132 cat > _pypath.py << EOF
133 import distutils
134 import distutils.sysconfig
135 import sys
136 sys.stdout.write(distutils.sysconfig.get_python_lib(plat_specific=0,standard_lib=0))
137 EOF 
138     echo $PYTHON_EXECUTABLE _pypath.py 1>&5
139     if $PYTHON_EXECUTABLE _pypath.py >_pypath.txt 2>&5;then
140         PYTHON_INSTALL_PATH=`cat _pypath.txt`
141         AC_SUBST(PYTHON_INSTALL_PATH)
142         AC_MSG_RESULT($PYTHON_INSTALL_PATH)
143     else
144         AC_MSG_RESULT([failed])
145     fi
146     #rm -f _pypath.txt _pypath.py
148     if test "x$PYTHON_OK" = "xyes";then
149         AC_MSG_CHECKING([for Python-Imaging])
150     cat > conftest.c << EOF
151 #   include <Python.h>
152 #   include <Imaging.h>
154     int main()
155     {
156         Py_Main(0, 0);
157         return 0;
158     }
160         if test "$HAVE_PYTHON_IMAGING_LIB"; then
161             ac_link='$CC $CPPFLAGS $CFLAGS $PYTHON_INCLUDES conftest.c $LDFLAGS ${PYTHON_LIB2} $LIBS -o conftest${ac_exeext}'
162             if { (eval echo python.m4: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
163                 PYTHON_LIB="${PYTHON_LIB2}"
164                 AC_DEFINE([HAVE_PYTHON_IMAGING], [1], [whether Python-Imaging was found])
165                 HAVE_PYTHON_IMAGING=yes
166                 export HAVE_PYTHON_IMAGING
167                 AC_SUBST(HAVE_PYTHON_IMAGING)
168                 AC_MSG_RESULT(yes)
169             else
170                 echo "configure: failed program was:" >&5
171                 cat conftest.c >&5
172                 AC_MSG_RESULT(no)
173             fi
174         else
175             echo "(didn't find the Python-Imaging libraries)" >&5
176             AC_MSG_RESULT(no)
177         fi
178     fi
179     rm -f conftest*