PKGBUILD
From Gnuffy
A PKGBUILD is small shell-script which describes the procedure necessary to build a package from source code, consisting of some variables describing the package-name, version, description, URL... and a function "build()" which is called by spaceman to build the package. If you ever used gentoo you are already familiar with the idea. PKGBUILDs are much like gentoo's ebuilds, but typically simpler.
PKGBUILDs are typically very small in size. Here's an example:
# pkg-maintainer: Nicolai Lissner <nlissne@linux01.gwdg.de>
pkgname=spaceman
pkgver=0.8.0.beta.2341
pkgrel=1
pkgdesc="secure package manager for gnuffy"
url="http://www.gnuffy.org"
license="GPL2"
depends=(awk bash gcc diffutils grep fakeroot wget coreutils tar gzip bzip2 'gnupg>=1.4.2.1' util-linux sed)
install=spaceman.install
source=(http://linux01.gwdg.de/~nlissne/spaceman-0.8.0.beta.2341.tar.bz2)
md5sums=(82f8d3e31058b3d9541b997f2e6b08e5)
sha1sums=(284a0af574470b737ebf168480a86e210bea49bd)
build() {
mkdir -p $startdir/pkg/usr/bin
install -m 755 $startdir/src/spaceman/spaceman $startdir/pkg/usr/bin/spaceman
mkdir -p $startdir/pkg/usr/{bin,man/man1,man/man5}
mkdir -p $startdir/pkg/etc/bash_completion.d/
install -m 755 $startdir/src/spaceman/spaceman $startdir/pkg/usr/bin/spaceman
install -m 644 $startdir/src/spaceman/spaceman.1 $startdir/pkg/usr/man/man1
install -m 644 $startdir/src/spaceman/pkglist.5 $startdir/pkg/usr/man/man5
install -m 644 $startdir/src/spaceman/spaceman.bash_completion $startdir/pkg/
}
Contents |
The variables in a PKGBUILD and their meaning
pkgname contains the name of the package. The name should be lowercase only and without spaces. Normally there is no version given in the name, however sometimes a version should install parallel to another version, and to prevent the package manager to remove the installed version the pkgname must be different and therefore could contain a version, as in "firefox3" which can be installed parallel to "firefox".
pkgver contains the version number of a package. It is alpha-numeric, and can contain dots, but it cannot contain dashes, because the filename of the finished package is $pkgname-$pkgver-$pkgrel-$architecture.pkg.tar.gz or $pkgname-$pkgver-$pkgrel.pkg.tar.bz2
pkgrel contains the release number of a package. Whenever a package is released the first time in a given source version, this number should start with one. Each time the same package-version is released again with some changes in the way it is builded, this number should increase by one, so the package-manager can realize the package is more recent, although the version number has not been changed. Please keep in mind that pkgrel is internal only, it's not a new version release of the author of the sourcecode, but a new version of a PKGBUILD.
pkgdesc contains a verbal description of the package, its purpose and sometimes the difference to another offered version of the same package, which might have been the reason to offer the package.
useflags is an array with a list of useflags a PKGBUILD supports.
url contains the URL of the homepage of the package, i.e. where you can get more information about it.
license contains the license-model the sourcecode (or binary) is released under
depends is an array which contains the names of packages needed to use the package. These dependencies will install automatically by the package-manager when the package is installed. As you can see in the example above, it is possible to depend on certain versions of package.
makedepends (not used in the example above) is an array which contains the names of packages, which are needed to build the package from sources, but not necessarily needed to run the binary.
optdepends is an array that can be used to make the user aware of packages that would add some functionality to a package, although they are not needed by the package (as it doesn't link to these packages). The format would be something similar to optdepends=('dvd+rw-tools: for dvd burning support','vcdimager: for vcd burning support','transcode: for advanced mpeg conversion support','emovix: for bootable multimedia cd/dvd support') (these are the optdepends of package k3b). Spaceman prints out a message about them on installation of the package.
provides (not used in the example above) can be an alternative name the package should be found as, typically used to make it possible to depend on a library, which might come in different implementations, i.e. libgl is provided by proprietary nvidia-drivers, so the PKGBUILD of these drivers contains provides=(libgl) and the package-manager can offer to install it, although the package-name differs from the wanted dependency.
install can contain the name of an installation script which make it possible to run specific commands before and/or after the installation, update or remove of a package.
source is an array which contains URLs to additional files needed to build the package. Typically the URL to the source-archive. It also can contain additional files not downloadable from internet (like patches, package specific init-script...) Such files would be part of the *.pkgbuild.tar.bz2 file and should be mentioned by name only (and located in the same directory as the PKGBUILD itself)
md5sums is an array which contains the md5sums of the files given in array source in the same order.
sha1sums since md5sum might be faked this array contains additionally the sha1sums of the files given in array source. It's probably possible to create a file with different content but same md5sum, but it is much harder to change the content of a file while not changing md5sum and sha1sum.
Both, md5sums and sha1sums are created automatically by spaceman when you create your own PKGBUILD.
build(){...} is a function which contains the commands to be executed to build the package. There is an additional variable $startdir used here, which is automatically filled by spaceman at build-time. The source-code will extract at $startdir/src/ and the install-procedure should install to $startdir/pkg. Any files and directories in $startdir/pkg will go to the *.pkg.tar.bz2 and are going to be installed when the package is installed. So, a file in $startdir/pkg/usr/bin would install to /usr/bin later.
Building a package is normally run with normal user privilegs (even when run as root), so build() cannot write to system files while building.
Creating a PKGBUILD
If you already have a PKGBUILD file or modified an existing one, you may proceed to the next paragraph "Building the package", elsewise, if you want to build a new package, first of all, you need a PKGBUILD. You can create a PKGBUILD from scratch using
spaceman --newpkgbuild http://someprogram.org/my-src-archive-1.5.tar.gz
This command will just create a simple PKGBUILD that is likely to need some editing before it will really work. You will need to fill out some meta information like the license and especially the build() function. Some information, such as the md5sums, the sha1sums and the dependencies of the package, is likely to be unknown to you. Don't worry about those now, you will see later. Alternatively,
spaceman --interactive http://someprogram.org/my-src-archive-1.5.tar.gz
will open a shell where you can manually configure and compile the package. You can work in this environment as you would normally do when compiling and installing a package by hand. All commands you type will be included in the build() function of the resulting PKGBUILD. Some certain commands, like `./configure --help` are cut out by spaceman. If you use your editor to make changes, make sure you have set the $EDITOR variable correctly. These changes will be included in build() as well. For example, if you use nano, you would run the following command before invoking spaceman --interactive:
EDITOR=nano
Please do neither give a full path to the editor binary nor add any parameters. Keep in mind that this variable is used by spaceman only to determine whether the command you execute is a call of your editor.
The resulting PKGBUILD will also need some editing as far as meta information is concerned.
Building the package
Now, that you have a PKGBUILD in the current working directory ./ that should work, you can try it out. Run
spaceman -b
without any other arguments. It will create the corresponding *.pkgbuild.tar.bz2, ask you to sign it with your key (and accordingly create the *pkgbuild.tar.bz2.sign), build the package and store the *.pkg.tar.bz2 in the current working directory.
As it comes to it, spaceman will suggest you to automatically correct the md5sums and dependencies. You can safely permit him to do so.
Installing/Publishing the new package
You can install the package locally by running
spaceman -i <myprogram>-<version>.pkg.tar.bz2
and publish it on your repository (read the article for more information).

