Oracle Blog

Ramblings of an Ordinary Joe

Friday Aug 15, 2008

One of my responsibilities on the
Update Center 2.0
project is to perform
builds of Python and wxPython for all of our supported platforms.
One unique aspect of our environment is that we need these builds
to be relocatable as far as the filesystem is concerned in order to
support our
multi-install requirement. That is:
plop the build anywhere on your system and it must work.

A key to making relocatable software work is relative paths. All
references to files within the relocatable install image must be
relative (or at least start out relative and only made absolute
dynamically at runtime). This includes any dependencies that binaries
in the install image might have on dynamic libraries in
the install image.

You can always use LD_LIBRARY_PATH (or
DYLD_LIBRARY_PATH on
the Mac) to force the runtime linker to locate the right
dynamic libraries -- but that's cheating, and should only be used as
a hack of last resort. Better to construct the binaries correctly
so that they can locate their dependencies without the need to
alter the user's environment.

I'm pretty familiar with dynamic libraries on Solaris and Linux.
On those platforms you can embed an RPATH into a binary that is
searched by the runtime linker to locate libraries. Plus both Solaris
and Linux support the $ORIGIN token so you can make these paths
relative to the install location of the binary.

Here the _core_.so binary has dependencies on a number
of wx libraries. RPATH is set to look for libraries relative to the
install location of _core_.so, and therefore the linker can
find these libraries without needing to set LD_LIBRARY_PATH. You
specify the value for RPATH at link time using the
-R option.

Linux is similar. In this case you use objdump -p to inspect
the binaries:

But what about the Mac? That's new territory for me. Here is what
I learned -- I welcome comments since I likely know just enough to
be dangerous.

On the Mac a dynamic library (dylib) has an "install name". The
install name is a path baked into the dynamic library that says
where to find the library at runtime. When you link against the dylib
this path is saved in your binary so that your binary can find
the dylib at runtime. Seems a bit backwards to me -- but that's how
it works.

So wx/_core_.so depends on libwx_macu-2.8.0.dylib,
which is fine. But notice that evil absolute path. That won't work in our
"install anywhere" world. So how do we fix this? What's the Mac OS equivalent
of $ORIGIN and RPATH?

Well, there isn't any. But there is something we can do instead. After
the build is complete I use the install_name_tool utility to
fix up the dylib install names and dependencies in our binaries. Fortunately
the Mac also supports a magic token: @loader_path that can be used
in a fashion similar to $ORIGIN on Solaris/Linux.

So when building wxPython on the Mac I first do a complete build. This results
in the absolute paths being used as mentioned above. Then as part of
my "make install" step I fix up these paths to be relative using the
install_name_tool utility.

For example, this command changes the install name of
libwx_macu-2.8.0.4.0.dylib
to be relative to the location of the binary using it:

This mechanism is still not as flexible as the Solaris/Linux approach, since
binaries can't specify a search path. It's also a bit more difficult to
automate since you must determine the current install name in order to replace
it with the new install name. But it does work.

Note that for your project you may be able to simplify this. For example,
if you build your dynamic libraries with the correct install names first,
then your binaries will pick up the correct install names at link time
and you shouldn't need to change the dependencies post-build.