\section{Defined Variables}
\label{sec:ebuild-env-vars}

The package manager must define the following environment variables. Not all variables are
meaningful in all phases; variables that are not meaningful in a given phase may be unset or set to
any value. Ebuilds must not attempt to modify any of these variables, unless otherwise specified.

Because of their special meanings, these variables may not be preserved consistently across all
phases as would normally happen due to environment saving (see~\ref{sec:ebuild-env-state}). For example,
\t{EBUILD\_PHASE} is different for every phase, and \t{ROOT} may have changed between the various
different \t{pkg\_*} phases. Ebuilds must recalculate any variable they derive from an inconsistent
variable.

\begin{landscape}
\reversemarginpar
\addtolength{\marginparsep}{-25mm}
% Workaround for broken marginnote positioning in lscape environment
\addtolength{\marginparsep}{-\textwidth} % FIXME
\setlength{\LTleft}{25mm plus 1fil}
\setlength{\LTright}{0pt plus 1fil}
\begin{longtable}{!{\extracolsep{\fill}} l P{7.5em} l p{0.5\linewidth}}
\caption{Defined variables\label{tab:defined_vars}}\\
\toprule
\multicolumn{1}{c}{\b{Variable}} &
\multicolumn{1}{c}{\b{Legal in}} &
\multicolumn{1}{c}{\b{Consistent?}} &
\multicolumn{1}{c}{\b{Description}} \\
\midrule
\endfirsthead
\midrule
\multicolumn{1}{c}{\b{Variable}} &
\multicolumn{1}{c}{\b{Legal in}} &
\multicolumn{1}{c}{\b{Consistent?}} &
\multicolumn{1}{c}{\b{Description}} \\
\midrule
\endhead
\midrule
\endfoot
\bottomrule
\endlastfoot
\t{P} &
    All &
    No\footnote{May change if a package has been updated (see~\ref{sec:updates-dir})} &
    Package name and version, without the revision part. For example, \t{vim-7.0.174}. \\
\t{PN} &
    All &
    Ditto &
    Package name, for example \t{vim}. \\
\t{CATEGORY} &
    All &
    Ditto &
    The package's category, for example \t{app-editors}. \\
\t{PV} &
    All &
    Yes &
    Package version, with no revision. For example \t{7.0.174}. \\
\t{PR} &
    All &
    Yes &
    Package revision, or \t{r0} if none exists. \\
\t{PVR} &
    All &
    Yes &
    Package version and revision (if any), for example \t{7.0.174} or \t{7.0.174-r1}. \\
\t{PF} &
    All &
    Yes &
    Package name, version, and revision (if any), for example \t{vim-7.0.174-r1}. \\
\t{A} &
    \t{src\_*} &
    Yes &
    All source files available for the package, whitespace separated with no leading or trailing
    whitespace, and in the order in which the item first appears in a matched component of
    \t{SRC\_URI}\@. Does not include any that are disabled because of USE conditionals. The value is
    calculated from the base names of each element of the \t{SRC\_URI} ebuild metadata variable. \\
\t{AA}\footnote{This variable is generally considered deprecated. However, ebuilds must still
    assume that the package manager sets it in the EAPIs supporting it. For example, a few
    configure scripts use this variable to find the \t{aalib} package; ebuilds calling such
    configure scripts must thus work around this.} &
    \t{src\_*} &
    Yes &
    \featurelabel{aa} All source files that could be available for the package, including any that
    are disabled in \t{A} because of USE conditionals. The value is calculated from the base names
    of each element of the \t{SRC\_URI} ebuild metadata variable. Only for EAPIs listed in
    table~\ref{tab:removed-env-vars-table} as supporting \t{AA}. \\
\t{FILESDIR} &
    \t{src\_*}\footnote{Not necessarily present when installing from a binary package} &
    No &
    The full path to the package's files directory, used for small support files or
    patches. See section~\ref{sec:package-dirs}. May or may not exist; if a repository provides no
    support files for the package in question then an ebuild must be prepared for the situation
    where \t{FILESDIR} points to a non-existent directory. \\
\t{PORTDIR} &
    Ditto &
    No &
    The full path to the master repository's base directory. \\
\t{DISTDIR} &
    Ditto &
    No &
    The full path to the directory in which the files in the \t{A} variable are stored. \\
\t{ECLASSDIR} &
    Ditto &
    No &
    The full path to the master repository's eclass directory. \\
