Eclass Writing Guide

This section provides a brief introduction to eclass authoring.

You should reread the and sections before continuing.
Purpose of Eclasses

An eclass is a collection of code which can be used by more than one ebuild. At the time of writing, all eclasses live in the eclass/ directory in the tree. Roughly speaking, there are three kinds of eclass:

  • Those which provide common functions which are used by many ebuilds (for example, autotools, bash-completion-r1, flag-o-matic, toolchain-funcs)
  • Those which provide a basic build system for many similar packages (for example, perl-module, vim-plugin)
  • Those which handle one or a small number of packages with complex build systems (for example, kernel-2, toolchain)
Adding and Updating Eclasses

Before committing a new eclass to the tree, it must be emailed to the gentoo-dev mailing list with a justification and a proposed implementation. Do not skip this step sometimes a better implementation or an alternative which does not require a new eclass will be suggested.

Before updating any eclass, email patches to the gentoo-dev list. It may be that your proposed change is broken in a way you had not anticipated, or that there is an existing function which performs the same purpose, or that your function may be better off in its own eclass. If you don't email gentoo-dev first, and end up breaking something, expect to be in a lot of trouble.

Note that you should also, when sending your patch, CC any maintainers of the eclass listed in the @MAINTAINER tag. You may want to contact them ahead of time and get their opinions too.

The exceptions to this rule are updates to per-package eclasses. For example, the apache-2 eclass is only used by the www-servers/apache package, and thus does not typically require changes to be emailed for review.

When removing a function or changing the API of an eclass, make sure that it doesn't break any ebuilds in the Gentoo repository. Optionally, you may wish to do a survey of some (official) overlays like ::guru, ::musl, or ::science in order to better understand how it is currently used and where possible avoid breakage by pinging the maintainers directly.

If there is an existing maintainer for an eclass (this is usually the case), you must talk to the maintainer before committing any changes.

Committing a broken eclass can kill huge numbers of packages. Since repoman is not eclass-aware, be very sure you do proper testing.

A simple way to verify syntax is bash -n foo.eclass.

Given that updating an eclass will invalidate the cache of all consumer ebuilds, it is good etiquette to ping other developers in e.g. #gentoo-dev on IRC or informally determine if there are other similar changes (mainly documentation) which should be pushed at the same time in order to avoid unnecessary cache regeneration within a few hours or days of each other. The same applies to your own work please prepare all of your commits and batch push them at once where possible.
Removing Eclasses

No longer used eclasses may be removed from the tree, but developers must adhere to the following process:

  1. Make sure that no packages or other eclasses in the tree inherit the eclass.
  2. Send a lastrite message to the gentoo-dev and gentoo-dev-announce mailing-lists, announcing that the not-used eclass will be removed in 30 days.
  3. Add a one line comment to the eclass, saying exactly # @DEAD so that the eclass-manpages package will not attempt to document it. @DEAD must be the first documentation tag in the eclass, i.e. it goes before the @ECLASS tag.
  4. Wait for the 30-day period to pass, then remove the eclass from the tree.
Before considering removal, please mark the eclass as # @DEPRECATED to ensure consumers are warned if they are still using the eclass. Do this for a period before removing the eclass.
Documenting Eclasses

Eclasses are documented with comment blocks that follow a special markup syntax. The comment blocks are separated by blank lines and each block documents an individual element of an eclass.

Documentation tags for various eclass elements are explained in their respective sections below. Common to all the tags that accept multiline freetext, the @CODE tag should be used when necessary to create unformatted code chunks (such as example code) by placing the tag at the beginning and the end. The @SUBSECTION tag inserts a subsection heading; it is allowed only in the main @DESCRIPTION.

Note that pkgcheck can check for issues in eclass documentation. You could run e.g. pkgcheck scan -s eclass to check for issues in the tree or pkgcheck scan --commits to check for issues in your staged git commits.

Basic Eclass Format

An eclass is a bash script which is sourced within the ebuild environment. Files should be a simple text file with a .eclass extension. Allowed characters in the filename are lowercase letters, the digits 0-9, hyphens, underscores and dots. Eclasses are not intrinsically versioned.

Eclasses should start with the standard ebuild header, along with comments explaining the maintainer and purpose of the eclass, and any other relevant documentation. Eclass documentation block should be the first documentation block to appear in the eclass. The following table summarizes the available documentation tags:

