1 # lib-link.m4 serial 6 (gettext-0.14.3)
2 dnl Copyright (C) 2001-2005 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.
11 dnl AC_LIB_LINKFLAGS(name [, dependencies]) searches for libname and
12 dnl the libraries corresponding to explicit and implicit dependencies.
13 dnl Sets and AC_SUBSTs the LIB${NAME} and LTLIB${NAME} variables and
14 dnl augments the CPPFLAGS variable.
15 AC_DEFUN([AC_LIB_LINKFLAGS],
17 AC_REQUIRE([AC_LIB_PREPARE_PREFIX])
18 AC_REQUIRE([AC_LIB_RPATH])
19 define([Name],[translit([$1],[./-], [___])])
20 define([NAME],[translit([$1],[abcdefghijklmnopqrstuvwxyz./-],
21 [ABCDEFGHIJKLMNOPQRSTUVWXYZ___])])
22 AC_CACHE_CHECK([how to link with lib[]$1], [ac_cv_lib[]Name[]_libs], [
23 AC_LIB_LINKFLAGS_BODY([$1], [$2])
24 ac_cv_lib[]Name[]_libs="$LIB[]NAME"
25 ac_cv_lib[]Name[]_ltlibs="$LTLIB[]NAME"
26 ac_cv_lib[]Name[]_cppflags="$INC[]NAME"
28 LIB[]NAME="$ac_cv_lib[]Name[]_libs"
29 LTLIB[]NAME="$ac_cv_lib[]Name[]_ltlibs"
30 INC[]NAME="$ac_cv_lib[]Name[]_cppflags"
31 AC_LIB_APPENDTOVAR([CPPFLAGS], [$INC]NAME)
34 dnl Also set HAVE_LIB[]NAME so that AC_LIB_HAVE_LINKFLAGS can reuse the
35 dnl results of this search when this library appears as a dependency.
41 dnl AC_LIB_HAVE_LINKFLAGS(name, dependencies, includes, testcode)
42 dnl searches for libname and the libraries corresponding to explicit and
43 dnl implicit dependencies, together with the specified include files and
44 dnl the ability to compile and link the specified testcode. If found, it
45 dnl sets and AC_SUBSTs HAVE_LIB${NAME}=yes and the LIB${NAME} and
46 dnl LTLIB${NAME} variables and augments the CPPFLAGS variable, and
47 dnl #defines HAVE_LIB${NAME} to 1. Otherwise, it sets and AC_SUBSTs
48 dnl HAVE_LIB${NAME}=no and LIB${NAME} and LTLIB${NAME} to empty.
49 AC_DEFUN([AC_LIB_HAVE_LINKFLAGS],
51 AC_REQUIRE([AC_LIB_PREPARE_PREFIX])
52 AC_REQUIRE([AC_LIB_RPATH])
53 define([Name],[translit([$1],[./-], [___])])
54 define([NAME],[translit([$1],[abcdefghijklmnopqrstuvwxyz./-],
55 [ABCDEFGHIJKLMNOPQRSTUVWXYZ___])])
57 dnl Search for lib[]Name and define LIB[]NAME, LTLIB[]NAME and INC[]NAME
59 AC_LIB_LINKFLAGS_BODY([$1], [$2])
61 dnl Add $INC[]NAME to CPPFLAGS before performing the following checks,
62 dnl because if the user has installed lib[]Name and not disabled its use
63 dnl via --without-lib[]Name-prefix, he wants to use it.
64 ac_save_CPPFLAGS="$CPPFLAGS"
65 AC_LIB_APPENDTOVAR([CPPFLAGS], [$INC]NAME)
67 AC_CACHE_CHECK([for lib[]$1], [ac_cv_lib[]Name], [
69 LIBS="$LIBS $LIB[]NAME"
70 AC_TRY_LINK([$3], [$4], [ac_cv_lib[]Name=yes], [ac_cv_lib[]Name=no])
73 if test "$ac_cv_lib[]Name" = yes; then
75 AC_DEFINE([HAVE_LIB]NAME, 1, [Define if you have the $1 library.])
76 AC_MSG_CHECKING([how to link with lib[]$1])
77 AC_MSG_RESULT([$LIB[]NAME])
80 dnl If $LIB[]NAME didn't lead to a usable library, we don't need
81 dnl $INC[]NAME either.
82 CPPFLAGS="$ac_save_CPPFLAGS"
86 AC_SUBST([HAVE_LIB]NAME)
93 dnl Determine the platform dependent parameters needed to use rpath:
94 dnl libext, shlibext, hardcode_libdir_flag_spec, hardcode_libdir_separator,
95 dnl hardcode_direct, hardcode_minus_L.
96 AC_DEFUN([AC_LIB_RPATH],
98 dnl Tell automake >= 1.10 to complain if config.rpath is missing.
99 m4_ifdef([AC_REQUIRE_AUX_FILE], [AC_REQUIRE_AUX_FILE([config.rpath])])
100 AC_REQUIRE([AC_PROG_CC]) dnl we use $CC, $GCC, $LDFLAGS
101 AC_REQUIRE([AC_LIB_PROG_LD]) dnl we use $LD, $with_gnu_ld
102 AC_REQUIRE([AC_CANONICAL_HOST]) dnl we use $host
103 AC_REQUIRE([AC_CONFIG_AUX_DIR_DEFAULT]) dnl we use $ac_aux_dir
104 AC_CACHE_CHECK([for shared library run path origin], acl_cv_rpath, [
105 CC="$CC" GCC="$GCC" LDFLAGS="$LDFLAGS" LD="$LD" with_gnu_ld="$with_gnu_ld" \
106 ${CONFIG_SHELL-/bin/sh} "$ac_aux_dir/config.rpath" "$host" > conftest.sh
112 libext="$acl_cv_libext"
113 shlibext="$acl_cv_shlibext"
114 hardcode_libdir_flag_spec="$acl_cv_hardcode_libdir_flag_spec"
115 hardcode_libdir_separator="$acl_cv_hardcode_libdir_separator"
116 hardcode_direct="$acl_cv_hardcode_direct"
117 hardcode_minus_L="$acl_cv_hardcode_minus_L"
118 dnl Determine whether the user wants rpath handling at all.
120 [ --disable-rpath do not hardcode runtime library paths],
124 dnl AC_LIB_LINKFLAGS_BODY(name [, dependencies]) searches for libname and
125 dnl the libraries corresponding to explicit and implicit dependencies.
126 dnl Sets the LIB${NAME}, LTLIB${NAME} and INC${NAME} variables.
127 AC_DEFUN([AC_LIB_LINKFLAGS_BODY],
129 define([NAME],[translit([$1],[abcdefghijklmnopqrstuvwxyz./-],
130 [ABCDEFGHIJKLMNOPQRSTUVWXYZ___])])
131 dnl By default, look in $includedir and $libdir.
133 AC_LIB_WITH_FINAL_PREFIX([
134 eval additional_includedir=\"$includedir\"
135 eval additional_libdir=\"$libdir\"
137 AC_LIB_ARG_WITH([lib$1-prefix],
138 [ --with-lib$1-prefix[=DIR] search for lib$1 in DIR/include and DIR/lib
139 --without-lib$1-prefix don't search for lib$1 in includedir and libdir],
141 if test "X$withval" = "Xno"; then
144 if test "X$withval" = "X"; then
145 AC_LIB_WITH_FINAL_PREFIX([
146 eval additional_includedir=\"$includedir\"
147 eval additional_libdir=\"$libdir\"
150 additional_includedir="$withval/include"
151 additional_libdir="$withval/lib"
155 dnl Search the library and its dependencies in $additional_libdir and
156 dnl $LDFLAGS. Using breadth-first-seach.
162 names_already_handled=
163 names_next_round='$1 $2'
164 while test -n "$names_next_round"; do
165 names_this_round="$names_next_round"
167 for name in $names_this_round; do
169 for n in $names_already_handled; do
170 if test "$n" = "$name"; then
175 if test -z "$already_handled"; then
176 names_already_handled="$names_already_handled $name"
177 dnl See if it was already located by an earlier AC_LIB_LINKFLAGS
178 dnl or AC_LIB_HAVE_LINKFLAGS call.
179 uppername=`echo "$name" | sed -e 'y|abcdefghijklmnopqrstuvwxyz./-|ABCDEFGHIJKLMNOPQRSTUVWXYZ___|'`
180 eval value=\"\$HAVE_LIB$uppername\"
181 if test -n "$value"; then
182 if test "$value" = yes; then
183 eval value=\"\$LIB$uppername\"
184 test -z "$value" || LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$value"
185 eval value=\"\$LTLIB$uppername\"
186 test -z "$value" || LTLIB[]NAME="${LTLIB[]NAME}${LTLIB[]NAME:+ }$value"
188 dnl An earlier call to AC_LIB_HAVE_LINKFLAGS has determined
189 dnl that this library doesn't exist. So just drop it.
193 dnl Search the library lib$name in $additional_libdir and $LDFLAGS
194 dnl and the already constructed $LIBNAME/$LTLIBNAME.
199 if test $use_additional = yes; then
200 if test -n "$shlibext" && test -f "$additional_libdir/lib$name.$shlibext"; then
201 found_dir="$additional_libdir"
202 found_so="$additional_libdir/lib$name.$shlibext"
203 if test -f "$additional_libdir/lib$name.la"; then
204 found_la="$additional_libdir/lib$name.la"
207 if test -f "$additional_libdir/lib$name.$libext"; then
208 found_dir="$additional_libdir"
209 found_a="$additional_libdir/lib$name.$libext"
210 if test -f "$additional_libdir/lib$name.la"; then
211 found_la="$additional_libdir/lib$name.la"
216 if test "X$found_dir" = "X"; then
217 for x in $LDFLAGS $LTLIB[]NAME; do
218 AC_LIB_WITH_FINAL_PREFIX([eval x=\"$x\"])
221 dir=`echo "X$x" | sed -e 's/^X-L//'`
222 if test -n "$shlibext" && test -f "$dir/lib$name.$shlibext"; then
224 found_so="$dir/lib$name.$shlibext"
225 if test -f "$dir/lib$name.la"; then
226 found_la="$dir/lib$name.la"
229 if test -f "$dir/lib$name.$libext"; then
231 found_a="$dir/lib$name.$libext"
232 if test -f "$dir/lib$name.la"; then
233 found_la="$dir/lib$name.la"
239 if test "X$found_dir" != "X"; then
244 if test "X$found_dir" != "X"; then
245 dnl Found the library.
246 LTLIB[]NAME="${LTLIB[]NAME}${LTLIB[]NAME:+ }-L$found_dir -l$name"
247 if test "X$found_so" != "X"; then
248 dnl Linking with a shared library. We attempt to hardcode its
249 dnl directory into the executable's runpath, unless it's the
250 dnl standard /usr/lib.
251 if test "$enable_rpath" = no || test "X$found_dir" = "X/usr/lib"; then
252 dnl No hardcoding is needed.
253 LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$found_so"
255 dnl Use an explicit option to hardcode DIR into the resulting
257 dnl Potentially add DIR to ltrpathdirs.
258 dnl The ltrpathdirs will be appended to $LTLIBNAME at the end.
260 for x in $ltrpathdirs; do
261 if test "X$x" = "X$found_dir"; then
266 if test -z "$haveit"; then
267 ltrpathdirs="$ltrpathdirs $found_dir"
269 dnl The hardcoding into $LIBNAME is system dependent.
270 if test "$hardcode_direct" = yes; then
271 dnl Using DIR/libNAME.so during linking hardcodes DIR into the
272 dnl resulting binary.
273 LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$found_so"
275 if test -n "$hardcode_libdir_flag_spec" && test "$hardcode_minus_L" = no; then
276 dnl Use an explicit option to hardcode DIR into the resulting
278 LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$found_so"
279 dnl Potentially add DIR to rpathdirs.
280 dnl The rpathdirs will be appended to $LIBNAME at the end.
282 for x in $rpathdirs; do
283 if test "X$x" = "X$found_dir"; then
288 if test -z "$haveit"; then
289 rpathdirs="$rpathdirs $found_dir"
292 dnl Rely on "-L$found_dir".
293 dnl But don't add it if it's already contained in the LDFLAGS
294 dnl or the already constructed $LIBNAME
296 for x in $LDFLAGS $LIB[]NAME; do
297 AC_LIB_WITH_FINAL_PREFIX([eval x=\"$x\"])
298 if test "X$x" = "X-L$found_dir"; then
303 if test -z "$haveit"; then
304 LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }-L$found_dir"
306 if test "$hardcode_minus_L" != no; then
307 dnl FIXME: Not sure whether we should use
308 dnl "-L$found_dir -l$name" or "-L$found_dir $found_so"
310 LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$found_so"
312 dnl We cannot use $hardcode_runpath_var and LD_RUN_PATH
313 dnl here, because this doesn't fit in flags passed to the
314 dnl compiler. So give up. No hardcoding. This affects only
315 dnl very old systems.
316 dnl FIXME: Not sure whether we should use
317 dnl "-L$found_dir -l$name" or "-L$found_dir $found_so"
319 LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }-l$name"
325 if test "X$found_a" != "X"; then
326 dnl Linking with a static library.
327 LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$found_a"
329 dnl We shouldn't come here, but anyway it's good to have a
331 LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }-L$found_dir -l$name"
334 dnl Assume the include files are nearby.
335 additional_includedir=
338 basedir=`echo "X$found_dir" | sed -e 's,^X,,' -e 's,/lib/*$,,'`
339 additional_includedir="$basedir/include"
342 if test "X$additional_includedir" != "X"; then
343 dnl Potentially add $additional_includedir to $INCNAME.
345 dnl 1. if it's the standard /usr/include,
346 dnl 2. if it's /usr/local/include and we are using GCC on Linux,
347 dnl 3. if it's already present in $CPPFLAGS or the already
348 dnl constructed $INCNAME,
349 dnl 4. if it doesn't exist as a directory.
350 if test "X$additional_includedir" != "X/usr/include"; then
352 if test "X$additional_includedir" = "X/usr/local/include"; then
353 if test -n "$GCC"; then
355 linux* | gnu* | k*bsd*-gnu) haveit=yes;;
359 if test -z "$haveit"; then
360 for x in $CPPFLAGS $INC[]NAME; do
361 AC_LIB_WITH_FINAL_PREFIX([eval x=\"$x\"])
362 if test "X$x" = "X-I$additional_includedir"; then
367 if test -z "$haveit"; then
368 if test -d "$additional_includedir"; then
369 dnl Really add $additional_includedir to $INCNAME.
370 INC[]NAME="${INC[]NAME}${INC[]NAME:+ }-I$additional_includedir"
376 dnl Look for dependencies.
377 if test -n "$found_la"; then
378 dnl Read the .la file. It defines the variables
379 dnl dlname, library_names, old_library, dependency_libs, current,
380 dnl age, revision, installed, dlopen, dlpreopen, libdir.
381 save_libdir="$libdir"
383 */* | *\\*) . "$found_la" ;;
384 *) . "./$found_la" ;;
386 libdir="$save_libdir"
387 dnl We use only dependency_libs.
388 for dep in $dependency_libs; do
391 additional_libdir=`echo "X$dep" | sed -e 's/^X-L//'`
392 dnl Potentially add $additional_libdir to $LIBNAME and $LTLIBNAME.
394 dnl 1. if it's the standard /usr/lib,
395 dnl 2. if it's /usr/local/lib and we are using GCC on Linux,
396 dnl 3. if it's already present in $LDFLAGS or the already
397 dnl constructed $LIBNAME,
398 dnl 4. if it doesn't exist as a directory.
399 if test "X$additional_libdir" != "X/usr/lib"; then
401 if test "X$additional_libdir" = "X/usr/local/lib"; then
402 if test -n "$GCC"; then
404 linux* | gnu* | k*bsd*-gnu) haveit=yes;;
408 if test -z "$haveit"; then
410 for x in $LDFLAGS $LIB[]NAME; do
411 AC_LIB_WITH_FINAL_PREFIX([eval x=\"$x\"])
412 if test "X$x" = "X-L$additional_libdir"; then
417 if test -z "$haveit"; then
418 if test -d "$additional_libdir"; then
419 dnl Really add $additional_libdir to $LIBNAME.
420 LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }-L$additional_libdir"
424 for x in $LDFLAGS $LTLIB[]NAME; do
425 AC_LIB_WITH_FINAL_PREFIX([eval x=\"$x\"])
426 if test "X$x" = "X-L$additional_libdir"; then
431 if test -z "$haveit"; then
432 if test -d "$additional_libdir"; then
433 dnl Really add $additional_libdir to $LTLIBNAME.
434 LTLIB[]NAME="${LTLIB[]NAME}${LTLIB[]NAME:+ }-L$additional_libdir"
441 dir=`echo "X$dep" | sed -e 's/^X-R//'`
442 if test "$enable_rpath" != no; then
443 dnl Potentially add DIR to rpathdirs.
444 dnl The rpathdirs will be appended to $LIBNAME at the end.
446 for x in $rpathdirs; do
447 if test "X$x" = "X$dir"; then
452 if test -z "$haveit"; then
453 rpathdirs="$rpathdirs $dir"
455 dnl Potentially add DIR to ltrpathdirs.
456 dnl The ltrpathdirs will be appended to $LTLIBNAME at the end.
458 for x in $ltrpathdirs; do
459 if test "X$x" = "X$dir"; then
464 if test -z "$haveit"; then
465 ltrpathdirs="$ltrpathdirs $dir"
470 dnl Handle this in the next round.
471 names_next_round="$names_next_round "`echo "X$dep" | sed -e 's/^X-l//'`
474 dnl Handle this in the next round. Throw away the .la's
475 dnl directory; it is already contained in a preceding -L
477 names_next_round="$names_next_round "`echo "X$dep" | sed -e 's,^X.*/,,' -e 's,^lib,,' -e 's,\.la$,,'`
480 dnl Most likely an immediate library name.
481 LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$dep"
482 LTLIB[]NAME="${LTLIB[]NAME}${LTLIB[]NAME:+ }$dep"
488 dnl Didn't find the library; assume it is in the system directories
489 dnl known to the linker and runtime loader. (All the system
490 dnl directories known to the linker should also be known to the
491 dnl runtime loader, otherwise the system is severely misconfigured.)
492 LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }-l$name"
493 LTLIB[]NAME="${LTLIB[]NAME}${LTLIB[]NAME:+ }-l$name"
499 if test "X$rpathdirs" != "X"; then
500 if test -n "$hardcode_libdir_separator"; then
501 dnl Weird platform: only the last -rpath option counts, the user must
502 dnl pass all path elements in one option. We can arrange that for a
503 dnl single library, but not when more than one $LIBNAMEs are used.
505 for found_dir in $rpathdirs; do
506 alldirs="${alldirs}${alldirs:+$hardcode_libdir_separator}$found_dir"
508 dnl Note: hardcode_libdir_flag_spec uses $libdir and $wl.
509 acl_save_libdir="$libdir"
511 eval flag=\"$hardcode_libdir_flag_spec\"
512 libdir="$acl_save_libdir"
513 LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$flag"
515 dnl The -rpath options are cumulative.
516 for found_dir in $rpathdirs; do
517 acl_save_libdir="$libdir"
519 eval flag=\"$hardcode_libdir_flag_spec\"
520 libdir="$acl_save_libdir"
521 LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$flag"
525 if test "X$ltrpathdirs" != "X"; then
526 dnl When using libtool, the option that works for both libraries and
527 dnl executables is -R. The -R options are cumulative.
528 for found_dir in $ltrpathdirs; do
529 LTLIB[]NAME="${LTLIB[]NAME}${LTLIB[]NAME:+ }-R$found_dir"
534 dnl AC_LIB_APPENDTOVAR(VAR, CONTENTS) appends the elements of CONTENTS to VAR,
535 dnl unless already present in VAR.
536 dnl Works only for CPPFLAGS, not for LIB* variables because that sometimes
537 dnl contains two or three consecutive elements that belong together.
538 AC_DEFUN([AC_LIB_APPENDTOVAR],
540 for element in [$2]; do
543 AC_LIB_WITH_FINAL_PREFIX([eval x=\"$x\"])
544 if test "X$x" = "X$element"; then
549 if test -z "$haveit"; then
550 [$1]="${[$1]}${[$1]:+ }$element"