\t{ROOT} &
   \t{pkg\_*} &
   No &
   The absolute path to the root directory into which the package is to be merged.  Phases which run
   with full filesystem access must not touch any files outside of the directory given in
   \t{ROOT}\@. Also of note is that in a cross-compiling environment, binaries inside of \t{ROOT}
   will not be executable on the build machine, so ebuilds must not call them. \t{ROOT} must be
   non-empty and end in a trailing slash. \\
\t{EROOT} &
    \t{pkg\_*} &
    No &
    Contains the path \t{\$\{ROOT\%/\}\$\{EPREFIX\}/} for convenience. See also the
    \t{EPREFIX} variable. Only for EAPIs listed in table~\ref{tab:offset-env-vars-table} as
    supporting \t{EROOT}. \\
\t{T} &
    All &
    Partially\footnote{Consistent and preserved across a single connected sequence of install or
    uninstall phases, but not between install and uninstall. When reinstalling a package, this
    variable must have different values for the install and the replacement.} &
    The full path to a temporary directory for use by the ebuild. \\
\t{TMPDIR} &
    All &
    Ditto &
    Must be set to the location of a usable temporary directory, for any applications
    called by an ebuild. Must not be used by ebuilds directly; see \t{T} above. \\
\t{HOME} &
    All &
    Ditto &
    The full path to an appropriate temporary directory for use by any programs invoked by the
    ebuild that may read or modify the home directory. \\
\t{EPREFIX} &
    All &
    Yes &
    The normalised offset-prefix path of an offset installation.  When \t{EPREFIX} is not set in the
    calling environment, \t{EPREFIX} defaults to the built-in offset-prefix that was set during
    installation of the package manager. When a different \t{EPREFIX} value than the built-in value
    is set in the calling environment, a cross-prefix build is performed where using the existing
    utilities, a package is built for the given \t{EPREFIX}, akin to \t{ROOT}\@.
    See also~\ref{sec:offset-vars}. Only for EAPIs listed in table~\ref{tab:offset-env-vars-table}
    as supporting \t{EPREFIX}. \\
\t{D} &
    \t{src\_install} &
    No &
    Contains the full path to the image directory into which the package should be installed.
    Must be non-empty and end in a trailing slash. \\
\t{D} (continued) &
    \t{pkg\_preinst}, \t{pkg\_postinst} &
    Yes &
    Contains the full path to the image that is about to be or has just been merged. Must be
    non-empty and end in a trailing slash. \\
\t{ED} &
    \t{src\_install}, \t{pkg\_preinst}, \t{pkg\_postinst} &
    See \t{D} &
    Contains the path \t{\$\{D\%/\}\$\{EPREFIX\}/} for convenience. See also the
    \t{EPREFIX} variable. Only for EAPIs listed in table~\ref{tab:offset-env-vars-table} as
    supporting \t{ED}. \\
\t{DESTTREE} &
    \t{src\_install} &
    No &
    Controls the location where \t{dobin}, \t{dolib}, \t{domo}, and \t{dosbin} install things. \\
\t{INSDESTTREE} &
    \t{src\_install} &
    No &
    Controls the location where \t{doins} installs things. \\
\t{USE} &
    All &
    Yes &
    A whitespace-delimited list of all active USE flags for this ebuild. See
    section~\ref{sec:use-iuse-handling} for details. \\
\t{EBUILD\_PHASE} &
    All &
    No &
    Takes one of the values \t{config}, \t{setup}, \t{nofetch}, \t{unpack}, \t{prepare},
    \t{configure}, \t{compile}, \t{test}, \t{install}, \t{preinst}, \t{postinst}, \t{prerm},
    \t{postrm}, \t{info}, \t{pretend} according to the top level ebuild function that was executed
    by the package manager. May be unset or any single word that is not any of the above when the
    ebuild is being sourced for other (e.\,g.\ metadata or QA) purposes. \\
\t{EBUILD\_PHASE\_FUNC} &
    All &
    No &
    \featurelabel{ebuild-phase-func} Takes one of the values \t{pkg\_config}, \t{pkg\_setup},
    \t{pkg\_nofetch}, \t{src\_unpack}, \t{src\_prepare}, \t{src\_configure}, \t{src\_compile},
    \t{src\_test}, \t{src\_install}, \t{pkg\_preinst}, \t{pkg\_postinst}, \t{pkg\_prerm},
    \t{pkg\_postrm}, \t{pkg\_info}, \t{pkg\_pretend} according to the top level ebuild function that
    was executed by the package manager. May be unset or any single word that is not any of the
    above when the ebuild is being sourced for other (e.\,g.\ metadata or QA) purposes. Only for
    EAPIs listed in table~\ref{tab:added-env-vars-table} as supporting \t{EBUILD\_PHASE\_FUNC}. \\
