This is Gentoo's testing wiki. It is a non-operational environment and its textual content is outdated.

Please visit our production wiki at https://wiki.gentoo.org

Basic guide to write Gentoo Ebuilds

From Gentoo Wiki (test)
Jump to:navigation Jump to:search
This page contains changes which are not marked for translation.
This article is a stub. You can help by expanding it.

This article contains instructions for beginners on ebuilds development.

Portage, the heart of Gentoo, uses ebuilds!

An ebuild file is a text file, used by Gentoo package managers, which identifies a specific software package and how the Gentoo package manager should handle it. It uses a bash-like syntax style and is standardized through the EAPI version.

Gentoo Linux uses ebuilds as the package management format for individual software titles. These ebuilds contain metadata about the software (the name and version of the software, which license the software uses, and the home page), dependency information (both build-time as well as run-time dependencies), and instructions on how to deal with the software (configure, build, install, test ...).

Ebuilds, where do they live? How do I create one?

When you had set up Gentoo, you probably remember that you had to download and unpack a Gentoo repository snapshot. This snapshot (which you later update when you run emerge --sync) is full of ebuilds, it is the Gentoo repository and once unpacked is usually located at /usr/portage.

Now, you can't just create a file /usr/portage/hello-world.ebuild and be done with it; there are several reasons:

  1. It is your local copy of the remote Gentoo repository: If you place an ebuild in there or update an ebuild, then you run emerge --sync, your changes will be gone. Therefore, you will want to work in /usr/local/portage, in a sub directory of your home directory or in a custom repository instead.
  2. The ebuild file is not in the right directory: An ebuild has to be located in the "package name" subdirectory of "category" directory; so, for an ebuild to work you have to place it in a directory like /usr/local/portage/app-misc/hello-world/.
  3. The ebuild file name has no version listed: Packages have versions, they need to be specified in the file name; therefore we would like to create a file like /usr/local/portage/app-misc/hello-world/hello-world-1.0.ebuild.

So, let's create us a minimal ebuild; to keep it simple I will assume you run under root privileges, you can always use sudo if you feel like.

root #mkdir -p /usr/local/portage/app-misc/hello-world
root #cd $_

We recursively create the directories and cd into it ($_ recalls the last argument), then we create an ebuild out of the ebuild header which is a necessity if you want it added to the Gentoo repository.

Users of app-editors/vim get the basic skeleton automatically (provided by app-vim/gentoo-syntax):

root #vim ./hello-world-1.0.ebuild
CODE vim starts from the template
# Copyright 1999-2024 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

EAPI=6

DESCRIPTION=""
HOMEPAGE=""
SRC_URI=""

LICENSE=""
SLOT="0"
KEYWORDS="~amd64 ~x86"
IUSE=""

DEPEND=""
RDEPEND="${DEPEND}"

Users of other editors manually copy from the header.txt:

root #cp /usr/portage/header.txt ./hello-world-1.0.ebuild

This won't run yet, it requires us to define a minimal amount of variables, so let's add the following code inside the ebuild:

CODE The minimum code you need to put inside an ebuild
DESCRIPTION="A classical example to use when starting on something new"
SLOT="0"

Just two variables? Exactly, it is required that we have a one-line DESCRIPTION of our package, and that we explicitly state that we won't use SLOTs, which is what "0" means.

We can now install the package to the system by running:

root #ebuild hello-world-1.0.ebuild manifest clean merge

This will manifest (create hashes, to avoid corruption), clean any present temporary work directories and (e)merge the ebuild.

Good, you have just made and tested your first ebuild, it doesn't really do much but it's a good start!

Example: Package for a file that echoes "hello world!"

Adding more useful variables

If you take a look at /usr/portage/skel.ebuild you see a skeleton with a lot of documentation, we will be adding some of these variables and functions to our ebuild as we proceed; so, it seems wise to read over this file as we go. Add the following code blocks to our hello-world-1.0.ebuild:

CODE The council suggests to use the latest ebuild API
EAPI=6
Important
The above variable must be listed first after the header! So, add it above the variables we already have.

More information about the EAPI can be found here.

CODE The homepage which the package was found on, for developer reference
HOMEPAGE="https://wiki.gentoo.org/index.php?title=Basic_guide_to_write_Gentoo_Ebuilds"
CODE The source code which we will download, hosted by the developer who wrote this documentation
SRC_URI="https://dev.gentoo.org/~tomwij/files/wiki/hello-world-1.0.tar.gz"

This is a simple tarball which contains a hello-world shell script which echoes "Hello world!".

Next, we need to specify a license, I hereby tell you I am licensing it under the MIT license so let us specify that.

CODE This package is licensed under the MIT license
LICENSE="MIT"

