About

This post serves as a reminder for myself and other FreeBSD port maintainers who sometimes have to patch a FreeBSD port. The process is simple but sometimes easy to forget, and that's why I decided to document the process.

Background

For the purposes of this post we're going to assume we have an imaginary port /usr/ports/devel/foo that includes the tarball foo-v1.0.0.tgz. The tarball includes the following C program that we will want to patch, and our patch is going to return EXIT_SUCCESS instead of EXIT_FAILURE:

#include <stdio.h>
#include <stdlib.h>

int
main(void)
{
  fprintf(stderr, "i am a teapot\n");
  return (EXIT_FAILURE);
}

Experiment

1. make extract

The first step in the process is to extract the tarball into the work/ directory. This will provide us with the source code that we want to patch. After this step we can expect the source code to be available in work/foo-v1.0.0/, and our C program will be available at work/foo-v1.0.0/main.c:

cd /usr/ports/devel/foo
make extract

2. cp

The next step is to create a copy of the file we want to patch. The copy should have the suffix .orig and placed alongside the original file. We will eventually edit main.c and main.c.orig will act as a reference point for the original file when we create the patch:

cp main.c main.c.orig

3. sed

This might normally be done with a text editor but for the purposes of this example we're going to use sed to patch the file in place:

sed -i '' s|EXIT_FAILURE|EXIT_SUCCESS| main.c

4. make makepatch

We're almost there. This step will create a patch file in the files/ directory:

make makepatch

5. make clean patch

And finally, we can clean the working directory and apply our patch:

make clean patch

Conclusion

Sometimes being able to patch a port can be the difference between being being able to build a port or not, so it is useful to know how to do it.