@ECLASS:NOName of the eclass being documented Documents various information about the eclass. Must appear as the first tag in the comment block. @MAINTAINER:NOOne or more name and email pairs List of maintainers for the eclass to be contacted. One line per maintainer. Must start on a newline after the tag. @AUTHOR:YESOne or more name and email pairs List of authors for the eclass. One line per author. Must start on a newline after the tag. @BUGREPORTS:YESMultiline freetextDescribes how bugs related to this eclass should be reported@VCSURL:YESA URI stringPoints to the URL of the VCS that contains the eclass source@SUPPORTED_EAPIS:YESSpace-separated list of EAPIsList of EAPIs supported by this eclass@PROVIDES:YESSpace-separated list of eclass names List of indirectly inherited eclasses whose functions and variables may be used by an ebuild inheriting this eclass @BLURB:NOSingle line freetext Contains a short description for the eclass. Must be on the same line with the tag. @DEPRECATED:YESReplacement (or "none")Declares that this eclass should no longer be used@DESCRIPTION:YESMultiline freetextLong description for the eclass@EXAMPLE:YESMultiline freetextExamples that illustrate various uses of this eclass
tag optional? arguments description
Eclass Variables

Top level variables may be defined as for ebuilds. If any USE flags are used, IUSE must be set. The KEYWORDS variable must not be set in an eclass.

You should document the meaning, usage, and default value of every variable in your eclass. The tags available for documenting eclass variables are as follows:

@ECLASS-VARIABLE:NOName of the eclass variable to which the documentation applies This tag applies to eclass specific variables that affect the default behavior of the eclass. Read-only variables, which are exported by the eclass for developer use, are also documented with this tag. Must appear as the first tag in the comment block. @PRE_INHERITYES This tag describes whether the variable must be defined before the eclass is inherited. This is important if any e.g. dependencies are modified in global scope at the point of sourcing. @USER_VARIABLEYES This tag describes whether the variable is unsuitable for use in ebuilds, i.e. if it is solely for user consumption via e.g. make.conf or a similar mechanism @OUTPUT_VARIABLEYES This tag indicates that the variable's value (which will be set by the eclass) should be read within an ebuild @DEFAULT_UNSETYES Indicates that this variable is unset by default if not set by the developer @INCLUDES_EPREFIXYES Indicates that the variable is a path which has ${EPREFIX} prepended to it @INTERNALYESIndicates that the variable is internal to the eclass@REQUIREDYESIndicates that this variable must be set by the developer@DEPRECATEDYESOptionally, the name of any replacement variableDeclares that this variable should no longer be used in ebuilds@DESCRIPTION:NOMultiline freetextLong description for the eclass variable
tag optional? arguments description
Eclass Functions

Eclasses may define functions. These functions will be visible to anything which inherits the eclass.

You should document the purpose, arguments, usage, and return value of every function in your eclass. The standard tags available for documentation are:

@FUNCTION:NOName of the function to which the documentation block applies Documents information about an eclass function such as its calling convention etc. Must appear as the first tag in the comment block @USAGE:NOList of required and optional arguments to the function List of arguments that the eclass function accepts, specified in the following syntax: <Required arguments> [Optional arguments] @RETURN:*Return value of the function

Description for the value returned by the function.

*: Not optional for functions that return a value.

@MAINTAINER:YESMultiline freetext List of contacts for the eclass function. One line per maintainer. Must start on a newline after the tag. @INCLUDES_EPREFIXYES Indicates whether the function returns a path with ${EPREFIX} prepended to it @INTERNALYES Indicates that the function is internal to the eclass and should not be called from outside @DEPRECATEDYESOptionally, the name of a replacement functionDeclares that this function should no longer be used in ebuilds@DESCRIPTION:*Multiline freetext

Long description for the eclass function.

*: Not optional if RETURN: is absent.

tag optional? arguments description
Eclass Function Variables

Eclass functions may make use of variables just like any other bash function. Special function-specific variables should be documented using the following tags:

@VARIABLE:NO Name of the function-specific variable to which the documentation applies This tag applies to variables consumed by specific functions of the eclass. They are in effect only when the specific function is called. @USER_VARIABLEYES This tag describes whether the variable is unsuitable for use in ebuilds, i.e. if it is solely for user consumption via e.g. make.conf or a similar mechanism @DEFAULT_UNSETYES Indicates that this variable is unset by default if not set by the developer @INCLUDES_EPREFIXYESIndicates that the variable is a path which has ${EPREFIX} prepended@INTERNALYESIndicates that the variable is internal to the eclass function@REQUIREDYESIndicates that this variable must be set by the developer@DEPRECATEDYESOptionally, the name of any replacement variableDeclares that this variable should no longer be used in ebuilds@DESCRIPTION:NOMultiline freetextLong description for the function variable
tag optional? arguments description
Simple Common Functions Eclass Example

