lib-prefix.m4 (12582B)
1 # lib-prefix.m4 2 # serial 23 3 dnl Copyright (C) 2001-2005, 2008-2024 Free Software Foundation, Inc. 4 dnl This file is free software; the Free Software Foundation 5 dnl gives unlimited permission to copy and/or distribute it, 6 dnl with or without modifications, as long as this notice is preserved. 7 dnl This file is offered as-is, without any warranty. 8 9 dnl From Bruno Haible. 10 11 dnl AC_LIB_PREFIX adds to the CPPFLAGS and LDFLAGS the flags that are needed 12 dnl to access previously installed libraries. The basic assumption is that 13 dnl a user will want packages to use other packages he previously installed 14 dnl with the same --prefix option. 15 dnl This macro is not needed if only AC_LIB_LINKFLAGS is used to locate 16 dnl libraries, but is otherwise very convenient. 17 AC_DEFUN([AC_LIB_PREFIX], 18 [ 19 AC_BEFORE([$0], [AC_LIB_LINKFLAGS]) 20 AC_REQUIRE([AC_PROG_CC]) 21 AC_REQUIRE([AC_CANONICAL_HOST]) 22 AC_REQUIRE([AC_LIB_PREPARE_MULTILIB]) 23 AC_REQUIRE([AC_LIB_PREPARE_PREFIX]) 24 dnl By default, look in $includedir and $libdir. 25 use_additional=yes 26 AC_LIB_WITH_FINAL_PREFIX([ 27 eval additional_includedir=\"$includedir\" 28 eval additional_libdir=\"$libdir\" 29 ]) 30 AC_ARG_WITH([lib-prefix], 31 [[ --with-lib-prefix[=DIR] search for libraries in DIR/include and DIR/lib 32 --without-lib-prefix don't search for libraries in includedir and libdir]], 33 [ 34 if test "X$withval" = "Xno"; then 35 use_additional=no 36 else 37 if test "X$withval" = "X"; then 38 AC_LIB_WITH_FINAL_PREFIX([ 39 eval additional_includedir=\"$includedir\" 40 eval additional_libdir=\"$libdir\" 41 ]) 42 else 43 additional_includedir="$withval/include" 44 additional_libdir="$withval/$acl_libdirstem" 45 fi 46 fi 47 ]) 48 if test $use_additional = yes; then 49 dnl Potentially add $additional_includedir to $CPPFLAGS. 50 dnl But don't add it 51 dnl 1. if it's the standard /usr/include, 52 dnl 2. if it's already present in $CPPFLAGS, 53 dnl 3. if it's /usr/local/include and we are using GCC on Linux, 54 dnl 4. if it doesn't exist as a directory. 55 if test "X$additional_includedir" != "X/usr/include"; then 56 haveit= 57 for x in $CPPFLAGS; do 58 AC_LIB_WITH_FINAL_PREFIX([eval x=\"$x\"]) 59 if test "X$x" = "X-I$additional_includedir"; then 60 haveit=yes 61 break 62 fi 63 done 64 if test -z "$haveit"; then 65 if test "X$additional_includedir" = "X/usr/local/include"; then 66 if test -n "$GCC"; then 67 case $host_os in 68 linux* | gnu* | k*bsd*-gnu) haveit=yes;; 69 esac 70 fi 71 fi 72 if test -z "$haveit"; then 73 if test -d "$additional_includedir"; then 74 dnl Really add $additional_includedir to $CPPFLAGS. 75 CPPFLAGS="${CPPFLAGS}${CPPFLAGS:+ }-I$additional_includedir" 76 fi 77 fi 78 fi 79 fi 80 dnl Potentially add $additional_libdir to $LDFLAGS. 81 dnl But don't add it 82 dnl 1. if it's the standard /usr/lib, 83 dnl 2. if it's already present in $LDFLAGS, 84 dnl 3. if it's /usr/local/lib and we are using GCC on Linux, 85 dnl 4. if it doesn't exist as a directory. 86 if test "X$additional_libdir" != "X/usr/$acl_libdirstem"; then 87 haveit= 88 for x in $LDFLAGS; do 89 AC_LIB_WITH_FINAL_PREFIX([eval x=\"$x\"]) 90 if test "X$x" = "X-L$additional_libdir"; then 91 haveit=yes 92 break 93 fi 94 done 95 if test -z "$haveit"; then 96 if test "X$additional_libdir" = "X/usr/local/$acl_libdirstem"; then 97 if test -n "$GCC"; then 98 case $host_os in 99 linux*) haveit=yes;; 100 esac 101 fi 102 fi 103 if test -z "$haveit"; then 104 if test -d "$additional_libdir"; then 105 dnl Really add $additional_libdir to $LDFLAGS. 106 LDFLAGS="${LDFLAGS}${LDFLAGS:+ }-L$additional_libdir" 107 fi 108 fi 109 fi 110 fi 111 fi 112 ]) 113 114 dnl AC_LIB_PREPARE_PREFIX creates variables acl_final_prefix, 115 dnl acl_final_exec_prefix, containing the values to which $prefix and 116 dnl $exec_prefix will expand at the end of the configure script. 117 AC_DEFUN([AC_LIB_PREPARE_PREFIX], 118 [ 119 dnl Unfortunately, prefix and exec_prefix get only finally determined 120 dnl at the end of configure. 121 if test "X$prefix" = "XNONE"; then 122 acl_final_prefix="$ac_default_prefix" 123 else 124 acl_final_prefix="$prefix" 125 fi 126 if test "X$exec_prefix" = "XNONE"; then 127 acl_final_exec_prefix='${prefix}' 128 else 129 acl_final_exec_prefix="$exec_prefix" 130 fi 131 acl_saved_prefix="$prefix" 132 prefix="$acl_final_prefix" 133 eval acl_final_exec_prefix=\"$acl_final_exec_prefix\" 134 prefix="$acl_saved_prefix" 135 ]) 136 137 dnl AC_LIB_WITH_FINAL_PREFIX([statement]) evaluates statement, with the 138 dnl variables prefix and exec_prefix bound to the values they will have 139 dnl at the end of the configure script. 140 AC_DEFUN([AC_LIB_WITH_FINAL_PREFIX], 141 [ 142 acl_saved_prefix="$prefix" 143 prefix="$acl_final_prefix" 144 acl_saved_exec_prefix="$exec_prefix" 145 exec_prefix="$acl_final_exec_prefix" 146 $1 147 exec_prefix="$acl_saved_exec_prefix" 148 prefix="$acl_saved_prefix" 149 ]) 150 151 dnl AC_LIB_PREPARE_MULTILIB creates 152 dnl - a function acl_is_expected_elfclass, that tests whether standard input 153 dn; has a 32-bit or 64-bit ELF header, depending on the host CPU ABI, 154 dnl - 3 variables acl_libdirstem, acl_libdirstem2, acl_libdirstem3, containing 155 dnl the basename of the libdir to try in turn, either "lib" or "lib64" or 156 dnl "lib/64" or "lib32" or "lib/sparcv9" or "lib/amd64" or similar. 157 AC_DEFUN([AC_LIB_PREPARE_MULTILIB], 158 [ 159 dnl There is no formal standard regarding lib, lib32, and lib64. 160 dnl On most glibc systems, the current practice is that on a system supporting 161 dnl 32-bit and 64-bit instruction sets or ABIs, 64-bit libraries go under 162 dnl $prefix/lib64 and 32-bit libraries go under $prefix/lib. However, on 163 dnl Arch Linux based distributions, it's the opposite: 32-bit libraries go 164 dnl under $prefix/lib32 and 64-bit libraries go under $prefix/lib. 165 dnl We determine the compiler's default mode by looking at the compiler's 166 dnl library search path. If at least one of its elements ends in /lib64 or 167 dnl points to a directory whose absolute pathname ends in /lib64, we use that 168 dnl for 64-bit ABIs. Similarly for 32-bit ABIs. Otherwise we use the default, 169 dnl namely "lib". 170 dnl On Solaris systems, the current practice is that on a system supporting 171 dnl 32-bit and 64-bit instruction sets or ABIs, 64-bit libraries go under 172 dnl $prefix/lib/64 (which is a symlink to either $prefix/lib/sparcv9 or 173 dnl $prefix/lib/amd64) and 32-bit libraries go under $prefix/lib. 174 AC_REQUIRE([AC_CANONICAL_HOST]) 175 AC_REQUIRE([gl_HOST_CPU_C_ABI_32BIT]) 176 177 AC_CACHE_CHECK([for ELF binary format], [gl_cv_elf], 178 [AC_EGREP_CPP([Extensible Linking Format], 179 [#if defined __ELF__ || (defined __linux__ && (defined __EDG__ || defined __SUNPRO_C)) 180 Extensible Linking Format 181 #endif 182 ], 183 [gl_cv_elf=yes], 184 [gl_cv_elf=no]) 185 ]) 186 if test $gl_cv_elf = yes; then 187 # Extract the ELF class of a file (5th byte) in decimal. 188 # Cf. https://en.wikipedia.org/wiki/Executable_and_Linkable_Format#File_header 189 if od -A x < /dev/null >/dev/null 2>/dev/null; then 190 # Use POSIX od. 191 func_elfclass () 192 { 193 od -A n -t d1 -j 4 -N 1 194 } 195 else 196 # Use BSD hexdump. 197 func_elfclass () 198 { 199 dd bs=1 count=1 skip=4 2>/dev/null | hexdump -e '1/1 "%3d "' 200 echo 201 } 202 fi 203 # Use 'expr', not 'test', to compare the values of func_elfclass, because on 204 # Solaris 11 OpenIndiana and Solaris 11 OmniOS, the result is 001 or 002, 205 # not 1 or 2. 206 changequote(,)dnl 207 case $HOST_CPU_C_ABI_32BIT in 208 yes) 209 # 32-bit ABI. 210 acl_is_expected_elfclass () 211 { 212 expr "`func_elfclass | sed -e 's/[ ]//g'`" = 1 > /dev/null 213 } 214 ;; 215 no) 216 # 64-bit ABI. 217 acl_is_expected_elfclass () 218 { 219 expr "`func_elfclass | sed -e 's/[ ]//g'`" = 2 > /dev/null 220 } 221 ;; 222 *) 223 # Unknown. 224 acl_is_expected_elfclass () 225 { 226 : 227 } 228 ;; 229 esac 230 changequote([,])dnl 231 else 232 acl_is_expected_elfclass () 233 { 234 : 235 } 236 fi 237 238 dnl Allow the user to override the result by setting acl_cv_libdirstems. 239 AC_CACHE_CHECK([for the common suffixes of directories in the library search path], 240 [acl_cv_libdirstems], 241 [dnl Try 'lib' first, because that's the default for libdir in GNU, see 242 dnl <https://www.gnu.org/prep/standards/html_node/Directory-Variables.html>. 243 acl_libdirstem=lib 244 acl_libdirstem2= 245 acl_libdirstem3= 246 case "$host_os" in 247 solaris*) 248 dnl See Solaris 10 Software Developer Collection > Solaris 64-bit Developer's Guide > The Development Environment 249 dnl <https://docs.oracle.com/cd/E19253-01/816-5138/dev-env/index.html>. 250 dnl "Portable Makefiles should refer to any library directories using the 64 symbolic link." 251 dnl But we want to recognize the sparcv9 or amd64 subdirectory also if the 252 dnl symlink is missing, so we set acl_libdirstem2 too. 253 if test $HOST_CPU_C_ABI_32BIT = no; then 254 acl_libdirstem2=lib/64 255 case "$host_cpu" in 256 sparc*) acl_libdirstem3=lib/sparcv9 ;; 257 i*86 | x86_64) acl_libdirstem3=lib/amd64 ;; 258 esac 259 fi 260 ;; 261 netbsd*) 262 dnl On NetBSD/sparc64, there is a 'sparc' subdirectory that contains 263 dnl 32-bit libraries. 264 if test $HOST_CPU_C_ABI_32BIT != no; then 265 case "$host_cpu" in 266 sparc*) acl_libdirstem2=lib/sparc ;; 267 esac 268 fi 269 ;; 270 *) 271 dnl If $CC generates code for a 32-bit ABI, the libraries are 272 dnl surely under $prefix/lib or $prefix/lib32, not $prefix/lib64. 273 dnl Similarly, if $CC generates code for a 64-bit ABI, the libraries 274 dnl are surely under $prefix/lib or $prefix/lib64, not $prefix/lib32. 275 dnl Find the compiler's search path. However, non-system compilers 276 dnl sometimes have odd library search paths. But we can't simply invoke 277 dnl '/usr/bin/gcc -print-search-dirs' because that would not take into 278 dnl account the -m32/-m31 or -m64 options from the $CC or $CFLAGS. 279 searchpath=`(LC_ALL=C $CC $CPPFLAGS $CFLAGS -print-search-dirs) 2>/dev/null \ 280 | sed -n -e 's,^libraries: ,,p' | sed -e 's,^=,,'` 281 if test $HOST_CPU_C_ABI_32BIT != no; then 282 # 32-bit or unknown ABI. 283 if test -d /usr/lib32; then 284 acl_libdirstem2=lib32 285 fi 286 fi 287 if test $HOST_CPU_C_ABI_32BIT != yes; then 288 # 64-bit or unknown ABI. 289 if test -d /usr/lib64; then 290 acl_libdirstem3=lib64 291 fi 292 fi 293 if test -n "$searchpath"; then 294 acl_saved_IFS="${IFS= }"; IFS=":" 295 for searchdir in $searchpath; do 296 if test -d "$searchdir"; then 297 case "$searchdir" in 298 */lib32/ | */lib32 ) acl_libdirstem2=lib32 ;; 299 */lib64/ | */lib64 ) acl_libdirstem3=lib64 ;; 300 */../ | */.. ) 301 # Better ignore directories of this form. They are misleading. 302 ;; 303 *) searchdir=`cd "$searchdir" && pwd` 304 case "$searchdir" in 305 */lib32 ) acl_libdirstem2=lib32 ;; 306 */lib64 ) acl_libdirstem3=lib64 ;; 307 esac ;; 308 esac 309 fi 310 done 311 IFS="$acl_saved_IFS" 312 if test $HOST_CPU_C_ABI_32BIT = yes; then 313 # 32-bit ABI. 314 acl_libdirstem3= 315 fi 316 if test $HOST_CPU_C_ABI_32BIT = no; then 317 # 64-bit ABI. 318 acl_libdirstem2= 319 fi 320 fi 321 ;; 322 esac 323 test -n "$acl_libdirstem2" || acl_libdirstem2="$acl_libdirstem" 324 test -n "$acl_libdirstem3" || acl_libdirstem3="$acl_libdirstem" 325 acl_cv_libdirstems="$acl_libdirstem,$acl_libdirstem2,$acl_libdirstem3" 326 ]) 327 dnl Decompose acl_cv_libdirstems into acl_libdirstem, acl_libdirstem2, and 328 dnl acl_libdirstem3. 329 changequote(,)dnl 330 acl_libdirstem=`echo "$acl_cv_libdirstems" | sed -e 's/,.*//'` 331 acl_libdirstem2=`echo "$acl_cv_libdirstems" | sed -e 's/^[^,]*,//' -e 's/,.*//'` 332 acl_libdirstem3=`echo "$acl_cv_libdirstems" | sed -e 's/^[^,]*,[^,]*,//' -e 's/,.*//'` 333 changequote([,])dnl 334 ])