lib-prefix.m4 12 KB

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