Imported Upstream version 3.2.2
[debian/gnuradio] / config / gr_python.m4
1 dnl
2 dnl Copyright 2003,2004,2005 Free Software Foundation, Inc.
3 dnl 
4 dnl This file is part of GNU Radio
5 dnl 
6 dnl GNU Radio is free software; you can redistribute it and/or modify
7 dnl it under the terms of the GNU General Public License as published by
8 dnl the Free Software Foundation; either version 3, or (at your option)
9 dnl any later version.
10 dnl 
11 dnl GNU Radio is distributed in the hope that it will be useful,
12 dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
13 dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 dnl GNU General Public License for more details.
15 dnl 
16 dnl You should have received a copy of the GNU General Public License
17 dnl along with GNU Radio; see the file COPYING.  If not, write to
18 dnl the Free Software Foundation, Inc., 51 Franklin Street,
19 dnl Boston, MA 02110-1301, USA.
20 dnl 
21
22 # PYTHON_DEVEL()
23 #
24 # Checks for Python and tries to get the include path to 'Python.h'.
25 # It sets the $(PYTHON_CPPFLAGS), $(PYTHON_LDFLAGS) and $(pythondir) output variables,
26 #
27 AC_DEFUN([PYTHON_DEVEL],[
28         AC_REQUIRE([AM_PATH_PYTHON])
29         AC_REQUIRE([AC_CANONICAL_HOST])
30
31         AC_ARG_WITH(pythondir,
32                     AC_HELP_STRING([--with-pythondir=DIR], 
33                        [python installation directory (cross-compiling) [[default=$prefix/lib/python2.5/site-packages]]]),
34                     [with_pythondir=${withval}],[with_pythondir=${prefix}/lib/python2.5/site-packages])
35
36         # if we're cross-compiling, asking the host python about any of
37         # this is completely useless...
38
39         if test x$cross_compiling != xno
40         then
41                 pythondir=$with_pythondir
42                 pyexecdir=$with_pythondir
43                 AC_SUBST(PYTHON_CPPFLAGS)
44                 AC_SUBST(PYTHON_LDFLAGS)
45         else
46
47             # For Fedora Core 5 and 6, see ticket:39 in Trac
48             if test -f '/etc/redhat-release'; then
49                     if  (echo $pyexecdir | grep -q lib64); then
50                             pythondir="$pyexecdir"
51                     fi
52             fi
53
54             # Check for Python include path
55             AC_MSG_CHECKING([for Python include path])
56             if test -z "$PYTHON" ; then
57                     AC_MSG_ERROR([cannot find Python path])
58             fi
59
60             # ask distutils which include path we should use
61             python_cmd='
62 import distutils.sysconfig
63 import os
64 path = distutils.sysconfig.get_python_inc(plat_specific=False)
65 if os.sep == "\\":
66   path = path.replace("\\", "/")
67 print path
68 '
69             python_path=`$PYTHON -c "$python_cmd"`
70             AC_MSG_RESULT([$python_path])
71             if test -z "$python_path" ; then
72                     AC_MSG_ERROR([cannot find Python include path])
73             fi
74
75             AC_SUBST(PYTHON_CPPFLAGS,[-I$python_path])
76
77             # Check for Python headers usability
78             python_save_CPPFLAGS=$CPPFLAGS
79             CPPFLAGS="$CPPFLAGS $PYTHON_CPPFLAGS"
80             AC_CHECK_HEADERS([Python.h], [],
81                             [AC_MSG_ERROR([cannot find usable Python headers])])
82             CPPFLAGS="$python_save_CPPFLAGS"
83
84             # Only set this on mingw and cygwin hosts, (only implemented
85             # for mingw host, for crosscompiling you need to trick this)
86
87             PYTHON_LDFLAGS=""
88             case $host_os in
89                  *mingw* | *cygwin* )
90               AC_MSG_CHECKING([for Python LDFLAGS])
91
92             python_cmd='
93 import distutils.sysconfig
94 import os
95 path = distutils.sysconfig.get_config_var("LIBPL")
96 if path == None:
97   path = distutils.sysconfig.PREFIX + "/libs"
98 if os.sep == "\\":
99   path = path.replace("\\", "/")
100 print path
101 '
102               python_stdlib_path=`$PYTHON -c "$python_cmd"`
103
104               python_version_nodot=`echo $PYTHON_VERSION | sed "s,\.,,"`
105               libpython_name="python$PYTHON_VERSION"
106
107               # Standard install of python for win32 has libpython24.a
108               # instead of libpython2.4.a so we check for the library
109               # without the dot in the version number.
110
111               python_stdlib_filename=`find $python_stdlib_path -type f -name libpython$python_version_nodot.* -print | sed "1q"`
112               if test -n "$python_stdlib_filename" ; then
113                     libpython_name="python$python_version_nodot"
114               fi
115
116               PYTHON_LDFLAGS="-L$python_stdlib_path -l$libpython_name"
117               AC_MSG_RESULT($PYTHON_LDFLAGS) 
118               # Replace all backslashes in PYTHON Paths with forward slashes
119               pythondir=`echo $pythondir |sed 's,\\\\,/,g'`
120               pkgpythondir=`echo $pkgpythondir |sed 's,\\\\,/,g'`
121               pyexecdir=`echo $pyexecdir |sed 's,\\\\,/,g'`
122               pkgpyexecdir=`echo $pkgpyexecdir |sed 's,\\\\,/,g'`
123               ;;
124             esac
125
126             AC_SUBST(PYTHON_LDFLAGS)
127         fi
128 ])
129
130 # PYTHON_CHECK_MODULE
131 #
132 # Determines if a particular Python module can be imported
133 #
134 # $1 - module name
135 # $2 - module description
136 # $3 - action if found
137 # $4 - action if not found
138 # $5 - test command
139
140 AC_DEFUN([PYTHON_CHECK_MODULE],[
141     AC_MSG_CHECKING([for $2])
142     dnl ########################################
143     dnl # import and test checking
144     dnl ########################################
145     if test "$5"; then
146         python_cmd='
147 try:
148     import $1
149     assert $5
150 except: exit(1)'
151     dnl ########################################
152     dnl # import checking only
153     dnl ########################################
154     else
155         python_cmd='
156 try: import $1
157 except: exit(1)'
158     fi
159     if ! $PYTHON -c "$python_cmd" 2> /dev/null; then
160         AC_MSG_RESULT([no])
161         $4
162     else
163         AC_MSG_RESULT([yes])
164         $3
165     fi
166 ])