Backport rlm_python from v3.1.x Closes #1589, Closes #1015, Closes #408
[freeradius.git] / src / modules / rlm_python / configure.ac
1 AC_PREREQ([2.53])
2 AC_INIT(rlm_python.c)
3 AC_REVISION($Revision$)
4 AC_DEFUN(modname,[rlm_python])
5
6 if test x$with_[]modname != xno; then
7
8         AC_PROG_CC
9         AC_PROG_CPP
10
11         dnl extra argument: --with-rlm-python-bin
12         PYHTON_BIN=
13         AC_ARG_WITH(rlm-python-bin,
14         [  --with-rlm-python-bin=PATH   Path to python binary []],
15         [ case "$withval" in
16             no)
17                 AC_MSG_ERROR(Need rlm-python-bin)
18                 ;;
19             yes)
20                 ;;
21             *)
22                 PYTHON_BIN="$withval"
23                 ;;
24           esac ]
25         )
26
27         if test "x$PYTHON_BIN" = x; then
28                 AC_CHECK_PROGS(PYTHON_BIN, [ python2.7 python2.6 python ], not-found, [${PATH}:/usr/bin:/usr/local/bin])
29         fi
30
31         if test "x$PYTHON_BIN" = "xnot-found"; then
32                 fail="python-binary"
33         fi
34
35         dnl extra argument: --with-rlm-python-lib-dir
36         PY_LIB_DIR=
37         AC_ARG_WITH(rlm-python-lib-dir,
38         [  --with-rlm-python-lib-dir=DIR       Directory for Python library files []],
39         [ case "$withval" in
40             no)
41                 AC_MSG_ERROR(Need rlm-python-lib-dir)
42                 ;;
43             yes)
44                 ;;
45             *)
46                 PY_LIB_DIR="$withval"
47                 ;;
48           esac ]
49         )
50
51         dnl extra argument: --with-rlm-python-include-dir
52         PY_INC_DIR=
53         AC_ARG_WITH(rlm-python-include-dir,
54         [  --with-rlm-python-include-dir=DIR   Directory for Python include files []],
55         [ case "$withval" in
56             no)
57                 AC_MSG_ERROR(Need rlm-python-include-dir)
58                 ;;
59             yes)
60                 ;;
61             *)
62                 PY_INC_DIR="$withval"
63                 ;;
64           esac ]
65         )
66
67         if test x$fail = x; then
68                 PY_PREFIX=`${PYTHON_BIN} -c 'import sys ; print(sys.prefix)'`
69                 AC_MSG_NOTICE([Python sys.prefix \"${PY_PREFIX}\"])
70
71                 PY_EXEC_PREFIX=`${PYTHON_BIN} -c 'import sys ; print(sys.exec_prefix)'`
72                 AC_MSG_NOTICE([Python sys.exec_prefix \"${PY_EXEC_PREFIX}\"])
73
74                 PY_SYS_VERSION=`${PYTHON_BIN} -c 'import sys ; print(sys.version[[0:3]])'`
75                 AC_MSG_NOTICE([Python sys.version \"${PY_SYS_VERSION}\"])
76
77                 PY_LIB_DIR="$PY_EXEC_PREFIX/lib/python${PY_SYS_VERSION}/config"
78                 PY_LIB_LOC="-L$PY_EXEC_PREFIX/lib/python${PY_SYS_VERSION}/config"
79
80                 PY_MAKEFILE="$PY_EXEC_PREFIX/lib/python${PY_SYS_VERSION}/config/Makefile"
81                 if test -f ${PY_MAKEFILE}; then
82                         PY_LOCAL_MOD_LIBS=`sed -n -e 's/^LOCALMODLIBS=\(.*\)/\1/p' $PY_MAKEFILE | sed -e 's/[[[:blank:]]]/ /g;s/^ *//;s/ *$//'`
83                         AC_MSG_NOTICE([Python local_mod_libs \"${PY_LOCAL_MOD_LIBS}\"])
84
85                         PY_BASE_MOD_LIBS=`sed -n -e 's/^BASEMODLIBS=\(.*\)/\1/p' $PY_MAKEFILE | sed -e 's/[[[:blank:]]]/ /g;s/^ *//;s/ *$//'`
86                         AC_MSG_NOTICE([Python base_mod_libs \"${PY_BASE_MOD_LIBS}\"])
87
88                         PY_OTHER_LIBS=`sed -n -e 's/^LIBS=\(.*\)/\1/p' $PY_MAKEFILE | sed -e 's/[[[:blank:]]]/ /g;s/  / /g;s/^ *//;s/ *$//'`
89                         PY_OTHER_LDFLAGS=`sed -n -e 's/^LINKFORSHARED=\(.*\)/\1/p' $PY_MAKEFILE | sed -e 's/[[[:blank:]]]/ /g;s/  / /g;s/^ *//;s/ *$//'`
90                         AC_MSG_NOTICE([Python other_libs \"${PY_OTHER_LDFLAGS} ${PY_OTHER_LIBS}\"])
91                 fi
92                 PY_EXTRA_LIBS="$PY_LOCALMODLIBS $PY_BASE_MOD_LIBS $PY_OTHER_LIBS"
93
94                 old_CFLAGS=$CFLAGS
95                 CFLAGS="$CFLAGS $PY_CFLAGS"
96                 smart_try_dir="$PY_PREFIX/include/python$PY_SYS_VERSION"
97                 FR_SMART_CHECK_INCLUDE(Python.h)
98                 CFLAGS=$old_CFLAGS
99
100                 if test "x$ac_cv_header_Python_h" = "xyes"; then
101                         mod_cflags="$SMART_CPPFLAGS"
102                 else
103                         fail="$fail Python.h"
104                         targetname=
105                 fi
106
107                 old_LIBS=$LIBS
108                 LIBS="$LIBS $PY_LIB_LOC $PY_EXTRA_LIBS -lm"
109                 smart_try_dir=$PY_LIB_DIR
110                 FR_SMART_CHECK_LIB(python${PY_SYS_VERSION}, Py_Initialize)
111                 LIBS=$old_LIBS
112
113                 eval t=\${ac_cv_lib_${sm_lib_safe}_${sm_func_safe}}
114                 if test "x$t" = "xyes"; then
115                         mod_ldflags="$PY_LIB_LOC $PY_EXTRA_LIBS $SMART_LIBS -lm"
116                         targetname=modname
117                 else
118                         FR_SMART_CHECK_LIB(python${PY_SYS_VERSION}m, Py_Initialize)
119                         eval t=\${ac_cv_lib_${sm_lib_safe}_${sm_func_safe}}
120                         if test "x$t" = "xyes"; then
121                                 mod_ldflags="$PY_LIB_LOC $PY_EXTRA_LIBS $SMART_LIBS -lm"
122                                 targetname=modname
123                         else
124                                 targetname=
125                                 fail="$fail libpython$PY_SYS_VERSION"
126                         fi
127                 fi
128         fi
129 else
130         targetname=
131         echo \*\*\* module modname is disabled.
132 fi
133
134 if test x"$fail" != x""; then
135         if test x"${enable_strict_dependencies}" = x"yes"; then
136                 AC_MSG_ERROR([set --without-]modname[ to disable it explicitly.])
137         else
138                 AC_MSG_WARN([silently not building ]modname[.])
139                 AC_MSG_WARN([FAILURE: ]modname[ requires: $fail.]);
140                 targetname=""
141         fi
142 fi
143
144 AC_SUBST(mod_ldflags)
145 AC_SUBST(mod_cflags)
146 AC_SUBST(targetname)
147 AC_OUTPUT(all.mk)