bootstrap: Remove undesired output.
[gnulib.git] / doc / relocatable-maint.texi
blobd21d2f9725141413ffb758a22c10891fbee67bf3
1 @node Supporting Relocation
2 @section Supporting Relocation
4 It has been a pain for many users of GNU packages for a long time that
5 packages are not relocatable.  It means a user cannot copy a program,
6 installed by another user on the same machine, to his home directory,
7 and have it work correctly (including i18n).  So many users need to go
8 through @code{configure; make; make install} with all its
9 dependencies, options, and hurdles.
11 Red Hat, Debian, and other binary distributions solve the ``ease of
12 installation'' problem, but they hardwire path names, usually to
13 @file{/usr} or @file{/usr/local}.  This means that users need root
14 privileges to install a binary package, and prevents installing two
15 different versions of the same binary package.
17 A relocatable program can be moved or copied to a different location
18 on the file system.  It is possible to make symlinks to the installed
19 and moved programs, and invoke them through the symlink. It is
20 possible to do the same thing with a hard link @emph{only} if the hard
21 link file is in the same directory as the real program.
23 @mindex relocatable-prog
24 The @code{relocatable-prog} module aims to ease the process of making a
25 GNU program relocatable.  It helps overcome two obstacles.  First, it aids
26 with relocating the hard-coded references to absolute file names that
27 GNU programs often contain.  These references must be fixed up at
28 runtime if a program is to be successfully relocated.  The
29 @code{relocatable-prog} module provides a function @code{relocate} that
30 does this job.
32 Second, the loader must be able to find shared libraries linked to
33 relocatable executables or referenced by other shared libraries linked
34 to relocatable executables.  The @code{relocatable-prog} module helps out
35 here in a platform-specific way:
37 @itemize
38 @item
39 On most operating systems, it adds a linker option (@option{-rpath}) that
40 causes the dynamic linker to search for libraries in a directory relative
41 to the location of the invoked executable.  This works on GNU/Linux and
42 modern versions of GNU/Hurd, GNU/kFreeBSD, macOS, FreeBSD, NetBSD, OpenBSD,
43 Solaris, Haiku.
45 @item
46 On other Unix systems, it installs a trampoline executable.  The trampoline
47 sets the environment variable that controls shared library searching
48 (usually @env{LD_LIBRARY_PATH}) and then invokes the real executable.
49 This applies to operating systems such as AIX, HP-UX, or Minix.
51 @item
52 On Windows, the executable's own directory is searched for libraries,
53 so installing shared libraries into the executable's directory is
54 sufficient.
55 @end itemize
57 You can make your program relocatable by following these steps:
59 @enumerate
60 @item
61 @mindex relocatable-lib
62 @mindex relocatable-lib-lgpl
63 Import the @code{relocatable-prog} module.  For libraries, use the
64 @code{relocatable-lib} or @code{relocatable-lib-lgpl} module, if
65 the libraries are independent.  For installing multiple libraries,
66 at least one of which depends on another one, use the @code{relocatable-prog}
67 module.
68 If you need more than one module, or you need to use them with different
69 settings, you will need multiple copies of gnulib (@pxref{Multiple instances}).
71 @item
72 In every program, add to @code{main} as the first statement (even
73 before setting the locale or doing anything related to libintl):
75 @example
76 set_program_name (argv[0]);
77 @end example
79 The prototype for this function is in @file{progname.h}.
81 @item
82 If you want your code to be portable to platforms that do not support
83 automatic initialization, call @code{set_relocation_prefix}.
85 @item
86 Everywhere where you use a constant pathname from installation-time,
87 wrap it in @code{relocate} so it gets translated to the run-time situation.
88 Example:
90 @example
91 bindtextdomain (PACKAGE, LOCALEDIR);
92 @end example
94 @noindent
95 becomes:
97 @example
98 bindtextdomain (PACKAGE, relocate (LOCALEDIR));
99 @end example
101 The prototype for this function is in @file{relocatable.h}.
103 There is also a variant of this function, named @code{relocate2}, that
104 makes it easy to reclaim the memory allocated by the call.
106 @item
107 The @code{set_program_name} function can also configure some
108 additional libraries to relocate files that they access, by defining
109 corresponding C preprocessor symbols to 1.  The libraries for which
110 this is supported and the corresponding preprocessor symbols are:
112 @table @asis
113 @item libcharset
114 @code{DEPENDS_ON_LIBCHARSET}
116 @item libiconv
117 @code{DEPENDS_ON_LIBICONV}
119 @item libintl
120 @code{DEPENDS_ON_LIBINTL}
121 @end table
123 Defining the symbol for a library makes every program in the package
124 depend on that library, whether the program really uses the library or
125 not, so this feature should be used with some caution.
127 @item
128 @mindex relocatable-script
129 If your package installs shell scripts, also import the
130 @code{relocatable-script} module.  Then, near the beginning of each
131 shell script that your package installs, add the following:
133 @smallexample
134 @@relocatable_sh@@
136 prefix="@@prefix@@"
137 exec_prefix="@@exec_prefix@@"   # usually needs $prefix.
138 datarootdir="@@datarootdir@@"   # usually needs $prefix.
140 if test "@@RELOCATABLE@@" = yes; then
141   bindir="@@bindir@@"
142   orig_installdir="$bindir" # see Makefile.am's *_SCRIPTS variables
143   func_find_curr_installdir # determine curr_installdir
144   func_find_prefixes
145   relocate () @{
146     echo "$1/" \
147     | sed -e "s%^$@{orig_installprefix@}/%$@{curr_installprefix@}/%" \
148     | sed -e 's,/$,,'
149   @}
150 else
151   relocate () @{
152     echo "$1"
153   @}
156 # Get some relocated directory names.
157 sysconfdir=`relocate "@@sysconfdir@@"`          # usually needs $prefix.
158 some_datadir=`relocate "@@datadir@@/something"` # usually needs $datarootdir.
159 bindir=`relocate "@@bindir@@"`       # usually needs $exec_prefix, hence $prefix.
160 @end smallexample
162 You must adapt the definition of @code{orig_installdir}, depending on
163 where the script gets installed.  Also, at the end, instead of
164 @code{sysconfdir} and @code{some_datadir}, transform those variables
165 that you need.
167 @item
168 @mindex relocatable-perl
169 If your package installs Perl scripts, also import the
170 @code{relocatable-perl} module.  Then, near the beginning of each
171 Perl script that your package installs, add the following:
173 @smallexample
174 @@relocatable_pl@@
175 if ("@@RELOCATABLE@@" eq "yes") @{
176   my $exec_prefix = "@@exec_prefix@@";
177   my $orig_installdir = "@@bindir@@"; # see Makefile.am's *_SCRIPTS variables
178   my ($orig_installprefix, $curr_installprefix) =
179     find_prefixes($orig_installdir, find_curr_installdir());
181   # the subroutine is defined whether or not the enclosing block is executed
182   sub relocate @{
183     my ($dir) = @@_;
184     if ("@@RELOCATABLE@@" eq "yes") @{
185       $dir =~ s%^$orig_installprefix/%$curr_installprefix/%;
186       $dir =~ s,/$,,;
187     @}
188     return $dir;
189   @}
192 # Get some relocated directory names.
193 # (The gnulib module 'configmake' can help with this.)
194 $sysconfdir = relocate("@@sysconfdir@@");
195 $some_datadir = relocate(@@datadir@@/something");
196 @end smallexample
198 You must adapt the definition of @code{$orig_installdir}, depending on
199 where the script gets installed.  Also, at the end, instead of
200 @code{sysconfdir} and @code{some_datadir}, transform those variables
201 that you need.
203 @item
204 In your @file{Makefile.am}, for every program @command{foo} that gets
205 installed in, say, @file{$(bindir)}, you add:
207 @example
208 foo_CPPFLAGS = -DINSTALLDIR=$(bindir_c_make)
209 if RELOCATABLE_VIA_LD
210 foo_LDFLAGS = `$(RELOCATABLE_LDFLAGS) $(bindir)`
211 endif
212 @end example
214 Similarly, if a program @command{foo} gets installed in @file{$(pkglibdir)},
215 you add:
217 @example
218 foo_CPPFLAGS = -DINSTALLDIR=$(pkglibdir_c_make)
219 if RELOCATABLE_VIA_LD
220 foo_LDFLAGS = `$(RELOCATABLE_LDFLAGS) $(pkglibdir)`
221 endif
222 @end example
224 The Makefile variables @code{bindir_c_make} or @code{pkglibdir_c_make}
225 get defined by the Autoconf macros
226 @code{gl_BUILD_TO_HOST_BINDIR} or @code{gl_BUILD_TO_HOST_PKGLIBDIR},
227 respectively.
228 These Autoconf macros are defined in the file @code{m4/build-to-host.m4}.
229 You need to
230 @itemize @bullet
231 @item
232 Import this file @code{m4/build-to-host.m4} into your package, for example
233 by using of a command like @samp{gnulib-tool --copy m4/build-to-host.m4}.
234 @item
235 Invoke the corresponding macro(s) from your package's @file{configure.ac}.
236 @end itemize
238 @item
239 When building gnulib to use with a relocatable library, you need to
240 define the preprocessor symbol @code{IN_LIBRARY}.
241 You may also want to build with @code{ENABLE_COSTLY_RELOCATABLE}, in which case
242 you will also need to define @code{INSTALLDIR}.
243 The following fragment can be added to an override @code{Makefile.am} used
244 to build gnulib (@pxref{Modified build rules}).
246 @example
247 AM_CPPFLAGS += -DIN_LIBRARY -DENABLE_COSTLY_RELOCATABLE
249 if SHLIBS_IN_BINDIR
250 AM_CPPFLAGS += -DINSTALLDIR=$(bindir_c_make)
251 else
252 AM_CPPFLAGS += -DINSTALLDIR=$(libdir_c_make)
253 endif
254 @end example
256 @code{SHLIBS_IN_BINDIR} is defined in @file{configure.ac} as follows:
258 @smallexample
259 AM_CONDITIONAL([SHLIBS_IN_BINDIR],
260                [case "$host_os" in mingw* | cygwin*) true;; *) false;; esac])
261 @end smallexample
263 @item
264 In your @file{Makefile.am}, for every library @command{libfoo} that gets
265 installed in, say, @file{$(libdir)}, you add:
267 @example
268 if RELOCATABLE_VIA_LD
269 libfoo_la_LDFLAGS = `$(RELOCATABLE_LDFLAGS) $(libdir)`
270 endif
271 @end example
273 @item
274 Add a couple of variable assignments to your @file{Makefile.am}.
276 If your package (or any package you rely on, e.g.@: gettext-runtime)
277 will be relocated together with a set of installed shared libraries,
278 then set @code{RELOCATABLE_LIBRARY_PATH} to a colon-separated list
279 of those libraries' directories, e.g.
280 @example
281 RELOCATABLE_LIBRARY_PATH = $(libdir)
282 @end example
284 If your @file{config.h} is not in @file{$(top_builddir)}, then set
285 @code{RELOCATABLE_CONFIG_H_DIR} to its directory, e.g.
286 @example
287 RELOCATABLE_CONFIG_H_DIR = $(top_builddir)/src
288 @end example
289 @end enumerate