✦ For everyone, free.

Practical knowledge for real and everyday life

Home

Portage Build and Merge Lifecycle

Portage's build and merge lifecycle manages package updates efficiently, ensuring system stability through automated dependency resolution and integration.

Portage Build and Merge Lifecycle defines the structured sequence of steps that the Gentoo Portage system follows to build, configure, and install software packages from source code. This lifecycle ensures that packages are properly prepared, compiled, merged into the system, and cleaned up, maintaining system integrity, dependency resolution, and customization through USE flags and environment variables.


Overview of the Lifecycle

The lifecycle begins when a package is selected for installation or upgrade and proceeds through several well-defined stages:

  • Preparation: Setting up the environment, unpacking source code, and applying patches.
  • Configuration: Adjusting build options based on system profile, USE flags, and package-specific settings.
  • Compilation: Building the software from source.
  • Testing (optional): Running any provided test suits.
  • Installation: Merging the built software into the system.
  • Post-installation: Running any necessary post-install commands such as updating caches.
  • Cleanup: Removing temporary files and preparing for the next task.

Each stage is controlled by specific ebuild functions and Portage infrastructure, allowing flexibility and customization.


Detailed Stages

1. Fetching and Unpacking

Before building, Portage downloads the source files from specified mirrors or local caches. It verifies checksums to ensure integrity. Once downloaded, the source archive is unpacked into a temporary build directory, typically located under /var/tmp/portage/category/package-version/.

Relevant ebuild functions involved include:

  • src_unpack(): Unpacks the source archive.
  • src_prepare(): Applies patches and prepares the source for configuration.

2. Preparation and Patching

After unpacking, Portage applies patches and prepares the build environment. This involves:

  • Applying patches provided by the ebuild or overlays.
  • Setting up environment variables such as CFLAGS, LDFLAGS, USE flags, and others.
  • Running src_prepare() function where ebuild authors can customize patch application and preparation.

This step ensures that the source code is ready for configuration and compilation with the correct options.


3. Configuration

Configuration tailors the build process to the system and user preferences. It typically involves:

  • Executing the src_configure() function, which usually calls the package’s ./configure script.
  • Passing USE flags, environment variables, and other parameters to control features and optimizations.
  • Handling cross-compilation or special build environments.

Configuration generates Makefiles or build scripts adapted to the target environment.


4. Compilation

The actual compilation step builds the software binaries:

  • Controlled by src_compile(), which typically runs make or equivalent build tools.
  • Uses environment variables and options set during the configuration step.
  • May include parallel compilation controlled by Portage’s MAKEOPTS variable.

This step translates source code into executable binaries and libraries.


5. Testing (Optional)

If enabled, the build may include a testing phase:

  • Run by the src_test() function.
  • Executes test suites provided by the package to verify build integrity.
  • Test failures may halt the merge process depending on configuration.

Testing is optional and may be skipped for performance or reliability reasons.


6. Installation and Merging

Once compiled and tested, the software is installed into a temporary Image directory:

  • src_install() copies files into ${D}, the installation root directory.
  • Portage then merges the content from ${D} into the live filesystem, under /usr, /etc, and other locations.
  • This step ensures that all installed files conform to Gentoo’s filesystem standards and dependency tracking.

The merge process updates the installed package database and triggers any necessary system updates.


7. Post-Installation

After merging, Portage performs post-installation tasks such as:

  • Running pkg_postinst() ebuild function for additional setup.
  • Updating system caches (e.g., font cache, icon cache).
  • Running ldconfig to refresh dynamic linker cache.
  • Registering the package in the Portage database for tracking.

This guarantees the system is aware of the new package and its resources.


8. Cleanup

Finally, Portage cleans up the build environment:

  • Removing temporary files and build directories unless FEATURES like buildpkg or keepwork are enabled.
  • Saving build logs for troubleshooting.
  • Preparing the system for subsequent package operations.

This stage maintains system hygiene and frees disk space.


Supporting Mechanisms and Features

USE Flags and Environment

USE flags influence conditional compilation and configuration, allowing fine-grained control over package features. They affect:

  • Which dependencies are pulled in.
  • Configuration options passed during src_configure().
  • Code paths enabled or disabled during compilation.

Environment variables such as CFLAGS, LDFLAGS, and MAKEOPTS tailor the build performance and optimization.


Dependency Resolution and Order

Portage resolves dependencies before build starts, ensuring all required libraries and tools are available. It respects:

  • Build-time dependencies (DEPEND).
  • Run-time dependencies (RDEPEND).
  • Post-build dependencies (PDEPEND).

This guarantees that packages are built and merged in a correct order.


Ebuild Functions and Hooks

The lifecycle is implemented via standard ebuild functions, which may be overridden or extended:

FunctionPurpose
src_unpackUnpack source archives
src_prepareApply patches and prepare source
src_configureConfigure the build
src_compileCompile the source
src_testRun package test suites
src_installInstall compiled files into image
pkg_postinstPost-installation tasks

These functions provide modular control over each phase.


Build Environment Isolation

Portage builds occur in isolated temporary directories to avoid contaminating the live system. This isolation:

  • Prevents partial or broken installs.
  • Allows multi-package parallel builds.
  • Facilitates rollbacks and package maintenance.

Logging and Error Handling

Each stage generates detailed logs stored under /var/log/portage/ or /var/tmp/portage/. If errors occur:

  • Portage aborts the build or merge.
  • Logs provide diagnostic information.
  • Users can intervene or retry with adjusted options.

Summary Table of the Lifecycle Phases

PhaseFunction(s)Description
Fetching-Download and verify source archives
Unpackingsrc_unpackExtract source code
Preparationsrc_prepareApply patches, set environment
Configurationsrc_configureGenerate build scripts with options
Compilationsrc_compileBuild source into binaries
Testingsrc_testRun test suites (optional)
Installationsrc_installCopy files into image for merging
MergingInternal PortageMove files into live filesystem
Post-Installpkg_postinstSystem updates and cache refresh
CleanupInternal PortageRemove temporary files

This comprehensive lifecycle enables Gentoo’s Portage package manager to efficiently and reliably build and install software customized to user preferences and system requirements.