\t{WORKDIR} &
    \t{src\_*}, global~scope &
    Yes &
    The full path to the ebuild's working directory, in which all build data should be
    contained. \label{env-var-WORKDIR} \\
\t{S} &
    \t{src\_*} &
    Yes &
    The full path to the temporary build directory, used by \t{src\_compile}, \t{src\_install} etc.
    Defaults to \t{\$\{WORKDIR\}/\$\{P\}}. May be modified by ebuilds. If \t{S} is assigned in the
    global scope of an ebuild, then the restrictions of section~\ref{sec:ebuild-env-state} for
    global variables apply. \\
\t{KV} &
    All &
    Yes &
    \featurelabel{kv} The version of the running kernel at the time the ebuild was first executed,
    as returned by the \t{uname~-r} command or equivalent.  May be modified by ebuilds.  Only for
    EAPIs listed in table~\ref{tab:removed-env-vars-table} as supporting \t{KV}. \\
\t{MERGE\_TYPE} &
    \t{pkg\_*} &
    No &
    \featurelabel{merge-type} The type of package that is being merged. Possible values are:
    \t{source} if building and installing a package from source, \t{binary} if installing a binary
    package, and \t{buildonly} if building a binary package without installing it. Only for EAPIs
    listed in table~\ref{tab:added-env-vars-table} as supporting \t{MERGE\_TYPE}. \\
\t{REPLACING\_VERSIONS} &
    \t{pkg\_*} (see text) &
    Yes &
    A whitespace-separated list of versions of this package (including revision, if specified) that
    are being replaced (uninstalled or overwritten) as a result of this install. See
    section~\ref{sec:replacing-versions}.  Only for EAPIs listed in table~\ref{tab:added-env-vars-table}
    as supporting \t{REPLACING\_VERSIONS}. \\
\t{REPLACED\_BY\_VERSION} &
    \t{pkg\_prerm}, \t{pkg\_postrm} &
    Yes &
    The single version of this package (including revision, if specified) that is replacing us, if
    we are being uninstalled as part of an install, or an empty string otherwise. See
    section~\ref{sec:replacing-versions}.  Only for EAPIs listed in table~\ref{tab:added-env-vars-table}
    as supporting \t{REPLACED\_BY\_VERSION}.
\end{longtable}
\end{landscape}

\ChangeWhenAddingAnEAPI{6}
\begin{centertable}{EAPIs supporting various added env variables}
    \label{tab:added-env-vars-table}
    \begin{tabular}{lllll}
      \toprule
      \multicolumn{1}{c}{\textbf{EAPI}} &
      \multicolumn{1}{c}{\textbf{\t{MERGE\_TYPE}?}} &
      \multicolumn{1}{c}{\textbf{\t{REPLACING\_VERSIONS}?}} &
      \multicolumn{1}{c}{\textbf{\t{REPLACED\_BY\_VERSION}?}} &
      \multicolumn{1}{c}{\textbf{\t{EBUILD\_PHASE\_FUNC}?}} \\
      \midrule
      0, 1, 2, 3        & No  & No  & No  & No  \\
      4                 & Yes & Yes & Yes & No  \\
      5, 6              & Yes & Yes & Yes & Yes \\
      \bottomrule
    \end{tabular}
\end{centertable}

\ChangeWhenAddingAnEAPI{6}
\begin{centertable}{EAPIs supporting various removed env variables}
    \label{tab:removed-env-vars-table}
    \begin{tabular}{lll}
      \toprule
      \multicolumn{1}{c}{\textbf{EAPI}} &
      \multicolumn{1}{c}{\textbf{\t{AA}?}} &
      \multicolumn{1}{c}{\textbf{\t{KV}?}} \\
      \midrule
      0, 1, 2, 3        & Yes & Yes \\
      4, 5, 6           & No  & No  \\
      \bottomrule
    \end{tabular}
\end{centertable}

\ChangeWhenAddingAnEAPI{6}
\begin{centertable}{EAPIs supporting offset-prefix env variables}
    \label{tab:offset-env-vars-table}
    \begin{tabular}{llll}
      \toprule
      \multicolumn{1}{c}{\textbf{EAPI}} &
      \multicolumn{1}{c}{\textbf{\t{EPREFIX}?}} &
      \multicolumn{1}{c}{\textbf{\t{EROOT}?}} &
      \multicolumn{1}{c}{\textbf{\t{ED}?}} \\
      \midrule
      0, 1, 2           & No  & No  & No  \\
      3, 4, 5, 6        & Yes & Yes & Yes \\
      \bottomrule
    \end{tabular}
