[HowTo] Read a PKGBUILD

Difficulty: ★☆☆☆☆

What is a PKGBUILD ?

A PKGBUILD is a file containing information used for creating a package:

  • technical information: package name and version, target architecture, dependencies…
  • global description: content description, licence…
  • packaging information: source(s) (with checksums), build process, packaging process, installation process…

What is it used for?

A PKGBUILD serves as a description entry for makepkg, a utility used by Arch-based distributions – including Manjaro – for making packages.

How can i read a PKGBUILD ?

The content of a PKGBUILD can be read by any text editor. It usually contains two parts of information: variables and functions.

Variables

The variables in a PKGBUILD are used for defining fixed or patterned values. Depending on the variable defined, they use any of the following patterns:

<name>=<value>
<name>=(<value> <value> ...)

The following variables are commonly used:

  • arch : target system architectures (x86_64, aarch64 …)
    • any indicates that the content can be used on any architecture
  • license : license of the package content
  • pkgdesc : description of the package content
  • pkgname : name of the package
  • pkgrel : release number of the package
    • For a same package version, a change in the release number usually indicates a change in the packaging process rather than in the package content
  • pkgver : version of the package
  • source : source of the package content
    • The source here can be either source code, a compiled binary, or a package in another format (DEB, RPM …)
  • <algorithm>sums : source checksum, using the prefixed algorithm
  • url : project URL

The following variables may be used when necessary:

  • conflicts : list of packages conflicting with this one
  • depends : list of dependencies
  • makedepends : list of dependencies required for building the package
  • optdepends : list of optional dependencies
  • provides : list of package references provided by this package
    • This is commonly used when a single library/software may be bundled in multiple package variations, thus requiring them to use distinct package names.
    • When omitted, the reference is the package name.

For more details, and the complete list of variables, see: PKGBUILD - ArchWiki

Variables used in other variables/functions

In many cases, a variable – either standard or custom – is used later in the PKGBUILD, with one of the following patterns:

$<name>
${<name>}

You can thus deduce the final value by replacing the used variable by its value.

For instance, the two following PKGBUILDs are equivalent:

pkgname=myCoolSoftware
pkgver=1.2.3
source=(https://mySources.com/$pkgname/$pkgver.zip)
pkgname=myCoolSoftware
pkgver=1.2.3
source=(https://mySources.com/myCoolSoftware/1.2.3.zip)

Functions

The functions in a PKGBUILD list (automaton) instructions for the various steps of the packaging process. They are recognizable from their pattern:

<name>() {
  <instruction>
  <instruction>
  ...
}

Below is the list of the most notable functions, executed in order. For the full list, see: Creating packages - ArchWiki

  • prepare() : prepare the source before building the package content from it
  • build() : build/compile the package content from the (prepared) source
  • package() : organize the content of the package
    • Consider the root of the package as the root the system on which the package will be extracted when installed.

Instructions

The instructions used in PKGBUILD functions are usually commands using applications/utilities, including the usual default kit available on any Linux system.
Below is a (very small) list of commonly used instructions. For details on how they can be used, you can check their respective manual: man <command>

  • cd : change directory for the following instructions
  • cp : copy a file/folder
  • install : copy a file/folder and apply the defined permissions
  • ln : create a (usually symbolic) link towards another file/folder
  • rm : remove file/folder
17 Likes