9.4 PREFIX and DESTDIR

PREFIX determines the location where the port will install. It is usually /usr/local, or /opt. User can set PREFIX to anything he wants. Your port must respect this variable.

DESTDIR, if set by user, determines the complete alternative environment, usually a jail, or an installed system mounted elsewhere than /. A port will actually install into DESTDIR/PREFIX, and register with the package database in DESTDIR/var/db/pkg. It is very important to write ports that respect DESTDIR.

The value of PREFIX will be set to LOCALBASE_REL (default /usr/local). If USE_X_PREFIX or USE_IMAKE is set, PREFIX will be X11BASE_REL (default /usr/X11R6). If USE_LINUX_PREFIX is set, PREFIX will be LINUXBASE_REL (default /compat/linux).

Avoiding the hard-coding of /usr/local or /usr/X11R6 anywhere in the source will make the port much more flexible and able to cater to the needs of other sites. For X ports that use imake, this is automatic; otherwise, this can often be done by simply replacing the occurrences of /usr/local (or /usr/X11R6 for X ports that do not use imake) in the various Makefiles in the port to read ${PREFIX}, as this variable is automatically passed down to every stage of the build and install processes.

Make sure your application is not installing things in /usr/local instead of PREFIX. A quick test for this is to do this is:

# make clean; make package PREFIX=/var/tmp/$(make -V PORTNAME)

If anything is installed outside of PREFIX, the package creation process will complain that it cannot find the files.

This does not test for the existence of internal references, or correct use of LOCALBASE for references to files from other ports. Testing the installation in /var/tmp/$(make -V PORTNAME) to do that while you have it installed would do that.

Do not set USE_X_PREFIX unless your port truly requires it (i.e., it needs to reference files in X11BASE).

The variable PREFIX can be reassigned in your Makefile or in the user's environment. However, it is strongly discouraged for individual ports to set this variable explicitly in the Makefiles.

Also, refer to programs/files from other ports with the variables mentioned above, not explicit pathnames. For instance, if your port requires a macro PAGER to be the full pathname of less, use the compiler flag:

-DPAGER=\"${LOCALBASE}/bin/less\"
instead of -DPAGER=\"/usr/local/bin/less\". This way it will have a better chance of working if the system administrator has moved the whole /usr/local tree somewhere else.

Note that LOCALBASE, LINUXBASE, X11BASE, DOCSDIR, EXAMPLESDIR, DATADIR, DESKTOPDIR variables already contain DESTDIR. Using DESTDIR LOCALBASE is wrong. Use LOCALBASE_REL, LINUXBASE_REL, X11BASE_REL if you need a variable relative to DESTDIR. To keep things terse, TARGETDIR can be used to replace DESTDIR PREFIX.

Example of correct usage:

post-install:
    ${INSTALL_PROGRAM} ${WRKSRC}/helper ${TARGETDIR}/bin/helper
    ${INSTALL_DATA} ${WRKSRC}/guide.txt ${DOCSDIR}

When referencing dependencies in the port, the LOCALBASE is used, as we are working with dependencies inside the target environment. For hardcoding file paths in the software, LOCALBASE_REL must be used, because the software will run inside the target environment.

Example of correct usage:

RUN_DEPENDS=   ${LOCALBASE}/share/gonzo/launch.dat:${PORTSDIR}/games/gonzo

post-patch:
    @${REINPLACE_CMD} -e 's|/usr/gonzo/launch.dat|${LOCALBASE_REL}/share/gonzo/launch.dat}' ${WRKSRC}/main.c
    @${REINPLACE_CMD} -e 's|/etc/game.conf|${PREFIX}/etc/game.conf|' ${WRKSRC}/loader.c

post-install:
    @${INSTALL_DATA} ${WRKSRC}/example/conf ${TARGETDIR}/etc/game.conf

In packing lists and in pkg-* scripts, %%LOCALBASE%%, %%LINUXBASE%% and %%X11BASE%% expansions will contain paths stripped of DESTDIR, as all these files are processed of a context of target environment.

For questions about the FreeBSD ports system, e-mail <ports@FreeBSD.org>.
For questions about this documentation, e-mail <doc@FreeBSD.org>.