makefilebuildconfigurebuild-systembuild-settings

DESTDIR vs prefix options in a build system?


Can someone explain me the purpose of a $(DESTDIR) variable in a build system? I mean that I know that it points a temporary directory for currently installed package, but I can not imagine what is practical use of it.

To clarify, well, I know what --prefix option is, for instance if we point the buldsystem like ./configure --prefix="/usr" all the package's files will belong /usr, like /usr/lib, /usr/share and so on, but in Makefiles I've also seen the following constructions:

$(DESTDIR)/$(prefix)

And what's id purpose of that? In short, is there a difference between DESTDIR and prefix and when should both be used?


Solution

  • Yes, there's a very important difference... in some environments.

    The prefix is intended to be the location where the package will be installed (or appear to be installed) after everything is finalized. For example, if there are hardcoded paths in the package anywhere they would be based on the prefix path (of course, we all hope packages avoid hardcoded paths for many reasons).

    DESTDIR allows people to actually install the content somewhere other than the actual prefix: the DESTDIR is prepended to all prefix values so that the install location has exactly the same directory structure/layout as the final location, but rooted somewhere other than /.

    This can be useful for all sorts of reasons. One example are facilities like GNU stow which allow multiple instances to be installed at the same time and easily controlled. Other examples are creating package files using RPM or DEB: after the package is installed you want it unpacked at root, but in order to create the package you need it installed at some other location.

    And other people use it for their own reasons: basically it all boils down to DESTDIR is used to create a "staging area" for the installation, without actually installing into the final location.

    Etc.