2
0

lib-prefix.m4 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. # lib-prefix.m4 serial 17
  2. dnl Copyright (C) 2001-2005, 2008-2020 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. [#ifdef __ELF__
  172. Extensible Linking Format
  173. #endif
  174. ],
  175. [gl_cv_elf=yes],
  176. [gl_cv_elf=no])
  177. ])
  178. if test $gl_cv_elf; 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. changequote(,)dnl
  196. case $HOST_CPU_C_ABI_32BIT in
  197. yes)
  198. # 32-bit ABI.
  199. acl_is_expected_elfclass ()
  200. {
  201. test "`func_elfclass | sed -e 's/[ ]//g'`" = 1
  202. }
  203. ;;
  204. no)
  205. # 64-bit ABI.
  206. acl_is_expected_elfclass ()
  207. {
  208. test "`func_elfclass | sed -e 's/[ ]//g'`" = 2
  209. }
  210. ;;
  211. *)
  212. # Unknown.
  213. acl_is_expected_elfclass ()
  214. {
  215. :
  216. }
  217. ;;
  218. esac
  219. changequote([,])dnl
  220. else
  221. acl_is_expected_elfclass ()
  222. {
  223. :
  224. }
  225. fi
  226. dnl Allow the user to override the result by setting acl_cv_libdirstems.
  227. AC_CACHE_CHECK([for the common suffixes of directories in the library search path],
  228. [acl_cv_libdirstems],
  229. [dnl Try 'lib' first, because that's the default for libdir in GNU, see
  230. dnl <https://www.gnu.org/prep/standards/html_node/Directory-Variables.html>.
  231. acl_libdirstem=lib
  232. acl_libdirstem2=
  233. acl_libdirstem3=
  234. case "$host_os" in
  235. solaris*)
  236. dnl See Solaris 10 Software Developer Collection > Solaris 64-bit Developer's Guide > The Development Environment
  237. dnl <https://docs.oracle.com/cd/E19253-01/816-5138/dev-env/index.html>.
  238. dnl "Portable Makefiles should refer to any library directories using the 64 symbolic link."
  239. dnl But we want to recognize the sparcv9 or amd64 subdirectory also if the
  240. dnl symlink is missing, so we set acl_libdirstem2 too.
  241. if test $HOST_CPU_C_ABI_32BIT = no; then
  242. acl_libdirstem2=lib/64
  243. case "$host_cpu" in
  244. sparc*) acl_libdirstem3=lib/sparcv9 ;;
  245. i*86 | x86_64) acl_libdirstem3=lib/amd64 ;;
  246. esac
  247. fi
  248. ;;
  249. *)
  250. dnl If $CC generates code for a 32-bit ABI, the libraries are
  251. dnl surely under $prefix/lib or $prefix/lib32, not $prefix/lib64.
  252. dnl Similarly, if $CC generates code for a 64-bit ABI, the libraries
  253. dnl are surely under $prefix/lib or $prefix/lib64, not $prefix/lib32.
  254. dnl Find the compiler's search path. However, non-system compilers
  255. dnl sometimes have odd library search paths. But we can't simply invoke
  256. dnl '/usr/bin/gcc -print-search-dirs' because that would not take into
  257. dnl account the -m32/-m31 or -m64 options from the $CC or $CFLAGS.
  258. searchpath=`(LC_ALL=C $CC $CPPFLAGS $CFLAGS -print-search-dirs) 2>/dev/null \
  259. | sed -n -e 's,^libraries: ,,p' | sed -e 's,^=,,'`
  260. if test $HOST_CPU_C_ABI_32BIT != no; then
  261. # 32-bit or unknown ABI.
  262. if test -d /usr/lib32; then
  263. acl_libdirstem2=lib32
  264. fi
  265. fi
  266. if test $HOST_CPU_C_ABI_32BIT != yes; then
  267. # 64-bit or unknown ABI.
  268. if test -d /usr/lib64; then
  269. acl_libdirstem3=lib64
  270. fi
  271. fi
  272. if test -n "$searchpath"; then
  273. acl_save_IFS="${IFS= }"; IFS=":"
  274. for searchdir in $searchpath; do
  275. if test -d "$searchdir"; then
  276. case "$searchdir" in
  277. */lib32/ | */lib32 ) acl_libdirstem2=lib32 ;;
  278. */lib64/ | */lib64 ) acl_libdirstem3=lib64 ;;
  279. */../ | */.. )
  280. # Better ignore directories of this form. They are misleading.
  281. ;;
  282. *) searchdir=`cd "$searchdir" && pwd`
  283. case "$searchdir" in
  284. */lib32 ) acl_libdirstem2=lib32 ;;
  285. */lib64 ) acl_libdirstem3=lib64 ;;
  286. esac ;;
  287. esac
  288. fi
  289. done
  290. IFS="$acl_save_IFS"
  291. if test $HOST_CPU_C_ABI_32BIT = yes; then
  292. # 32-bit ABI.
  293. acl_libdirstem3=
  294. fi
  295. if test $HOST_CPU_C_ABI_32BIT = no; then
  296. # 64-bit ABI.
  297. acl_libdirstem2=
  298. fi
  299. fi
  300. ;;
  301. esac
  302. test -n "$acl_libdirstem2" || acl_libdirstem2="$acl_libdirstem"
  303. test -n "$acl_libdirstem3" || acl_libdirstem3="$acl_libdirstem"
  304. acl_cv_libdirstems="$acl_libdirstem,$acl_libdirstem2,$acl_libdirstem3"
  305. ])
  306. dnl Decompose acl_cv_libdirstems into acl_libdirstem, acl_libdirstem2, and
  307. dnl acl_libdirstem3.
  308. changequote(,)dnl
  309. acl_libdirstem=`echo "$acl_cv_libdirstems" | sed -e 's/,.*//'`
  310. acl_libdirstem2=`echo "$acl_cv_libdirstems" | sed -e 's/^[^,]*,//' -e 's/,.*//'`
  311. acl_libdirstem3=`echo "$acl_cv_libdirstems" | sed -e 's/^[^,]*,[^,]*,//' -e 's/,.*//'`
  312. changequote([,])dnl
  313. ])