ABS PKGBUILD Explained
From ArchWiki
| i18n |
|---|
| English |
| Dansk |
| 简体中文 |
| 正體中文 |
With ABS, an Arch user can easily compile sources into a .pkg.tar.gz package.
Contents |
What is a package file?
ABS automatically downloads the source code for the particular software you are compiling. It then unpacks, compiles the sources and squeezes everything into an installable package. Typically, the resulting package file is a file called foo.pkg.tar.gz.
In fact, it is no more than a gzipped tar archive or 'tarball' which contains:
- The files to install
- .PKGINFO: contains all the metadata needed by pacman to deal with packages, dependencies, etc.
- .FILELIST: lists all the files of the archive. It's used in order to uninstall the software or to check for file conflicts.
- .INSTALL: a file used to execute commands after the install/upgrade/remove stage. (This file is present only if specified in the PKGBUILD.)
Since pacman manages tar.gz packages, foo.pkg.tar.gz can now easily be installed/removed.
What is a PKGBUILD and what does it contain?
As explained before, the PKGBUILD file contains metadata about a package. It is a plain text file. The following is a prototype PKGBUILD. It can be found in /usr/share/pacman along with other templates.
# This is an example PKGBUILD file. Use this as a start to creating your own,
# and remove these comments. For more information, see 'man PKGBUILD'.
# NOTE: Please fill out the license field for your package! If it is unknown,
# then please put 'unknown'.
# Contributor: Your Name <youremail@domain.com>
pkgname=NAME
pkgver=VERSION
pkgrel=1
pkgdesc=""
arch=()
url=""
license=('GPL')
groups=()
depends=()
makedepends=()
provides=()
conflicts=()
replaces=()
backup=()
install=
source=($pkgname-$pkgver.tar.gz)
noextract=()
md5sums=() #generate with 'makepkg -g'
build() {
cd "$srcdir/$pkgname-$pkgver"
./configure --prefix=/usr
make || return 1
make DESTDIR="$pkgdir/" install
}
So let's explain each field:
- # text: comments
- # $Id: PKGBUILD...: the cvs-tag for this pkg (from the archlinux-cvs system created)
- # Maintainer: the maintainer responsible for official repository packages (not for unsupported AUR packages)
- # Contributor: anyone who has contributed, past or present, to the PKGBUILD for this package (multiple 'Contributor' lines can and should be used in cases where a PKGBUILD has past contributors)
- pkgname: the name of the package
- pkgver: the version of the software as released from the author
- pkgrel: the release number of the Arch Linux release. It is different from the version of the package and is changed when the PKGBUILD is modified. This can happen for many reasons, for example if you enable compile-time support for something. When a new version of the package comes out, the release number resets to 1.
- pkgdesc: a brief description of the package. This is what you see when you browse the package database
- arch: shows on what architectures it is known to build and work - see Arch64_FAQ for porting details
- url: the homepage of the software (which appears when you click on a package on the package database)
- license: the license(s) under which the software is distributed
- groups: this is used to group packages; when you try to install kde, for example, it installs all packages that belong to the kde group
- depends: this lists the run-time dependencies of the package (what it needs to work)
- makedepends: dependencies needed to build the package but which are not needed once the package is built
- provides: this is used if the package provides another package, for example, kernel-scsi provides kernel
- conflicts: these packages cannot be installed at the same time. Here, foo conflicts with yafoo (yet another foo). They cannot be installed at the same time.
- replaces: the new package replaces the old one. Here, mffoo (my first foo) is no longer supported and is being replaced with foo
- backup: which files to back up (as file.pacsave) when the package is removed or upgraded (if the file has changed) : files should be relative paths (e.g. etc/pacman.conf) not absolute paths (e.g. /etc/pacman.conf)
- install: specifies a special install script that is to be included in the package (it has to be in the same directory as PKGBUILD)
- source: this specifies from where to download the package's source code. It can be a local package as well as a "http" of "ftp" one. It is named using pkgver in order to avoid changing the source each time the version changes.
- noextract: files listed here will not be extracted with the rest of the source files
- md5sums: md5sums of the source to check their integrity
So now, let's explain the function:
- build: all the actions needed to build the package (it will be explained in more detail later in this document)
Well, you see that the PKGBUILD file contains all the information that might be needed by the package manager. It is the heart of pacman and abs.
There are also install files. This PKGBUILD specifies 'foo.install' as the package's install file. Here is an example install file:
post_install() {
/bin/true
}
post_upgrade() {
/bin/true
}
pre_remove() {
/bin/true
}
Here are the function explanations:
- post_install: this script is run right after files are installed; it takes one argument:
- the package version
- post_upgrade: this script is run after all files have been upgraded; it takes two arguments:
- the new package version
- the old package version
- pre_remove: this script is run right before files are removed (stop a daemon, for example) and takes one argument:
- the package version
Build function explained
So let's take a look at an ABS build function as dictated by the PKGBUILD example from above. Note the build section:
build() {
cd "$srcdir/$pkgname-$pkgver"
./configure --prefix=/usr
make || return 1
make DESTDIR="$pkgdir/" install
}
What is happening:
- enter the directory where sources were uncompressed:
cd "$srcdir/$pkgname-$pkgver"
- configure the package, and tell it to install in the
/usrdirectory:
./configure --prefix=/usr
- compile
make || return 1
- install the software not in
/usr, but rather, in our fake root environment under$pkgdir/:
make DESTDIR="$pkgdir/" install
Note that what we want to do is to build the package, not install it to our true root (/) filesystem just yet. So, instead of installing to the standard place under root, (/usr), we tell make to put all files under $pkgdir/usr/, inside the fake root directory: $pkgdir/. (By default, the fakeroot program is invoked by makepkg, to make all files owned by root during this stage). A fake root is merely a subdirectory tree that will function as, or mimic the behavior of, the true root directory. Thus, makepkg can look and see which files the package installs, and then compress them into the .pkg.tar.gz package. The package, therefore, simply contains a compressed archive of a fake root filesystem, all files associated with the desired software, and metadata. Installing is easily achieved by pacman, which, when invoked, will then untar the package to the real root (/) filesystem, while tracking all associated files specified in the metadata. Pacman will now be able to manipulate the package cleanly; this is the advantage of ABS, over a typical ports-like system.
NOTE: It is sometimes the case where DESTDIR is not used in the Makefile; you may need to use prefix instead. If the package is built with autoconf/automake, use DESTDIR; this is what is documented in the manuals. If DESTDIR does not work, try building with make prefix="$pkgdir/usr/" install. If that does not work, you'll have to look further into the install commands that are executed by "make <...> install".