mktime: add libc-config dependency
[gnulib.git] / doc / relocatable-maint.texi
blobd1e7090bc08daecbc1d6c78363631aa5be050157
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
29 does this job.
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:
36 @itemize
37 @item
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.
42 @item
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.
47 @item
48 On Windows, the executable's own directory is searched for libraries,
49 so installing shared libraries into the executable's directory is
50 sufficient.
51 @end itemize
53 You can make your program relocatable by following these steps:
55 @enumerate
56 @item
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}).
62 @item
63 In every program, add to @code{main} as the first statement (even
64 before setting the locale or doing anything related to libintl):
66 @example
67 set_program_name (argv[0]);
68 @end example
70 The prototype for this function is in @file{progname.h}.
72 @item
73 If you want your code to be portable to platforms that do not support
74 automatic initialization, call @code{set_relocation_prefix}.
76 @item
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.
79 Example:
81 @example
82 bindtextdomain (PACKAGE, LOCALEDIR);
83 @end example
85 @noindent
86 becomes:
88 @example
89 bindtextdomain (PACKAGE, relocate (LOCALEDIR));
90 @end example
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.
97 @item
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:
103 @table @asis
104 @item libcharset
105 @code{DEPENDS_ON_LIBCHARSET}
107 @item libiconv
108 @code{DEPENDS_ON_LIBICONV}
110 @item libintl
111 @code{DEPENDS_ON_LIBINTL}
112 @end table
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.
118 @item
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:
123 @smallexample
124 @@relocatable_sh@@
125 if test "@@RELOCATABLE@@" = yes; then
126   exec_prefix="@@exec_prefix@@"
127   bindir="@@bindir@@"
128   orig_installdir="$bindir" # see Makefile.am's *_SCRIPTS variables
129   func_find_curr_installdir # determine curr_installdir
130   func_find_prefixes
131   relocate () @{
132     echo "$1/" \
133     | sed -e "s%^$@{orig_installprefix@}/%$@{curr_installprefix@}/%" \
134     | sed -e 's,/$,,'
135   @}
136 else
137   relocate () @{
138     echo "$1"
139   @}
142 # Get some relocated directory names.
143 sysconfdir=`relocate "@@sysconfdir@@"`
144 some_datadir=`relocate "@@datadir@@/something"`
145 @end smallexample
147 You must adapt the definition of @code{orig_installdir}, depending on
148 where the script gets installed.  Also, at the end, instead of
149 @code{sysconfdir} and @code{some_datadir}, transform those variables
150 that you need.
152 @item
153 If your package installs Perl scripts, also import the
154 @code{relocatable-perl} module.  Then, near the beginning of each
155 Perl script that your package installs, add the following:
157 @smallexample
158 @@relocatable_pl@@
159 if ("@@RELOCATABLE@@" eq "yes") @{
160   my $exec_prefix = "@@exec_prefix@@";
161   my $orig_installdir = "@@bindir@@"; # see Makefile.am's *_SCRIPTS variables
162   my ($orig_installprefix, $curr_installprefix) =
163     find_prefixes($orig_installdir, find_curr_installdir());
165   # the subroutine is defined whether or not the enclosing block is executed
166   sub relocate @{
167     my ($dir) = @@_;
168     if ("@@RELOCATABLE@@" eq "yes") @{
169       $dir =~ s%^$orig_installprefix/%$curr_installprefix/%;
170       $dir =~ s,/$,,;
171     @}
172     return $dir;
173   @}
176 # Get some relocated directory names.
177 # (The gnulib module 'configmake' can help with this.)
178 $sysconfdir = relocate("@@sysconfdir@@");
179 $some_datadir = relocate(@@datadir@@/something");
180 @end smallexample
182 You must adapt the definition of @code{$orig_installdir}, depending on
183 where the script gets installed.  Also, at the end, instead of
184 @code{sysconfdir} and @code{some_datadir}, transform those variables
185 that you need.
187 @item
188 In your @file{Makefile.am}, for every program @command{foo} that gets
189 installed in, say, @file{$(bindir)}, you add:
191 @example
192 foo_CPPFLAGS = -DINSTALLDIR=\"$(bindir)\"
193 if RELOCATABLE_VIA_LD
194 foo_LDFLAGS = `$(RELOCATABLE_LDFLAGS) $(bindir)`
195 endif
196 @end example
198 When building gnulib to use with a relocatable library, you need to
199 define the preprocessor symbol @code{IN_LIBRARY}.
200 You may also want to build with @code{ENABLE_COSTLY_RELOCATABLE}, in which case
201 you will also need to define @code{INSTALLDIR}.
202 The following fragment can be added to an override @code{Makefile.am} used
203 to build gnulib (@pxref{Modified build rules}).
205 @example
206 AM_CPPFLAGS += -DIN_LIBRARY -DENABLE_COSTLY_RELOCATABLE
208 if SHLIBS_IN_BINDIR
209 AM_CPPFLAGS += -DINSTALLDIR=\"$(bindir)\"
210 else
211 AM_CPPFLAGS += -DINSTALLDIR=\"$(libdir)\"
212 endif
213 @end example
215 @code{SHLIBS_IN_BINDIR} is defined in @file{configure.ac} as follows:
217 @smallexample
218 AM_CONDITIONAL([SHLIBS_IN_BINDIR],
219                [case "$host_os" in mingw* | cygwin*) true;; *) false;; esac])
220 @end smallexample
222 @item
223 You may also need to add a couple of variable assignments to your
224 @file{configure.ac}.
226 If your package (or any package you rely on, e.g.@: gettext-runtime)
227 will be relocated together with a set of installed shared libraries,
228 then set @var{RELOCATABLE_LIBRARY_PATH} to a colon-separated list
229 of those libraries' directories, e.g.
230 @example
231 RELOCATABLE_LIBRARY_PATH='$(libdir)'
232 @end example
234 If your @file{config.h} is not in @file{$(top_builddir)}, then set
235 @var{RELOCATABLE_CONFIG_H_DIR} to its directory, e.g.
236 @example
237 RELOCATABLE_CONFIG_H_DIR='$(top_builddir)/src'
238 @end example
239 @end enumerate