Patching with eapply

The canonical way of applying patches in ebuilds is to use the package manager's eapply command, either by calling it explicitly, or by assigning the PATCHES variable supported by the default src_prepare implementation.

The eapply command takes one or more regular file or directory paths as its arguments. Optionally, these can be preceded by GNU patch options.

eapply was added in EAPI 6. It differs from the previously available epatch in several ways:

Basic eapply

In its simplest form, eapply takes a single filename and applies that patch. It will automatically die if the apply fails. The following is taken from sys-libs/gpm:

	eapply "${FILESDIR}"/${P}-musl.patch

In the following simplified example taken from www-client/firefox, a patchset is added to SRC_URI in order to fetch and unpack it. eapply is then called with a directory argument. It applies all patches found in that directory:

SRC_URI+="https://dev.gentoo.org/~larry/patchsets/${P}-patches-01.tar.xz"

src_prepare() {
	eapply "${WORKDIR}/firefox-patches"
	eapply_user
}

The Patches chapter gives some guidelines about where patches should be hosted and about their formatting.

The default src_prepare function will look for a global PATCHES array to apply a list of patches for you.

PATCHES=(
	# Fix install location
	"${FILESDIR}/${P}-destdir.patch"
	# Respect MAKEOPTS #876543
	"${FILESDIR}/${P}-parallel_build.patch"
)

Advanced eapply

This example shows how different patch levels can be applied:

src_prepare() {
	eapply -p2 "${WORKDIR}/${P}-suse-update.patch"
	eapply -p0 "${FILESDIR}/${PV}-no-TIOCGDEV.patch"
	eapply "${FILESDIR}/${PV}-gcc-6.patch"
	eapply_user
}