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 The @code{relocatable-prog} module aims to ease the process of making a
24 GNU program relocatable. It helps overcome two obstacles. First, it aids
25 with relocating the hard-coded references to absolute file names that
26 GNU programs often contain. These references must be fixed up at
27 runtime if a program is to be successfully relocated. The
28 @code{relocatable-prog} module provides a function @code{relocate} that
31 Second, the loader must be able to find shared libraries linked to
32 relocatable executables or referenced by other shared libraries linked
33 to relocatable executables. The @code{relocatable-prog} module helps out
34 here in a platform-specific way:
38 On GNU/Linux, it adds a linker option (@option{-rpath}) that causes
39 the dynamic linker to search for libraries in a directory relative to
40 the location of the invoked executable.
43 On other Unix systems, it installs a wrapper executable. The wrapper
44 sets the environment variable that controls shared library searching
45 (usually @env{LD_LIBRARY_PATH}) and then invokes the real executable.
48 On Windows, the executable's own directory is searched for libraries,
49 so installing shared libraries into the executable's directory is
53 You can make your program relocatable by following these steps:
57 Import the @code{relocatable-prog} module. For libraries, use the
58 @code{relocatable-lib} or @code{relocatable-lib-lgpl} module.
59 If you need more than one module, or you need to use them with different
60 settings, you will need multiple copies of gnulib (@pxref{Multiple instances}).
63 In every program, add to @code{main} as the first statement (even
64 before setting the locale or doing anything related to libintl):
67 set_program_name (argv[0]);
70 The prototype for this function is in @file{progname.h}.
73 If you want your code to be portable to platforms that do not support
74 automatic initialization, call @code{set_relocation_prefix}.
77 Everywhere where you use a constant pathname from installation-time,
78 wrap it in @code{relocate} so it gets translated to the run-time situation.
82 bindtextdomain (PACKAGE, LOCALEDIR);
89 bindtextdomain (PACKAGE, relocate (LOCALEDIR));
92 The prototype for this function is in @file{relocatable.h}.
94 There is also a variant of this function, named @code{relocate2}, that
95 makes it easy to reclaim the memory allocated by the call.
98 The @code{set_program_name} function can also configure some
99 additional libraries to relocate files that they access, by defining
100 corresponding C preprocessor symbols to 1. The libraries for which
101 this is supported and the corresponding preprocessor symbols are:
105 @code{DEPENDS_ON_LIBCHARSET}
108 @code{DEPENDS_ON_LIBICONV}
111 @code{DEPENDS_ON_LIBINTL}
114 Defining the symbol for a library makes every program in the package
115 depend on that library, whether the program really uses the library or
116 not, so this feature should be used with some caution.
119 If your package installs shell scripts, also import the
120 @code{relocatable-script} module. Then, near the beginning of each
121 shell script that your package installs, add the following:
127 exec_prefix="@@exec_prefix@@" # usually needs $prefix.
128 datarootdir="@@datarootdir@@" # usually needs $prefix.
130 if test "@@RELOCATABLE@@" = yes; then
132 orig_installdir="$bindir" # see Makefile.am's *_SCRIPTS variables
133 func_find_curr_installdir # determine curr_installdir
137 | sed -e "s%^$@{orig_installprefix@}/%$@{curr_installprefix@}/%" \
146 # Get some relocated directory names.
147 sysconfdir=`relocate "@@sysconfdir@@"` # usually needs $prefix.
148 some_datadir=`relocate "@@datadir@@/something"` # usually needs $datarootdir.
149 bindir=`relocate "@@bindir@@"` # usually needs $exec_prefix, hence $prefix.
152 You must adapt the definition of @code{orig_installdir}, depending on
153 where the script gets installed. Also, at the end, instead of
154 @code{sysconfdir} and @code{some_datadir}, transform those variables
158 If your package installs Perl scripts, also import the
159 @code{relocatable-perl} module. Then, near the beginning of each
160 Perl script that your package installs, add the following:
164 if ("@@RELOCATABLE@@" eq "yes") @{
165 my $exec_prefix = "@@exec_prefix@@";
166 my $orig_installdir = "@@bindir@@"; # see Makefile.am's *_SCRIPTS variables
167 my ($orig_installprefix, $curr_installprefix) =
168 find_prefixes($orig_installdir, find_curr_installdir());
170 # the subroutine is defined whether or not the enclosing block is executed
173 if ("@@RELOCATABLE@@" eq "yes") @{
174 $dir =~ s%^$orig_installprefix/%$curr_installprefix/%;
181 # Get some relocated directory names.
182 # (The gnulib module 'configmake' can help with this.)
183 $sysconfdir = relocate("@@sysconfdir@@");
184 $some_datadir = relocate(@@datadir@@/something");
187 You must adapt the definition of @code{$orig_installdir}, depending on
188 where the script gets installed. Also, at the end, instead of
189 @code{sysconfdir} and @code{some_datadir}, transform those variables
193 In your @file{Makefile.am}, for every program @command{foo} that gets
194 installed in, say, @file{$(bindir)}, you add:
197 foo_CPPFLAGS = -DINSTALLDIR=\"$(bindir)\"
198 if RELOCATABLE_VIA_LD
199 foo_LDFLAGS = `$(RELOCATABLE_LDFLAGS) $(bindir)`
203 When building gnulib to use with a relocatable library, you need to
204 define the preprocessor symbol @code{IN_LIBRARY}.
205 You may also want to build with @code{ENABLE_COSTLY_RELOCATABLE}, in which case
206 you will also need to define @code{INSTALLDIR}.
207 The following fragment can be added to an override @code{Makefile.am} used
208 to build gnulib (@pxref{Modified build rules}).
211 AM_CPPFLAGS += -DIN_LIBRARY -DENABLE_COSTLY_RELOCATABLE
214 AM_CPPFLAGS += -DINSTALLDIR=\"$(bindir)\"
216 AM_CPPFLAGS += -DINSTALLDIR=\"$(libdir)\"
220 @code{SHLIBS_IN_BINDIR} is defined in @file{configure.ac} as follows:
223 AM_CONDITIONAL([SHLIBS_IN_BINDIR],
224 [case "$host_os" in mingw* | cygwin*) true;; *) false;; esac])
228 You may also need to add a couple of variable assignments to your
231 If your package (or any package you rely on, e.g.@: gettext-runtime)
232 will be relocated together with a set of installed shared libraries,
233 then set @var{RELOCATABLE_LIBRARY_PATH} to a colon-separated list
234 of those libraries' directories, e.g.
236 RELOCATABLE_LIBRARY_PATH='$(libdir)'
239 If your @file{config.h} is not in @file{$(top_builddir)}, then set
240 @var{RELOCATABLE_CONFIG_H_DIR} to its directory, e.g.
242 RELOCATABLE_CONFIG_H_DIR='$(top_builddir)/src'