Download, modify sources from FreeBSD ports repository and build it

Makefiles in the FreeBSD Ports Collection contain links (to source codes) and instructuions (how to build a program specifically for FreeBSD). Therefore, when the program source code is written with an accent on Linux, it is better to install it from the ports collection since there may be differences (paths to libraries, paths to kernel sources, use of linux-specific utilities and commands and many others).

Therefore, in this article we will look at how we can download the source code, modify it (or patch) and build it specifically for FreeBSD.

First we need an example port. Let it be the dwm window manager. Go to the port directory:

cd /usr/ports/x11-wm/dwm/

Here is the Makefile of this port, which contains an internet link to source codes and compiling instructions specific for FreeBSD.

We can download the source code archive using fetch command. It will be downloaded to /usr/ports/distfiles/ directory:

sudo make fetch
ls /usr/ports/distfiles/

Now we can extract it without leaving the port directory. Use extract command. The archive will be unpacked in /usr/ports/${PORT_GROUP}/${PORT_NAME}/work/${ARCHIVE_NAME}/ directory:

sudo make extract
ls /usr/ports/x11-wm/dwm/work/dwm-6.2/
LICENSE Makefile README config.def.h drw.c drw.h dwm.1 dwm.c ...

We can go to the sources directory:

cd work/dwm-6.2/     ### Here we can edit sources, configs and patch them.

Here we can make patches and other modifications of programs:

sudo mv config.def.h config.h

Once we’ve finished changing the source code, we’ll need to go back to the root directory of the port. Because the main Makefile (with instructions specifically for FreeBSD) is located in the root directory of the port. For example, I failed to compile dwm with Makefile which located in work/dwm-6.2/ directory. It uses other fonts, libs paths that are popular in Linux. I added them again and again as additional parameters, but there are too many errors. Therefore, in order not to waste time, we need to build with an already configured Makefile which is located in the root directory of the port:

cd /usr/ports/x11-wm/dwm/  ## Here is the main Makefile with already prepared FreeBSD settings

Now we can build and install the port. All changes in the source files will take effect:

sudo make install clean

Don’t forget to delete the archive with source codes which is no longer needed:

sudo rm /usr/ports/distfiles/dwm-6.2.tar.gz