Initialize project
[guile-gdal] / m4 / m4_ax_lib_gdal.m4
1 # ===========================================================================
2 #       https://www.gnu.org/software/autoconf-archive/ax_lib_gdal.html
3 # ===========================================================================
4 #
5 # SYNOPSIS
6 #
7 #   AX_LIB_GDAL([MINIMUM-VERSION])
8 #
9 # DESCRIPTION
10 #
11 #   This macro provides tests of availability of GDAL/OGR library of
12 #   particular version or newer.
13 #
14 #   AX_LIB_GDAL macro takes only one argument which is optional. If there is
15 #   no required version passed, then macro does not run version test.
16 #
17 #   The --with-gdal option takes complete path to gdal-config utility,
18 #
19 #   This macro calls AC_SUBST for:
20 #
21 #     GDAL_VERSION
22 #     GDAL_CFLAGS
23 #     GDAL_LDFLAGS
24 #     GDAL_DEP_LDFLAGS
25 #     GDAL_OGR_ENABLED
26 #
27 #   and AC_DEFINE for:
28 #
29 #     HAVE_GDAL
30 #     HAVE_GDAL_OGR
31 #
32 # LICENSE
33 #
34 #   Copyright (c) 2011 Mateusz Loskot <mateusz@loskot.net>
35 #   Copyright (c) 2011 Alessandro Candini <candini@meeo.it>
36 #
37 #   Copying and distribution of this file, with or without modification, are
38 #   permitted in any medium without royalty provided the copyright notice
39 #   and this notice are preserved. This file is offered as-is, without any
40 #   warranty.
41
42 #serial 5
43
44 AC_DEFUN([AX_LIB_GDAL],
45 [
46     dnl If gdal-config path is not given in ---with-gdal option,
47     dnl check if it is present in the system anyway
48     AC_ARG_WITH([gdal],
49         AS_HELP_STRING([--with-gdal=@<:@ARG@:>@],
50             [Specify full path to gdal-config script]),
51         [ac_gdal_config_path=$withval],
52         [gdal_config_system=check])
53
54     dnl if gdal-config is present in the system, fill the ac_gdal_config_path variable with it full path
55     AS_IF([test "x$gdal_config_system" = xcheck],
56           [AC_PATH_PROG([GDAL_CONFIG], [gdal-config])],
57           [AC_PATH_PROG([GDAL_CONFIG], [gdal-config],
58               [no], [`dirname $ac_gdal_config_path 2> /dev/null`])]
59     )
60
61     if test ! -x "$GDAL_CONFIG"; then
62         AC_MSG_ERROR([gdal-config does not exist or it is not an executable file])
63             GDAL_CONFIG="no"
64             found_gdal="no"
65     fi
66
67     GDAL_VERSION=""
68     GDAL_CFLAGS=""
69     GDAL_LDFLAGS=""
70     GDAL_DEP_LDFLAGS=""
71     GDAL_OGR_ENABLED=""
72
73
74     dnl
75     dnl Check GDAL library (libgdal)
76     dnl
77
78     if test "$GDAL_CONFIG" != "no"; then
79         AC_MSG_CHECKING([for GDAL library])
80
81         GDAL_VERSION="`$GDAL_CONFIG --version`"
82         GDAL_CFLAGS="`$GDAL_CONFIG --cflags`"
83         GDAL_LDFLAGS="`$GDAL_CONFIG --libs`"
84         GDAL_DEP_LDFLAGS="`$GDAL_CONFIG --dep-libs`"
85
86         AC_DEFINE([HAVE_GDAL], [1], [Define to 1 if GDAL library are available])
87
88         found_gdal="yes"
89     else
90         found_gdal="no"
91     fi
92
93     AC_MSG_RESULT([$found_gdal])
94
95     if test "$found_gdal" = "yes"; then
96         AC_MSG_CHECKING([for OGR support in GDAL library])
97
98         GDAL_OGR_ENABLED="`$GDAL_CONFIG --ogr-enabled`"
99         AC_DEFINE([HAVE_GDAL_OGR], [1], [Define to 1 if GDAL library includes OGR support])
100
101         AC_MSG_RESULT([$GDAL_OGR_ENABLED])
102     fi
103
104     dnl
105     dnl Check if required version of GDAL is available
106     dnl
107
108     gdal_version_req=ifelse([$1], [], [], [$1])
109     if test "$found_gdal" = "yes" -a -n "$gdal_version_req"; then
110
111         AC_MSG_CHECKING([if GDAL version is >= $gdal_version_req])
112
113         dnl Decompose required version string of GDAL
114         dnl and calculate its number representation
115         gdal_version_req_major=`expr $gdal_version_req : '\([[0-9]]*\)'`
116         gdal_version_req_minor=`expr $gdal_version_req : '[[0-9]]*\.\([[0-9]]*\)'`
117         gdal_version_req_micro=`expr $gdal_version_req : '[[0-9]]*\.[[0-9]]*\.\([[0-9]]*\)'`
118         if test "x$gdal_version_req_micro" = "x"; then
119             gdal_version_req_micro="0"
120         fi
121
122         gdal_version_req_number=`expr $gdal_version_req_major \* 1000000 \
123                                    \+ $gdal_version_req_minor \* 1000 \
124                                    \+ $gdal_version_req_micro`
125
126         dnl Decompose version string of installed GDAL
127         dnl and calculate its number representation
128         gdal_version_major=`expr $GDAL_VERSION : '\([[0-9]]*\)'`
129         gdal_version_minor=`expr $GDAL_VERSION : '[[0-9]]*\.\([[0-9]]*\)'`
130         gdal_version_micro=`expr $GDAL_VERSION : '[[0-9]]*\.[[0-9]]*\.\([[0-9]]*\)'`
131         if test "x$gdal_version_micro" = "x"; then
132             gdal_version_micro="0"
133         fi
134
135         gdal_version_number=`expr $gdal_version_major \* 1000000 \
136                                    \+ $gdal_version_minor \* 1000 \
137                                    \+ $gdal_version_micro`
138
139         gdal_version_check=`expr $gdal_version_number \>\= $gdal_version_req_number`
140         if test "$gdal_version_check" = "1"; then
141             AC_MSG_RESULT([yes])
142         else
143             AC_MSG_RESULT([no])
144             AC_MSG_ERROR([GDAL $GDAL_VERSION found, but required version is $gdal_version_req])
145         fi
146     fi
147
148     AC_SUBST(GDAL_VERSION)
149     AC_SUBST(GDAL_CFLAGS)
150     AC_SUBST(GDAL_LDFLAGS)
151     AC_SUBST(GDAL_DEP_LDFLAGS)
152     AC_SUBST(GDAL_OGR_ENABLED)
153 ])