Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top


Whenever an ebuild is sourced, the functions and variables within it are loaded into memory by the script interpreter. However, only variables and instructions that are not part of a function are interpreted - functions such as src_compile() are only executed by Portage when the ebuild has reached the compile stage.

The code within these functions are considered in "local scope" while everything outside of the functions is in the "global scope" meaning they are executed every time the ebuild is sourced.

An external application (such as grep, sed or awk) should never be called in global scope for performance reasons, and alternatives such as using built-in bash replacement should be used instead. Useful alternatives can be found in the Advanced Bash Scripting Guide.

Additionally, any external application that may be called in global scope can not be guaranteed to exist on the system. If the command is placed in local scope (for example, in the pkg_setup() function), we can guarantee its presence by placing it in the ebuild's ${DEPEND}.

  1. What it means to ebuild to be sourced? Can it be the source command (btw I don't understand it, it was from and book and it doesn't have "man source".

  2. Okay, variables - how they can be interpreted? I somehow understand how functions can. Functions probably are of some type of programming. It is compiled one or interpreted one. And if ebuild language is scripting (may be it is bash) - like pearl or python that is widely used in Gentoo Portage, how ebuild can contain compilation functions (I remember some from pascal from school).

  3. And thus what is src_compile() function - it was not mentioned in revdel.

  4. "compile stage" - what is this terminology is telling? Which are those ebuild stages, or stages of ebuild?

  5. The same is about local/global scope. I completely don't understand, together with some internal applications - couldn't catch the thought also the opposite (awk..) are mentioned.

  6. pkg_setup() function, $DEPEND - are those only example or imply implicit meanings?

share|improve this question
You should not have an entire series of questions in one post. – Kazark Feb 19 '13 at 21:09
Kazark, how I downvote your comment? – Xsi Feb 21 '13 at 3:50
These sites, as Q&A rather than forums, are designed to have one question per post. One question can have multiple facets, but yours are clearly a series of different questions. To answer your question tho, there is no way to downvote comments. If you think it is inappropriate, you can of course flag it. – Kazark Feb 21 '13 at 4:31
up vote 3 down vote accepted

Ebuilds are shell scripts (bash scripts in fact). So they work like shell scripts, have variables and define functions. But ebuilds aren't run directly - you run them via emerge (or ebuild sometimes directly) that drive the preparation, build and install process by calling the appropriate functions from the ebuild in the required sequence.

Q1: What it means to ebuild to be sourced?
Q2: variables - how they can be interpreted?
Q5: scopes

Same as for shell scripts, using the source builtin command (see man bash for this). It means the script (the ebuild in this case) is read by the current shell (driven by emerge) and processed as if it was part of the calling script.
This processes all the variable definitions, adding them to the calling script's environment, and parses all the functions defined in the ebuild - but does not run these functions.
Variables can be very simple, e.g.:


which doesn't need interpreting, or can contain references to other variables, e.g.:


which needs variable interpolation, or could use even more complex definitions that require running bash builtin commands or external programs to be interpreted. This is nothing specific to ebuilds, but ordinary scripting.

There is nothing specific about scope in ebuilds, it is the same thing as in ordinary scripts.

Q4: stages
Q3&6: src_compile, pkg_setup, ${DEPEND}

The process of installing a package from source is decomposed into multiple steps, roughly preparation (download, unpack and configure sources), compilation, then installation and post-installation tasks. Ebuild maintainers can provide functions to be executed at each different stage to customize the build. You can find the list of stages in the EAPI Usage and Description of the dev. manual.
Predefined and ebuild variables are described in the Variables section of that same manual. ${DEPEND} is one of them.

src_compile is one of the functions that an ebuild must provide (directly or indirectly) if something needs to be done to compile the sources before installing the package. Not all ebuilds need this (e.g. could install only icons/themes/images that don't need compiling), but they usually do. It's the job of the ebuild maintainer to create the function so that it builds the source package correctly.
pkg_setup is one of the functions that will be called at an earlier stage in the installation.

share|improve this answer
brilliant, thorough and cognitively detailed – Xsi Nov 19 '12 at 17:48
Thx again. There's a good piece of knowledge in your answer. – Xsi Sep 29 '13 at 10:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.