As an example, the following eclass was written to illustrate a better way of installing OSX application files during a discussion on this subject. It defines a single function, domacosapp.

# Copyright 1999-2021 Gentoo Authors # Distributed under the terms of the GNU General Public License v2 # @ECLASS: macosapp.eclass # @MAINTAINER: # Ciaran McCreesh <ciaranm@gentoo.org> # @BLURB: install macos .app files to the relevant location. # @FUNCTION: domacosapp # @USAGE: <app-file> [new-file] # @DESCRIPTION: # Install the given .app file into the appropriate location. If # [new-file] is given, it will be used as the new (installed) name of # the file. Otherwise <app-file> is installed as-is. domacosapp() { [[ -z "${1}" ]] && die "usage: domacosapp <file> [new file]" if use ppc-macos ; then insinto /Applications newins "$1" "${2:-${1}}" fi }
Export Functions

An eclass may provide default implementations for any of the ebuild phase functions (src_unpack, pkg_postinst, etc). This can be done either as a simple function definition (which is not multiple eclass friendly) or using EXPORT_FUNCTIONS. Functions given to EXPORT_FUNCTIONS are implemented as normal, but have their name prefixed ("namespaced") with ${ECLASS}_.

Only the ebuild phase functions may be specified in EXPORT_FUNCTIONS. EXPORT_FUNCTIONS is a function, not a variable.

If multiple eclasses export the same function, the latest (inherited last) defined version wins. If an ebuild defines a function that is exported, this gets priority over any eclass version. This can be used to override eclass-defined defaults for example, say we had fnord.eclass:

EXPORT_FUNCTIONS src_compile fnord_src_compile() { do_stuff || die }

Then an ebuild could do this:

inherit fnord src_compile() { do_pre_stuff || die fnord_src_compile do_post_stuff || die }
Simple Build System Eclass Example

A simple eclass which defines a number of functions and a default src_compile for the (hypothetical) jmake build system might look something like the following:

# Copyright 1999-2021 Gentoo Authors # Distributed under the terms of the GNU General Public License v2 # @ECLASS: jmake.eclass # @MAINTAINER: # Gentoo Devmanual Project <devmanual@gentoo.org> # @AUTHOR: # Ciaran McCreesh <ciaranm@gentoo.org> # @BLURB: Demonstrate a simple build system eclass. # @DESCRIPTION: # Demonstrates EXPORT_FUNCTIONS and defines simple wrappers for the # (hypothetical) jmake build system and a default src_compile. EXPORT_FUNCTIONS src_compile BDEPEND=">=sys-devel/jmake-2" # @FUNCTION: jmake-configure # @USAGE: [additional-args] # @DESCRIPTION: # Passes all arguments through to the appropriate "jmake configure" # command. jmake-configure() { jmake configure --prefix=/usr "$@" } # @FUNCTION: jmake-build # @USAGE: [additional-args] # @DESCRIPTION: # First builds all dependencies, and then passes through its arguments # to the appropriate "jmake build" command. jmake-build() { jmake dep && jmake build "$@" } # @FUNCTION: jmake-src_compile # @DESCRIPTION: # Calls jmake-configure() and jmake-build() to compile a jmake project. jmake_src_compile() { jmake-configure || die "configure failed" jmake-build || die "build failed" }
Handling incorrect usage of an eclass

Sometimes an eclass is used incorrectly by an ebuild and the eclass knows it is being used incorrectly the common example is an eclass that only works with a specific set of EAPIs, but is being accessed inherited by an ebuild with a different EAPI. In those cases, used sparingly as a last resort, it is allowed for an eclass to invoke die from the global scope. For example:

# Copyright 1999-2021 Gentoo Authors # Distributed under the terms of the GNU General Public License v2 # @ECLASS: eapi-die.eclass # @MAINTAINER: # Gentoo Devmanual Project <devmanual@gentoo.org> # @SUPPORTED_EAPIS: 7 8 # @BLURB: Calls die when used with an invalid EAPI. case ${EAPI} in 7|8) ;; *) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;; esac