\end{centertable}

Except where otherwise noted, all variables set in the active profiles' \t{make.defaults} files must
be exported to the ebuild environment. \t{CHOST}, \t{CBUILD} and \t{CTARGET}, if not set by
profiles, must contain either an appropriate machine tuple (the definition of appropriate is beyond
the scope of this specification) or be unset.

\t{PATH} must be initialized by the package manager to a ``usable'' default.  The exact value here
is left up to interpretation, but it should include the equivalent ``sbin'' and ``bin'' and any
package manager specific directories.

\t{GZIP}, \t{BZIP}, \t{BZIP2}, \t{CDPATH}, \t{GREP\_OPTIONS}, \t{GREP\_COLOR} and \t{GLOBIGNORE}
must not be set.

\featurelabel{locale-settings} The package manager must ensure that the \t{LC\_CTYPE} and
\t{LC\_COLLATE} locale categories are equivalent to the POSIX locale, as far as characters in the
ASCII range (U+0000 to U+007F) are concerned. Only for EAPIs listed in such a manner in
table~\ref{tab:locale-settings}.

\ChangeWhenAddingAnEAPI{6}
\begin{centertable}{Locale settings for EAPIs}
    \label{tab:locale-settings}
    \begin{tabular}{ll}
      \toprule
      \multicolumn{1}{c}{\textbf{EAPI}} &
      \multicolumn{1}{c}{\textbf{Sane \t{LC\_CTYPE} and \t{LC\_COLLATE}?}} \\
      \midrule
      0, 1, 2, 3, 4, 5  & Undefined \\
      6                 & Yes       \\
      \bottomrule
    \end{tabular}
\end{centertable}

\subsection{USE and IUSE Handling}
\label{sec:use-iuse-handling}

This section discusses the handling of four variables:

\begin{description}
\item[IUSE] is the variable calculated from the \t{IUSE} values defined in ebuilds and eclasses.
\item[IUSE\_REFERENCEABLE] is a variable calculated from \t{IUSE} and a variety of other sources
    described below. It is purely a conceptual variable; it is not exported to the ebuild
    environment. Values in \t{IUSE\_REFERENCEABLE} may legally be used in queries from other
    packages about an ebuild's state (for example, for use dependencies).
\item[IUSE\_EFFECTIVE] is another conceptual, unexported variable. Values in \t{IUSE\_EFFECTIVE} are
    those which an ebuild may legally use in queries about itself (for example, for the \t{use}
    function, and for use in dependency specification conditional blocks).
\item[USE] is a variable calculated by the package manager and exported to the ebuild environment.
\end{description}

In all cases, the values of \t{IUSE\_REFERENCEABLE} and \t{IUSE\_EFFECTIVE} are undefined during
metadata generation.

For EAPIs listed in table~\ref{tab:profile-iuse-injection-table} as not supporting profile defined
\t{IUSE} injection, \t{IUSE\_REFERENCEABLE} is equal to the calculated \t{IUSE} value. For EAPIs
where profile defined \t{IUSE} injection is supported, \t{IUSE\_REFERENCEABLE} is equal to
\t{IUSE\_EFFECTIVE}.

For EAPIs listed in table~\ref{tab:profile-iuse-injection-table} as not supporting profile defined
\t{IUSE} injection, \t{IUSE\_EFFECTIVE} contains the following values:

\begin{compactitem}
\item All values in the calculated \t{IUSE} value.
\item All possible values for the \t{ARCH} variable.
\item All legal use flag names whose name starts with the lowercase equivalent of any value in
    the profile \t{USE\_EXPAND} variable followed by an underscore.
\end{compactitem}

\featurelabel{profile-iuse-inject} For EAPIs listed in table~\ref{tab:profile-iuse-injection-table}
as supporting profile defined \t{IUSE} injection, \t{IUSE\_EFFECTIVE} contains the following values:

\begin{compactitem}
\item All values in the calculated \t{IUSE} value.
\item All values in the profile \t{IUSE\_IMPLICIT} variable.
\item All values in the profile variable named \t{USE\_EXPAND\_VALUES\_\$\{v\}}, where \t{\$\{v\}}
    is any value in the intersection of the profile \t{USE\_EXPAND\_UNPREFIXED} and
    \t{USE\_EXPAND\_IMPLICIT} variables.
