jpayne@68: # lib-prefix.m4 serial 22 jpayne@68: dnl Copyright (C) 2001-2005, 2008-2024 Free Software Foundation, Inc. jpayne@68: dnl This file is free software; the Free Software Foundation jpayne@68: dnl gives unlimited permission to copy and/or distribute it, jpayne@68: dnl with or without modifications, as long as this notice is preserved. jpayne@68: jpayne@68: dnl From Bruno Haible. jpayne@68: jpayne@68: dnl AC_LIB_PREFIX adds to the CPPFLAGS and LDFLAGS the flags that are needed jpayne@68: dnl to access previously installed libraries. The basic assumption is that jpayne@68: dnl a user will want packages to use other packages he previously installed jpayne@68: dnl with the same --prefix option. jpayne@68: dnl This macro is not needed if only AC_LIB_LINKFLAGS is used to locate jpayne@68: dnl libraries, but is otherwise very convenient. jpayne@68: AC_DEFUN([AC_LIB_PREFIX], jpayne@68: [ jpayne@68: AC_BEFORE([$0], [AC_LIB_LINKFLAGS]) jpayne@68: AC_REQUIRE([AC_PROG_CC]) jpayne@68: AC_REQUIRE([AC_CANONICAL_HOST]) jpayne@68: AC_REQUIRE([AC_LIB_PREPARE_MULTILIB]) jpayne@68: AC_REQUIRE([AC_LIB_PREPARE_PREFIX]) jpayne@68: dnl By default, look in $includedir and $libdir. jpayne@68: use_additional=yes jpayne@68: AC_LIB_WITH_FINAL_PREFIX([ jpayne@68: eval additional_includedir=\"$includedir\" jpayne@68: eval additional_libdir=\"$libdir\" jpayne@68: ]) jpayne@68: AC_ARG_WITH([lib-prefix], jpayne@68: [[ --with-lib-prefix[=DIR] search for libraries in DIR/include and DIR/lib jpayne@68: --without-lib-prefix don't search for libraries in includedir and libdir]], jpayne@68: [ jpayne@68: if test "X$withval" = "Xno"; then jpayne@68: use_additional=no jpayne@68: else jpayne@68: if test "X$withval" = "X"; then jpayne@68: AC_LIB_WITH_FINAL_PREFIX([ jpayne@68: eval additional_includedir=\"$includedir\" jpayne@68: eval additional_libdir=\"$libdir\" jpayne@68: ]) jpayne@68: else jpayne@68: additional_includedir="$withval/include" jpayne@68: additional_libdir="$withval/$acl_libdirstem" jpayne@68: fi jpayne@68: fi jpayne@68: ]) jpayne@68: if test $use_additional = yes; then jpayne@68: dnl Potentially add $additional_includedir to $CPPFLAGS. jpayne@68: dnl But don't add it jpayne@68: dnl 1. if it's the standard /usr/include, jpayne@68: dnl 2. if it's already present in $CPPFLAGS, jpayne@68: dnl 3. if it's /usr/local/include and we are using GCC on Linux, jpayne@68: dnl 4. if it doesn't exist as a directory. jpayne@68: if test "X$additional_includedir" != "X/usr/include"; then jpayne@68: haveit= jpayne@68: for x in $CPPFLAGS; do jpayne@68: AC_LIB_WITH_FINAL_PREFIX([eval x=\"$x\"]) jpayne@68: if test "X$x" = "X-I$additional_includedir"; then jpayne@68: haveit=yes jpayne@68: break jpayne@68: fi jpayne@68: done jpayne@68: if test -z "$haveit"; then jpayne@68: if test "X$additional_includedir" = "X/usr/local/include"; then jpayne@68: if test -n "$GCC"; then jpayne@68: case $host_os in jpayne@68: linux* | gnu* | k*bsd*-gnu) haveit=yes;; jpayne@68: esac jpayne@68: fi jpayne@68: fi jpayne@68: if test -z "$haveit"; then jpayne@68: if test -d "$additional_includedir"; then jpayne@68: dnl Really add $additional_includedir to $CPPFLAGS. jpayne@68: CPPFLAGS="${CPPFLAGS}${CPPFLAGS:+ }-I$additional_includedir" jpayne@68: fi jpayne@68: fi jpayne@68: fi jpayne@68: fi jpayne@68: dnl Potentially add $additional_libdir to $LDFLAGS. jpayne@68: dnl But don't add it jpayne@68: dnl 1. if it's the standard /usr/lib, jpayne@68: dnl 2. if it's already present in $LDFLAGS, jpayne@68: dnl 3. if it's /usr/local/lib and we are using GCC on Linux, jpayne@68: dnl 4. if it doesn't exist as a directory. jpayne@68: if test "X$additional_libdir" != "X/usr/$acl_libdirstem"; then jpayne@68: haveit= jpayne@68: for x in $LDFLAGS; do jpayne@68: AC_LIB_WITH_FINAL_PREFIX([eval x=\"$x\"]) jpayne@68: if test "X$x" = "X-L$additional_libdir"; then jpayne@68: haveit=yes jpayne@68: break jpayne@68: fi jpayne@68: done jpayne@68: if test -z "$haveit"; then jpayne@68: if test "X$additional_libdir" = "X/usr/local/$acl_libdirstem"; then jpayne@68: if test -n "$GCC"; then jpayne@68: case $host_os in jpayne@68: linux*) haveit=yes;; jpayne@68: esac jpayne@68: fi jpayne@68: fi jpayne@68: if test -z "$haveit"; then jpayne@68: if test -d "$additional_libdir"; then jpayne@68: dnl Really add $additional_libdir to $LDFLAGS. jpayne@68: LDFLAGS="${LDFLAGS}${LDFLAGS:+ }-L$additional_libdir" jpayne@68: fi jpayne@68: fi jpayne@68: fi jpayne@68: fi jpayne@68: fi jpayne@68: ]) jpayne@68: jpayne@68: dnl AC_LIB_PREPARE_PREFIX creates variables acl_final_prefix, jpayne@68: dnl acl_final_exec_prefix, containing the values to which $prefix and jpayne@68: dnl $exec_prefix will expand at the end of the configure script. jpayne@68: AC_DEFUN([AC_LIB_PREPARE_PREFIX], jpayne@68: [ jpayne@68: dnl Unfortunately, prefix and exec_prefix get only finally determined jpayne@68: dnl at the end of configure. jpayne@68: if test "X$prefix" = "XNONE"; then jpayne@68: acl_final_prefix="$ac_default_prefix" jpayne@68: else jpayne@68: acl_final_prefix="$prefix" jpayne@68: fi jpayne@68: if test "X$exec_prefix" = "XNONE"; then jpayne@68: acl_final_exec_prefix='${prefix}' jpayne@68: else jpayne@68: acl_final_exec_prefix="$exec_prefix" jpayne@68: fi jpayne@68: acl_saved_prefix="$prefix" jpayne@68: prefix="$acl_final_prefix" jpayne@68: eval acl_final_exec_prefix=\"$acl_final_exec_prefix\" jpayne@68: prefix="$acl_saved_prefix" jpayne@68: ]) jpayne@68: jpayne@68: dnl AC_LIB_WITH_FINAL_PREFIX([statement]) evaluates statement, with the jpayne@68: dnl variables prefix and exec_prefix bound to the values they will have jpayne@68: dnl at the end of the configure script. jpayne@68: AC_DEFUN([AC_LIB_WITH_FINAL_PREFIX], jpayne@68: [ jpayne@68: acl_saved_prefix="$prefix" jpayne@68: prefix="$acl_final_prefix" jpayne@68: acl_saved_exec_prefix="$exec_prefix" jpayne@68: exec_prefix="$acl_final_exec_prefix" jpayne@68: $1 jpayne@68: exec_prefix="$acl_saved_exec_prefix" jpayne@68: prefix="$acl_saved_prefix" jpayne@68: ]) jpayne@68: jpayne@68: dnl AC_LIB_PREPARE_MULTILIB creates jpayne@68: dnl - a function acl_is_expected_elfclass, that tests whether standard input jpayne@68: dn; has a 32-bit or 64-bit ELF header, depending on the host CPU ABI, jpayne@68: dnl - 3 variables acl_libdirstem, acl_libdirstem2, acl_libdirstem3, containing jpayne@68: dnl the basename of the libdir to try in turn, either "lib" or "lib64" or jpayne@68: dnl "lib/64" or "lib32" or "lib/sparcv9" or "lib/amd64" or similar. jpayne@68: AC_DEFUN([AC_LIB_PREPARE_MULTILIB], jpayne@68: [ jpayne@68: dnl There is no formal standard regarding lib, lib32, and lib64. jpayne@68: dnl On most glibc systems, the current practice is that on a system supporting jpayne@68: dnl 32-bit and 64-bit instruction sets or ABIs, 64-bit libraries go under jpayne@68: dnl $prefix/lib64 and 32-bit libraries go under $prefix/lib. However, on jpayne@68: dnl Arch Linux based distributions, it's the opposite: 32-bit libraries go jpayne@68: dnl under $prefix/lib32 and 64-bit libraries go under $prefix/lib. jpayne@68: dnl We determine the compiler's default mode by looking at the compiler's jpayne@68: dnl library search path. If at least one of its elements ends in /lib64 or jpayne@68: dnl points to a directory whose absolute pathname ends in /lib64, we use that jpayne@68: dnl for 64-bit ABIs. Similarly for 32-bit ABIs. Otherwise we use the default, jpayne@68: dnl namely "lib". jpayne@68: dnl On Solaris systems, the current practice is that on a system supporting jpayne@68: dnl 32-bit and 64-bit instruction sets or ABIs, 64-bit libraries go under jpayne@68: dnl $prefix/lib/64 (which is a symlink to either $prefix/lib/sparcv9 or jpayne@68: dnl $prefix/lib/amd64) and 32-bit libraries go under $prefix/lib. jpayne@68: AC_REQUIRE([AC_CANONICAL_HOST]) jpayne@68: AC_REQUIRE([gl_HOST_CPU_C_ABI_32BIT]) jpayne@68: jpayne@68: AC_CACHE_CHECK([for ELF binary format], [gl_cv_elf], jpayne@68: [AC_EGREP_CPP([Extensible Linking Format], jpayne@68: [#if defined __ELF__ || (defined __linux__ && defined __EDG__) jpayne@68: Extensible Linking Format jpayne@68: #endif jpayne@68: ], jpayne@68: [gl_cv_elf=yes], jpayne@68: [gl_cv_elf=no]) jpayne@68: ]) jpayne@68: if test $gl_cv_elf = yes; then jpayne@68: # Extract the ELF class of a file (5th byte) in decimal. jpayne@68: # Cf. https://en.wikipedia.org/wiki/Executable_and_Linkable_Format#File_header jpayne@68: if od -A x < /dev/null >/dev/null 2>/dev/null; then jpayne@68: # Use POSIX od. jpayne@68: func_elfclass () jpayne@68: { jpayne@68: od -A n -t d1 -j 4 -N 1 jpayne@68: } jpayne@68: else jpayne@68: # Use BSD hexdump. jpayne@68: func_elfclass () jpayne@68: { jpayne@68: dd bs=1 count=1 skip=4 2>/dev/null | hexdump -e '1/1 "%3d "' jpayne@68: echo jpayne@68: } jpayne@68: fi jpayne@68: # Use 'expr', not 'test', to compare the values of func_elfclass, because on jpayne@68: # Solaris 11 OpenIndiana and Solaris 11 OmniOS, the result is 001 or 002, jpayne@68: # not 1 or 2. jpayne@68: changequote(,)dnl jpayne@68: case $HOST_CPU_C_ABI_32BIT in jpayne@68: yes) jpayne@68: # 32-bit ABI. jpayne@68: acl_is_expected_elfclass () jpayne@68: { jpayne@68: expr "`func_elfclass | sed -e 's/[ ]//g'`" = 1 > /dev/null jpayne@68: } jpayne@68: ;; jpayne@68: no) jpayne@68: # 64-bit ABI. jpayne@68: acl_is_expected_elfclass () jpayne@68: { jpayne@68: expr "`func_elfclass | sed -e 's/[ ]//g'`" = 2 > /dev/null jpayne@68: } jpayne@68: ;; jpayne@68: *) jpayne@68: # Unknown. jpayne@68: acl_is_expected_elfclass () jpayne@68: { jpayne@68: : jpayne@68: } jpayne@68: ;; jpayne@68: esac jpayne@68: changequote([,])dnl jpayne@68: else jpayne@68: acl_is_expected_elfclass () jpayne@68: { jpayne@68: : jpayne@68: } jpayne@68: fi jpayne@68: jpayne@68: dnl Allow the user to override the result by setting acl_cv_libdirstems. jpayne@68: AC_CACHE_CHECK([for the common suffixes of directories in the library search path], jpayne@68: [acl_cv_libdirstems], jpayne@68: [dnl Try 'lib' first, because that's the default for libdir in GNU, see jpayne@68: dnl . jpayne@68: acl_libdirstem=lib jpayne@68: acl_libdirstem2= jpayne@68: acl_libdirstem3= jpayne@68: case "$host_os" in jpayne@68: solaris*) jpayne@68: dnl See Solaris 10 Software Developer Collection > Solaris 64-bit Developer's Guide > The Development Environment jpayne@68: dnl . jpayne@68: dnl "Portable Makefiles should refer to any library directories using the 64 symbolic link." jpayne@68: dnl But we want to recognize the sparcv9 or amd64 subdirectory also if the jpayne@68: dnl symlink is missing, so we set acl_libdirstem2 too. jpayne@68: if test $HOST_CPU_C_ABI_32BIT = no; then jpayne@68: acl_libdirstem2=lib/64 jpayne@68: case "$host_cpu" in jpayne@68: sparc*) acl_libdirstem3=lib/sparcv9 ;; jpayne@68: i*86 | x86_64) acl_libdirstem3=lib/amd64 ;; jpayne@68: esac jpayne@68: fi jpayne@68: ;; jpayne@68: netbsd*) jpayne@68: dnl On NetBSD/sparc64, there is a 'sparc' subdirectory that contains jpayne@68: dnl 32-bit libraries. jpayne@68: if test $HOST_CPU_C_ABI_32BIT != no; then jpayne@68: case "$host_cpu" in jpayne@68: sparc*) acl_libdirstem2=lib/sparc ;; jpayne@68: esac jpayne@68: fi jpayne@68: ;; jpayne@68: *) jpayne@68: dnl If $CC generates code for a 32-bit ABI, the libraries are jpayne@68: dnl surely under $prefix/lib or $prefix/lib32, not $prefix/lib64. jpayne@68: dnl Similarly, if $CC generates code for a 64-bit ABI, the libraries jpayne@68: dnl are surely under $prefix/lib or $prefix/lib64, not $prefix/lib32. jpayne@68: dnl Find the compiler's search path. However, non-system compilers jpayne@68: dnl sometimes have odd library search paths. But we can't simply invoke jpayne@68: dnl '/usr/bin/gcc -print-search-dirs' because that would not take into jpayne@68: dnl account the -m32/-m31 or -m64 options from the $CC or $CFLAGS. jpayne@68: searchpath=`(LC_ALL=C $CC $CPPFLAGS $CFLAGS -print-search-dirs) 2>/dev/null \ jpayne@68: | sed -n -e 's,^libraries: ,,p' | sed -e 's,^=,,'` jpayne@68: if test $HOST_CPU_C_ABI_32BIT != no; then jpayne@68: # 32-bit or unknown ABI. jpayne@68: if test -d /usr/lib32; then jpayne@68: acl_libdirstem2=lib32 jpayne@68: fi jpayne@68: fi jpayne@68: if test $HOST_CPU_C_ABI_32BIT != yes; then jpayne@68: # 64-bit or unknown ABI. jpayne@68: if test -d /usr/lib64; then jpayne@68: acl_libdirstem3=lib64 jpayne@68: fi jpayne@68: fi jpayne@68: if test -n "$searchpath"; then jpayne@68: acl_saved_IFS="${IFS= }"; IFS=":" jpayne@68: for searchdir in $searchpath; do jpayne@68: if test -d "$searchdir"; then jpayne@68: case "$searchdir" in jpayne@68: */lib32/ | */lib32 ) acl_libdirstem2=lib32 ;; jpayne@68: */lib64/ | */lib64 ) acl_libdirstem3=lib64 ;; jpayne@68: */../ | */.. ) jpayne@68: # Better ignore directories of this form. They are misleading. jpayne@68: ;; jpayne@68: *) searchdir=`cd "$searchdir" && pwd` jpayne@68: case "$searchdir" in jpayne@68: */lib32 ) acl_libdirstem2=lib32 ;; jpayne@68: */lib64 ) acl_libdirstem3=lib64 ;; jpayne@68: esac ;; jpayne@68: esac jpayne@68: fi jpayne@68: done jpayne@68: IFS="$acl_saved_IFS" jpayne@68: if test $HOST_CPU_C_ABI_32BIT = yes; then jpayne@68: # 32-bit ABI. jpayne@68: acl_libdirstem3= jpayne@68: fi jpayne@68: if test $HOST_CPU_C_ABI_32BIT = no; then jpayne@68: # 64-bit ABI. jpayne@68: acl_libdirstem2= jpayne@68: fi jpayne@68: fi jpayne@68: ;; jpayne@68: esac jpayne@68: test -n "$acl_libdirstem2" || acl_libdirstem2="$acl_libdirstem" jpayne@68: test -n "$acl_libdirstem3" || acl_libdirstem3="$acl_libdirstem" jpayne@68: acl_cv_libdirstems="$acl_libdirstem,$acl_libdirstem2,$acl_libdirstem3" jpayne@68: ]) jpayne@68: dnl Decompose acl_cv_libdirstems into acl_libdirstem, acl_libdirstem2, and jpayne@68: dnl acl_libdirstem3. jpayne@68: changequote(,)dnl jpayne@68: acl_libdirstem=`echo "$acl_cv_libdirstems" | sed -e 's/,.*//'` jpayne@68: acl_libdirstem2=`echo "$acl_cv_libdirstems" | sed -e 's/^[^,]*,//' -e 's/,.*//'` jpayne@68: acl_libdirstem3=`echo "$acl_cv_libdirstems" | sed -e 's/^[^,]*,[^,]*,//' -e 's/,.*//'` jpayne@68: changequote([,])dnl jpayne@68: ])