1 # lib-link.m4 serial 4 (gettext-0.12)
2 dnl Copyright (C) 2001-2003 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.
9 dnl AC_LIB_LINKFLAGS(name [, dependencies]) searches for libname and
10 dnl the libraries corresponding to explicit and implicit dependencies.
11 dnl Sets and AC_SUBSTs the LIB${NAME} and LTLIB${NAME} variables and
12 dnl augments the CPPFLAGS variable.
13 AC_DEFUN([AC_LIB_LINKFLAGS],
15 AC_REQUIRE([AC_LIB_PREPARE_PREFIX])
16 AC_REQUIRE([AC_LIB_RPATH])
17 define([Name],[translit([$1],[./-], [___])])
18 define([NAME],[translit([$1],[abcdefghijklmnopqrstuvwxyz./-],
19 [ABCDEFGHIJKLMNOPQRSTUVWXYZ___])])
20 AC_CACHE_CHECK([how to link with lib[]$1], [ac_cv_lib[]Name[]_libs], [
21 AC_LIB_LINKFLAGS_BODY([$1], [$2])
22 ac_cv_lib[]Name[]_libs="$LIB[]NAME"
23 ac_cv_lib[]Name[]_ltlibs="$LTLIB[]NAME"
24 ac_cv_lib[]Name[]_cppflags="$INC[]NAME"
26 LIB[]NAME="$ac_cv_lib[]Name[]_libs"
27 LTLIB[]NAME="$ac_cv_lib[]Name[]_ltlibs"
28 INC[]NAME="$ac_cv_lib[]Name[]_cppflags"
29 AC_LIB_APPENDTOVAR([CPPFLAGS], [$INC]NAME)
32 dnl Also set HAVE_LIB[]NAME so that AC_LIB_HAVE_LINKFLAGS can reuse the
33 dnl results of this search when this library appears as a dependency.
39 dnl AC_LIB_HAVE_LINKFLAGS(name, dependencies, includes, testcode)
40 dnl searches for libname and the libraries corresponding to explicit and
41 dnl implicit dependencies, together with the specified include files and
42 dnl the ability to compile and link the specified testcode. If found, it
43 dnl sets and AC_SUBSTs HAVE_LIB${NAME}=yes and the LIB${NAME} and
44 dnl LTLIB${NAME} variables and augments the CPPFLAGS variable, and
45 dnl #defines HAVE_LIB${NAME} to 1. Otherwise, it sets and AC_SUBSTs
46 dnl HAVE_LIB${NAME}=no and LIB${NAME} and LTLIB${NAME} to empty.
47 AC_DEFUN([AC_LIB_HAVE_LINKFLAGS],
49 AC_REQUIRE([AC_LIB_PREPARE_PREFIX])
50 AC_REQUIRE([AC_LIB_RPATH])
51 define([Name],[translit([$1],[./-], [___])])
52 define([NAME],[translit([$1],[abcdefghijklmnopqrstuvwxyz./-],
53 [ABCDEFGHIJKLMNOPQRSTUVWXYZ___])])
55 dnl Search for lib[]Name and define LIB[]NAME, LTLIB[]NAME and INC[]NAME
57 AC_LIB_LINKFLAGS_BODY([$1], [$2])
59 dnl Add $INC[]NAME to CPPFLAGS before performing the following checks,
60 dnl because if the user has installed lib[]Name and not disabled its use
61 dnl via --without-lib[]Name-prefix, he wants to use it.
62 ac_save_CPPFLAGS="$CPPFLAGS"
63 AC_LIB_APPENDTOVAR([CPPFLAGS], [$INC]NAME)
65 AC_CACHE_CHECK([for lib[]$1], [ac_cv_lib[]Name], [
67 LIBS="$LIBS $LIB[]NAME"
68 AC_TRY_LINK([$3], [$4], [ac_cv_lib[]Name=yes], [ac_cv_lib[]Name=no])
71 if test "$ac_cv_lib[]Name" = yes; then
73 AC_DEFINE([HAVE_LIB]NAME, 1, [Define if you have the $1 library.])
74 AC_MSG_CHECKING([how to link with lib[]$1])
75 AC_MSG_RESULT([$LIB[]NAME])
78 dnl If $LIB[]NAME didn't lead to a usable library, we don't need
79 dnl $INC[]NAME either.
80 CPPFLAGS="$ac_save_CPPFLAGS"
84 AC_SUBST([HAVE_LIB]NAME)
91 dnl Determine the platform dependent parameters needed to use rpath:
92 dnl libext, shlibext, hardcode_libdir_flag_spec, hardcode_libdir_separator,
93 dnl hardcode_direct, hardcode_minus_L.
94 AC_DEFUN([AC_LIB_RPATH],
96 AC_REQUIRE([AC_PROG_CC]) dnl we use $CC, $GCC, $LDFLAGS
97 AC_REQUIRE([AC_LIB_PROG_LD]) dnl we use $LD, $with_gnu_ld
98 AC_REQUIRE([AC_CANONICAL_HOST]) dnl we use $host
99 AC_REQUIRE([AC_CONFIG_AUX_DIR_DEFAULT]) dnl we use $ac_aux_dir
100 AC_CACHE_CHECK([for shared library run path origin], acl_cv_rpath, [
101 CC="$CC" GCC="$GCC" LDFLAGS="$LDFLAGS" LD="$LD" with_gnu_ld="$with_gnu_ld" \
102 ${CONFIG_SHELL-/bin/sh} "$ac_aux_dir/config.rpath" "$host" > conftest.sh
108 libext="$acl_cv_libext"
109 shlibext="$acl_cv_shlibext"
110 hardcode_libdir_flag_spec="$acl_cv_hardcode_libdir_flag_spec"
111 hardcode_libdir_separator="$acl_cv_hardcode_libdir_separator"
112 hardcode_direct="$acl_cv_hardcode_direct"
113 hardcode_minus_L="$acl_cv_hardcode_minus_L"
114 dnl Determine whether the user wants rpath handling at all.
116 [ --disable-rpath do not hardcode runtime library paths],
120 dnl AC_LIB_LINKFLAGS_BODY(name [, dependencies]) searches for libname and
121 dnl the libraries corresponding to explicit and implicit dependencies.
122 dnl Sets the LIB${NAME}, LTLIB${NAME} and INC${NAME} variables.
123 AC_DEFUN([AC_LIB_LINKFLAGS_BODY],
125 define([NAME],[translit([$1],[abcdefghijklmnopqrstuvwxyz./-],
126 [ABCDEFGHIJKLMNOPQRSTUVWXYZ___])])
127 dnl By default, look in $includedir and $libdir.
129 AC_LIB_WITH_FINAL_PREFIX([
130 eval additional_includedir=\"$includedir\"
131 eval additional_libdir=\"$libdir\"
133 AC_LIB_ARG_WITH([lib$1-prefix],
134 [ --with-lib$1-prefix[=DIR] search for lib$1 in DIR/include and DIR/lib
135 --without-lib$1-prefix don't search for lib$1 in includedir and libdir],
137 if test "X$withval" = "Xno"; then
140 if test "X$withval" = "X"; then
141 AC_LIB_WITH_FINAL_PREFIX([
142 eval additional_includedir=\"$includedir\"
143 eval additional_libdir=\"$libdir\"
146 additional_includedir="$withval/include"
147 additional_libdir="$withval/lib"
151 dnl Search the library and its dependencies in $additional_libdir and
152 dnl $LDFLAGS. Using breadth-first-seach.
158 names_already_handled=
159 names_next_round='$1 $2'
160 while test -n "$names_next_round"; do
161 names_this_round="$names_next_round"
163 for name in $names_this_round; do
165 for n in $names_already_handled; do
166 if test "$n" = "$name"; then
171 if test -z "$already_handled"; then
172 names_already_handled="$names_already_handled $name"
173 dnl See if it was already located by an earlier AC_LIB_LINKFLAGS
174 dnl or AC_LIB_HAVE_LINKFLAGS call.
175 uppername=`echo "$name" | sed -e 'y|abcdefghijklmnopqrstuvwxyz./-|ABCDEFGHIJKLMNOPQRSTUVWXYZ___|'`
176 eval value=\"\$HAVE_LIB$uppername\"
177 if test -n "$value"; then
178 if test "$value" = yes; then
179 eval value=\"\$LIB$uppername\"
180 test -z "$value" || LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$value"
181 eval value=\"\$LTLIB$uppername\"
182 test -z "$value" || LTLIB[]NAME="${LTLIB[]NAME}${LTLIB[]NAME:+ }$value"
184 dnl An earlier call to AC_LIB_HAVE_LINKFLAGS has determined
185 dnl that this library doesn't exist. So just drop it.
189 dnl Search the library lib$name in $additional_libdir and $LDFLAGS
190 dnl and the already constructed $LIBNAME/$LTLIBNAME.
195 if test $use_additional = yes; then
196 if test -n "$shlibext" && test -f "$additional_libdir/lib$name.$shlibext"; then
197 found_dir="$additional_libdir"
198 found_so="$additional_libdir/lib$name.$shlibext"
199 if test -f "$additional_libdir/lib$name.la"; then
200 found_la="$additional_libdir/lib$name.la"
203 if test -f "$additional_libdir/lib$name.$libext"; then
204 found_dir="$additional_libdir"
205 found_a="$additional_libdir/lib$name.$libext"
206 if test -f "$additional_libdir/lib$name.la"; then
207 found_la="$additional_libdir/lib$name.la"
212 if test "X$found_dir" = "X"; then
213 for x in $LDFLAGS $LTLIB[]NAME; do
214 AC_LIB_WITH_FINAL_PREFIX([eval x=\"$x\"])
217 dir=`echo "X$x" | sed -e 's/^X-L//'`
218 if test -n "$shlibext" && test -f "$dir/lib$name.$shlibext"; then
220 found_so="$dir/lib$name.$shlibext"
221 if test -f "$dir/lib$name.la"; then
222 found_la="$dir/lib$name.la"
225 if test -f "$dir/lib$name.$libext"; then
227 found_a="$dir/lib$name.$libext"
228 if test -f "$dir/lib$name.la"; then
229 found_la="$dir/lib$name.la"
235 if test "X$found_dir" != "X"; then
240 if test "X$found_dir" != "X"; then
241 dnl Found the library.
242 LTLIB[]NAME="${LTLIB[]NAME}${LTLIB[]NAME:+ }-L$found_dir -l$name"
243 if test "X$found_so" != "X"; then
244 dnl Linking with a shared library. We attempt to hardcode its
245 dnl directory into the executable's runpath, unless it's the
246 dnl standard /usr/lib.
247 if test "$enable_rpath" = no || test "X$found_dir" = "X/usr/lib"; then
248 dnl No hardcoding is needed.
249 LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$found_so"
251 dnl Use an explicit option to hardcode DIR into the resulting
253 dnl Potentially add DIR to ltrpathdirs.
254 dnl The ltrpathdirs will be appended to $LTLIBNAME at the end.
256 for x in $ltrpathdirs; do
257 if test "X$x" = "X$found_dir"; then
262 if test -z "$haveit"; then
263 ltrpathdirs="$ltrpathdirs $found_dir"
265 dnl The hardcoding into $LIBNAME is system dependent.
266 if test "$hardcode_direct" = yes; then
267 dnl Using DIR/libNAME.so during linking hardcodes DIR into the
268 dnl resulting binary.
269 LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$found_so"
271 if test -n "$hardcode_libdir_flag_spec" && test "$hardcode_minus_L" = no; then
272 dnl Use an explicit option to hardcode DIR into the resulting
274 LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$found_so"
275 dnl Potentially add DIR to rpathdirs.
276 dnl The rpathdirs will be appended to $LIBNAME at the end.
278 for x in $rpathdirs; do
279 if test "X$x" = "X$found_dir"; then
284 if test -z "$haveit"; then
285 rpathdirs="$rpathdirs $found_dir"
288 dnl Rely on "-L$found_dir".
289 dnl But don't add it if it's already contained in the LDFLAGS
290 dnl or the already constructed $LIBNAME
292 for x in $LDFLAGS $LIB[]NAME; do
293 AC_LIB_WITH_FINAL_PREFIX([eval x=\"$x\"])
294 if test "X$x" = "X-L$found_dir"; then
299 if test -z "$haveit"; then
300 LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }-L$found_dir"
302 if test "$hardcode_minus_L" != no; then
303 dnl FIXME: Not sure whether we should use
304 dnl "-L$found_dir -l$name" or "-L$found_dir $found_so"
306 LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$found_so"
308 dnl We cannot use $hardcode_runpath_var and LD_RUN_PATH
309 dnl here, because this doesn't fit in flags passed to the
310 dnl compiler. So give up. No hardcoding. This affects only
311 dnl very old systems.
312 dnl FIXME: Not sure whether we should use
313 dnl "-L$found_dir -l$name" or "-L$found_dir $found_so"
315 LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }-l$name"
321 if test "X$found_a" != "X"; then
322 dnl Linking with a static library.
323 LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$found_a"
325 dnl We shouldn't come here, but anyway it's good to have a
327 LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }-L$found_dir -l$name"
330 dnl Assume the include files are nearby.
331 additional_includedir=
334 basedir=`echo "X$found_dir" | sed -e 's,^X,,' -e 's,/lib/*$,,'`
335 additional_includedir="$basedir/include"
338 if test "X$additional_includedir" != "X"; then
339 dnl Potentially add $additional_includedir to $INCNAME.
341 dnl 1. if it's the standard /usr/include,
342 dnl 2. if it's /usr/local/include and we are using GCC on Linux,
343 dnl 3. if it's already present in $CPPFLAGS or the already
344 dnl constructed $INCNAME,
345 dnl 4. if it doesn't exist as a directory.
346 if test "X$additional_includedir" != "X/usr/include"; then
348 if test "X$additional_includedir" = "X/usr/local/include"; then
349 if test -n "$GCC"; then
355 if test -z "$haveit"; then
356 for x in $CPPFLAGS $INC[]NAME; do
357 AC_LIB_WITH_FINAL_PREFIX([eval x=\"$x\"])
358 if test "X$x" = "X-I$additional_includedir"; then
363 if test -z "$haveit"; then
364 if test -d "$additional_includedir"; then
365 dnl Really add $additional_includedir to $INCNAME.
366 INC[]NAME="${INC[]NAME}${INC[]NAME:+ }-I$additional_includedir"
372 dnl Look for dependencies.
373 if test -n "$found_la"; then
374 dnl Read the .la file. It defines the variables
375 dnl dlname, library_names, old_library, dependency_libs, current,
376 dnl age, revision, installed, dlopen, dlpreopen, libdir.
377 save_libdir="$libdir"
379 */* | *\\*) . "$found_la" ;;
380 *) . "./$found_la" ;;
382 libdir="$save_libdir"
383 dnl We use only dependency_libs.
384 for dep in $dependency_libs; do
387 additional_libdir=`echo "X$dep" | sed -e 's/^X-L//'`
388 dnl Potentially add $additional_libdir to $LIBNAME and $LTLIBNAME.
390 dnl 1. if it's the standard /usr/lib,
391 dnl 2. if it's /usr/local/lib and we are using GCC on Linux,
392 dnl 3. if it's already present in $LDFLAGS or the already
393 dnl constructed $LIBNAME,
394 dnl 4. if it doesn't exist as a directory.
395 if test "X$additional_libdir" != "X/usr/lib"; then
397 if test "X$additional_libdir" = "X/usr/local/lib"; then
398 if test -n "$GCC"; then
404 if test -z "$haveit"; then
406 for x in $LDFLAGS $LIB[]NAME; do
407 AC_LIB_WITH_FINAL_PREFIX([eval x=\"$x\"])
408 if test "X$x" = "X-L$additional_libdir"; then
413 if test -z "$haveit"; then
414 if test -d "$additional_libdir"; then
415 dnl Really add $additional_libdir to $LIBNAME.
416 LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }-L$additional_libdir"
420 for x in $LDFLAGS $LTLIB[]NAME; do
421 AC_LIB_WITH_FINAL_PREFIX([eval x=\"$x\"])
422 if test "X$x" = "X-L$additional_libdir"; then
427 if test -z "$haveit"; then
428 if test -d "$additional_libdir"; then
429 dnl Really add $additional_libdir to $LTLIBNAME.
430 LTLIB[]NAME="${LTLIB[]NAME}${LTLIB[]NAME:+ }-L$additional_libdir"
437 dir=`echo "X$dep" | sed -e 's/^X-R//'`
438 if test "$enable_rpath" != no; then
439 dnl Potentially add DIR to rpathdirs.
440 dnl The rpathdirs will be appended to $LIBNAME at the end.
442 for x in $rpathdirs; do
443 if test "X$x" = "X$dir"; then
448 if test -z "$haveit"; then
449 rpathdirs="$rpathdirs $dir"
451 dnl Potentially add DIR to ltrpathdirs.
452 dnl The ltrpathdirs will be appended to $LTLIBNAME at the end.
454 for x in $ltrpathdirs; do
455 if test "X$x" = "X$dir"; then
460 if test -z "$haveit"; then
461 ltrpathdirs="$ltrpathdirs $dir"
466 dnl Handle this in the next round.
467 names_next_round="$names_next_round "`echo "X$dep" | sed -e 's/^X-l//'`
470 dnl Handle this in the next round. Throw away the .la's
471 dnl directory; it is already contained in a preceding -L
473 names_next_round="$names_next_round "`echo "X$dep" | sed -e 's,^X.*/,,' -e 's,^lib,,' -e 's,\.la$,,'`
476 dnl Most likely an immediate library name.
477 LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$dep"
478 LTLIB[]NAME="${LTLIB[]NAME}${LTLIB[]NAME:+ }$dep"
484 dnl Didn't find the library; assume it is in the system directories
485 dnl known to the linker and runtime loader. (All the system
486 dnl directories known to the linker should also be known to the
487 dnl runtime loader, otherwise the system is severely misconfigured.)
488 LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }-l$name"
489 LTLIB[]NAME="${LTLIB[]NAME}${LTLIB[]NAME:+ }-l$name"
495 if test "X$rpathdirs" != "X"; then
496 if test -n "$hardcode_libdir_separator"; then
497 dnl Weird platform: only the last -rpath option counts, the user must
498 dnl pass all path elements in one option. We can arrange that for a
499 dnl single library, but not when more than one $LIBNAMEs are used.
501 for found_dir in $rpathdirs; do
502 alldirs="${alldirs}${alldirs:+$hardcode_libdir_separator}$found_dir"
504 dnl Note: hardcode_libdir_flag_spec uses $libdir and $wl.
505 acl_save_libdir="$libdir"
507 eval flag=\"$hardcode_libdir_flag_spec\"
508 libdir="$acl_save_libdir"
509 LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$flag"
511 dnl The -rpath options are cumulative.
512 for found_dir in $rpathdirs; do
513 acl_save_libdir="$libdir"
515 eval flag=\"$hardcode_libdir_flag_spec\"
516 libdir="$acl_save_libdir"
517 LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$flag"
521 if test "X$ltrpathdirs" != "X"; then
522 dnl When using libtool, the option that works for both libraries and
523 dnl executables is -R. The -R options are cumulative.
524 for found_dir in $ltrpathdirs; do
525 LTLIB[]NAME="${LTLIB[]NAME}${LTLIB[]NAME:+ }-R$found_dir"
530 dnl AC_LIB_APPENDTOVAR(VAR, CONTENTS) appends the elements of CONTENTS to VAR,
531 dnl unless already present in VAR.
532 dnl Works only for CPPFLAGS, not for LIB* variables because that sometimes
533 dnl contains two or three consecutive elements that belong together.
534 AC_DEFUN([AC_LIB_APPENDTOVAR],
536 for element in [$2]; do
539 AC_LIB_WITH_FINAL_PREFIX([eval x=\"$x\"])
540 if test "X$x" = "X$element"; then
545 if test -z "$haveit"; then
546 [$1]="${[$1]}${[$1]:+ }$element"