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([LIBMYSQL_OPTIONS_TRY],
50[
51	AC_MSG_CHECKING([for MySQL init options function])
52	AC_TRY_LINK(
53[
54#include <mysql.h>
55],
56[
57	MYSQL		*mysql;
58
59	mysql_options(mysql, MYSQL_INIT_COMMAND, "set @@session.auto_increment_offset=1");
60],
61	AC_DEFINE_UNQUOTED([MYSQL_OPTIONS], [mysql_options], [Define mysql options])
62	AC_DEFINE_UNQUOTED([MYSQL_OPTIONS_ARGS_VOID_CAST], [], [Define void cast for mysql options args])
63	found_mysql_options="yes"
64	AC_MSG_RESULT(yes),
65	AC_MSG_RESULT(no))
66])
67
68AC_DEFUN([LIBMARIADB_OPTIONS_TRY],
69[
70	AC_MSG_CHECKING([for MariaDB init options function])
71	AC_TRY_LINK(
72[
73#include <mysql.h>
74],
75[
76	MYSQL	*mysql;
77
78	mysql_optionsv(mysql, MYSQL_INIT_COMMAND, (void *)"set @@session.auto_increment_offset=1");
79],
80	AC_DEFINE_UNQUOTED([MYSQL_OPTIONS], [mysql_optionsv], [Define mysql options])
81	AC_DEFINE_UNQUOTED([MYSQL_OPTIONS_ARGS_VOID_CAST], [(void *)], [Define void cast for mysql options args])
82	found_mariadb_options="yes"
83	AC_MSG_RESULT(yes),
84	AC_MSG_RESULT(no))
85])
86
87AC_DEFUN([LIBMYSQL_TLS_TRY_LINK],
88[
89	AC_MSG_CHECKING([for TLS support in MySQL library])
90	AC_TRY_LINK(
91[
92#include <mysql.h>
93],
94[
95	unsigned int	mysql_tls_mode;
96	MYSQL		*mysql;
97
98	mysql_tls_mode = SSL_MODE_REQUIRED;
99	mysql_options(mysql, MYSQL_OPT_SSL_MODE, &mysql_tls_mode);
100
101	mysql_tls_mode = SSL_MODE_VERIFY_CA;
102	mysql_options(mysql, MYSQL_OPT_SSL_MODE, &mysql_tls_mode);
103
104	mysql_tls_mode = SSL_MODE_VERIFY_IDENTITY;
105	mysql_options(mysql, MYSQL_OPT_SSL_MODE, &mysql_tls_mode);
106
107	mysql_options(mysql, MYSQL_OPT_SSL_CA, "");
108	mysql_options(mysql, MYSQL_OPT_SSL_KEY, "");
109	mysql_options(mysql, MYSQL_OPT_SSL_CERT, "");
110	mysql_options(mysql, MYSQL_OPT_SSL_CIPHER, "");
111],
112	AC_DEFINE([HAVE_MYSQL_TLS], [1], [Define to 1 if TLS is supported in MySQL library])
113	found_mysql_tls="yes"
114	AC_MSG_RESULT(yes),
115	AC_MSG_RESULT(no))
116])
117
118AC_DEFUN([LIBMYSQL_TLS_CIPHERS_TRY_LINK],
119[
120	AC_MSG_CHECKING([for TLS ciphersuites in MySQL library])
121	AC_TRY_LINK(
122[
123#include <mysql.h>
124],
125[
126	MYSQL	*mysql;
127
128	mysql_options(mysql, MYSQL_OPT_TLS_CIPHERSUITES, "");
129],
130	AC_DEFINE([HAVE_MYSQL_TLS_CIPHERSUITES], [1], [Define to 1 if TLS ciphersuites are supported in MySQL library])
131	AC_MSG_RESULT(yes),
132	AC_MSG_RESULT(no))
133])
134
135AC_DEFUN([LIBMARIADB_TLS_TRY_LINK],
136[
137	AC_MSG_CHECKING([for TLS support in MariaDB library])
138	AC_TRY_LINK(
139[
140#include <mysql.h>
141],
142[
143	MYSQL	*mysql;
144
145	mysql_optionsv(mysql, MYSQL_OPT_SSL_ENFORCE, (void *)"");
146	mysql_optionsv(mysql, MYSQL_OPT_SSL_VERIFY_SERVER_CERT, (void *)"");
147	mysql_optionsv(mysql, MYSQL_OPT_SSL_CA, (void *)"");
148	mysql_optionsv(mysql, MYSQL_OPT_SSL_KEY, (void *)"");
149	mysql_optionsv(mysql, MYSQL_OPT_SSL_CERT, (void *)"");
150	mysql_optionsv(mysql, MYSQL_OPT_SSL_CIPHER, (void *)"");
151],
152	AC_DEFINE([HAVE_MARIADB_TLS], [1], [Define to 1 if TLS is supported in MariaDB library])
153	found_mariadb_tls="yes"
154	AC_MSG_RESULT(yes),
155	AC_MSG_RESULT(no))
156])
157
158AC_DEFUN([AX_LIB_MYSQL],
159[
160    MYSQL_CONFIG="no"
161
162    AC_ARG_WITH([mysql],
163        AC_HELP_STRING([--with-mysql@<:@=ARG@:>@],
164            [use MySQL client library @<:@default=no@:>@, optionally specify path to mysql_config]
165        ),
166        [
167        if test "$withval" = "no"; then
168            want_mysql="no"
169        elif test "$withval" = "yes"; then
170            want_mysql="yes"
171        else
172            want_mysql="yes"
173            MYSQL_CONFIG="$withval"
174        fi
175        ],
176        [want_mysql="no"]
177    )
178
179    MYSQL_CFLAGS=""
180    MYSQL_LDFLAGS=""
181    MYSQL_LIBS=""
182    MYSQL_VERSION=""
183
184    dnl
185    dnl Check MySQL libraries
186    dnl
187
188    if test "$want_mysql" = "yes"; then
189
190        AC_PATH_PROGS(MYSQL_CONFIG, mysql_config mariadb_config)
191
192        if test -x "$MYSQL_CONFIG"; then
193            MYSQL_CFLAGS="`$MYSQL_CONFIG --cflags`"
194            _full_libmysql_libs="`$MYSQL_CONFIG --libs`"
195
196            _save_mysql_ldflags="${LDFLAGS}"
197            _save_mysql_cflags="${CFLAGS}"
198            _save_mysql_libs="${LIBS}"
199            LDFLAGS="${LDFLAGS} ${_full_libmysql_libs}"
200            CFLAGS="${CFLAGS} ${MYSQL_CFLAGS}"
201
202            for i in $_full_libmysql_libs; do
203                case $i in
204                    -lmysqlclient|-lperconaserverclient|-lmariadbclient|-lmariadb)
205
206                        _lib_name="`echo "$i" | cut -b3-`"
207                        AC_CHECK_LIB($_lib_name, main, [
208                        	MYSQL_LIBS="-l${_lib_name} ${MYSQL_LIBS}"
209                        	],[
210                        	AC_MSG_ERROR([Not found $_lib_name library])
211                        	])
212                ;;
213                    -L*)
214
215                        MYSQL_LDFLAGS="${MYSQL_LDFLAGS} $i"
216                ;;
217                    -R*)
218
219                        MYSQL_LDFLAGS="${MYSQL_LDFLAGS} -Wl,$i"
220                ;;
221                    -l*)
222
223                        _lib_name="`echo "$i" | cut -b3-`"
224                        AC_CHECK_LIB($_lib_name, main, [
225                        	MYSQL_LIBS="${MYSQL_LIBS} ${i}"
226                        	],[
227                        	AC_MSG_ERROR([Not found $i library])
228                        	])
229                ;;
230                esac
231            done
232            LDFLAGS="${_save_mysql_ldflags}"
233            CFLAGS="${_save_mysql_cflags}"
234
235            CFLAGS="${CFLAGS} ${MYSQL_CFLAGS}"
236            LDFLAGS="${LDFLAGS} ${MYSQL_LDFLAGS}"
237            LIBS="${LIBS} ${MYSQL_LIBS}"
238            LIBMYSQL_TLS_TRY_LINK([no])
239            if test "$found_mysql_tls" == "yes"; then
240                LIBMYSQL_TLS_CIPHERS_TRY_LINK([no])
241            else
242                LIBMARIADB_TLS_TRY_LINK([no])
243            fi
244
245            LIBMARIADB_OPTIONS_TRY([no])
246            if test "$found_mariadb_options" != "yes"; then
247                LIBMYSQL_OPTIONS_TRY([no])
248                if test "$found_mysql_options" != "yes"; then
249                    AC_MSG_RESULT([no])
250                    AC_MSG_ERROR([Could not find the options function for mysql init])
251                fi
252            fi
253
254            LDFLAGS="${_save_mysql_ldflags}"
255            CFLAGS="${_save_mysql_cflags}"
256            LIBS="${_save_mysql_libs}"
257            unset _save_mysql_ldflags
258            unset _save_mysql_cflags
259            MYSQL_VERSION=`$MYSQL_CONFIG --version`
260
261            AC_DEFINE([HAVE_MYSQL], [1],
262                [Define to 1 if MySQL libraries are available])
263
264            found_mysql="yes"
265        else
266            found_mysql="no"
267        fi
268    fi
269
270    dnl
271    dnl Check if required version of MySQL is available
272    dnl
273
274    mysql_version_req=ifelse([$1], [], [], [$1])
275
276    if test "$found_mysql" = "yes" -a -n "$mysql_version_req"; then
277
278        AC_MSG_CHECKING([if MySQL version is >= $mysql_version_req])
279
280        dnl Decompose required version string of MySQL
281        dnl and calculate its number representation
282        mysql_version_req_major=`expr $mysql_version_req : '\([[0-9]]*\)'`
283        mysql_version_req_minor=`expr $mysql_version_req : '[[0-9]]*\.\([[0-9]]*\)'`
284        mysql_version_req_micro=`expr $mysql_version_req : '[[0-9]]*\.[[0-9]]*\.\([[0-9]]*\)'`
285        if test "x$mysql_version_req_micro" = "x"; then
286            mysql_version_req_micro="0"
287        fi
288
289        mysql_version_req_number=`expr $mysql_version_req_major \* 1000000 \
290                                   \+ $mysql_version_req_minor \* 1000 \
291                                   \+ $mysql_version_req_micro`
292
293        dnl Decompose version string of installed MySQL
294        dnl and calculate its number representation
295        mysql_version_major=`expr $MYSQL_VERSION : '\([[0-9]]*\)'`
296        mysql_version_minor=`expr $MYSQL_VERSION : '[[0-9]]*\.\([[0-9]]*\)'`
297        mysql_version_micro=`expr $MYSQL_VERSION : '[[0-9]]*\.[[0-9]]*\.\([[0-9]]*\)'`
298        if test "x$mysql_version_micro" = "x"; then
299            mysql_version_micro="0"
300        fi
301
302        mysql_version_number=`expr $mysql_version_major \* 1000000 \
303                                   \+ $mysql_version_minor \* 1000 \
304                                   \+ $mysql_version_micro`
305
306        mysql_version_check=`expr $mysql_version_number \>\= $mysql_version_req_number`
307        if test "$mysql_version_check" = "1"; then
308            AC_MSG_RESULT([yes])
309        else
310            AC_MSG_RESULT([no])
311        fi
312    fi
313
314    AC_SUBST([MYSQL_VERSION])
315    AC_SUBST([MYSQL_CFLAGS])
316    AC_SUBST([MYSQL_LDFLAGS])
317    AC_SUBST([MYSQL_LIBS])
318])
319