1# lib-link.m4 serial 1 (gettext-0.11)
2dnl Copyright (C) 2001-2002 Free Software Foundation, Inc.
3dnl This file is free software, distributed under the terms of the GNU
4dnl General Public License.  As a special exception to the GNU General
5dnl Public License, this file may be distributed as part of a program
6dnl that contains a configuration script generated by Autoconf, under
7dnl the same distribution terms as the rest of that program.
8
9dnl From Bruno Haible.
10
11dnl AC_LIB_LINKFLAGS(name [, dependencies]) searches for libname and
12dnl the libraries corresponding to explicit and implicit dependencies.
13dnl Sets and AC_SUBSTs the LIB${NAME} and LTLIB${NAME} variables and
14dnl augments the CPPFLAGS variable.
15AC_DEFUN([AC_LIB_LINKFLAGS],
16[
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"
27  ])
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)
32  AC_SUBST([LIB]NAME)
33  AC_SUBST([LTLIB]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.
36  HAVE_LIB[]NAME=yes
37  undefine([Name])
38  undefine([NAME])
39])
40
41dnl AC_LIB_HAVE_LINKFLAGS(name, dependencies, includes, testcode)
42dnl searches for libname and the libraries corresponding to explicit and
43dnl implicit dependencies, together with the specified include files and
44dnl the ability to compile and link the specified testcode. If found, it
45dnl sets and AC_SUBSTs HAVE_LIB${NAME}=yes and the LIB${NAME} and
46dnl LTLIB${NAME} variables and augments the CPPFLAGS variable, and
47dnl #defines HAVE_LIB${NAME} to 1. Otherwise, it sets and AC_SUBSTs
48dnl HAVE_LIB${NAME}=no and LIB${NAME} and LTLIB${NAME} to empty.
49AC_DEFUN([AC_LIB_HAVE_LINKFLAGS],
50[
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___])])
56
57  dnl Search for lib[]Name and define LIB[]NAME, LTLIB[]NAME and INC[]NAME
58  dnl accordingly.
59  AC_LIB_LINKFLAGS_BODY([$1], [$2])
60
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)
66
67  AC_CACHE_CHECK([for lib[]$1], [ac_cv_lib[]Name], [
68    ac_save_LIBS="$LIBS"
69    LIBS="$LIBS $LIB[]NAME"
70    AC_TRY_LINK([$3], [$4], [ac_cv_lib[]Name=yes], [ac_cv_lib[]Name=no])
71    LIBS="$ac_save_LIBS"
72  ])
73  if test "$ac_cv_lib[]Name" = yes; then
74    HAVE_LIB[]NAME=yes
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])
78  else
79    HAVE_LIB[]NAME=no
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"
83    LIB[]NAME=
84    LTLIB[]NAME=
85  fi
86  AC_SUBST([HAVE_LIB]NAME)
87  AC_SUBST([LIB]NAME)
88  AC_SUBST([LTLIB]NAME)
89  undefine([Name])
90  undefine([NAME])
91])
92
93dnl Determine the platform dependent parameters needed to use rpath:
94dnl libext, shlibext, hardcode_libdir_flag_spec, hardcode_libdir_separator,
95dnl hardcode_direct, hardcode_minus_L,
96dnl sys_lib_search_path_spec, sys_lib_dlsearch_path_spec.
97AC_DEFUN([AC_LIB_RPATH],
98[
99  AC_REQUIRE([AC_PROG_CC])                dnl we use $CC, $GCC, $LDFLAGS
100  AC_REQUIRE([AC_LIB_PROG_LD])            dnl we use $LD, $with_gnu_ld
101  AC_REQUIRE([AC_CANONICAL_HOST])         dnl we use $host
102  AC_REQUIRE([AC_CONFIG_AUX_DIR_DEFAULT]) dnl we use $ac_aux_dir
103  AC_CACHE_CHECK([for shared library run path origin], acl_cv_rpath, [
104    CC="$CC" GCC="$GCC" LDFLAGS="$LDFLAGS" LD="$LD" with_gnu_ld="$with_gnu_ld" \
105    ${CONFIG_SHELL-/bin/sh} "$ac_aux_dir/config.rpath" "$host" > conftest.sh
106    . ./conftest.sh
107    rm -f ./conftest.sh
108    acl_cv_rpath=done
109  ])
110  wl="$acl_cv_wl"
111  libext="$acl_cv_libext"
112  shlibext="$acl_cv_shlibext"
113  hardcode_libdir_flag_spec="$acl_cv_hardcode_libdir_flag_spec"
114  hardcode_libdir_separator="$acl_cv_hardcode_libdir_separator"
115  hardcode_direct="$acl_cv_hardcode_direct"
116  hardcode_minus_L="$acl_cv_hardcode_minus_L"
117  sys_lib_search_path_spec="$acl_cv_sys_lib_search_path_spec"
118  sys_lib_dlsearch_path_spec="$acl_cv_sys_lib_dlsearch_path_spec"
119])
120
121dnl AC_LIB_LINKFLAGS_BODY(name [, dependencies]) searches for libname and
122dnl the libraries corresponding to explicit and implicit dependencies.
123dnl Sets the LIB${NAME}, LTLIB${NAME} and INC${NAME} variables.
124AC_DEFUN([AC_LIB_LINKFLAGS_BODY],
125[
126  define([NAME],[translit([$1],[abcdefghijklmnopqrstuvwxyz./-],
127                               [ABCDEFGHIJKLMNOPQRSTUVWXYZ___])])
128  dnl By default, look in $includedir and $libdir.
129  use_additional=yes
130  AC_LIB_WITH_FINAL_PREFIX([
131    eval additional_includedir=\"$includedir\"
132    eval additional_libdir=\"$libdir\"
133  ])
134  AC_ARG_WITH([lib$1-prefix],
135[  --with-lib$1-prefix[=DIR]  search for lib$1 in DIR/include and DIR/lib
136  --without-lib$1-prefix     don't search for lib$1 in includedir and libdir],
137[
138    if test "X$withval" = "Xno"; then
139      use_additional=no
140    else
141      if test "X$withval" = "X"; then
142        AC_LIB_WITH_FINAL_PREFIX([
143          eval additional_includedir=\"$includedir\"
144          eval additional_libdir=\"$libdir\"
145        ])
146      else
147        additional_includedir="$withval/include"
148        additional_libdir="$withval/lib"
149      fi
150    fi
151])
152  dnl Search the library and its dependencies in $additional_libdir and
153  dnl $LDFLAGS. Using breadth-first-seach.
154  LIB[]NAME=
155  LTLIB[]NAME=
156  INC[]NAME=
157  rpathdirs=
158  ltrpathdirs=
159  names_already_handled=
160  names_next_round='$1 $2'
161  while test -n "$names_next_round"; do
162    names_this_round="$names_next_round"
163    names_next_round=
164    for name in $names_this_round; do
165      already_handled=
166      for n in $names_already_handled; do
167        if test "$n" = "$name"; then
168          already_handled=yes
169          break
170        fi
171      done
172      if test -z "$already_handled"; then
173        names_already_handled="$names_already_handled $name"
174        dnl See if it was already located by an earlier AC_LIB_LINKFLAGS
175        dnl or AC_LIB_HAVE_LINKFLAGS call.
176        uppername=`echo "$name" | sed -e 'y|abcdefghijklmnopqrstuvwxyz./-|ABCDEFGHIJKLMNOPQRSTUVWXYZ___|'`
177        eval value=\"\$HAVE_LIB$uppername\"
178        if test -n "$value"; then
179          if test "$value" = yes; then
180            eval value=\"\$LIB$uppername\"
181            test -z "$value" || LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$value"
182            eval value=\"\$LTLIB$uppername\"
183            test -z "$value" || LTLIB[]NAME="${LTLIB[]NAME}${LTLIB[]NAME:+ }$value"
184          else
185            dnl An earlier call to AC_LIB_HAVE_LINKFLAGS has determined
186            dnl that this library doesn't exist. So just drop it.
187            :
188          fi
189        else
190          dnl Search the library lib$name in $additional_libdir and $LDFLAGS
191          dnl and the already constructed $LIBNAME/$LTLIBNAME.
192          found_dir=
193          found_la=
194          found_so=
195          found_a=
196          if test $use_additional = yes; then
197            if test -n "$shlibext" && test -f "$additional_libdir/lib$name.$shlibext"; then
198              found_dir="$additional_libdir"
199              found_so="$additional_libdir/lib$name.$shlibext"
200              if test -f "$additional_libdir/lib$name.la"; then
201                found_la="$additional_libdir/lib$name.la"
202              fi
203            else
204              if test -f "$additional_libdir/lib$name.$libext"; then
205                found_dir="$additional_libdir"
206                found_a="$additional_libdir/lib$name.$libext"
207                if test -f "$additional_libdir/lib$name.la"; then
208                  found_la="$additional_libdir/lib$name.la"
209                fi
210              fi
211            fi
212          fi
213          if test "X$found_dir" = "X"; then
214            for x in $LDFLAGS $LTLIB[]NAME; do
215              AC_LIB_WITH_FINAL_PREFIX([eval x=\"$x\"])
216              case "$x" in
217                -L*)
218                  dir=`echo "X$x" | sed -e 's/^X-L//'`
219                  if test -n "$shlibext" && test -f "$dir/lib$name.$shlibext"; then
220                    found_dir="$dir"
221                    found_so="$dir/lib$name.$shlibext"
222                    if test -f "$dir/lib$name.la"; then
223                      found_la="$dir/lib$name.la"
224                    fi
225                  else
226                    if test -f "$dir/lib$name.$libext"; then
227                      found_dir="$dir"
228                      found_a="$dir/lib$name.$libext"
229                      if test -f "$dir/lib$name.la"; then
230                        found_la="$dir/lib$name.la"
231                      fi
232                    fi
233                  fi
234                  ;;
235              esac
236              if test "X$found_dir" != "X"; then
237                break
238              fi
239            done
240          fi
241          if test "X$found_dir" != "X"; then
242            dnl Found the library.
243            LTLIB[]NAME="${LTLIB[]NAME}${LTLIB[]NAME:+ }-L$found_dir -l$name"
244            if test "X$found_so" != "X"; then
245              dnl Linking with a shared library. We attempt to hardcode its
246              dnl directory into the executable's runpath, unless it's the
247              dnl standard /usr/lib.
248              if test "X$found_dir" = "X/usr/lib"; then
249                dnl No hardcoding is needed.
250                LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$found_so"
251              else
252                dnl Use an explicit option to hardcode DIR into the resulting
253                dnl binary.
254                dnl Potentially add DIR to ltrpathdirs.
255                dnl The ltrpathdirs will be appended to $LTLIBNAME at the end.
256                haveit=
257                for x in $ltrpathdirs; do
258                  if test "X$x" = "X$found_dir"; then
259                    haveit=yes
260                    break
261                  fi
262                done
263                if test -z "$haveit"; then
264                  ltrpathdirs="$ltrpathdirs $found_dir"
265                fi
266                dnl The hardcoding into $LIBNAME is system dependent.
267                if test "$hardcode_direct" = yes; then
268                  dnl Using DIR/libNAME.so during linking hardcodes DIR into the
269                  dnl resulting binary.
270                  LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$found_so"
271                else
272                  if test -n "$hardcode_libdir_flag_spec" && test "$hardcode_minus_L" = no; then
273                    dnl Use an explicit option to hardcode DIR into the resulting
274                    dnl binary.
275                    LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$found_so"
276                    dnl Potentially add DIR to rpathdirs.
277                    dnl The rpathdirs will be appended to $LIBNAME at the end.
278                    haveit=
279                    for x in $rpathdirs; do
280                      if test "X$x" = "X$found_dir"; then
281                        haveit=yes
282                        break
283                      fi
284                    done
285                    if test -z "$haveit"; then
286                      rpathdirs="$rpathdirs $found_dir"
287                    fi
288                  else
289                    dnl Rely on "-L$found_dir".
290                    dnl But don't add it if it's already contained in the LDFLAGS
291                    dnl or the already constructed $LIBNAME
292                    haveit=
293                    for x in $LDFLAGS $LIB[]NAME; do
294                      AC_LIB_WITH_FINAL_PREFIX([eval x=\"$x\"])
295                      if test "X$x" = "X-L$found_dir"; then
296                        haveit=yes
297                        break
298                      fi
299                    done
300                    if test -z "$haveit"; then
301                      LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }-L$found_dir"
302                    fi
303                    if test "$hardcode_minus_L" != no; then
304                      dnl FIXME: Not sure whether we should use
305                      dnl "-L$found_dir -l$name" or "-L$found_dir $found_so"
306                      dnl here.
307                      LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$found_so"
308                    else
309                      dnl We cannot use $hardcode_runpath_var and LD_RUN_PATH
310                      dnl here, because this doesn't fit in flags passed to the
311                      dnl compiler. So give up. No hardcoding. This affects only
312                      dnl very old systems.
313                      dnl FIXME: Not sure whether we should use
314                      dnl "-L$found_dir -l$name" or "-L$found_dir $found_so"
315                      dnl here.
316                      LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }-l$name"
317                    fi
318                  fi
319                fi
320              fi
321            else
322              if test "X$found_a" != "X"; then
323                dnl Linking with a static library.
324                LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$found_a"
325              else
326                dnl We shouldn't come here, but anyway it's good to have a
327                dnl fallback.
328                LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }-L$found_dir -l$name"
329              fi
330            fi
331            dnl Assume the include files are nearby.
332            additional_includedir=
333            case "$found_dir" in
334              */lib | */lib/)
335                basedir=`echo "X$found_dir" | sed -e 's,^X,,' -e 's,/lib/*$,,'`
336                additional_includedir="$basedir/include"
337                ;;
338            esac
339            if test "X$additional_includedir" != "X"; then
340              dnl Potentially add $additional_includedir to $INCNAME.
341              dnl But don't add it
342              dnl   1. if it's the standard /usr/include,
343              dnl   2. if it's /usr/local/include and we are using GCC on Linux,
344              dnl   3. if it's already present in $CPPFLAGS or the already
345              dnl      constructed $INCNAME,
346              dnl   4. if it doesn't exist as a directory.
347              if test "X$additional_includedir" != "X/usr/include"; then
348                haveit=
349                if test "X$additional_includedir" = "X/usr/local/include"; then
350                  if test -n "$GCC"; then
351                    case $host_os in
352                      linux*) haveit=yes;;
353                    esac
354                  fi
355                fi
356                if test -z "$haveit"; then
357                  for x in $CPPFLAGS $INC[]NAME; do
358                    AC_LIB_WITH_FINAL_PREFIX([eval x=\"$x\"])
359                    if test "X$x" = "X-I$additional_includedir"; then
360                      haveit=yes
361                      break
362                    fi
363                  done
364                  if test -z "$haveit"; then
365                    if test -d "$additional_includedir"; then
366                      dnl Really add $additional_includedir to $INCNAME.
367                      INC[]NAME="${INC[]NAME}${INC[]NAME:+ }-I$additional_includedir"
368                    fi
369                  fi
370                fi
371              fi
372            fi
373            dnl Look for dependencies.
374            if test -n "$found_la"; then
375              dnl Read the .la file. It defines the variables
376              dnl dlname, library_names, old_library, dependency_libs, current,
377              dnl age, revision, installed, dlopen, dlpreopen, libdir.
378              save_libdir="$libdir"
379              case "$found_la" in
380                */* | *\\*) . "$found_la" ;;
381                *) . "./$found_la" ;;
382              esac
383              libdir="$save_libdir"
384              dnl We use only dependency_libs.
385              for dep in $dependency_libs; do
386                case "$dep" in
387                  -L*)
388                    additional_libdir=`echo "X$dep" | sed -e 's/^X-L//'`
389                    dnl Potentially add $additional_libdir to $LIBNAME and $LTLIBNAME.
390                    dnl But don't add it
391                    dnl   1. if it's the standard /usr/lib,
392                    dnl   2. if it's /usr/local/lib and we are using GCC on Linux,
393                    dnl   3. if it's already present in $LDFLAGS or the already
394                    dnl      constructed $LIBNAME,
395                    dnl   4. if it doesn't exist as a directory.
396                    if test "X$additional_libdir" != "X/usr/lib"; then
397                      haveit=
398                      if test "X$additional_libdir" = "X/usr/local/lib"; then
399                        if test -n "$GCC"; then
400                          case $host_os in
401                            linux*) haveit=yes;;
402                          esac
403                        fi
404                      fi
405                      if test -z "$haveit"; then
406                        haveit=
407                        for x in $LDFLAGS $LIB[]NAME; do
408                          AC_LIB_WITH_FINAL_PREFIX([eval x=\"$x\"])
409                          if test "X$x" = "X-L$additional_libdir"; then
410                            haveit=yes
411                            break
412                          fi
413                        done
414                        if test -z "$haveit"; then
415                          if test -d "$additional_libdir"; then
416                            dnl Really add $additional_libdir to $LIBNAME.
417                            LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }-L$additional_libdir"
418                          fi
419                        fi
420                        haveit=
421                        for x in $LDFLAGS $LTLIB[]NAME; do
422                          AC_LIB_WITH_FINAL_PREFIX([eval x=\"$x\"])
423                          if test "X$x" = "X-L$additional_libdir"; then
424                            haveit=yes
425                            break
426                          fi
427                        done
428                        if test -z "$haveit"; then
429                          if test -d "$additional_libdir"; then
430                            dnl Really add $additional_libdir to $LTLIBNAME.
431                            LTLIB[]NAME="${LTLIB[]NAME}${LTLIB[]NAME:+ }-L$additional_libdir"
432                          fi
433                        fi
434                      fi
435                    fi
436                    ;;
437                  -l*)
438                    dnl Handle this in the next round.
439                    names_next_round="$names_next_round "`echo "X$dep" | sed -e 's/^X-l//'`
440                    ;;
441                  *.la)
442                    dnl Handle this in the next round. Throw away the .la's
443                    dnl directory; it is already contained in a preceding -L
444                    dnl option.
445                    names_next_round="$names_next_round "`echo "X$dep" | sed -e 's,^X.*/,,' -e 's,^lib,,' -e 's,\.la$,,'`
446                    ;;
447                  *)
448                    dnl Most likely an immediate library name.
449                    LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$dep"
450                    LTLIB[]NAME="${LTLIB[]NAME}${LTLIB[]NAME:+ }$dep"
451                    ;;
452                esac
453              done
454            fi
455          else
456            dnl Didn't find the library; assume it is in the system directories
457            dnl known to the linker and runtime loader. (All the system
458            dnl directories known to the linker should also be known to the
459            dnl runtime loader, otherwise the system is severely misconfigured.)
460            LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }-l$name"
461            LTLIB[]NAME="${LTLIB[]NAME}${LTLIB[]NAME:+ }-l$name"
462          fi
463        fi
464      fi
465    done
466  done
467  if test "X$rpathdirs" != "X"; then
468    if test -n "$hardcode_libdir_separator"; then
469      dnl Weird platform: only the last -rpath option counts, the user must
470      dnl pass all path elements in one option. We can arrange that for a
471      dnl single library, but not when more than one $LIBNAMEs are used.
472      alldirs=
473      for found_dir in $rpathdirs; do
474        alldirs="${alldirs}${alldirs:+$hardcode_libdir_separator}$found_dir"
475      done
476      dnl Note: hardcode_libdir_flag_spec uses $libdir and $wl.
477      acl_save_libdir="$libdir"
478      libdir="$alldirs"
479      eval flag=\"$hardcode_libdir_flag_spec\"
480      libdir="$acl_save_libdir"
481      LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$flag"
482    else
483      dnl The -rpath options are cumulative.
484      for found_dir in $rpathdirs; do
485        acl_save_libdir="$libdir"
486        libdir="$found_dir"
487        eval flag=\"$hardcode_libdir_flag_spec\"
488        libdir="$acl_save_libdir"
489        LIB[]NAME="${LIB[]NAME}${LIB[]NAME:+ }$flag"
490      done
491    fi
492  fi
493  if test "X$ltrpathdirs" != "X"; then
494    dnl When using libtool, the option that works for both libraries and
495    dnl executables is -R. The -R options are cumulative.
496    for found_dir in $ltrpathdirs; do
497      LTLIB[]NAME="${LTLIB[]NAME}${LTLIB[]NAME:+ }-R$found_dir"
498    done
499  fi
500])
501
502dnl AC_LIB_APPENDTOVAR(VAR, CONTENTS) appends the elements of CONTENTS to VAR,
503dnl unless already present in VAR.
504dnl Works only for CPPFLAGS, not for LIB* variables because that sometimes
505dnl contains two or three consecutive elements that belong together.
506AC_DEFUN([AC_LIB_APPENDTOVAR],
507[
508  for element in [$2]; do
509    haveit=
510    for x in $[$1]; do
511      AC_LIB_WITH_FINAL_PREFIX([eval x=\"$x\"])
512      if test "X$x" = "X$element"; then
513        haveit=yes
514        break
515      fi
516    done
517    if test -z "$haveit"; then
518      [$1]="${[$1]}${[$1]:+ }$element"
519    fi
520  done
521])
522