## Adding Mercurial patches before building Sage

The following problem and accompanying solution were posted to sage-devel. I have polished them up a bit and put them here so they won’t be buried in the huge sage-devel mailing list.

**Problem**

You have a number of Mercurial patches that you want to apply to a Sage source distribution. However, you don’t want to go through the following work flow:

- Build a Sage source distribution.
- Apply your patches to the Sage library.
- Produce a new source tarball based on your patched Sage source distribution.
- Compile your newly wrapped up modified Sage source tarball.

The key problem is that you don’t want to run through two separate compilation processes.

**Solution**

The following solution uses the queues extension of Mercurial. In a Sage source tarball, the Sage library is wrapped up as the package

SAGE_ROOT/spkg/standard/sage-x.y.z.spkg

Uncompress that bzip2 compressed tarball to get a directory named

SAGE_ROOT/spkg/standard/sage-x.y.z/

If you want, you could delete

SAGE_ROOT/spkg/standard/sage-x.y.z.spkg

Now patch the Sage library as if it had been built and found under SAGE_ROOT/devel:

$ cd SAGE_ROOT/spkg/standard/sage-x.y.z/ $ hg qimport /URL/or/path/to/patch.patch $ hg qpush $ <repeat-previous-two-commands-as-often-as-required>

The command

$ hg qapplied

should return a list of patches you have applied using “hg qpush“. Once you are happy that you have applied all necessary patches, wrap up the patched Sage library:

$ pwd SAGE_ROOT/spkg/standard/sage-x.y.z/ $ hg qfinish -a $ cd ..

And see this section of the Developer’s Guide to find out how to package the newly patched Sage library, i.e. the directory

SAGE_ROOT/spkg/standard/sage-x.y.z/

Now you should have

SAGE_ROOT/spkg/standard/sage-x.y.z.spkg

so you could delete

SAGE_ROOT/spkg/standard/sage-x.y.z/

Finally, proceed to build Sage from source.