How to install a patched uwsgi package in FreeBSD
In my case, I needed a patched version of the uwsgi package because I found a bug on my current version (0.9.8) that was already fixed in the -current/trunk version of uwsgi, but it hasn't been backported to any stable release yet (hopefully it will be in 0.9.8.4).
So, to fix the bug I needed the latest -current/trunk code or the latest stable release (0.9.8.3) + the needed patch.
Installing -current/trunk means compiling the sources and installing the binaries by myself, which will lead to some problems next time I had to upgrade the packages and ports in that server, so it seemed like a much better option to install the latest stable version using the ports tree. Anyway, I still would need to apply the patch to the 0.9.8.3 sources before installing... this is what I've done to add the patch cleanly.
I ran all the commands as root, if needed use sudo or some other tool to gain root privileges in order to use the ports tree.
First I got the sources of uwsgi using the ports tree:
# cd /usr/ports/www/uwsgi
# make fetch
That saved the source tarball into /usr/ports/distfiles:
-rw-r--r-- 1 root wheel 351168 Jul 23 06:28 /usr/ports/distfiles/uwsgi-0.9.8.3.tar.gz
Then I extracted the sources:
# make extract
That extracted the sources into /usr/ports/www/uwsgi/work/:
drwxr-xr-x 18 root wheel 3072 Aug 18 15:47 work/uwsgi-0.9.8.3/
At this point we have two options, we could download an already existing patch or we can generate the patch ourselves. In my case, I generated the patch manually because the already existing patch didn't work.
To generate the patch I renamed the file that was going to be patched:
# cd work/uwsgi-0.9.8.3/
# mv plugins/python/wsgi_headers.c plugins/python/wsgi_headers.c.orig
Then I downloaded a copy of the patched version of the file:
# fetch -o plugins/python/wsgi_headers.c "http://projects.unbit.it/uwsgi/export/ddccfbda7423c8a61f178ba5b1526872d38cc285/plugins/python/wsgi_headers.c"
And finally I generated the patch:
# diff -ruN plugins/python/wsgi_headers.c.orig plugins/python/wsgi_headers.c > ../../files/patch-wsgi_headers.c
"Each patch you wish to apply should be saved into a file named patch-* where * indicates the pathname of the file that is patched, such as patch-Imakefile or patch-src-config.h. These files should be stored in PATCHDIR (usually files/, from where they will be automatically applied. All patches must be relative to WRKSRC (generally the directory your port's tarball unpacks itself into, that being where the build is done)."
Once the patch was created, I cleaned up the work directory:
# make clean
And finally I install the new uwsgi package:
# make install clean
The bug was fixed and everything is running smoothly now.
Just some final considerations:
If you would like to know if your patch was applied properly, check the output of make install, you should see something like:
===> Patching for uwsgi-0.9.8.3_1 ===> Applying FreeBSD patches for uwsgi-0.9.8.3_1
Any errors that could have happened while applying the patch would be logged there.
You can build the package before installing it too:
and then you can check the file that should be patched (in this example it was /usr/ports/www/uwsgi/work/uwsgi-0.9.8.3/plugins/python/wsgi_headers.c) to see if it has been patched properly before proceeding with make install.
Remember that before installing the new package, you will have to delete the old one, use pkg_delete or make deinstall to do it.
In this case, the patch will be imported into the next uwsgi release (0.9.8.4) and so it will be available by default in the next upgrade of the uwsgi port, but, if you think that the patch will not be available directly from the official sources of the project, you should contact the author of the FreeBSD port and tell him/her about the patch and how you could help importing it into the ports tree. You can read more about this in the FreeBSD porters handbook.