\item All values for \t{\$\{lower\_v\}\_\$\{x\}}, where \t{\$\{x\}} is all values in the profile
    variable named \t{USE\_EXPAND\_VALUES\_\$\{v\}}, where \t{\$\{v\}} is any value in the
    intersection of the profile \t{USE\_EXPAND} and \t{USE\_EXPAND\_IMPLICIT} variables and
    \t{\$\{lower\_v\}} is the lowercase equivalent of \t{\$\{v\}}.
\end{compactitem}

The \t{USE} variable is set by the package manager. For each value in \t{IUSE\_EFFECTIVE}, \t{USE}
shall contain that value if the flag is to be enabled for the ebuild in question, and shall not
contain that value if it is to be disabled. In EAPIs listed in
table~\ref{tab:profile-iuse-injection-table} as not supporting profile defined \t{IUSE} injection,
\t{USE} may contain other flag names that are not relevant for the ebuild.

For EAPIs listed in table~\ref{tab:profile-iuse-injection-table} as supporting profile defined
\t{IUSE} injection, the variables named in \t{USE\_EXPAND} and \t{USE\_EXPAND\_UNPREFIXED} shall
have their profile-provided values reduced to contain only those values that are present in
\t{IUSE\_EFFECTIVE}.

For EAPIs listed in table~\ref{tab:profile-iuse-injection-table} as supporting profile defined
\t{IUSE} injection, the package manager must save the calculated value of \t{IUSE\_EFFECTIVE} when
installing a package. Details are beyond the scope of this specification.

\subsection{\t{REPLACING\_VERSIONS} and \t{REPLACED\_BY\_VERSION}}
\label{sec:replacing-versions}

\featurelabel{replace-version-vars} In EAPIs listed in table~\ref{tab:added-env-vars-table} as
supporting it, the \t{REPLACING\_VERSIONS} variable shall be defined in \t{pkg\_preinst} and
\t{pkg\_postinst}.  In addition, it \e{may} be defined in \t{pkg\_pretend} and \t{pkg\_setup},
although ebuild authors should take care to handle binary package creation and installation
correctly when using it in these phases.

\t{REPLACING\_VERSIONS} is a list, not a single optional value, to handle pathological cases such as
installing \t{foo-2:2} to replace \t{foo-2:1} and \t{foo-3:2}.

In EAPIs listed in table~\ref{tab:added-env-vars-table} as supporting it, the
\t{REPLACED\_BY\_VERSION} variable shall be defined in \t{pkg\_prerm} and \t{pkg\_postrm}. It shall
contain at most one value.

\subsection{Offset-prefix variables \t{EPREFIX}, \t{EROOT} and \t{ED}}
\label{sec:offset-vars}

\ChangeWhenAddingAnEAPI{6}
\begin{centertable}{EAPIs supporting offset-prefix}
    \label{tab:offset-support-table}
    \begin{tabular}{ll}
      \toprule
      \multicolumn{1}{c}{\textbf{EAPI}} &
      \multicolumn{1}{c}{\textbf{Supports offset-prefix?}}\\
      \midrule
      0, 1, 2           & No  \\
      3, 4, 5, 6        & Yes \\
      \bottomrule
    \end{tabular}
\end{centertable}

\featurelabel{offset-prefix-vars} Table~\ref{tab:offset-support-table} lists the EAPIs which
support offset-prefix installations. This support was initially added in EAPI 3, in the form of
three extra variables. Two of these, \t{EROOT} and \t{ED}, are convenience variables using the
variable \t{EPREFIX}\@. In EAPIs that do not support an offset-prefix, the installation offset is
hardwired to \t{/usr}. In offset-prefix supporting EAPIs the installation offset is set as
\t{\$\{EPREFIX\}/usr} and hence can be adjusted using the variable \t{EPREFIX}\@. Note that the
behaviour of offset-prefix aware and agnostic is the same when \t{EPREFIX} is set to the empty
string in offset-prefix aware EAPIs. The latter do have the variables \t{ED} and \t{EROOT} properly
set, though.

% vim: set filetype=tex fileencoding=utf8 et tw=100 spell spelllang=en :

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "pms"
%%% LaTeX-indent-level: 4
%%% LaTeX-item-indent: 0
%%% TeX-brace-indent-level: 4
%%% fill-column: 100
%%% End:
