1##### http://autoconf-archive.cryp.to/ax_lib_mysql.html
2#
3# SYNOPSIS
4#
5#   AX_LIB_MYSQL([MINIMUM-VERSION])
6#
7# DESCRIPTION
8#
9#   This macro provides tests of availability of MySQL client library
10#   of particular version or newer.
11#
12#   AX_LIB_MYSQL macro takes only one argument which is optional. If
13#   there is no required version passed, then macro does not run
14#   version test.
15#
16#   The --with-mysql option takes one of three possible values:
17#
18#   no - do not check for MySQL client library
19#
20#   yes - do check for MySQL library in standard locations
21#   (mysql_config should be in the PATH)
22#
23#   path - complete path to mysql_config utility, use this option if
24#   mysql_config can't be found in the PATH
25#
26#   This macro calls:
27#
28#     AC_SUBST(MYSQL_CFLAGS)
29#     AC_SUBST(MYSQL_LDFLAGS)
30#     AC_SUBST(MYSQL_LIBS)
31#     AC_SUBST(MYSQL_VERSION)
32#
33#   And sets:
34#
35#     HAVE_MYSQL
36#
37# LAST MODIFICATION
38#
39#   2006-07-16
40#
41# COPYLEFT
42#
43#   Copyright (c) 2006 Mateusz Loskot <mateusz@loskot.net>
44#
45#   Copying and distribution of this file, with or without
46#   modification, are permitted in any medium without royalty provided
47#   the copyright notice and this notice are preserved.
48
49AC_DEFUN([AX_LIB_MYSQL],
50[
51    MYSQL_CONFIG="no"
52
53    AC_ARG_WITH([mysql],
54        AC_HELP_STRING([--with-mysql@<:@=ARG@:>@],
55            [use MySQL client library @<:@default=no@:>@, optionally specify path to mysql_config]
56        ),
57        [
58        if test "$withval" = "no"; then
59            want_mysql="no"
60        elif test "$withval" = "yes"; then
61            want_mysql="yes"
62        else
63            want_mysql="yes"
64            MYSQL_CONFIG="$withval"
65        fi
66        ],
67        [want_mysql="no"]
68    )
69
70    MYSQL_CFLAGS=""
71    MYSQL_LDFLAGS=""
72    MYSQL_LIBS=""
73    MYSQL_VERSION=""
74
75    dnl
76    dnl Check MySQL libraries
77    dnl
78
79    if test "$want_mysql" = "yes"; then
80
81        AC_PATH_PROGS(MYSQL_CONFIG, mysql_config mariadb_config)
82
83        if test -x "$MYSQL_CONFIG"; then
84            MYSQL_CFLAGS="`$MYSQL_CONFIG --cflags`"
85            _full_libmysql_libs="`$MYSQL_CONFIG --libs`"
86
87             _save_mysql_ldflags="${LDFLAGS}"
88            _save_mysql_cflags="${CFLAGS}"
89            LDFLAGS="${LDFLAGS} ${_full_libmysql_libs}"
90            CFLAGS="${CFLAGS} ${MYSQL_CFLAGS}"
91
92            for i in $_full_libmysql_libs; do
93                case $i in
94                    -lmysqlclient|-lperconaserverclient|-lmariadbclient|-lmariadb)
95
96                        _lib_name="`echo "$i" | cut -b3-`"
97                        AC_CHECK_LIB($_lib_name, main, [
98                        	MYSQL_LIBS="-l${_lib_name} ${MYSQL_LIBS}"
99                        	],[
100                        	AC_MSG_ERROR([Not found $_lib_name library])
101                        	])
102                ;;
103                    -L*)
104
105                        MYSQL_LDFLAGS="${MYSQL_LDFLAGS} $i"
106                ;;
107                    -l*)
108
109                        _lib_name="`echo "$i" | cut -b3-`"
110                        AC_CHECK_LIB($_lib_name, main, [
111                        	MYSQL_LIBS="${MYSQL_LIBS} ${i}"
112                        	],[
113                        	AC_MSG_ERROR([Not found $i library])
114                        	])
115                ;;
116                esac
117            done
118
119            LDFLAGS="${_save_mysql_ldflags}"
120            CFLAGS="${_save_mysql_cflags}"
121            unset _save_mysql_ldflags
122            unset _save_mysql_cflags
123
124            MYSQL_VERSION=`$MYSQL_CONFIG --version`
125
126            AC_DEFINE([HAVE_MYSQL], [1],
127                [Define to 1 if MySQL libraries are available])
128
129            found_mysql="yes"
130        else
131            found_mysql="no"
132        fi
133    fi
134
135    dnl
136    dnl Check if required version of MySQL is available
137    dnl
138
139    mysql_version_req=ifelse([$1], [], [], [$1])
140
141    if test "$found_mysql" = "yes" -a -n "$mysql_version_req"; then
142
143        AC_MSG_CHECKING([if MySQL version is >= $mysql_version_req])
144
145        dnl Decompose required version string of MySQL
146        dnl and calculate its number representation
147        mysql_version_req_major=`expr $mysql_version_req : '\([[0-9]]*\)'`
148        mysql_version_req_minor=`expr $mysql_version_req : '[[0-9]]*\.\([[0-9]]*\)'`
149        mysql_version_req_micro=`expr $mysql_version_req : '[[0-9]]*\.[[0-9]]*\.\([[0-9]]*\)'`
150        if test "x$mysql_version_req_micro" = "x"; then
151            mysql_version_req_micro="0"
152        fi
153
154        mysql_version_req_number=`expr $mysql_version_req_major \* 1000000 \
155                                   \+ $mysql_version_req_minor \* 1000 \
156                                   \+ $mysql_version_req_micro`
157
158        dnl Decompose version string of installed MySQL
159        dnl and calculate its number representation
160        mysql_version_major=`expr $MYSQL_VERSION : '\([[0-9]]*\)'`
161        mysql_version_minor=`expr $MYSQL_VERSION : '[[0-9]]*\.\([[0-9]]*\)'`
162        mysql_version_micro=`expr $MYSQL_VERSION : '[[0-9]]*\.[[0-9]]*\.\([[0-9]]*\)'`
163        if test "x$mysql_version_micro" = "x"; then
164            mysql_version_micro="0"
165        fi
166
167        mysql_version_number=`expr $mysql_version_major \* 1000000 \
168                                   \+ $mysql_version_minor \* 1000 \
169                                   \+ $mysql_version_micro`
170
171        mysql_version_check=`expr $mysql_version_number \>\= $mysql_version_req_number`
172        if test "$mysql_version_check" = "1"; then
173            AC_MSG_RESULT([yes])
174        else
175            AC_MSG_RESULT([no])
176        fi
177    fi
178
179    AC_SUBST([MYSQL_VERSION])
180    AC_SUBST([MYSQL_CFLAGS])
181    AC_SUBST([MYSQL_LDFLAGS])
182    AC_SUBST([MYSQL_LIBS])
183])
184