Imported Upstream version 1.6.3
[platform/upstream/libksba.git] / m4 / gpg-error.m4
1 # gpg-error.m4 - autoconf macro to detect libgpg-error.
2 # Copyright (C) 2002, 2003, 2004, 2011, 2014, 2018, 2020, 2021
3 #               g10 Code GmbH
4 #
5 # This file is free software; as a special exception the author gives
6 # unlimited permission to copy and/or distribute it, with or without
7 # modifications, as long as this notice is preserved.
8 #
9 # This file is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
11 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 #
13 # Last-changed: 2022-09-21
14
15
16 dnl AM_PATH_GPG_ERROR([MINIMUM-VERSION,
17 dnl                   [ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND ]]])
18 dnl
19 dnl Test for libgpg-error and define GPG_ERROR_CFLAGS, GPG_ERROR_LIBS,
20 dnl GPG_ERROR_MT_CFLAGS, and GPG_ERROR_MT_LIBS.  The _MT_ variants are
21 dnl used for programs requireing real multi thread support.
22 dnl
23 dnl If a prefix option is not used, the config script is first
24 dnl searched in $SYSROOT/bin and then along $PATH.  If the used
25 dnl config script does not match the host specification the script
26 dnl is added to the gpg_config_script_warn variable.
27 dnl
28 AC_DEFUN([AM_PATH_GPG_ERROR],
29 [ AC_REQUIRE([AC_CANONICAL_HOST])
30   gpg_error_config_prefix=""
31   dnl --with-libgpg-error-prefix=PFX is the preferred name for this option,
32   dnl since that is consistent with how our three siblings use the directory/
33   dnl package name in --with-$dir_name-prefix=PFX.
34   AC_ARG_WITH(libgpg-error-prefix,
35               AS_HELP_STRING([--with-libgpg-error-prefix=PFX],
36                              [prefix where GPG Error is installed (optional)]),
37               [gpg_error_config_prefix="$withval"])
38
39   dnl Accept --with-gpg-error-prefix and make it work the same as
40   dnl --with-libgpg-error-prefix above, for backwards compatibility,
41   dnl but do not document this old, inconsistently-named option.
42   AC_ARG_WITH(gpg-error-prefix,,
43               [gpg_error_config_prefix="$withval"])
44
45   if test x"${GPG_ERROR_CONFIG}" = x ; then
46      if test x"${gpg_error_config_prefix}" != x ; then
47         GPG_ERROR_CONFIG="${gpg_error_config_prefix}/bin/gpg-error-config"
48      else
49        case "${SYSROOT}" in
50          /*)
51            if test -x "${SYSROOT}/bin/gpg-error-config" ; then
52              GPG_ERROR_CONFIG="${SYSROOT}/bin/gpg-error-config"
53            fi
54            ;;
55          '')
56            ;;
57           *)
58            AC_MSG_WARN([Ignoring \$SYSROOT as it is not an absolute path.])
59            ;;
60        esac
61      fi
62   fi
63
64   AC_PATH_PROG(GPG_ERROR_CONFIG, gpg-error-config, no)
65   min_gpg_error_version=ifelse([$1], ,1.33,$1)
66   ok=no
67
68   AC_PATH_PROG(GPGRT_CONFIG, gpgrt-config, no, [$prefix/bin:$PATH])
69   if test "$GPGRT_CONFIG" != "no"; then
70     # Determine gpgrt_libdir
71     #
72     # Get the prefix of gpgrt-config assuming it's something like:
73     #   <PREFIX>/bin/gpgrt-config
74     gpgrt_prefix=${GPGRT_CONFIG%/*/*}
75     possible_libdir1=${gpgrt_prefix}/lib
76     # Determine by using system libdir-format with CC, it's like:
77     #   Normal style: /usr/lib
78     #   GNU cross style: /usr/<triplet>/lib
79     #   Debian style: /usr/lib/<multiarch-name>
80     #   Fedora/openSUSE style: /usr/lib, /usr/lib32 or /usr/lib64
81     # It is assumed that CC is specified to the one of host on cross build.
82     if libdir_candidates=$(${CC:-cc} -print-search-dirs | \
83           sed -n -e "/^libraries/{s/libraries: =//;s/:/\\
84 /g;p;}"); then
85       # From the output of -print-search-dirs, select valid pkgconfig dirs.
86       libdir_candidates=$(for dir in $libdir_candidates; do
87         if p=$(cd $dir 2>/dev/null && pwd); then
88           test -d "$p/pkgconfig" && echo $p;
89         fi
90       done)
91
92       for possible_libdir0 in $libdir_candidates; do
93         # possible_libdir0:
94         #   Fallback candidate, the one of system-installed (by $CC)
95         #   (/usr/<triplet>/lib, /usr/lib/<multiarch-name> or /usr/lib32)
96         # possible_libdir1:
97         #   Another candidate, user-locally-installed
98         #   (<gpgrt_prefix>/lib)
99         # possible_libdir2
100         #   Most preferred
101         #   (<gpgrt_prefix>/<triplet>/lib,
102         #    <gpgrt_prefix>/lib/<multiarch-name> or <gpgrt_prefix>/lib32)
103         if test "${possible_libdir0##*/}" = "lib"; then
104           possible_prefix0=${possible_libdir0%/lib}
105           possible_prefix0_triplet=${possible_prefix0##*/}
106           if test -z "$possible_prefix0_triplet"; then
107             continue
108           fi
109           possible_libdir2=${gpgrt_prefix}/$possible_prefix0_triplet/lib
110         else
111           possible_prefix0=${possible_libdir0%%/lib*}
112           possible_libdir2=${gpgrt_prefix}${possible_libdir0#$possible_prefix0}
113         fi
114         if test -f ${possible_libdir2}/pkgconfig/gpg-error.pc; then
115           gpgrt_libdir=${possible_libdir2}
116         elif test -f ${possible_libdir1}/pkgconfig/gpg-error.pc; then
117           gpgrt_libdir=${possible_libdir1}
118         elif test -f ${possible_libdir0}/pkgconfig/gpg-error.pc; then
119           gpgrt_libdir=${possible_libdir0}
120         fi
121         if test -n "$gpgrt_libdir"; then break; fi
122       done
123       if test -z "$libdir_candidates"; then
124         # No valid pkgconfig dir in any of the system directories, fallback
125         gpgrt_libdir=${possible_libdir1}
126       fi
127     else
128       # When we cannot determine system libdir-format, use this:
129       gpgrt_libdir=${possible_libdir1}
130     fi
131   else
132     unset GPGRT_CONFIG
133   fi
134
135   if test -n "$gpgrt_libdir"; then
136     GPGRT_CONFIG="$GPGRT_CONFIG --libdir=$gpgrt_libdir"
137     if $GPGRT_CONFIG gpg-error >/dev/null 2>&1; then
138       GPG_ERROR_CONFIG="$GPGRT_CONFIG gpg-error"
139       AC_MSG_NOTICE([Use gpgrt-config with $gpgrt_libdir as gpg-error-config])
140       gpg_error_config_version=`$GPG_ERROR_CONFIG --modversion`
141     else
142       unset GPGRT_CONFIG
143     fi
144   elif test "$GPG_ERROR_CONFIG" != "no"; then
145     gpg_error_config_version=`$GPG_ERROR_CONFIG --version`
146     unset GPGRT_CONFIG
147   fi
148   if test "$GPG_ERROR_CONFIG" != "no"; then
149     req_major=`echo $min_gpg_error_version | \
150                sed 's/\([[0-9]]*\)\.\([[0-9]]*\)/\1/'`
151     req_minor=`echo $min_gpg_error_version | \
152                sed 's/\([[0-9]]*\)\.\([[0-9]]*\)/\2/'`
153     major=`echo $gpg_error_config_version | \
154                sed 's/\([[0-9]]*\)\.\([[0-9]]*\).*/\1/'`
155     minor=`echo $gpg_error_config_version | \
156                sed 's/\([[0-9]]*\)\.\([[0-9]]*\).*/\2/'`
157     if test "$major" -gt "$req_major"; then
158         ok=yes
159     else
160         if test "$major" -eq "$req_major"; then
161             if test "$minor" -ge "$req_minor"; then
162                ok=yes
163             fi
164         fi
165     fi
166   fi
167   AC_MSG_CHECKING(for GPG Error - version >= $min_gpg_error_version)
168   if test $ok = yes; then
169     GPG_ERROR_CFLAGS=`$GPG_ERROR_CONFIG --cflags`
170     GPG_ERROR_LIBS=`$GPG_ERROR_CONFIG --libs`
171     if test -z "$GPGRT_CONFIG"; then
172       GPG_ERROR_MT_CFLAGS=`$GPG_ERROR_CONFIG --mt --cflags 2>/dev/null`
173       GPG_ERROR_MT_LIBS=`$GPG_ERROR_CONFIG --mt --libs 2>/dev/null`
174     else
175       GPG_ERROR_MT_CFLAGS=`$GPG_ERROR_CONFIG --variable=mtcflags 2>/dev/null`
176       GPG_ERROR_MT_CFLAGS="$GPG_ERROR_CFLAGS${GPG_ERROR_CFLAGS:+ }$GPG_ERROR_MT_CFLAGS"
177       GPG_ERROR_MT_LIBS=`$GPG_ERROR_CONFIG --variable=mtlibs 2>/dev/null`
178       GPG_ERROR_MT_LIBS="$GPG_ERROR_LIBS${GPG_ERROR_LIBS:+ }$GPG_ERROR_MT_LIBS"
179     fi
180     AC_MSG_RESULT([yes ($gpg_error_config_version)])
181     ifelse([$2], , :, [$2])
182     if test -z "$GPGRT_CONFIG"; then
183       gpg_error_config_host=`$GPG_ERROR_CONFIG --host 2>/dev/null || echo none`
184     else
185       gpg_error_config_host=`$GPG_ERROR_CONFIG --variable=host 2>/dev/null || echo none`
186     fi
187     if test x"$gpg_error_config_host" != xnone ; then
188       if test x"$gpg_error_config_host" != x"$host" ; then
189   AC_MSG_WARN([[
190 ***
191 *** The config script "$GPG_ERROR_CONFIG" was
192 *** built for $gpg_error_config_host and thus may not match the
193 *** used host $host.
194 *** You may want to use the configure option --with-libgpg-error-prefix
195 *** to specify a matching config script or use \$SYSROOT.
196 ***]])
197         gpg_config_script_warn="$gpg_config_script_warn libgpg-error"
198       fi
199     fi
200   else
201     GPG_ERROR_CFLAGS=""
202     GPG_ERROR_LIBS=""
203     GPG_ERROR_MT_CFLAGS=""
204     GPG_ERROR_MT_LIBS=""
205     AC_MSG_RESULT(no)
206     ifelse([$3], , :, [$3])
207   fi
208   AC_SUBST(GPG_ERROR_CFLAGS)
209   AC_SUBST(GPG_ERROR_LIBS)
210   AC_SUBST(GPG_ERROR_MT_CFLAGS)
211   AC_SUBST(GPG_ERROR_MT_LIBS)
212 ])