Support more special chars in $(LIBDIR) etc
[autoconf.git] / lib / autoconf / libs.m4
blob20daf8dd4c387ad76504c75d4c2109b072041ee2
1 # This file is part of Autoconf.                       -*- Autoconf -*-
2 # Checking for libraries.
3 # Copyright (C) 1992-1996, 1998-2006, 2008-2017, 2020-2024 Free Software
4 # Foundation, Inc.
6 # This file is part of Autoconf.  This program is free
7 # software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the
9 # Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
17 # Under Section 7 of GPL version 3, you are granted additional
18 # permissions described in the Autoconf Configure Script Exception,
19 # version 3.0, as published by the Free Software Foundation.
21 # You should have received a copy of the GNU General Public License
22 # and a copy of the Autoconf Configure Script Exception along with
23 # this program; see the files COPYINGv3 and COPYING.EXCEPTION
24 # respectively.  If not, see <https://www.gnu.org/licenses/>.
26 # Written by David MacKenzie, with help from
27 # François Pinard, Karl Berry, Richard Pixley, Ian Lance Taylor,
28 # Roland McGrath, Noah Friedman, david d zuhn, and many others.
30 # Table of contents
32 # 1. Generic tests for libraries
33 # 2. Tests for specific libraries
36 ## --------------------------------- ##
37 ## 1. Generic tests for libraries.## ##
38 ## --------------------------------- ##
42 # AC_SEARCH_LIBS(FUNCTION, SEARCH-LIBS,
43 #                [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND],
44 #                [OTHER-LIBRARIES])
45 # --------------------------------------------------------
46 # Search for a library defining FUNC, if it's not already available.
47 AC_DEFUN([AC_SEARCH_LIBS],
48 [AS_VAR_PUSHDEF([ac_Search], [ac_cv_search_$1])dnl
49 AC_CACHE_CHECK([for library containing $1], [ac_Search],
50 [ac_func_search_save_LIBS=$LIBS
51 AC_LANG_CONFTEST([AC_LANG_CALL([], [$1])])
52 for ac_lib in '' $2
54   if test -z "$ac_lib"; then
55     ac_res="none required"
56   else
57     ac_res=-l$ac_lib
58     LIBS="-l$ac_lib $5 $ac_func_search_save_LIBS"
59   fi
60   AC_LINK_IFELSE([], [AS_VAR_SET([ac_Search], [$ac_res])])
61   AS_VAR_SET_IF([ac_Search], [break])
62 done
63 AS_VAR_SET_IF([ac_Search], , [AS_VAR_SET([ac_Search], [no])])
64 rm conftest.$ac_ext
65 LIBS=$ac_func_search_save_LIBS])
66 AS_VAR_COPY([ac_res], [ac_Search])
67 AS_IF([test "$ac_res" != no],
68   [test "$ac_res" = "none required" || LIBS="$ac_res $LIBS"
69   $3],
70       [$4])
71 AS_VAR_POPDEF([ac_Search])dnl
76 # AC_CHECK_LIB(LIBRARY, FUNCTION,
77 #              [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND],
78 #              [OTHER-LIBRARIES])
79 # ------------------------------------------------------
81 # Use a cache variable name containing both the library and function name,
82 # because the test really is for library $1 defining function $2, not
83 # just for library $1.  Separate tests with the same $1 and different $2s
84 # may have different results.
86 # Note that using directly AS_VAR_PUSHDEF([ac_Lib], [ac_cv_lib_$1_$2])
87 # is asking for troubles, since AC_CHECK_LIB($lib, fun) would give
88 # ac_cv_lib_$lib_fun, which is definitely not what was meant.  Hence
89 # the AS_LITERAL_IF indirection.
91 # FIXME: This macro is extremely suspicious.  It DEFINEs unconditionally,
92 # whatever the FUNCTION, in addition to not being a *S macro.  Note
93 # that the cache does depend upon the function we are looking for.
95 # It is on purpose we used 'ac_check_lib_save_LIBS' and not just
96 # 'ac_save_LIBS': there are many macros which don't want to see 'LIBS'
97 # changed but still want to use AC_CHECK_LIB, so they save 'LIBS'.
98 # And 'ac_save_LIBS' is too tempting a name, so let's leave them some
99 # freedom.
100 AC_DEFUN([AC_CHECK_LIB],
101 [m4_ifval([$3], , [AH_CHECK_LIB([$1])])dnl
102 AS_LITERAL_WORD_IF([$1],
103               [AS_VAR_PUSHDEF([ac_Lib], [ac_cv_lib_$1_$2])],
104               [AS_VAR_PUSHDEF([ac_Lib], [ac_cv_lib_$1""_$2])])dnl
105 AC_CACHE_CHECK([for $2 in -l$1], [ac_Lib],
106 [ac_check_lib_save_LIBS=$LIBS
107 LIBS="-l$1 $5 $LIBS"
108 AC_LINK_IFELSE([AC_LANG_CALL([], [$2])],
109                [AS_VAR_SET([ac_Lib], [yes])],
110                [AS_VAR_SET([ac_Lib], [no])])
111 LIBS=$ac_check_lib_save_LIBS])
112 AS_VAR_IF([ac_Lib], [yes],
113       [m4_default([$3], [AC_DEFINE_UNQUOTED(AS_TR_CPP(HAVE_LIB$1))
114   LIBS="-l$1 $LIBS"
115 ])],
116       [$4])
117 AS_VAR_POPDEF([ac_Lib])dnl
118 ])# AC_CHECK_LIB
121 # AH_CHECK_LIB(LIBNAME)
122 # ---------------------
123 m4_define([AH_CHECK_LIB],
124 [AH_TEMPLATE(AS_TR_CPP([HAVE_LIB$1]),
125              [Define to 1 if you have the '$1' library (-l$1).])])
128 # AC_HAVE_LIBRARY(LIBRARY,
129 #                 [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND],
130 #                 [OTHER-LIBRARIES])
131 # ---------------------------------------------------------
133 # This macro is equivalent to calling 'AC_CHECK_LIB' with a FUNCTION
134 # argument of 'main'.  In addition, LIBRARY can be written as any of
135 # 'foo', '-lfoo', or 'libfoo.a'.  In all of those cases, the compiler
136 # is passed '-lfoo'.  However, LIBRARY cannot be a shell variable;
137 # it must be a literal name.
138 AU_DEFUN([AC_HAVE_LIBRARY],
139 [m4_pushdef([AC_Lib_Name],
140             m4_bpatsubst(m4_bpatsubst([[$1]],
141                                     [lib\([^\.]*\)\.a], [\1]),
142                         [-l], []))dnl
143 AC_CHECK_LIB(AC_Lib_Name, main, [$2], [$3], [$4])dnl
144 ac_cv_lib_[]AC_Lib_Name()=ac_cv_lib_[]AC_Lib_Name()_main
145 m4_popdef([AC_Lib_Name])dnl
151 ## --------------------------------- ##
152 ## 2. Tests for specific libraries.  ##
153 ## --------------------------------- ##
157 # --------------------- #
158 # Checks for X window.  #
159 # --------------------- #
162 # _AC_PATH_X_XMKMF
163 # ----------------
164 # Internal subroutine of _AC_PATH_X.
165 # Set ac_x_includes and/or ac_x_libraries.
166 m4_define([_AC_PATH_X_XMKMF],
167 [AC_ARG_VAR(XMKMF, [Path to xmkmf, Makefile generator for X Window System])dnl
168 rm -f -r conftest.dir
169 if mkdir conftest.dir; then
170   cd conftest.dir
171   cat >Imakefile <<'_ACEOF'
172 incroot:
173         @printf '%s\n' incroot='${INCROOT}'
174 usrlibdir:
175         @printf '%s\n' usrlibdir='${USRLIBDIR}'
176 libdir:
177         @printf '%s\n' libdir='${LIBDIR}'
178 _ACEOF
179   if (export CC; ${XMKMF-xmkmf}) >/dev/null 2>/dev/null && test -f Makefile; then
180     # GNU make sometimes prints "make[1]: Entering ...", which would confuse us.
181     for ac_var in incroot usrlibdir libdir; do
182       eval "ac_im_$ac_var=\`\${MAKE-make} $ac_var 2>/dev/null | sed -n 's/^$ac_var=//p'\`"
183     done
184     # Open Windows xmkmf reportedly sets LIBDIR instead of USRLIBDIR.
185     for ac_extension in a so sl dylib la dll; do
186       if test ! -f "$ac_im_usrlibdir/libX11.$ac_extension" &&
187          test -f "$ac_im_libdir/libX11.$ac_extension"; then
188         ac_im_usrlibdir=$ac_im_libdir; break
189       fi
190     done
191     # Screen out bogus values from the imake configuration.  They are
192     # bogus both because they are the default anyway, and because
193     # using them would break gcc on systems where it needs fixed includes.
194     case $ac_im_incroot in
195         /usr/include) ac_x_includes= ;;
196         *) test -f "$ac_im_incroot/X11/Xos.h" && ac_x_includes=$ac_im_incroot;;
197     esac
198     case $ac_im_usrlibdir in
199         /usr/lib | /usr/lib64 | /lib | /lib64) ;;
200         *) test -d "$ac_im_usrlibdir" && ac_x_libraries=$ac_im_usrlibdir ;;
201     esac
202   fi
203   cd ..
204   rm -f -r conftest.dir
206 ])# _AC_PATH_X_XMKMF
209 # _AC_PATH_X_DIRECT
210 # -----------------
211 # Internal subroutine of _AC_PATH_X.
212 # Set ac_x_includes and/or ac_x_libraries.
213 m4_define([_AC_PATH_X_DIRECT],
214 [# Standard set of common directories for X headers.
215 # Check X11 before X11Rn because it is often a symlink to the current release.
216 ac_x_header_dirs='
217 /usr/X11/include
218 /usr/X11R7/include
219 /usr/X11R6/include
220 /usr/X11R5/include
221 /usr/X11R4/include
223 /usr/include/X11
224 /usr/include/X11R7
225 /usr/include/X11R6
226 /usr/include/X11R5
227 /usr/include/X11R4
229 /usr/local/X11/include
230 /usr/local/X11R7/include
231 /usr/local/X11R6/include
232 /usr/local/X11R5/include
233 /usr/local/X11R4/include
235 /usr/local/include/X11
236 /usr/local/include/X11R7
237 /usr/local/include/X11R6
238 /usr/local/include/X11R5
239 /usr/local/include/X11R4
241 /opt/X11/include
243 /usr/X386/include
244 /usr/x386/include
245 /usr/XFree86/include/X11
247 /usr/include
248 /usr/local/include
249 /usr/unsupported/include
250 /usr/athena/include
251 /usr/local/x11r5/include
252 /usr/lpp/Xamples/include
254 /usr/openwin/include
255 /usr/openwin/share/include'
257 if test "$ac_x_includes" = no; then
258   # Guess where to find include files, by looking for Xlib.h.
259   # First, try using that file with no special directory specified.
260   AC_PREPROC_IFELSE([AC_LANG_SOURCE([@%:@include <X11/Xlib.h>])],
261 [# We can compile using X headers with no special include directory.
262 ac_x_includes=],
263 [for ac_dir in $ac_x_header_dirs; do
264   if test -r "$ac_dir/X11/Xlib.h"; then
265     ac_x_includes=$ac_dir
266     break
267   fi
268 done])
269 fi # $ac_x_includes = no
271 if test "$ac_x_libraries" = no; then
272   # Check for the libraries.
273   # See if we find them without any special options.
274   # Don't add to $LIBS permanently.
275   ac_save_LIBS=$LIBS
276   LIBS="-lX11 $LIBS"
277   AC_LINK_IFELSE([AC_LANG_PROGRAM([@%:@include <X11/Xlib.h>],
278                                   [XrmInitialize ()])],
279                  [LIBS=$ac_save_LIBS
280 # We can link X programs with no special library path.
281 ac_x_libraries=],
282                  [LIBS=$ac_save_LIBS
283 for ac_dir in `AS_ECHO(["$ac_x_includes $ac_x_header_dirs"]) | sed s/include/lib/g`
285   # Don't even attempt the hair of trying to link an X program!
286   for ac_extension in a so sl dylib la dll; do
287     if test -r "$ac_dir/libX11.$ac_extension"; then
288       ac_x_libraries=$ac_dir
289       break 2
290     fi
291   done
292 done])
293 fi # $ac_x_libraries = no
294 ])# _AC_PATH_X_DIRECT
297 # _AC_PATH_X
298 # ----------
299 # Compute ac_cv_have_x.
300 AC_DEFUN([_AC_PATH_X],
301 [AC_REQUIRE([AC_PROG_CC])]dnl To ensure that $cross_compiling is finalized.
302 [AC_CACHE_VAL(ac_cv_have_x,
303 [# One or both of the vars are not set, and there is no cached value.
304 ac_x_includes=no
305 ac_x_libraries=no
306 # Do we need to do anything special at all?
307 ac_save_LIBS=$LIBS
308 LIBS="-lX11 $LIBS"
309 AC_LINK_IFELSE([AC_LANG_PROGRAM([@%:@include <X11/Xlib.h>],
310                                 [XrmInitialize ()])],
311   [# We can compile and link X programs with no special options.
312   ac_x_includes=
313   ac_x_libraries=])
314 LIBS="$ac_save_LIBS"
315 # If that didn't work, only try xmkmf and file system searches
316 # for native compilation.
317 AS_IF([test x"$ac_x_includes" = xno && test "$cross_compiling" = no],
318   [_AC_PATH_X_XMKMF
319   _AC_PATH_X_DIRECT])
320 # Record the results.
321 AS_CASE([$ac_x_includes,$ac_x_libraries],
322   [no,* | *,no | *\'*],
323     [# Didn't find X, or a directory has "'" in its name.
324     ac_cv_have_x="have_x=no"],
325     [# Record where we found X for the cache.
326     ac_cv_have_x="have_x=yes\
327         ac_x_includes='$ac_x_includes'\
328         ac_x_libraries='$ac_x_libraries'"])])])
331 # AC_PATH_X
332 # ---------
333 # If we find X, set shell vars x_includes and x_libraries to the
334 # paths, otherwise set no_x=yes.
335 # Uses ac_ vars as temps to allow command line to override cache and checks.
336 # --without-x overrides everything else, but does not touch the cache.
337 AN_HEADER([X11/Xlib.h],  [AC_PATH_X])
338 AC_DEFUN([AC_PATH_X],
339 [dnl Document the X abnormal options inherited from history.
340 m4_divert_once([HELP_BEGIN], [
341 X features:
342   --x-includes=DIR    X include files are in DIR
343   --x-libraries=DIR   X library files are in DIR])dnl
344 AC_MSG_CHECKING([for X])
346 AC_ARG_WITH(x, [  --with-x                use the X Window System])
347 # $have_x is 'yes', 'no', 'disabled', or empty when we do not yet know.
348 if test "x$with_x" = xno; then
349   # The user explicitly disabled X.
350   have_x=disabled
351 else
352   case $x_includes,$x_libraries in #(
353     *\'*) AC_MSG_ERROR([cannot use X directory names containing ']);; #(
354     *,NONE | NONE,*) _AC_PATH_X;; #(
355     *) have_x=yes;;
356   esac
357   eval "$ac_cv_have_x"
358 fi # $with_x != no
360 if test "$have_x" != yes; then
361   AC_MSG_RESULT([$have_x])
362   no_x=yes
363 else
364   # If each of the values was on the command line, it overrides each guess.
365   test "x$x_includes" = xNONE && x_includes=$ac_x_includes
366   test "x$x_libraries" = xNONE && x_libraries=$ac_x_libraries
367   # Update the cache value to reflect the command line values.
368   ac_cv_have_x="have_x=yes\
369         ac_x_includes='$x_includes'\
370         ac_x_libraries='$x_libraries'"
371   AC_MSG_RESULT([libraries $x_libraries, headers $x_includes])
373 ])# AC_PATH_X
377 # AC_PATH_XTRA
378 # ------------
379 # Find additional X libraries, magic flags, etc.
380 AC_DEFUN([AC_PATH_XTRA],
381 [AC_REQUIRE([AC_PATH_X])dnl
382 if test "$no_x" = yes; then
383   # Not all programs may use this symbol, but it does not hurt to define it.
384   AC_DEFINE([X_DISPLAY_MISSING], 1,
385             [Define to 1 if the X Window System is missing or not being used.])
386   X_CFLAGS= X_PRE_LIBS= X_LIBS= X_EXTRA_LIBS=
387 else
388   if test -n "$x_includes"; then
389     X_CFLAGS="$X_CFLAGS -I$x_includes"
390   fi
392   # It would also be nice to do this for all -L options, not just this one.
393   if test -n "$x_libraries"; then
394     X_LIBS="$X_LIBS -L$x_libraries"
395     # For Solaris; some versions of Sun CC require a space after -R and
396     # others require no space.  Words are not sufficient . . . .
397     AC_MSG_CHECKING([whether -R must be followed by a space])
398     ac_xsave_LIBS=$LIBS; LIBS="$LIBS -R$x_libraries"
399     ac_xsave_[]_AC_LANG_ABBREV[]_werror_flag=$ac_[]_AC_LANG_ABBREV[]_werror_flag
400     ac_[]_AC_LANG_ABBREV[]_werror_flag=yes
401     AC_LINK_IFELSE([AC_LANG_PROGRAM()],
402       [AC_MSG_RESULT([no])
403        X_LIBS="$X_LIBS -R$x_libraries"],
404       [LIBS="$ac_xsave_LIBS -R $x_libraries"
405        AC_LINK_IFELSE([AC_LANG_PROGRAM()],
406          [AC_MSG_RESULT([yes])
407           X_LIBS="$X_LIBS -R $x_libraries"],
408          [AC_MSG_RESULT([neither works])])])
409     ac_[]_AC_LANG_ABBREV[]_werror_flag=$ac_xsave_[]_AC_LANG_ABBREV[]_werror_flag
410     LIBS=$ac_xsave_LIBS
411   fi
413   # Check for system-dependent libraries X programs must link with.
414   # Do this before checking for the system-independent R6 libraries
415   # (-lICE), since we may need -lsocket or whatever for X linking.
417   if test "$ISC" = yes; then
418     X_EXTRA_LIBS="$X_EXTRA_LIBS -lnsl_s -linet"
419   else
420     # Martyn Johnson says this is needed for Ultrix, if the X
421     # libraries were built with DECnet support.  And Karl Berry says
422     # the Alpha needs dnet_stub (dnet does not exist).
423     ac_xsave_LIBS="$LIBS"; LIBS="$LIBS $X_LIBS -lX11"
424     AC_LINK_IFELSE([AC_LANG_CALL([], [XOpenDisplay])],
425                    [],
426     [AC_CHECK_LIB(dnet, dnet_ntoa, [X_EXTRA_LIBS="$X_EXTRA_LIBS -ldnet"])
427     if test $ac_cv_lib_dnet_dnet_ntoa = no; then
428       AC_CHECK_LIB(dnet_stub, dnet_ntoa,
429         [X_EXTRA_LIBS="$X_EXTRA_LIBS -ldnet_stub"])
430     fi])
431     LIBS="$ac_xsave_LIBS"
433     # msh@cis.ufl.edu says -lnsl (and -lsocket) are needed for his 386/AT,
434     # to get the SysV transport functions.
435     # Chad R. Larson says the Pyramis MIS-ES running DC/OSx (SVR4)
436     # needs -lnsl.
437     # The nsl library prevents programs from opening the X display
438     # on Irix 5.2, according to T.E. Dickey.
439     # The functions gethostbyname, getservbyname, and inet_addr are
440     # in -lbsd on LynxOS 3.0.1/i386, according to Lars Hecking.
441     AC_CHECK_FUNC(gethostbyname)
442     if test $ac_cv_func_gethostbyname = no; then
443       AC_CHECK_LIB(nsl, gethostbyname, X_EXTRA_LIBS="$X_EXTRA_LIBS -lnsl")
444       if test $ac_cv_lib_nsl_gethostbyname = no; then
445         AC_CHECK_LIB(bsd, gethostbyname, X_EXTRA_LIBS="$X_EXTRA_LIBS -lbsd")
446       fi
447     fi
449     # lieder@skyler.mavd.honeywell.com says without -lsocket,
450     # socket/setsockopt and other routines are undefined under SCO ODT
451     # 2.0.  But -lsocket is broken on IRIX 5.2 (and is not necessary
452     # on later versions), says Simon Leinen: it contains gethostby*
453     # variants that don't use the name server (or something).  -lsocket
454     # must be given before -lnsl if both are needed.  We assume that
455     # if connect needs -lnsl, so does gethostbyname.
456     AC_CHECK_FUNC(connect)
457     if test $ac_cv_func_connect = no; then
458       AC_CHECK_LIB(socket, connect, X_EXTRA_LIBS="-lsocket $X_EXTRA_LIBS", ,
459         $X_EXTRA_LIBS)
460     fi
462     # Guillermo Gomez says -lposix is necessary on A/UX.
463     AC_CHECK_FUNC(remove)
464     if test $ac_cv_func_remove = no; then
465       AC_CHECK_LIB(posix, remove, X_EXTRA_LIBS="$X_EXTRA_LIBS -lposix")
466     fi
468     # BSDI BSD/OS 2.1 needs -lipc for XOpenDisplay.
469     AC_CHECK_FUNC(shmat)
470     if test $ac_cv_func_shmat = no; then
471       AC_CHECK_LIB(ipc, shmat, X_EXTRA_LIBS="$X_EXTRA_LIBS -lipc")
472     fi
473   fi
475   # Check for libraries that X11R6 Xt/Xaw programs need.
476   ac_save_LDFLAGS=$LDFLAGS
477   test -n "$x_libraries" && LDFLAGS="$LDFLAGS -L$x_libraries"
478   # SM needs ICE to (dynamically) link under SunOS 4.x (so we have to
479   # check for ICE first), but we must link in the order -lSM -lICE or
480   # we get undefined symbols.  So assume we have SM if we have ICE.
481   # These have to be linked with before -lX11, unlike the other
482   # libraries we check for below, so use a different variable.
483   # John Interrante, Karl Berry
484   AC_CHECK_LIB(ICE, IceConnectionNumber,
485     [X_PRE_LIBS="$X_PRE_LIBS -lSM -lICE"], , $X_EXTRA_LIBS)
486   LDFLAGS=$ac_save_LDFLAGS
489 AC_SUBST(X_CFLAGS)dnl
490 AC_SUBST(X_PRE_LIBS)dnl
491 AC_SUBST(X_LIBS)dnl
492 AC_SUBST(X_EXTRA_LIBS)dnl
493 ])# AC_PATH_XTRA