Using eclasses

An eclass is a collection (library) of functions or functionality that is shared between packages. See Eclass writing guide for the full story on what eclasses can do, how they work and how to write them, and Eclass reference for documentation on various commonly used eclasses. This section only explains how to use an eclass which has already been written.

The inherit function

To use an eclass, it must be 'inherited'. This is done via the inherit function, which is provided by ebuild.sh. The inherit statement must come at the top of the ebuild, before any functions. Conditional inherits are illegal (except where the inheritance criteria are cache-constant — see The Portage cache).

When using inherit, it is best practice to sort the arguments (eclasses) alphabetically. An exception is where the phases exported by an eclass are affected by subsequent arguments. For example, multilib-minimal.eclass mentions in its documentation that it should be inherited last because it overrides most phases.

After inheriting an eclass, its provided functions can be used as normal. Here's an example ebuild, foomatic-0.1-r2.ebuild, which uses three eclasses:

# Copyright 1999-2021 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=7

inherit autotools bash-completion-r1 flag-o-matic

DESCRIPTION="Tool for foo"
HOMEPAGE="https://foomatic.sf.net"
SRC_URI="mirror://sourceforge/${PN}/${P}.tar.gz"

LICENSE="GPL-2"
SLOT="0"
KEYWORDS="alpha ~amd64 ~x86 ~x64-macos"

RDEPEND="sys-libs/ncurses:0=
	>=sys-libs/readline:0="
DEPEND="${RDEPEND}"

src_prepare() {
	eapply "${FILESDIR}/${P}-gentoo.patch"
	eapply_user
	eautoreconf
}

src_configure() {
	econf --sysconfdir="${EPREFIX}"/etc/devtodo
}

src_compile() {
	replace-flags -O? -O1
	default
}

src_install() {
	default
	dobashcomp "${FILESDIR}/${PN}.bash-completion" ${PN}
}

Note the inherit immediately after the header.

The autotools eclass (see autotools.eclass) is needed to get the eautoreconf function. The flag-o-matic eclass (see flag-o-matic.eclass) is needed for replace-flags, and the bash-completion-r1 eclass (bash-completion-r1.eclass) is used to handle the bash completion file via dobashcomp.