xref: /freebsd/sys/contrib/openzfs/config/lib-prefix.m4 (revision e25152834cdf3b353892835a4f3b157e066a8ed4)
1*eda14cbcSMatt Macy# lib-prefix.m4 serial 14
2*eda14cbcSMatt Macydnl Copyright (C) 2001-2005, 2008-2019 Free Software Foundation, Inc.
3*eda14cbcSMatt Macydnl This file is free software; the Free Software Foundation
4*eda14cbcSMatt Macydnl gives unlimited permission to copy and/or distribute it,
5*eda14cbcSMatt Macydnl with or without modifications, as long as this notice is preserved.
6*eda14cbcSMatt Macy
7*eda14cbcSMatt Macydnl From Bruno Haible.
8*eda14cbcSMatt Macy
9*eda14cbcSMatt Macydnl AC_LIB_PREFIX adds to the CPPFLAGS and LDFLAGS the flags that are needed
10*eda14cbcSMatt Macydnl to access previously installed libraries. The basic assumption is that
11*eda14cbcSMatt Macydnl packages should use other packages that are installed with the same
12*eda14cbcSMatt Macydnl --prefix option.  This macro is not needed if only AC_LIB_LINKFLAGS is
13*eda14cbcSMatt Macydnl used to locate libraries, but is otherwise very convenient.
14*eda14cbcSMatt MacyAC_DEFUN([AC_LIB_PREFIX],
15*eda14cbcSMatt Macy[
16*eda14cbcSMatt Macy  AC_BEFORE([$0], [AC_LIB_LINKFLAGS])
17*eda14cbcSMatt Macy  AC_REQUIRE([AC_PROG_CC])
18*eda14cbcSMatt Macy  AC_REQUIRE([AC_CANONICAL_HOST])
19*eda14cbcSMatt Macy  AC_REQUIRE([AC_LIB_PREPARE_MULTILIB])
20*eda14cbcSMatt Macy  AC_REQUIRE([AC_LIB_PREPARE_PREFIX])
21*eda14cbcSMatt Macy  dnl By default, look in $includedir and $libdir.
22*eda14cbcSMatt Macy  use_additional=yes
23*eda14cbcSMatt Macy  AC_LIB_WITH_FINAL_PREFIX([
24*eda14cbcSMatt Macy    eval additional_includedir=\"$includedir\"
25*eda14cbcSMatt Macy    eval additional_libdir=\"$libdir\"
26*eda14cbcSMatt Macy  ])
27*eda14cbcSMatt Macy  AC_ARG_WITH([lib-prefix],
28*eda14cbcSMatt Macy[[  --with-lib-prefix[=DIR] search for libraries in DIR/include and DIR/lib
29*eda14cbcSMatt Macy  --without-lib-prefix    don't search for libraries in includedir and libdir]],
30*eda14cbcSMatt Macy[
31*eda14cbcSMatt Macy    if test "X$withval" = "Xno"; then
32*eda14cbcSMatt Macy      use_additional=no
33*eda14cbcSMatt Macy    else
34*eda14cbcSMatt Macy      if test "X$withval" = "X"; then
35*eda14cbcSMatt Macy        AC_LIB_WITH_FINAL_PREFIX([
36*eda14cbcSMatt Macy          eval additional_includedir=\"$includedir\"
37*eda14cbcSMatt Macy          eval additional_libdir=\"$libdir\"
38*eda14cbcSMatt Macy        ])
39*eda14cbcSMatt Macy      else
40*eda14cbcSMatt Macy        additional_includedir="$withval/include"
41*eda14cbcSMatt Macy        additional_libdir="$withval/$acl_libdirstem"
42*eda14cbcSMatt Macy      fi
43*eda14cbcSMatt Macy    fi
44*eda14cbcSMatt Macy])
45*eda14cbcSMatt Macy  if test $use_additional = yes; then
46*eda14cbcSMatt Macy    dnl Potentially add $additional_includedir to $CPPFLAGS.
47*eda14cbcSMatt Macy    dnl But don't add it
48*eda14cbcSMatt Macy    dnl   1. if it's the standard /usr/include,
49*eda14cbcSMatt Macy    dnl   2. if it's already present in $CPPFLAGS,
50*eda14cbcSMatt Macy    dnl   3. if it's /usr/local/include and we are using GCC on Linux,
51*eda14cbcSMatt Macy    dnl   4. if it doesn't exist as a directory.
52*eda14cbcSMatt Macy    if test "X$additional_includedir" != "X/usr/include"; then
53*eda14cbcSMatt Macy      haveit=
54*eda14cbcSMatt Macy      for x in $CPPFLAGS; do
55*eda14cbcSMatt Macy        AC_LIB_WITH_FINAL_PREFIX([eval x=\"$x\"])
56*eda14cbcSMatt Macy        if test "X$x" = "X-I$additional_includedir"; then
57*eda14cbcSMatt Macy          haveit=yes
58*eda14cbcSMatt Macy          break
59*eda14cbcSMatt Macy        fi
60*eda14cbcSMatt Macy      done
61*eda14cbcSMatt Macy      if test -z "$haveit"; then
62*eda14cbcSMatt Macy        if test "X$additional_includedir" = "X/usr/local/include"; then
63*eda14cbcSMatt Macy          if test -n "$GCC"; then
64*eda14cbcSMatt Macy            case $host_os in
65*eda14cbcSMatt Macy              linux* | gnu* | k*bsd*-gnu) haveit=yes;;
66*eda14cbcSMatt Macy            esac
67*eda14cbcSMatt Macy          fi
68*eda14cbcSMatt Macy        fi
69*eda14cbcSMatt Macy        if test -z "$haveit"; then
70*eda14cbcSMatt Macy          if test -d "$additional_includedir"; then
71*eda14cbcSMatt Macy            dnl Really add $additional_includedir to $CPPFLAGS.
72*eda14cbcSMatt Macy            CPPFLAGS="${CPPFLAGS}${CPPFLAGS:+ }-I$additional_includedir"
73*eda14cbcSMatt Macy          fi
74*eda14cbcSMatt Macy        fi
75*eda14cbcSMatt Macy      fi
76*eda14cbcSMatt Macy    fi
77*eda14cbcSMatt Macy    dnl Potentially add $additional_libdir to $LDFLAGS.
78*eda14cbcSMatt Macy    dnl But don't add it
79*eda14cbcSMatt Macy    dnl   1. if it's the standard /usr/lib,
80*eda14cbcSMatt Macy    dnl   2. if it's already present in $LDFLAGS,
81*eda14cbcSMatt Macy    dnl   3. if it's /usr/local/lib and we are using GCC on Linux,
82*eda14cbcSMatt Macy    dnl   4. if it doesn't exist as a directory.
83*eda14cbcSMatt Macy    if test "X$additional_libdir" != "X/usr/$acl_libdirstem"; then
84*eda14cbcSMatt Macy      haveit=
85*eda14cbcSMatt Macy      for x in $LDFLAGS; do
86*eda14cbcSMatt Macy        AC_LIB_WITH_FINAL_PREFIX([eval x=\"$x\"])
87*eda14cbcSMatt Macy        if test "X$x" = "X-L$additional_libdir"; then
88*eda14cbcSMatt Macy          haveit=yes
89*eda14cbcSMatt Macy          break
90*eda14cbcSMatt Macy        fi
91*eda14cbcSMatt Macy      done
92*eda14cbcSMatt Macy      if test -z "$haveit"; then
93*eda14cbcSMatt Macy        if test "X$additional_libdir" = "X/usr/local/$acl_libdirstem"; then
94*eda14cbcSMatt Macy          if test -n "$GCC"; then
95*eda14cbcSMatt Macy            case $host_os in
96*eda14cbcSMatt Macy              linux*) haveit=yes;;
97*eda14cbcSMatt Macy            esac
98*eda14cbcSMatt Macy          fi
99*eda14cbcSMatt Macy        fi
100*eda14cbcSMatt Macy        if test -z "$haveit"; then
101*eda14cbcSMatt Macy          if test -d "$additional_libdir"; then
102*eda14cbcSMatt Macy            dnl Really add $additional_libdir to $LDFLAGS.
103*eda14cbcSMatt Macy            LDFLAGS="${LDFLAGS}${LDFLAGS:+ }-L$additional_libdir"
104*eda14cbcSMatt Macy          fi
105*eda14cbcSMatt Macy        fi
106*eda14cbcSMatt Macy      fi
107*eda14cbcSMatt Macy    fi
108*eda14cbcSMatt Macy  fi
109*eda14cbcSMatt Macy])
110*eda14cbcSMatt Macy
111*eda14cbcSMatt Macydnl AC_LIB_PREPARE_PREFIX creates variables acl_final_prefix,
112*eda14cbcSMatt Macydnl acl_final_exec_prefix, containing the values to which $prefix and
113*eda14cbcSMatt Macydnl $exec_prefix will expand at the end of the configure script.
114*eda14cbcSMatt MacyAC_DEFUN([AC_LIB_PREPARE_PREFIX],
115*eda14cbcSMatt Macy[
116*eda14cbcSMatt Macy  dnl Unfortunately, prefix and exec_prefix get only finally determined
117*eda14cbcSMatt Macy  dnl at the end of configure.
118*eda14cbcSMatt Macy  if test "X$prefix" = "XNONE"; then
119*eda14cbcSMatt Macy    acl_final_prefix="$ac_default_prefix"
120*eda14cbcSMatt Macy  else
121*eda14cbcSMatt Macy    acl_final_prefix="$prefix"
122*eda14cbcSMatt Macy  fi
123*eda14cbcSMatt Macy  if test "X$exec_prefix" = "XNONE"; then
124*eda14cbcSMatt Macy    acl_final_exec_prefix='${prefix}'
125*eda14cbcSMatt Macy  else
126*eda14cbcSMatt Macy    acl_final_exec_prefix="$exec_prefix"
127*eda14cbcSMatt Macy  fi
128*eda14cbcSMatt Macy  acl_save_prefix="$prefix"
129*eda14cbcSMatt Macy  prefix="$acl_final_prefix"
130*eda14cbcSMatt Macy  eval acl_final_exec_prefix=\"$acl_final_exec_prefix\"
131*eda14cbcSMatt Macy  prefix="$acl_save_prefix"
132*eda14cbcSMatt Macy])
133*eda14cbcSMatt Macy
134*eda14cbcSMatt Macydnl AC_LIB_WITH_FINAL_PREFIX([statement]) evaluates statement, with the
135*eda14cbcSMatt Macydnl variables prefix and exec_prefix bound to the values they will have
136*eda14cbcSMatt Macydnl at the end of the configure script.
137*eda14cbcSMatt MacyAC_DEFUN([AC_LIB_WITH_FINAL_PREFIX],
138*eda14cbcSMatt Macy[
139*eda14cbcSMatt Macy  acl_save_prefix="$prefix"
140*eda14cbcSMatt Macy  prefix="$acl_final_prefix"
141*eda14cbcSMatt Macy  acl_save_exec_prefix="$exec_prefix"
142*eda14cbcSMatt Macy  exec_prefix="$acl_final_exec_prefix"
143*eda14cbcSMatt Macy  $1
144*eda14cbcSMatt Macy  exec_prefix="$acl_save_exec_prefix"
145*eda14cbcSMatt Macy  prefix="$acl_save_prefix"
146*eda14cbcSMatt Macy])
147*eda14cbcSMatt Macy
148*eda14cbcSMatt Macydnl AC_LIB_PREPARE_MULTILIB creates
149*eda14cbcSMatt Macydnl - a variable acl_libdirstem, containing the basename of the libdir, either
150*eda14cbcSMatt Macydnl   "lib" or "lib64" or "lib/64",
151*eda14cbcSMatt Macydnl - a variable acl_libdirstem2, as a secondary possible value for
152*eda14cbcSMatt Macydnl   acl_libdirstem, either the same as acl_libdirstem or "lib/sparcv9" or
153*eda14cbcSMatt Macydnl   "lib/amd64".
154*eda14cbcSMatt MacyAC_DEFUN([AC_LIB_PREPARE_MULTILIB],
155*eda14cbcSMatt Macy[
156*eda14cbcSMatt Macy  dnl There is no formal standard regarding lib and lib64.
157*eda14cbcSMatt Macy  dnl On glibc systems, the current practice is that on a system supporting
158*eda14cbcSMatt Macy  dnl 32-bit and 64-bit instruction sets or ABIs, 64-bit libraries go under
159*eda14cbcSMatt Macy  dnl $prefix/lib64 and 32-bit libraries go under $prefix/lib. We determine
160*eda14cbcSMatt Macy  dnl the compiler's default mode by looking at the compiler's library search
161*eda14cbcSMatt Macy  dnl path. If at least one of its elements ends in /lib64 or points to a
162*eda14cbcSMatt Macy  dnl directory whose absolute pathname ends in /lib64, we assume a 64-bit ABI.
163*eda14cbcSMatt Macy  dnl Otherwise we use the default, namely "lib".
164*eda14cbcSMatt Macy  dnl On Solaris systems, the current practice is that on a system supporting
165*eda14cbcSMatt Macy  dnl 32-bit and 64-bit instruction sets or ABIs, 64-bit libraries go under
166*eda14cbcSMatt Macy  dnl $prefix/lib/64 (which is a symlink to either $prefix/lib/sparcv9 or
167*eda14cbcSMatt Macy  dnl $prefix/lib/amd64) and 32-bit libraries go under $prefix/lib.
168*eda14cbcSMatt Macy  AC_REQUIRE([AC_CANONICAL_HOST])
169*eda14cbcSMatt Macy  AC_REQUIRE([gl_HOST_CPU_C_ABI_32BIT])
170*eda14cbcSMatt Macy
171*eda14cbcSMatt Macy  case "$host_os" in
172*eda14cbcSMatt Macy    solaris*)
173*eda14cbcSMatt Macy      AC_CACHE_CHECK([for 64-bit host], [gl_cv_solaris_64bit],
174*eda14cbcSMatt Macy        [AC_COMPILE_IFELSE(
175*eda14cbcSMatt Macy           [AC_LANG_SOURCE(
176*eda14cbcSMatt Macy              [[#ifdef _LP64
177*eda14cbcSMatt Macy                 int ok;
178*eda14cbcSMatt Macy                #else
179*eda14cbcSMatt Macy                 error fail
180*eda14cbcSMatt Macy                #endif
181*eda14cbcSMatt Macy              ]])],
182*eda14cbcSMatt Macy           [gl_cv_solaris_64bit=yes],
183*eda14cbcSMatt Macy           [gl_cv_solaris_64bit=no])
184*eda14cbcSMatt Macy        ]);;
185*eda14cbcSMatt Macy  esac
186*eda14cbcSMatt Macy
187*eda14cbcSMatt Macy  dnl Allow the user to override the result by setting acl_cv_libdirstems.
188*eda14cbcSMatt Macy  AC_CACHE_CHECK([for the common suffixes of directories in the library search path],
189*eda14cbcSMatt Macy    [acl_cv_libdirstems],
190*eda14cbcSMatt Macy    [acl_libdirstem=lib
191*eda14cbcSMatt Macy     acl_libdirstem2=
192*eda14cbcSMatt Macy     case "$host_os" in
193*eda14cbcSMatt Macy       solaris*)
194*eda14cbcSMatt Macy         dnl See Solaris 10 Software Developer Collection > Solaris 64-bit Developer's Guide > The Development Environment
195*eda14cbcSMatt Macy         dnl <https://docs.oracle.com/cd/E19253-01/816-5138/dev-env/index.html>.
196*eda14cbcSMatt Macy         dnl "Portable Makefiles should refer to any library directories using the 64 symbolic link."
197*eda14cbcSMatt Macy         dnl But we want to recognize the sparcv9 or amd64 subdirectory also if the
198*eda14cbcSMatt Macy         dnl symlink is missing, so we set acl_libdirstem2 too.
199*eda14cbcSMatt Macy         if test $gl_cv_solaris_64bit = yes; then
200*eda14cbcSMatt Macy           acl_libdirstem=lib/64
201*eda14cbcSMatt Macy           case "$host_cpu" in
202*eda14cbcSMatt Macy             sparc*)        acl_libdirstem2=lib/sparcv9 ;;
203*eda14cbcSMatt Macy             i*86 | x86_64) acl_libdirstem2=lib/amd64 ;;
204*eda14cbcSMatt Macy           esac
205*eda14cbcSMatt Macy         fi
206*eda14cbcSMatt Macy         ;;
207*eda14cbcSMatt Macy       *)
208*eda14cbcSMatt Macy         dnl If $CC generates code for a 32-bit ABI, the libraries are
209*eda14cbcSMatt Macy         dnl surely under $prefix/lib, not $prefix/lib64.
210*eda14cbcSMatt Macy         if test "$HOST_CPU_C_ABI_32BIT" != yes; then
211*eda14cbcSMatt Macy           dnl The result is a property of the system. However, non-system
212*eda14cbcSMatt Macy           dnl compilers sometimes have odd library search paths. Therefore
213*eda14cbcSMatt Macy           dnl prefer asking /usr/bin/gcc, if available, rather than $CC.
214*eda14cbcSMatt Macy           searchpath=`(if test -f /usr/bin/gcc \
215*eda14cbcSMatt Macy                           && LC_ALL=C /usr/bin/gcc -print-search-dirs >/dev/null 2>/dev/null; then \
216*eda14cbcSMatt Macy                          LC_ALL=C /usr/bin/gcc -print-search-dirs; \
217*eda14cbcSMatt Macy                        else \
218*eda14cbcSMatt Macy                          LC_ALL=C $CC -print-search-dirs; \
219*eda14cbcSMatt Macy                        fi) 2>/dev/null \
220*eda14cbcSMatt Macy                       | sed -n -e 's,^libraries: ,,p' | sed -e 's,^=,,'`
221*eda14cbcSMatt Macy           if test -n "$searchpath"; then
222*eda14cbcSMatt Macy             acl_save_IFS="${IFS= 	}"; IFS=":"
223*eda14cbcSMatt Macy             for searchdir in $searchpath; do
224*eda14cbcSMatt Macy               if test -d "$searchdir"; then
225*eda14cbcSMatt Macy                 case "$searchdir" in
226*eda14cbcSMatt Macy                   */lib64/ | */lib64 ) acl_libdirstem=lib64 ;;
227*eda14cbcSMatt Macy                   */../ | */.. )
228*eda14cbcSMatt Macy                     # Better ignore directories of this form. They are misleading.
229*eda14cbcSMatt Macy                     ;;
230*eda14cbcSMatt Macy                   *) searchdir=`cd "$searchdir" && pwd`
231*eda14cbcSMatt Macy                      case "$searchdir" in
232*eda14cbcSMatt Macy                        */lib64 ) acl_libdirstem=lib64 ;;
233*eda14cbcSMatt Macy                      esac ;;
234*eda14cbcSMatt Macy                 esac
235*eda14cbcSMatt Macy               fi
236*eda14cbcSMatt Macy             done
237*eda14cbcSMatt Macy             IFS="$acl_save_IFS"
238*eda14cbcSMatt Macy           fi
239*eda14cbcSMatt Macy         fi
240*eda14cbcSMatt Macy         ;;
241*eda14cbcSMatt Macy     esac
242*eda14cbcSMatt Macy     test -n "$acl_libdirstem2" || acl_libdirstem2="$acl_libdirstem"
243*eda14cbcSMatt Macy     acl_cv_libdirstems="$acl_libdirstem,$acl_libdirstem2"
244*eda14cbcSMatt Macy    ])
245*eda14cbcSMatt Macy  # Decompose acl_cv_libdirstems into acl_libdirstem and acl_libdirstem2.
246*eda14cbcSMatt Macy  acl_libdirstem=`echo "$acl_cv_libdirstems" | sed -e 's/,.*//'`
247*eda14cbcSMatt Macy  acl_libdirstem2=`echo "$acl_cv_libdirstems" | sed -e '/,/s/.*,//'`
248*eda14cbcSMatt Macy])
249