We already did the SLOT, so we can move on to KEYWORDS. The KEYWORDS variable tells you on which architectures a package works and also tells you whether it is masked (not listed or explicitly listed with -), untested (~) or stable (listed, but with no character in front of it). Since we can't stabilize ourselves (bugs are to be filed for that), the best we can do for now is list all the architectures as untested. All the architectures, because they can all run shell scripts.

CODE We hereby confirm that our package runs on all architectures, but might not be stable yet
KEYWORDS="~alpha ~amd64 ~arm ~hppa ~ia64 ~ppc ~ppc64 ~s390 ~sh ~sparc ~x86"

The other variables define some more specific things (check them out in skel.ebuild) but we won't need them for now; you also see there are functions, but let us see what the ebuild already does by now.

CODE The ebuild should look like this
# Copyright 1999-2018 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

EAPI=6

DESCRIPTION="A classical example to use when starting on something new"
HOMEPAGE="https://wiki.gentoo.org/index.php?title=Basic_guide_to_write_Gentoo_Ebuilds"
SRC_URI="https://dev.gentoo.org/~tomwij/files/wiki/hello-world-1.0.tar.gz"

LICENSE="MIT"
SLOT="0"
KEYWORDS="~alpha ~amd64 ~arm ~hppa ~ia64 ~ppc ~ppc64 ~s390 ~sh ~sparc ~x86"
root #ebuild hello-world-1.0.ebuild manifest clean merge

We see that it first tries to download our file from a mirror, but since it is not on the Gentoo mirrors it will download it from the SRC_URI value that was specified.

Then, when it has the file it can create a manifest, this contains a hash of our ebuild and that downloaded file to ensure integrity such that corruption will yield errors.

Then, the emerge process kicks in, the integrity is first checked. Then, we can see the archive we downloaded is automatically unpacked, this is really useful as we don't have to implement this anymore. We can change this behavior by overriding its function (src_unpack), setting some variables or using eclasses whom define such behavior; but we don't need to do that in this case.

As we read further, we see that it tries to prepare, configure, compile and install. In the prepare phase, patches will typically be applied. In the configure and compile phases, the typical build process is done, by default in runs econf (a wrapper for ./configure) and emake (a wrapper for make) when it finds files to handle; but since we use a shell script, we won't need to adjust these phases.

Now, the last step doesn't look quite right; it doesn't install our file yet...

Telling the ebuild where to install our shell script.

In our development manual we can find a page about the phase functions, src_install seems useful for what we want to do. If you click on the src_install link you will see what it does by default for each EAPI as well as some examples. As the default doesn't look good, we'll define our own src_install function. In our function we will be calling other functions to do installation work for us, an overview for them is install functions.

Warning
Ebuilds should never directly install to the live file system, therefore we do not use typical commands like mv, cp, and rm. Moreover we do not use absolute paths. We use the above command and work against ${D} which is the destination directory.

So, we can proceed by adding the following function to our ebuild:

CODE call to src_install is required for our hello-world shell script to later be placed in /usr/bin and made executable
src_install() {
    dobin hello-world
}

That dobin call will copy hello-world to a temporary build directory (${D}/usr/bin/), make it executable; later on it will be checked by Portage and copied to the live file systems.

Let us try again...

root #ebuild hello-world-1.0.ebuild manifest clean merge

Ah, we see ">>> /usr/bin/hello-world", that looks good!

Let us try...

user $hello-world

And there we have it, we just installed a package that echoes "Hello world!".

To be continued!

New sections will get added as new examples get produced...

Here are some ideas for more examples if anyone wants to help writing this article:

  • Expand on the hello world ebuild by adding a src_prepare function where we will patch the package such that the shell script asks for the user's name and uses it instead of world.
  • Usage of variables like P, PN, PV, and PF to ease the maintenance (assume a new version of the package was released).
  • Installation of optional documentation (via IUSE="doc").
  • Usage of autotools, cmake, and other useful eclasses; in easy to use examples.
  • How to ensure QA, deal with QA warnings and errors and set up FEATURES for more reliable ebuild writing.
  • Explain how to contribute and how to become a developer.
Important
This article is meant to be a "hands-on introduction", its scope is therefore limited and it is not intended to replace the development manual.

Adding support for user patches to ebuilds

Since EAPI 6, the support for user patches is provided by eapply_user. This can be done by putting default on top in the src_prepare function:

CODE sample src_prepare
src_prepare() {
    default
    ...
}

EAPI versions prior to EAPI 6 should not be used for new ebuilds.

Using the PATCHES array

If you need to apply multiple patches to the source code you can declare an array called PATCHES and they will be automatically applied in src_prepare. Here is an example:

CODE sample src_prepare
PATCHES=(
	"${FILESDIR}"/${P}-fix-build.patch
	"${FILESDIR}"/${P}-fix-test.patch
)

See also

External resources