ax_lib_postgresql.m4 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. # ===========================================================================
  2. # https://www.gnu.org/software/autoconf-archive/ax_lib_postgresql.html
  3. # ===========================================================================
  4. #
  5. # SYNOPSIS
  6. #
  7. # AX_LIB_POSTGRESQL([MINIMUM-VERSION],[ACTION-IF-FOUND],[ACTION-IF-NOT-FOUND])
  8. #
  9. # DESCRIPTION
  10. #
  11. # This macro provides tests of availability of PostgreSQL 'libpq' library
  12. # of particular version or newer.
  13. #
  14. # AX_LIB_POSTGRESQL macro takes only one argument which is optional. If
  15. # there is no required version passed, then macro does not run version
  16. # test.
  17. #
  18. # The --with-postgresql option takes one of three possible values:
  19. #
  20. # no - do not check for PostgreSQL client library
  21. #
  22. # yes - do check for PostgreSQL library in standard locations (pg_config
  23. # should be in the PATH)
  24. #
  25. # path - complete path to pg_config utility, use this option if pg_config
  26. # can't be found in the PATH (You could set also PG_CONFIG variable)
  27. #
  28. # This macro calls:
  29. #
  30. # AC_SUBST(POSTGRESQL_CPPFLAGS)
  31. # AC_SUBST(POSTGRESQL_LDFLAGS)
  32. # AC_SUBST(POSTGRESQL_LIBS)
  33. # AC_SUBST(POSTGRESQL_VERSION)
  34. #
  35. # And sets:
  36. #
  37. # HAVE_POSTGRESQL
  38. #
  39. # It execute if found ACTION-IF-FOUND (empty by default) and
  40. # ACTION-IF-NOT-FOUND (AC_MSG_FAILURE by default) if not found.
  41. #
  42. # LICENSE
  43. #
  44. # Copyright (c) 2008 Mateusz Loskot <mateusz@loskot.net>
  45. # Copyright (c) 2014 Sree Harsha Totakura <sreeharsha@totakura.in>
  46. # Copyright (c) 2018 Bastien Roucaries <rouca@debian.org>
  47. #
  48. # Copying and distribution of this file, with or without modification, are
  49. # permitted in any medium without royalty provided the copyright notice
  50. # and this notice are preserved. This file is offered as-is, without any
  51. # warranty.
  52. #serial 22
  53. AC_DEFUN([_AX_LIB_POSTGRESQL_OLD],[
  54. found_postgresql="no"
  55. _AX_LIB_POSTGRESQL_OLD_fail="no"
  56. while true; do
  57. AC_CACHE_CHECK([for the pg_config program], [ac_cv_path_PG_CONFIG],
  58. [AC_PATH_PROGS_FEATURE_CHECK([PG_CONFIG], [pg_config],
  59. [[ac_cv_path_PG_CONFIG="";$ac_path_PG_CONFIG --includedir > /dev/null \
  60. && ac_cv_path_PG_CONFIG=$ac_path_PG_CONFIG ac_path_PG_CONFIG_found=:]],
  61. [ac_cv_path_PG_CONFIG=""])])
  62. PG_CONFIG=$ac_cv_path_PG_CONFIG
  63. AS_IF([test "X$PG_CONFIG" = "X"],[break])
  64. AC_CACHE_CHECK([for the PostgreSQL libraries CPPFLAGS],[ac_cv_POSTGRESQL_CPPFLAGS],
  65. [ac_cv_POSTGRESQL_CPPFLAGS="-I`$PG_CONFIG --includedir`" || _AX_LIB_POSTGRESQL_OLD_fail=yes])
  66. AS_IF([test "X$_AX_LIB_POSTGRESQL_OLD_fail" = "Xyes"],[break])
  67. POSTGRESQL_CPPFLAGS="$ac_cv_POSTGRESQL_CPPFLAGS"
  68. AC_CACHE_CHECK([for the PostgreSQL libraries LDFLAGS],[ac_cv_POSTGRESQL_LDFLAGS],
  69. [ac_cv_POSTGRESQL_LDFLAGS="-L`$PG_CONFIG --libdir`" || _AX_LIB_POSTGRESQL_OLD_fail=yes])
  70. AS_IF([test "X$_AX_LIB_POSTGRESQL_OLD_fail" = "Xyes"],[break])
  71. POSTGRESQL_LDFLAGS="$ac_cv_POSTGRESQL_LDFLAGS"
  72. AC_CACHE_CHECK([for the PostgreSQL libraries LIBS],[ac_cv_POSTGRESQL_LIBS],
  73. [ac_cv_POSTGRESQL_LIBS="-lpq"])
  74. POSTGRESQL_LIBS="$ac_cv_POSTGRESQL_LIBS"
  75. AC_CACHE_CHECK([for the PostgreSQL version],[ac_cv_POSTGRESQL_VERSION],
  76. [
  77. ac_cv_POSTGRESQL_VERSION=`$PG_CONFIG --version | sed "s/^PostgreSQL[[[:space:]]][[[:space:]]]*\([[0-9.]][[0-9.]]*\).*/\1/"` \
  78. || _AX_LIB_POSTGRESQL_OLD_fail=yes
  79. ])
  80. AS_IF([test "X$_AX_LIB_POSTGRESQL_OLD_fail" = "Xyes"],[break])
  81. POSTGRESQL_VERSION="$ac_cv_POSTGRESQL_VERSION"
  82. dnl
  83. dnl Check if required version of PostgreSQL is available
  84. dnl
  85. AS_IF([test X"$postgresql_version_req" != "X"],[
  86. AC_MSG_CHECKING([if PostgreSQL version $POSTGRESQL_VERSION is >= $postgresql_version_req])
  87. AX_COMPARE_VERSION([$POSTGRESQL_VERSION],[ge],[$postgresql_version_req],
  88. [found_postgresql_req_version=yes],[found_postgresql_req_version=no])
  89. AC_MSG_RESULT([$found_postgresql_req_version])
  90. ])
  91. AS_IF([test "Xfound_postgresql_req_version" = "Xno"],[break])
  92. found_postgresql="yes"
  93. break
  94. done
  95. ])
  96. AC_DEFUN([_AX_LIB_POSTGRESQL_PKG_CONFIG],
  97. [
  98. AC_REQUIRE([PKG_PROG_PKG_CONFIG])
  99. found_postgresql=no
  100. while true; do
  101. PKG_PROG_PKG_CONFIG
  102. AS_IF([test X$PKG_CONFIG = X],[break])
  103. _AX_LIB_POSTGRESQL_PKG_CONFIG_fail=no;
  104. AS_IF([test "X$postgresql_version_req" = "X"],
  105. [PKG_CHECK_EXISTS([libpq],[found_postgresql_pkg_config=yes],[found_postgresql=no])],
  106. [PKG_CHECK_EXISTS([libpq >= "$postgresql_version_req"],
  107. [found_postgresql=yes],[found_postgresql=no])])
  108. AS_IF([test "X$found_postgresql" = "no"],[break])
  109. AC_CACHE_CHECK([for the PostgreSQL libraries CPPFLAGS],[ac_cv_POSTGRESQL_CPPFLAGS],
  110. [ac_cv_POSTGRESQL_CPPFLAGS="`$PKG_CONFIG libpq --cflags-only-I`" || _AX_LIB_POSTGRESQL_PKG_CONFIG_fail=yes])
  111. AS_IF([test "X$_AX_LIB_POSTGRESQL_PKG_CONFIG_fail" = "Xyes"],[break])
  112. POSTGRESQL_CPPFLAGS="$ac_cv_POSTGRESQL_CPPFLAGS"
  113. AC_CACHE_CHECK([for the PostgreSQL libraries LDFLAGS],[ac_cv_POSTGRESQL_LDFLAGS],
  114. [ac_cv_POSTGRESQL_LDFLAGS="`$PKG_CONFIG libpq --libs-only-L --libs-only-other`" || _AX_LIB_POSTGRESQL_PKG_CONFIG_fail=yes])
  115. AS_IF([test "X$_AX_LIB_POSTGRESQL_PKG_CONFIG_fail" = "Xyes"],[break])
  116. POSTGRESQL_LDFLAGS="$ac_cv_POSTGRESQL_LDFLAGS"
  117. AC_CACHE_CHECK([for the PostgreSQL libraries LIBS],[ac_cv_POSTGRESQL_LIBS],
  118. [ac_cv_POSTGRESQL_LIBS="`$PKG_CONFIG libpq --libs-only-l`" || _AX_LIB_POSTGRESQL_PKG_CONFIG_fail=ye])
  119. AS_IF([test "X$_AX_LIB_POSTGRESQL_PKG_CONFIG_fail" = "Xyes"],[break])
  120. POSTGRESQL_LIBS="$ac_cv_POSTGRESQL_LIBS"
  121. dnl already checked by exist but need to be recovered
  122. AC_CACHE_CHECK([for the PostgreSQL version],[ac_cv_POSTGRESQL_VERSION],
  123. [ac_cv_POSTGRESQL_VERSION="`$PKG_CONFIG libpq --modversion`" || _AX_LIB_POSTGRESQL_PKG_CONFIG_fail=yes])
  124. AS_IF([test "X$_AX_LIB_POSTGRESQL_PKG_CONFIG_fail" = "Xyes"],[break])
  125. POSTGRESQL_VERSION="$ac_cv_POSTGRESQL_VERSION"
  126. found_postgresql=yes
  127. break;
  128. done
  129. ])
  130. AC_DEFUN([AX_LIB_POSTGRESQL],
  131. [
  132. AC_ARG_WITH([postgresql],
  133. AS_HELP_STRING([--with-postgresql=@<:@ARG@:>@],
  134. [use PostgreSQL library @<:@default=yes@:>@, optionally specify path to pg_config]
  135. ),
  136. [
  137. AS_CASE([$withval],
  138. [[[nN]][[oO]]],[want_postgresql="no"],
  139. [[[yY]][[eE]][[sS]]],[want_postgresql="yes"],
  140. [
  141. want_postgresql="yes"
  142. PG_CONFIG="$withval"
  143. ])
  144. ],
  145. [want_postgresql="yes"]
  146. )
  147. AC_ARG_VAR([POSTGRESQL_CPPFLAGS],[cpp flags for PostgreSQL overriding detected flags])
  148. AC_ARG_VAR([POSTGRESQL_LIBFLAGS],[libs for PostgreSQL overriding detected flags])
  149. AC_ARG_VAR([POSTGRESQL_LDFLAGS],[linker flags for PostgreSQL overriding detected flags])
  150. # populate cache
  151. AS_IF([test "X$POSTGRESQL_CPPFLAGS" != X],[ac_cv_POSTGRESQL_CPPFLAGS="$POSTGRESQL_CPPFLAGS"])
  152. AS_IF([test "X$POSTGRESQL_LDFLAGS" != X],[ac_cv_POSTGRESQL_LDFLAGS="$POSTGRESQL_LDFLAGS"])
  153. AS_IF([test "X$POSTGRESQL_LIBS" != X],[ac_cv_POSTGRESQL_LIBS="$POSTGRESQL_LIBS"])
  154. postgresql_version_req=ifelse([$1], [], [], [$1])
  155. found_postgresql="no"
  156. POSTGRESQL_VERSION=""
  157. dnl
  158. dnl Check PostgreSQL libraries (libpq)
  159. dnl
  160. AS_IF([test X"$want_postgresql" = "Xyes"],[
  161. _AX_LIB_POSTGRESQL_PKG_CONFIG
  162. AS_IF([test X"$found_postgresql" = "Xno"],
  163. [_AX_LIB_POSTGRESQL_OLD])
  164. AS_IF([test X"$found_postgresql" = Xyes],[
  165. _AX_LIB_POSTGRESQL_OLD_CPPFLAGS="$CPPFLAGS"
  166. CPPFLAGS="$CPPFLAGS $POSTGRESQL_CPPFLAGS"
  167. _AX_LIB_POSTGRESQL_OLD_LDFLAGS="$LDFLAGS"
  168. LDFLAGS="$LDFLAGS $POSTGRESQL_LDFLAGS"
  169. _AX_LIB_POSTGRESQL_OLD_LIBS="$LIBS"
  170. LIBS="$LIBS $POSTGRESQL_LIBS"
  171. while true; do
  172. dnl try to compile
  173. AC_CHECK_HEADER([libpq-fe.h],[],[found_postgresql=no])
  174. AS_IF([test "X$found_postgresql" = "Xno"],[break])
  175. dnl try now to link
  176. AC_CACHE_CHECK([for the PostgreSQL library linking is working],[ac_cv_postgresql_found],
  177. [
  178. AC_LINK_IFELSE([
  179. AC_LANG_PROGRAM(
  180. [
  181. #include <libpq-fe.h>
  182. ],
  183. [[
  184. char conninfo[]="dbname = postgres";
  185. PGconn *conn;
  186. conn = PQconnectdb(conninfo);
  187. ]]
  188. )
  189. ],[ac_cv_postgresql_found=yes],
  190. [ac_cv_postgresql_found=no])
  191. ])
  192. found_postgresql="$ac_cv_postgresql_found"
  193. AS_IF([test "X$found_postgresql" = "Xno"],[break])
  194. break
  195. done
  196. CPPFLAGS="$_AX_LIB_POSTGRESQL_OLD_CPPFLAGS"
  197. LDFLAGS="$_AX_LIB_POSTGRESQL_OLD_LDFLAGS"
  198. LIBS="$_AX_LIB_POSTGRESQL_OLD_LIBS"
  199. ])
  200. AS_IF([test "x$found_postgresql" = "xyes"],[
  201. AC_DEFINE([HAVE_POSTGRESQL], [1],
  202. [Define to 1 if PostgreSQL libraries are available])])
  203. ])
  204. AC_SUBST([POSTGRESQL_VERSION])
  205. AC_SUBST([POSTGRESQL_CPPFLAGS])
  206. AC_SUBST([POSTGRESQL_LDFLAGS])
  207. AC_SUBST([POSTGRESQL_LIBS])
  208. AS_IF([test "x$found_postgresql" = "xyes"],
  209. [ifelse([$2], , :, [$2])],
  210. [ifelse([$3], , AS_IF([test X"$want_postgresql" = "Xyes"],[AC_MSG_ERROR([Library requirements (PostgreSQL) not met.])],[:]), [$3])])
  211. ])