4b5cd40b67e5bf9a712d3b6182245170df2d59e1
[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-02-15
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     else
124       # When we cannot determine system libdir-format, use this:
125       gpgrt_libdir=${possible_libdir1}
126     fi
127   else
128     unset GPGRT_CONFIG
129   fi
130
131   if test -n "$gpgrt_libdir"; then
132     GPGRT_CONFIG="$GPGRT_CONFIG --libdir=$gpgrt_libdir"
133     if $GPGRT_CONFIG gpg-error >/dev/null 2>&1; then
134       GPG_ERROR_CONFIG="$GPGRT_CONFIG gpg-error"
135       AC_MSG_NOTICE([Use gpgrt-config with $gpgrt_libdir as gpg-error-config])
136       gpg_error_config_version=`$GPG_ERROR_CONFIG --modversion`
137     else
138       unset GPGRT_CONFIG
139     fi
140   elif test "$GPG_ERROR_CONFIG" != "no"; then
141     gpg_error_config_version=`$GPG_ERROR_CONFIG --version`
142     unset GPGRT_CONFIG
143   fi
144   if test "$GPG_ERROR_CONFIG" != "no"; then
145     req_major=`echo $min_gpg_error_version | \
146                sed 's/\([[0-9]]*\)\.\([[0-9]]*\)/\1/'`
147     req_minor=`echo $min_gpg_error_version | \
148                sed 's/\([[0-9]]*\)\.\([[0-9]]*\)/\2/'`
149     major=`echo $gpg_error_config_version | \
150                sed 's/\([[0-9]]*\)\.\([[0-9]]*\).*/\1/'`
151     minor=`echo $gpg_error_config_version | \
152                sed 's/\([[0-9]]*\)\.\([[0-9]]*\).*/\2/'`
153     if test "$major" -gt "$req_major"; then
154         ok=yes
155     else
156         if test "$major" -eq "$req_major"; then
157             if test "$minor" -ge "$req_minor"; then
158                ok=yes
159             fi
160         fi
161     fi
162   fi
163   AC_MSG_CHECKING(for GPG Error - version >= $min_gpg_error_version)
164   if test $ok = yes; then
165     GPG_ERROR_CFLAGS=`$GPG_ERROR_CONFIG --cflags`
166     GPG_ERROR_LIBS=`$GPG_ERROR_CONFIG --libs`
167     if test -z "$GPGRT_CONFIG"; then
168       GPG_ERROR_MT_CFLAGS=`$GPG_ERROR_CONFIG --mt --cflags 2>/dev/null`
169       GPG_ERROR_MT_LIBS=`$GPG_ERROR_CONFIG --mt --libs 2>/dev/null`
170     else
171       GPG_ERROR_MT_CFLAGS=`$GPG_ERROR_CONFIG --variable=mtcflags 2>/dev/null`
172       GPG_ERROR_MT_CFLAGS="$GPG_ERROR_CFLAGS${GPG_ERROR_CFLAGS:+ }$GPG_ERROR_MT_CFLAGS"
173       GPG_ERROR_MT_LIBS=`$GPG_ERROR_CONFIG --variable=mtlibs 2>/dev/null`
174       GPG_ERROR_MT_LIBS="$GPG_ERROR_LIBS${GPG_ERROR_LIBS:+ }$GPG_ERROR_MT_LIBS"
175     fi
176     AC_MSG_RESULT([yes ($gpg_error_config_version)])
177     ifelse([$2], , :, [$2])
178     if test -z "$GPGRT_CONFIG"; then
179       gpg_error_config_host=`$GPG_ERROR_CONFIG --host 2>/dev/null || echo none`
180     else
181       gpg_error_config_host=`$GPG_ERROR_CONFIG --variable=host 2>/dev/null || echo none`
182     fi
183     if test x"$gpg_error_config_host" != xnone ; then
184       if test x"$gpg_error_config_host" != x"$host" ; then
185   AC_MSG_WARN([[
186 ***
187 *** The config script "$GPG_ERROR_CONFIG" was
188 *** built for $gpg_error_config_host and thus may not match the
189 *** used host $host.
190 *** You may want to use the configure option --with-libgpg-error-prefix
191 *** to specify a matching config script or use \$SYSROOT.
192 ***]])
193         gpg_config_script_warn="$gpg_config_script_warn libgpg-error"
194       fi
195     fi
196   else
197     GPG_ERROR_CFLAGS=""
198     GPG_ERROR_LIBS=""
199     GPG_ERROR_MT_CFLAGS=""
200     GPG_ERROR_MT_LIBS=""
201     AC_MSG_RESULT(no)
202     ifelse([$3], , :, [$3])
203   fi
204   AC_SUBST(GPG_ERROR_CFLAGS)
205   AC_SUBST(GPG_ERROR_LIBS)
206   AC_SUBST(GPG_ERROR_MT_CFLAGS)
207   AC_SUBST(GPG_ERROR_MT_LIBS)
208 ])