u*-vasnprintf tests: Add more tests of %U, %lU, %llU directives.
[gnulib.git] / m4 / relocatable.m4
blobb86829e24633721b7fc4b7204c6ce98d138adc29
1 # relocatable.m4
2 # serial 25
3 dnl Copyright (C) 2003, 2005-2007, 2009-2024 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
8 dnl From Bruno Haible.
10 dnl gl_RELOCATABLE([RELOCWRAPPER-DIR])
11 dnl ----------------------------------------------------------
12 dnl Support for relocatable programs.
13 dnl Supply RELOCWRAPPER-DIR as the directory where relocwrapper.c may be found.
14 AC_DEFUN([gl_RELOCATABLE],
16   AC_REQUIRE([gl_RELOCATABLE_BODY])
17   gl_RELOCATABLE_LIBRARY
18   : ${RELOCATABLE_CONFIG_H_DIR='$(top_builddir)'}
19   RELOCATABLE_SRC_DIR="\$(top_srcdir)/$gl_source_base"
20   RELOCATABLE_BUILD_DIR="\$(top_builddir)/$gl_source_base"
22 dnl The guts of gl_RELOCATABLE. Needs to be expanded only once.
23 AC_DEFUN([gl_RELOCATABLE_BODY],
25   AC_REQUIRE([AC_PROG_INSTALL])
27   dnl This AC_BEFORE invocation leads to unjustified autoconf warnings
28   dnl when gl_RELOCATABLE_BODY is invoked more than once.
29   dnl
30   dnl We need this AC_BEFORE because AC_PROG_INSTALL is documented to
31   dnl overwrite earlier settings of INSTALL and INSTALL_PROGRAM (even
32   dnl though in autoconf-2.52..2.60 it doesn't do so), but we want this
33   dnl macro's setting of INSTALL_PROGRAM to persist.
34   dnl Arghh: AC_BEFORE does not work in this setting :-(
35   dnl AC_BEFORE([AC_PROG_INSTALL],[gl_RELOCATABLE_BODY])
36   dnl
37   dnl LT_INIT sets LIBTOOL, but we want this macro's setting of LIBTOOL to
38   dnl persist.
39   dnl Arghh: AC_BEFORE does not work in this setting :-(
40   dnl AC_BEFORE([LT_INIT],[gl_RELOCATABLE_BODY])
42   AC_REQUIRE([AC_LIB_LIBPATH])
43   AC_REQUIRE([gl_RELOCATABLE_LIBRARY_BODY])
44   AC_REQUIRE([AC_CANONICAL_HOST])
45   is_noop=no
46   use_elf_origin_trick=no
47   use_macos_tools=no
48   use_wrapper=no
49   if test $RELOCATABLE = yes; then
50     # --enable-relocatable implies --disable-rpath
51     enable_rpath=no
52     AC_CHECK_HEADERS([mach-o/dyld.h])
53     AC_CHECK_FUNCS([_NSGetExecutablePath])
54     case "$host_os" in
55       mingw* | windows*) is_noop=yes ;;
56       # For the platforms that support $ORIGIN, see
57       # <https://lekensteyn.nl/rpath.html>.
58       # glibc systems, Linux with musl libc: yes. Android: no.
59       linux*-android*) ;;
60       linux* | kfreebsd*) use_elf_origin_trick=yes ;;
61       # Hurd: <https://lists.gnu.org/r/bug-hurd/2019-02/msg00049.html>
62       # only after the glibc commit from 2018-01-08
63       # <https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=311ba8dc4416467947eff2ab327854f124226309>
64       gnu*)
65         # Test for a glibc version >= 2.27.
66         AC_CHECK_FUNCS([copy_file_range])
67         if test $ac_cv_func_copy_file_range = yes; then
68           use_elf_origin_trick=yes
69         fi
70         ;;
71 changequote(,)dnl
72       # FreeBSD >= 7.3, DragonFly >= 3.0, MidnightBSD >= 1.1: yes.
73       freebsd | freebsd[1-7] | freebsd[1-6].* | freebsd7.[0-2]) ;;
74       dragonfly | dragonfly[1-2] | dragonfly[1-2].*) ;;
75       midnightbsd | midnightbsd0* | midnightbsd1.0*) ;;
76       freebsd* | dragonfly* | midnightbsd*) use_elf_origin_trick=yes ;;
77       # NetBSD >= 8.0: yes.
78       netbsd | netbsd[1-7] | netbsd[1-7].*) ;;
79       netbsdelf | netbsdelf[1-7] | netbsdelf[1-7].*) ;;
80       netbsd*) use_elf_origin_trick=yes ;;
81       # OpenBSD >= 5.4: yes.
82       openbsd | openbsd[1-5] | openbsd[1-4].* | openbsd5.[0-3]) ;;
83       openbsd*) use_elf_origin_trick=yes ;;
84       # Solaris >= 10: yes.
85       solaris | solaris2.[1-9] | solaris2.[1-9].*) ;;
86       solaris*) use_elf_origin_trick=yes ;;
87       # Haiku: yes.
88       haiku*) use_elf_origin_trick=yes ;;
89       # On Mac OS X 10.4 or newer, use Mac OS X tools. See
90       # <https://wincent.com/wiki/@executable_path,_@load_path_and_@rpath>.
91       darwin | darwin[1-7].*) ;;
92       darwin*) use_macos_tools=yes ;;
93 changequote([,])dnl
94     esac
95     if test $is_noop = yes; then
96       RELOCATABLE_LDFLAGS=:
97       AC_SUBST([RELOCATABLE_LDFLAGS])
98     else
99       if test $use_elf_origin_trick = yes || test $use_macos_tools = yes; then
100         dnl Use the dynamic linker's support for relocatable programs.
101         case "$ac_aux_dir" in
102           /*) reloc_ldflags="$ac_aux_dir/reloc-ldflags" ;;
103           *) reloc_ldflags="\$(top_builddir)/$ac_aux_dir/reloc-ldflags" ;;
104         esac
105         RELOCATABLE_LDFLAGS="\"$reloc_ldflags\" \"\$(host)\" \"\$(RELOCATABLE_LIBRARY_PATH)\""
106         AC_SUBST([RELOCATABLE_LDFLAGS])
107         if test $use_macos_tools = yes; then
108           dnl Use a libtool wrapper that uses Mac OS X tools.
109           case "$ac_aux_dir" in
110             /*) LIBTOOL="${CONFIG_SHELL-$SHELL} $ac_aux_dir/libtool-reloc $LIBTOOL" ;;
111             *) LIBTOOL="${CONFIG_SHELL-$SHELL} \$(top_builddir)/$ac_aux_dir/libtool-reloc $LIBTOOL" ;;
112           esac
113         fi
114       else
115         use_wrapper=yes
116         dnl Unfortunately we cannot define INSTALL_PROGRAM to a command
117         dnl consisting of more than one word - libtool doesn't support this.
118         dnl So we abuse the INSTALL_PROGRAM_ENV hook, originally meant for the
119         dnl 'install-strip' target.
120         INSTALL_PROGRAM_ENV="RELOC_LIBRARY_PATH_VAR=\"$shlibpath_var\" RELOC_LIBRARY_PATH_VALUE=\"\$(RELOCATABLE_LIBRARY_PATH)\" RELOC_PREFIX=\"\$(prefix)\" RELOC_DESTDIR=\"\$(DESTDIR)\" RELOC_COMPILE_COMMAND=\"\$(CC) \$(CPPFLAGS) \$(CFLAGS) \$(LDFLAGS)\" RELOC_SRCDIR=\"\$(RELOCATABLE_SRC_DIR)\" RELOC_BUILDDIR=\"\$(RELOCATABLE_BUILD_DIR)\" RELOC_CONFIG_H_DIR=\"\$(RELOCATABLE_CONFIG_H_DIR)\" RELOC_EXEEXT=\"\$(EXEEXT)\" RELOC_STRIP_PROG=\"\$(RELOCATABLE_STRIP)\" RELOC_INSTALL_PROG=\"$INSTALL_PROGRAM\""
121         AC_SUBST([INSTALL_PROGRAM_ENV])
122         case "$ac_aux_dir" in
123           /*) INSTALL_PROGRAM="$ac_aux_dir/install-reloc" ;;
124           *) INSTALL_PROGRAM="\$(top_builddir)/$ac_aux_dir/install-reloc" ;;
125         esac
126       fi
127     fi
128   fi
129   AM_CONDITIONAL([RELOCATABLE_VIA_LD],
130     [test $is_noop = yes || test $use_elf_origin_trick = yes || test $use_macos_tools = yes])
131   AM_CONDITIONAL([RELOCATABLE_VIA_WRAPPER], [test $use_wrapper = yes])
133   dnl RELOCATABLE_LIBRARY_PATH can be set in configure.ac. Default is empty.
134   AC_SUBST([RELOCATABLE_LIBRARY_PATH])
136   AC_SUBST([RELOCATABLE_CONFIG_H_DIR])
137   AC_SUBST([RELOCATABLE_SRC_DIR])
138   AC_SUBST([RELOCATABLE_BUILD_DIR])
140   dnl Ensure RELOCATABLE_STRIP is defined in Makefiles (at least those
141   dnl generated by automake), with value ':'.
142   RELOCATABLE_STRIP=':'
143   AC_SUBST([RELOCATABLE_STRIP])
146 dnl Determine the platform dependent parameters needed to use relocatability:
147 dnl shlibpath_var.
148 AC_DEFUN([AC_LIB_LIBPATH],
150   AC_REQUIRE([AC_LIB_PROG_LD])            dnl we use $LD
151   AC_REQUIRE([AC_CANONICAL_HOST])         dnl we use $host
152   AC_REQUIRE([AC_CONFIG_AUX_DIR_DEFAULT]) dnl we use $ac_aux_dir
153   AC_CACHE_CHECK([for shared library path variable], [acl_cv_libpath], [
154     LD="$LD" \
155     ${CONFIG_SHELL-/bin/sh} "$ac_aux_dir/config.libpath" "$host" > conftest.sh
156     . ./conftest.sh
157     rm -f ./conftest.sh
158     acl_cv_libpath=${acl_cv_shlibpath_var:-none}
159   ])
160   shlibpath_var="$acl_cv_shlibpath_var"