fix CID 1596595: Resource leaks, and CID 1596594: (CHECKED_RETURN)
[claws.git] / m4 / gpgme.m4
1 # gpgme.m4 - autoconf macro to detect GPGME.
2 # Copyright (C) 2002, 2003, 2004, 2014, 2018, 2022 g10 Code GmbH
3 #
4 # This file is free software; as a special exception the author gives
5 # unlimited permission to copy and/or distribute it, with or without
6 # modifications, as long as this notice is preserved.
7 #
8 # This file is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
10 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 #
12 # Last-changed: 2022-11-25
13
14
15 dnl
16 dnl Find gpgrt-config, which uses .pc file
17 dnl (minimum pkg-config functionality, supporting cross build)
18 dnl
19 dnl _AM_PATH_GPGRT_CONFIG
20 AC_DEFUN([_AM_PATH_GPGRT_CONFIG],[dnl
21   AC_PATH_PROG(GPGRT_CONFIG, gpgrt-config, no, [$prefix/bin:$PATH])
22   if test "$GPGRT_CONFIG" != "no"; then
23     # Determine gpgrt_libdir
24     #
25     # Get the prefix of gpgrt-config assuming it's something like:
26     #   <PREFIX>/bin/gpgrt-config
27     gpgrt_prefix=${GPGRT_CONFIG%/*/*}
28     possible_libdir1=${gpgrt_prefix}/lib
29     # Determine by using system libdir-format with CC, it's like:
30     #   Normal style: /usr/lib
31     #   GNU cross style: /usr/<triplet>/lib
32     #   Debian style: /usr/lib/<multiarch-name>
33     #   Fedora/openSUSE style: /usr/lib, /usr/lib32 or /usr/lib64
34     # It is assumed that CC is specified to the one of host on cross build.
35     if libdir_candidates=$(${CC:-cc} -print-search-dirs | \
36           sed -n -e "/^libraries/{s/libraries: =//;s/:/\\
37 /g;p;}"); then
38       # From the output of -print-search-dirs, select valid pkgconfig dirs.
39       libdir_candidates=$(for dir in $libdir_candidates; do
40         if p=$(cd $dir 2>/dev/null && pwd); then
41           test -d "$p/pkgconfig" && echo $p;
42         fi
43       done)
44
45       for possible_libdir0 in $libdir_candidates; do
46         # possible_libdir0:
47         #   Fallback candidate, the one of system-installed (by $CC)
48         #   (/usr/<triplet>/lib, /usr/lib/<multiarch-name> or /usr/lib32)
49         # possible_libdir1:
50         #   Another candidate, user-locally-installed
51         #   (<gpgrt_prefix>/lib)
52         # possible_libdir2
53         #   Most preferred
54         #   (<gpgrt_prefix>/<triplet>/lib,
55         #    <gpgrt_prefix>/lib/<multiarch-name> or <gpgrt_prefix>/lib32)
56         if test "${possible_libdir0##*/}" = "lib"; then
57           possible_prefix0=${possible_libdir0%/lib}
58           possible_prefix0_triplet=${possible_prefix0##*/}
59           if test -z "$possible_prefix0_triplet"; then
60             continue
61           fi
62           possible_libdir2=${gpgrt_prefix}/$possible_prefix0_triplet/lib
63         else
64           possible_prefix0=${possible_libdir0%%/lib*}
65           possible_libdir2=${gpgrt_prefix}${possible_libdir0#$possible_prefix0}
66         fi
67         if test -f ${possible_libdir2}/pkgconfig/gpg-error.pc; then
68           gpgrt_libdir=${possible_libdir2}
69         elif test -f ${possible_libdir1}/pkgconfig/gpg-error.pc; then
70           gpgrt_libdir=${possible_libdir1}
71         elif test -f ${possible_libdir0}/pkgconfig/gpg-error.pc; then
72           gpgrt_libdir=${possible_libdir0}
73         fi
74         if test -n "$gpgrt_libdir"; then break; fi
75       done
76       if test -z "$libdir_candidates"; then
77         # No valid pkgconfig dir in any of the system directories, fallback
78         gpgrt_libdir=${possible_libdir1}
79       fi
80     else
81       # When we cannot determine system libdir-format, use this:
82       gpgrt_libdir=${possible_libdir1}
83     fi
84   else
85     unset GPGRT_CONFIG
86   fi
87
88   if test -n "$gpgrt_libdir"; then
89     GPGRT_CONFIG="$GPGRT_CONFIG --libdir=$gpgrt_libdir"
90     if $GPGRT_CONFIG gpg-error >/dev/null 2>&1; then
91       GPG_ERROR_CONFIG="$GPGRT_CONFIG gpg-error"
92       AC_MSG_NOTICE([Use gpgrt-config with $gpgrt_libdir as gpg-error-config])
93       gpg_error_config_version=`$GPG_ERROR_CONFIG --modversion`
94     else
95       unset GPGRT_CONFIG
96     fi
97   elif test "$GPG_ERROR_CONFIG" != "no"; then
98     gpg_error_config_version=`$GPG_ERROR_CONFIG --version`
99     unset GPGRT_CONFIG
100   fi
101 ])
102
103 AC_DEFUN([_AM_PATH_GPGME_CONFIG],[dnl
104 AC_REQUIRE([_AM_PATH_GPGRT_CONFIG])dnl
105   AC_ARG_WITH(gpgme-prefix,
106               AS_HELP_STRING([--with-gpgme-prefix=PFX],
107                              [prefix where GPGME is installed (optional)]),
108      gpgme_config_prefix="$withval", gpgme_config_prefix="")
109   if test x"${GPGME_CONFIG}" = x ; then
110      if test x"${gpgme_config_prefix}" != x ; then
111         GPGME_CONFIG="${gpgme_config_prefix}/bin/gpgme-config"
112      else
113        case "${SYSROOT}" in
114          /*)
115            if test -x "${SYSROOT}/bin/gpgme-config" ; then
116              GPGME_CONFIG="${SYSROOT}/bin/gpgme-config"
117            fi
118            ;;
119          '')
120            ;;
121           *)
122            AC_MSG_WARN([Ignoring \$SYSROOT as it is not an absolute path.])
123            ;;
124        esac
125      fi
126   fi
127
128   use_gpgrt_config=""
129   if test x"$GPGRT_CONFIG" != x -a "$GPGRT_CONFIG" != "no"; then
130     if $GPGRT_CONFIG gpgme --exists; then
131       GPGME_CONFIG="$GPGRT_CONFIG gpgme"
132       AC_MSG_NOTICE([Use gpgrt-config as gpgme-config])
133       use_gpgrt_config=yes
134     fi
135   fi
136   if test -z "$use_gpgrt_config"; then
137     AC_PATH_PROG(GPGME_CONFIG, gpgme-config, no)
138   fi
139
140   if test "$GPGME_CONFIG" != "no" ; then
141     if test -z "$use_gpgrt_config"; then
142       gpgme_version=`$GPGME_CONFIG --version`
143     else
144       gpgme_version=`$GPGME_CONFIG --modversion`
145     fi
146   fi
147   gpgme_version_major=`echo $gpgme_version | \
148                sed 's/\([[0-9]]*\)\.\([[0-9]]*\)\.\([[0-9]]*\).*/\1/'`
149   gpgme_version_minor=`echo $gpgme_version | \
150                sed 's/\([[0-9]]*\)\.\([[0-9]]*\)\.\([[0-9]]*\).*/\2/'`
151   gpgme_version_micro=`echo $gpgme_version | \
152                sed 's/\([[0-9]]*\)\.\([[0-9]]*\)\.\([[0-9]]*\).*/\3/'`
153 ])
154
155
156 AC_DEFUN([_AM_PATH_GPGME_CONFIG_HOST_CHECK],
157 [
158     if test -z "$use_gpgrt_config"; then
159       gpgme_config_host=`$GPGME_CONFIG --host 2>/dev/null || echo none`
160     else
161       gpgme_config_host=`$GPGME_CONFIG --variable=host 2>/dev/null || echo none`
162     fi
163     if test x"$gpgme_config_host" != xnone ; then
164       if test x"$gpgme_config_host" != x"$host" ; then
165   AC_MSG_WARN([[
166 ***
167 *** The config script "$GPGME_CONFIG" was
168 *** built for $gpgme_config_host and thus may not match the
169 *** used host $host.
170 *** You may want to use the configure option --with-gpgme-prefix
171 *** to specify a matching config script or use \$SYSROOT.
172 ***]])
173         gpg_config_script_warn="$gpg_config_script_warn gpgme"
174       fi
175     fi
176 ])
177
178
179 dnl AM_PATH_GPGME([MINIMUM-VERSION,
180 dnl               [ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND ]]])
181 dnl Test for libgpgme and define GPGME_CFLAGS and GPGME_LIBS.
182 dnl
183 dnl If a prefix option is not used, the config script is first
184 dnl searched in $SYSROOT/bin and then along $PATH.  If the used
185 dnl config script does not match the host specification the script
186 dnl is added to the gpg_config_script_warn variable.
187 dnl
188 AC_DEFUN([AM_PATH_GPGME],
189 [ AC_REQUIRE([_AM_PATH_GPGME_CONFIG])dnl
190   tmp=ifelse([$1], ,1:0.4.2,$1)
191   if echo "$tmp" | grep ':' >/dev/null 2>/dev/null ; then
192      req_gpgme_api=`echo "$tmp"     | sed 's/\(.*\):\(.*\)/\1/'`
193      min_gpgme_version=`echo "$tmp" | sed 's/\(.*\):\(.*\)/\2/'`
194   else
195      req_gpgme_api=0
196      min_gpgme_version="$tmp"
197   fi
198
199   AC_MSG_CHECKING(for GPGME - version >= $min_gpgme_version)
200   ok=no
201   if test "$GPGME_CONFIG" != "no" ; then
202     req_major=`echo $min_gpgme_version | \
203                sed 's/\([[0-9]]*\)\.\([[0-9]]*\)\.\([[0-9]]*\)/\1/'`
204     req_minor=`echo $min_gpgme_version | \
205                sed 's/\([[0-9]]*\)\.\([[0-9]]*\)\.\([[0-9]]*\)/\2/'`
206     req_micro=`echo $min_gpgme_version | \
207                sed 's/\([[0-9]]*\)\.\([[0-9]]*\)\.\([[0-9]]*\)/\3/'`
208     if test "$gpgme_version_major" -gt "$req_major"; then
209         ok=yes
210     else
211         if test "$gpgme_version_major" -eq "$req_major"; then
212             if test "$gpgme_version_minor" -gt "$req_minor"; then
213                ok=yes
214             else
215                if test "$gpgme_version_minor" -eq "$req_minor"; then
216                    if test "$gpgme_version_micro" -ge "$req_micro"; then
217                      ok=yes
218                    fi
219                fi
220             fi
221         fi
222     fi
223   fi
224   if test $ok = yes; then
225      # If we have a recent GPGME, we should also check that the
226      # API is compatible.
227      if test "$req_gpgme_api" -gt 0 ; then
228         if test -z "$use_gpgrt_config"; then
229           tmp=`$GPGME_CONFIG --api-version 2>/dev/null || echo 0`
230         else
231           tmp=`$GPGME_CONFIG --variable=api_version 2>/dev/null || echo 0`
232         fi
233         if test "$tmp" -gt 0 ; then
234            if test "$req_gpgme_api" -ne "$tmp" ; then
235              ok=no
236            fi
237         fi
238      fi
239   fi
240   if test $ok = yes; then
241     GPGME_CFLAGS=`$GPGME_CONFIG --cflags`
242     GPGME_LIBS=`$GPGME_CONFIG --libs`
243     AC_MSG_RESULT(yes)
244     ifelse([$2], , :, [$2])
245     _AM_PATH_GPGME_CONFIG_HOST_CHECK
246   else
247     GPGME_CFLAGS=""
248     GPGME_LIBS=""
249     AC_MSG_RESULT(no)
250     ifelse([$3], , :, [$3])
251   fi
252   AC_SUBST(GPGME_CFLAGS)
253   AC_SUBST(GPGME_LIBS)
254 ])
255
256 dnl AM_PATH_GPGME_PTHREAD([MINIMUM-VERSION,
257 dnl                       [ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND ]]])
258 dnl Test for libgpgme and define GPGME_PTHREAD_CFLAGS
259 dnl  and GPGME_PTHREAD_LIBS.
260 dnl
261 AC_DEFUN([AM_PATH_GPGME_PTHREAD],[
262   AC_OBSOLETE([$0], [; use AM_PATH_GPGME instead to use GPGME_CFLAGS and GPGME_LIBS])dnl
263   AC_REQUIRE([_AM_PATH_GPGME_CONFIG])dnl
264   tmp=ifelse([$1], ,1:0.4.2,$1)
265   if echo "$tmp" | grep ':' >/dev/null 2>/dev/null ; then
266      req_gpgme_api=`echo "$tmp"     | sed 's/\(.*\):\(.*\)/\1/'`
267      min_gpgme_version=`echo "$tmp" | sed 's/\(.*\):\(.*\)/\2/'`
268   else
269      req_gpgme_api=0
270      min_gpgme_version="$tmp"
271   fi
272
273   AC_MSG_CHECKING(for GPGME pthread - version >= $min_gpgme_version)
274   ok=no
275   if test "$GPGME_CONFIG" != "no" ; then
276     req_major=`echo $min_gpgme_version | \
277                sed 's/\([[0-9]]*\)\.\([[0-9]]*\)\.\([[0-9]]*\)/\1/'`
278     req_minor=`echo $min_gpgme_version | \
279                sed 's/\([[0-9]]*\)\.\([[0-9]]*\)\.\([[0-9]]*\)/\2/'`
280     req_micro=`echo $min_gpgme_version | \
281                sed 's/\([[0-9]]*\)\.\([[0-9]]*\)\.\([[0-9]]*\)/\3/'`
282     if test "$gpgme_version_major" -gt "$req_major"; then
283         ok=yes
284     else
285         if test "$gpgme_version_major" -eq "$req_major"; then
286             if test "$gpgme_version_minor" -gt "$req_minor"; then
287                ok=yes
288             else
289                if test "$gpgme_version_minor" -eq "$req_minor"; then
290                    if test "$gpgme_version_micro" -ge "$req_micro"; then
291                      ok=yes
292                    fi
293                fi
294             fi
295         fi
296     fi
297   fi
298   if test $ok = yes; then
299      # If we have a recent GPGME, we should also check that the
300      # API is compatible.
301      if test "$req_gpgme_api" -gt 0 ; then
302         if test -z "$use_gpgrt_config"; then
303           tmp=`$GPGME_CONFIG --api-version 2>/dev/null || echo 0`
304         else
305           tmp=`$GPGME_CONFIG --variable=api_version 2>/dev/null || echo 0`
306         fi
307         if test "$tmp" -gt 0 ; then
308            if test "$req_gpgme_api" -ne "$tmp" ; then
309              ok=no
310            fi
311         fi
312      fi
313   fi
314   if test $ok = yes; then
315     GPGME_PTHREAD_CFLAGS=`$GPGME_CONFIG --cflags`
316     GPGME_PTHREAD_LIBS=`$GPGME_CONFIG --libs`
317     AC_MSG_RESULT(yes)
318     ifelse([$2], , :, [$2])
319     _AM_PATH_GPGME_CONFIG_HOST_CHECK
320   else
321     GPGME_PTHREAD_CFLAGS=""
322     GPGME_PTHREAD_LIBS=""
323     AC_MSG_RESULT(no)
324     ifelse([$3], , :, [$3])
325   fi
326   AC_SUBST(GPGME_PTHREAD_CFLAGS)
327   AC_SUBST(GPGME_PTHREAD_LIBS)
328 ])
329
330
331 dnl AM_PATH_GPGME_GLIB([MINIMUM-VERSION,
332 dnl               [ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND ]]])
333 dnl Test for libgpgme-glib and define GPGME_GLIB_CFLAGS and GPGME_GLIB_LIBS.
334 dnl
335 AC_DEFUN([AM_PATH_GPGME_GLIB],
336 [ AC_REQUIRE([_AM_PATH_GPGME_CONFIG])dnl
337   tmp=ifelse([$1], ,1:0.4.2,$1)
338   if echo "$tmp" | grep ':' >/dev/null 2>/dev/null ; then
339      req_gpgme_api=`echo "$tmp"     | sed 's/\(.*\):\(.*\)/\1/'`
340      min_gpgme_version=`echo "$tmp" | sed 's/\(.*\):\(.*\)/\2/'`
341   else
342      req_gpgme_api=0
343      min_gpgme_version="$tmp"
344   fi
345
346   AC_MSG_CHECKING(for GPGME - version >= $min_gpgme_version)
347   ok=no
348   if test "$GPGME_CONFIG" != "no" ; then
349     req_major=`echo $min_gpgme_version | \
350                sed 's/\([[0-9]]*\)\.\([[0-9]]*\)\.\([[0-9]]*\)/\1/'`
351     req_minor=`echo $min_gpgme_version | \
352                sed 's/\([[0-9]]*\)\.\([[0-9]]*\)\.\([[0-9]]*\)/\2/'`
353     req_micro=`echo $min_gpgme_version | \
354                sed 's/\([[0-9]]*\)\.\([[0-9]]*\)\.\([[0-9]]*\)/\3/'`
355     if test "$gpgme_version_major" -gt "$req_major"; then
356         ok=yes
357     else
358         if test "$gpgme_version_major" -eq "$req_major"; then
359             if test "$gpgme_version_minor" -gt "$req_minor"; then
360                ok=yes
361             else
362                if test "$gpgme_version_minor" -eq "$req_minor"; then
363                    if test "$gpgme_version_micro" -ge "$req_micro"; then
364                      ok=yes
365                    fi
366                fi
367             fi
368         fi
369     fi
370   fi
371   if test $ok = yes; then
372      # If we have a recent GPGME, we should also check that the
373      # API is compatible.
374      if test "$req_gpgme_api" -gt 0 ; then
375         if test -z "$use_gpgrt_config"; then
376           tmp=`$GPGME_CONFIG --api-version 2>/dev/null || echo 0`
377         else
378           tmp=`$GPGME_CONFIG --variable=api_version 2>/dev/null || echo 0`
379         fi
380         if test "$tmp" -gt 0 ; then
381            if test "$req_gpgme_api" -ne "$tmp" ; then
382              ok=no
383            fi
384         fi
385      fi
386   fi
387   if test $ok = yes; then
388     if test -z "$use_gpgrt_config"; then
389       GPGME_GLIB_CFLAGS=`$GPGME_CONFIG --glib --cflags`
390       GPGME_GLIB_LIBS=`$GPGME_CONFIG --glib --libs`
391     else
392       if $GPGRT_CONFIG gpgme-glib --exists; then
393         GPGME_CONFIG="$GPGRT_CONFIG gpgme-glib"
394         GPGME_GLIB_CFLAGS=`$GPGME_CONFIG --cflags`
395         GPGME_GLIB_LIBS=`$GPGME_CONFIG --libs`
396       else
397         ok = no
398       fi
399     fi
400   fi
401   if test $ok = yes; then
402     AC_MSG_RESULT(yes)
403     ifelse([$2], , :, [$2])
404     _AM_PATH_GPGME_CONFIG_HOST_CHECK
405   else
406     GPGME_GLIB_CFLAGS=""
407     GPGME_GLIB_LIBS=""
408     AC_MSG_RESULT(no)
409     ifelse([$3], , :, [$3])
410   fi
411   AC_SUBST(GPGME_GLIB_CFLAGS)
412   AC_SUBST(GPGME_GLIB_LIBS)
413 ])