a83a6d0da97d18602905809b6d3c5515d79ecd9d
[libdcp.git] / asdcplib / m4 / ax_lib_xerces.m4
1 # ===========================================================================
2 #             http://autoconf-archive.cryp.to/ax_lib_xerces.html
3 # ===========================================================================
4 #
5 # SYNOPSIS
6 #
7 #   AX_LIB_XERCES([MINIMUM-VERSION])
8 #
9 # DESCRIPTION
10 #
11 #   This macro provides tests of availability of Apache Xerces C++ Parser of
12 #   particular version or newer. This macros checks for Apache Xerces C++
13 #   Parser headers and libraries and defines compilation flags
14 #
15 #   Macro supports following options and their values:
16 #
17 #   1) Single-option usage:
18 #
19 #     --with-xerces - yes, no or path to Xerces installation prefix
20 #
21 #   2) Three-options usage (all options are required):
22 #
23 #     --with-xerces=yes
24 #     --with-xerces-inc - path to base directory with Xerces headers
25 #     --with-xerces-lib - linker flags for Xerces
26 #
27 #   This macro calls:
28 #
29 #     AC_SUBST(XERCES_CFLAGS)
30 #     AC_SUBST(XERCES_LDFLAGS)
31 #     AC_SUBST(XERCES_VERSION) - only if version requirement is used
32 #
33 #   And sets:
34 #
35 #     HAVE_XERCES
36 #
37 # LAST MODIFICATION
38 #
39 #   2008-04-12
40 #
41 # COPYLEFT
42 #
43 #   Copyright (c) 2008 Mateusz Loskot <mateusz@loskot.net>
44 #
45 #   Copying and distribution of this file, with or without modification, are
46 #   permitted in any medium without royalty provided the copyright notice
47 #   and this notice are preserved.
48
49 AC_DEFUN([AX_LIB_XERCES],
50 [
51 #    AC_REQUIRE([ACX_PTHREAD])
52
53     AC_ARG_WITH([xerces],
54         AC_HELP_STRING([--with-xerces=@<:@ARG@:>@],
55             [use Xerces C++ Parser from given prefix (ARG=path); check standard prefixes (ARG=yes); disable (ARG=no)]
56         ),
57         [
58         if test "$withval" = "yes"; then
59             if test -d /usr/local/include/xercesc ; then
60                 xerces_prefix=/usr/local
61             elif test -d /usr/include/xercesc ; then
62                 xerces_prefix=/usr
63             else
64                 xerces_prefix=""
65             fi
66             xerces_requested="yes"
67         elif test -d "$withval"; then
68             xerces_prefix="$withval"
69             xerces_requested="yes"
70         else
71             xerces_prefix=""
72             xerces_requested="no"
73         fi
74         ],
75         [
76 #        dnl Default behavior is implicit yes
77 #        if test -d /usr/local/include/xercesc ; then
78 #            xerces_prefix=/usr/local
79 #        elif test -d /usr/include/xercesc ; then
80 #            xerces_prefix=/usr
81 #        else
82 #            xerces_prefix=""
83 #        fi
84         dnl Default behavior is implicit no
85         xerces_prefix=""
86         xerces_requested="no"
87         ]
88     )
89
90     AC_ARG_WITH([xerces-inc],
91         AC_HELP_STRING([--with-xerces-inc=@<:@DIR@:>@],
92             [path to Xerces C++ Parser headers]
93         ),
94         [xerces_include_dir="$withval"],
95         [xerces_include_dir=""]
96     )
97     AC_ARG_WITH([xerces-lib],
98         AC_HELP_STRING([--with-xerces-lib=@<:@ARG@:>@],
99             [link options for Xerces C++ Parser libraries]
100         ),
101         [xerces_ldflags="$withval"],
102         [xerces_ldflags=""]
103     )
104
105     XERCES_CPPFLAGS=""
106     XERCES_LDFLAGS=""
107     XERCES_VERSION=""
108
109     dnl
110     dnl Collect include/lib paths and flags
111     dnl
112     run_xerces_test="no"
113
114     if test -n "$xerces_prefix"; then
115         xerces_include_dir="$xerces_prefix/include"
116         xerces_include_dir2="$xerces_prefix/include/xercesc"
117         xerces_ldflags="-L$xerces_prefix/lib"
118         run_xerces_test="yes"
119     elif test "$xerces_requested" = "yes"; then
120         if test -n "$xerces_include_dir" -a -n "$xerces_lib_flags"; then
121             xerces_include_dir2="$xerces_include_dir/xercesc"
122             run_xerces_test="yes"
123         fi
124     else
125         run_xerces_test="no"
126     fi
127
128     xerces_libs="-lxerces-c"
129
130     dnl
131     dnl Check Xerces C++ Parser files
132     dnl
133     if test "$run_xerces_test" = "yes"; then
134
135         saved_CPPFLAGS="$CPPFLAGS"
136         CPPFLAGS="$CPPFLAGS -I$xerces_include_dir -I$xerces_include_dir2"
137
138         saved_LDFLAGS="$LDFLAGS"
139         LDFLAGS="$xerces_ldflags $PTHREAD_LDFLAGS $LDFLAGS"
140
141         saved_LIBS="$LIBS"
142         LIBS="$xerces_libs $PTHREAD_LIBS $LIBS"
143
144         dnl
145         dnl Check Xerces headers
146         dnl
147         AC_MSG_CHECKING([for Xerces C++ Parser headers in $xerces_include_dir and $xerces_include_dir2])
148
149         AC_LANG_PUSH([C++])
150         AC_COMPILE_IFELSE([
151             AC_LANG_PROGRAM(
152                 [[
153 @%:@include <xercesc/util/XercesDefs.hpp>
154 @%:@include <xercesc/util/PlatformUtils.hpp>
155                 ]],
156                 [[]]
157             )],
158             [
159             XERCES_CPPFLAGS="-I$xerces_include_dir -I$xerces_include_dir2"
160             xerces_header_found="yes"
161             AC_MSG_RESULT([found])
162             ],
163             [
164             xerces_header_found="no"
165             AC_MSG_RESULT([not found])
166             ]
167         )
168         AC_LANG_POP([C++])
169
170         dnl
171         dnl Check Xerces libraries
172         dnl
173         if test "$xerces_header_found" = "yes"; then
174
175             AC_MSG_CHECKING([for Xerces C++ Parser libraries])
176
177             AC_LANG_PUSH([C++])
178             AC_LINK_IFELSE([
179                 AC_LANG_PROGRAM(
180                     [[
181 @%:@include <xercesc/util/XercesDefs.hpp>
182 @%:@include <xercesc/util/PlatformUtils.hpp>
183 #if (_XERCES_VERSION >= 20200)
184 XERCES_CPP_NAMESPACE_USE
185 #endif
186                     ]],
187                     [[
188 XMLPlatformUtils::Initialize();
189                     ]]
190                 )],
191                 [
192                 XERCES_LDFLAGS="$xerces_ldflags $PTHREAD_LDFLAGS"
193                 XERCES_LIBS="$xerces_libs $PTHREAD_LIBS"
194                 xerces_lib_found="yes"
195                 AC_MSG_RESULT([found])
196                 ],
197                 [
198                 xerces_lib_found="no"
199                 AC_MSG_RESULT([not found])
200                 ]
201             )
202             AC_LANG_POP([C++])
203         fi
204
205         CPPFLAGS="$saved_CPPFLAGS"
206         LDFLAGS="$saved_LDFLAGS"
207         LIBS="$saved_LIBS"
208     fi
209
210     AC_MSG_CHECKING([for Xerces C++ Parser])
211
212     if test "$run_xerces_test" = "yes"; then
213         if test "$xerces_header_found" = "yes" -a "$xerces_lib_found" = "yes"; then
214
215             AC_SUBST([XERCES_CPPFLAGS])
216             AC_SUBST([XERCES_LDFLAGS])
217             AC_SUBST([XERCES_LIBS])
218
219             HAVE_XERCES="yes"
220         else
221             HAVE_XERCES="no"
222         fi
223
224         AC_MSG_RESULT([$HAVE_XERCES])
225
226         dnl
227         dnl Check Xerces version
228         dnl
229         if test "$HAVE_XERCES" = "yes"; then
230
231             xerces_version_req=ifelse([$1], [], [], [$1])
232
233             if test  -n "$xerces_version_req"; then
234
235                 AC_MSG_CHECKING([if Xerces C++ Parser version is >= $xerces_version_req])
236
237                 if test -f "$xerces_include_dir2/util/XercesVersion.hpp"; then
238
239                     xerces_major=`cat $xerces_include_dir2/util/XercesVersion.hpp | \
240                                     grep '^#define.*XERCES_VERSION_MAJOR.*[0-9]$' | \
241                                     sed -e 's/#define XERCES_VERSION_MAJOR.//'`
242
243                     xerces_minor=`cat $xerces_include_dir2/util/XercesVersion.hpp | \
244                                     grep '^#define.*XERCES_VERSION_MINOR.*[0-9]$' | \
245                                     sed -e 's/#define XERCES_VERSION_MINOR.//'`
246
247                     xerces_revision=`cat $xerces_include_dir2/util/XercesVersion.hpp | \
248                                     grep '^#define.*XERCES_VERSION_REVISION.*[0-9]$' | \
249                                     sed -e 's/#define XERCES_VERSION_REVISION.//'`
250
251                     XERCES_VERSION="$xerces_major.$xerces_minor.$xerces_revision"
252                     AC_SUBST([XERCES_VERSION])
253
254                     dnl Decompose required version string and calculate numerical representation
255                     xerces_version_req_major=`expr $xerces_version_req : '\([[0-9]]*\)'`
256                     xerces_version_req_minor=`expr $xerces_version_req : '[[0-9]]*\.\([[0-9]]*\)'`
257                     xerces_version_req_revision=`expr $xerces_version_req : '[[0-9]]*\.[[0-9]]*\.\([[0-9]]*\)'`
258                     if test "x$xerces_version_req_revision" = "x"; then
259                         xerces_version_req_revision="0"
260                     fi
261
262                     xerces_version_req_number=`expr $xerces_version_req_major \* 10000 \
263                                                \+ $xerces_version_req_minor \* 100 \
264                                                \+ $xerces_version_req_revision`
265
266                     dnl Calculate numerical representation of detected version
267                     xerces_version_number=`expr $xerces_major \* 10000 \
268                                           \+ $xerces_minor \* 100 \
269                                            \+ $xerces_revision`
270
271                     xerces_version_check=`expr $xerces_version_number \>\= $xerces_version_req_number`
272                     if test "$xerces_version_check" = "1"; then
273                         AC_MSG_RESULT([yes])
274                     else
275                         AC_MSG_RESULT([no])
276                         AC_MSG_WARN([Found Xerces C++ Parser $XERCES_VERSION, which is older than required. Possible compilation failure.])
277                     fi
278                 else
279                     AC_MSG_RESULT([no])
280                     AC_MSG_WARN([Missing header XercesVersion.hpp. Unable to determine Xerces version.])
281                 fi
282             fi
283         fi
284
285     else
286         HAVE_XERCES="no"
287         AC_MSG_RESULT([$HAVE_XERCES])
288
289         if test "$xerces_requested" = "yes"; then
290             AC_MSG_WARN([Xerces C++ Parser support requested but headers or library not found. Specify valid prefix of Xerces C++ using --with-xerces=@<:@DIR@:>@ or provide include directory and linker flags using --with-xerces-inc and --with-xerces-lib])
291         fi
292     fi
293     if test "$HAVE_XERCES" = "yes"; then
294         CPPFLAGS="$CPPFLAGS $XERCES_CPPFLAGS -DHAVE_XERCES_C=1"
295         LDFLAGS="$LDFLAGS $XERCES_LDFLAGS $XERCES_LIBS"
296     fi
297 ])