makepkg

From ParabolaWiki
Jump to: navigation, search
Summary
makepkg is a script used to compile and package software for use with pacman or a GUI pacman-wrapper like octopi etc.. This article details its configuration and usage.
Overview
Packages in Parabola are built using makepkg and a custom build script for each package (known as a PKGBUILD). Once packaged, software can be installed and managed with pacman.
Related
Creating Packages
Resources
makepkg(8) Manual Page
makepkg.conf(5) Manual Page

makepkg is used for compiling and building packages suitable for installation with pacman, Parabola's package manager or a GUI pacman-wrapper like octopi etc.. makepkg is a script that automates the building of packages; it can download and validate source files, check dependencies, configure build-time settings, compile the sources, install into a temporary root, make customizations, generate meta-info, and package everything together.

makepkg is provided by the pacman package.

1 Configuration

/etc/makepkg.conf is the main configuration file for makepkg. Most users will wish to fine-tune makepkg configuration options prior to building any packages.

1.1 Architecture, compile flags

The MAKEFLAGS, CFLAGS, and CXXFLAGS options are used by make, gcc, and g++ whilst compiling software with makepkg. By default, these options generate generic packages that can be installed on a wide range of machines. A performance improvement can be achieved by tuning compilation for the host machine. The downside is that packages compiled specifically for the compiling host's processor may not run on other machines.

Note: Do keep in mind that not all package build systems will use your exported variables. Some override them in the original Makefiles or the PKGBUILD.
/etc/makepkg.conf
...

#########################################################################
# ARCHITECTURE, COMPILE FLAGS
#########################################################################
#
CARCH="x86_64"
CHOST="x86_64-unknown-linux-gnu"

#-- Exclusive: will only run on x86_64
# -march (or -mcpu) builds exclusively for an architecture
# -mtune optimizes for an architecture, but builds for whole processor family
CFLAGS="-march=x86-64 -mtune=generic -O2 -pipe -fstack-protector --param=ssp-buffer-size=4 -D_FORTIFY_SOURCE=2"
CXXFLAGS="-march=x86-64 -mtune=generic -O2 -pipe -fstack-protector --param=ssp-buffer-size=4 -D_FORTIFY_SOURCE=2"
LDFLAGS="-Wl,-O1,--sort-common,--as-needed,-z,relro,--hash-style=gnu"
#-- Make Flags: change this for DistCC/SMP systems
#MAKEFLAGS="-j2"

...

The default makepkg.conf CFLAGS and CXXFLAGS are compatible with all machines within their respective architectures.

On x86_64 machines, there are rarely significant enough real world performance gains that would warrant investing the time to rebuild official packages.

As of version 4.3.0, GCC offers the -march=native switch that enables CPU auto-detection and automatically selects optimizations supported by the local machine at GCC runtime. To use it, just modify the default settings by changing the CFLAGS and CXXFLAGS lines as follows:

# -march=native also sets the correct -mtune=
CFLAGS="-march=native -O2 -pipe -fstack-protector --param=ssp-buffer-size=4 -D_FORTIFY_SOURCE=2"
CXXFLAGS="${CFLAGS}"

Further optimizing for CPU type can theoretically enhance performance because -march= enables all available instruction sets and improves scheduling for a particular CPU. This is especially noticeable when rebuilding applications (for example: audio/video encoding tools, scientific applications, math-heavy programs, etc.) that can take heavy advantage of newer instructions sets not enabled when using the default options (or packages) provided by Parabola.

It is very easy to reduce performance by using "non-standard" CFLAGS because compilers tend to heavily blow up the code size with loop unrolling, bad vectorization, crazy inlining, etc. depending on compiler switches. Unless you can verify/benchmark that something is faster, there is a very good chance it is not!

See the GCC man page for a complete list of available options. The Gentoo Compilation Optimization Guide and Safe Cflags wiki article provide more in-depth information.

1.1.1 MAKEFLAGS

The MAKEFLAGS option can be used to specify additional options for make. Users with multi-core/multi-processor systems can specify the number of jobs to run simultaneously. Typically -j2, plus 1 for each additional core/processor is an adequate choice. Some PKGBUILD's specifically override this with -j1, because of race conditions in certain versions or simply because it is not supported in the first place. Packages that fail to build because of this should be reported on the bug tracker after making sure that the error is indeed being caused by your MAKEFLAGS.

See man make for a complete list of available options.

1.2 Package output

Next, one can configure where source files and packages should be placed and identify themselves as the packager. This step is optional; packages will be created in the working directory where makepkg is run by default.

/etc/makepkg.conf
...

#########################################################################
# PACKAGE OUTPUT
#########################################################################
#
# Default: put built package and cached source in build directory
#
#-- Destination: specify a fixed directory where all packages will be placed
#PKGDEST=/home/packages
#-- Source cache: specify a fixed directory where source files will be cached
#SRCDEST=/home/sources
#-- Source packages: specify a fixed directory where all src packages will be placed
#SRCPKGDEST=/home/srcpackages
#-- Packager: name/email of the person or organization building packages
#PACKAGER="John Doe <john@doe.com>"

...

For example, create the directory:

$ mkdir /home/$USER/packages

Then modify the PKGDEST variable in /etc/makepkg.conf accordingly.

The PACKAGER variable will set the packager value within compiled packages' .PKGINFO metadata file. By default, user-compiled packages will display:

pacman -Qi package
...
Packager       : Unknown Packager
...

Afterwards:

pacman -Qi package
...
Packager       : John Doe <john@doe.com>
...

This is useful if multiple users will be compiling packages on a system, or you are otherwise distributing your packages to other users.

2 Usage

Before building packages with makepkg, remember that all packages in the base package group are assumed to be installed on all Parabola systems. Likewise, all packages in the base-devel package group are assumed to be installed when building packages with makepkg. According to those assumptions, packages defined in these groups are conventionally omitted from the list of dependencies in PKGBUILD files.

Before building packages, ensure that your package database and the build tools are up-to-date. The most sure way is by issuing (as root) the following command, choosing "all" when prompted:

# pacman -Syu base base-devel --needed

To build a package, one must have at least a PKGBUILD file, which is the build recipe, describing how to compile the software. There are often some auxiliary files accompanying it, which will be needed also, if they exist. For packages which are currently available in the parabola repos, these can be obtained from the AbsLibre tree. For new packages which are not in the parabola repos, you can often find a PKGBUILD that someone else has written for that program, or you can create one from scratch, as described in the Creating Packages article.

Warning: Only build and/or install packages from trusted sources. Use of any PKGBUILD, acquired from a source other than Parabola, is not supported, and is done at your own risk.

Once the PKGBUILD and any auxiliary files exist on your local file-system, change to the directory where they are saved, and issue the following command (as a regular unprivileged user) to build the package:

$ makepkg -csri

The letters (flags) after the '-' dash, add special behaviors. Any of them may be omitted, depending on your intention,

The -c flag instructs makepkg to delete files and directories from any previous build before starting, and to delete the new ones after the build is completed. This is useful for multiple builds of the same package or updating the package version, while reusing the same build directory. It prevents obsolete and remnant files from carrying over to the new builds.

The -s flag instructs makepkg to install needed build-time dependencies automatically before compiling. Otherwise, makepkg will report an error and fail, if any prerequisites are unsatisfied. Note that, in order for this to work, those dependencies must be available in the configured repositories; see pacman#Repositories for details. Alternatively, one can manually install any of the dependencies, prior to building (pacman -S --asdeps dep1 dep2).

The -r flag instructs makepkg to un-install, just before exiting, any build-time dependencies, which were automatically installed using the -s flag above. Otherwise, they would remain installed, but unused. This is useful for saving disk space and keeping the system minimal and tidy, with no unused software installed.

The -i flag instructs makepkg to automatically install the newly constructed package.

Once all dependencies are satisfied and the package builds successfully, a package file named according to the form: (pkgname-pkgver.pkg.tar.xz) will exist in the current working directory. To install the package, if you did not pass the -i flag, or to re-install it again at any time, run (as root):

# pacman -U pkgname-pkgver.pkg.tar.xz

The package can be un-installed just like any other, by running (as root):

# pacman -R pkgname

If you did not pass the -c flag, there will exist, in the current working directory, at least two new directories: pkg/ and src/, as well as any source-balls and VCS source repos that were downloaded. Those are no longer needed once the package is built successfully, and can be deleted manually.

3 Tips and Tricks

3.1 Replace checksums in PKGBUILD automatically

3.1.1 Option 1

Here is a very handy script that will generate new checksums for updated files and replace them inside the PKGBUILD in one step.

#!/bin/bash
# Script by Falconindy
# https://bbs.archlinux.org/viewtopic.php?id=131666

awk -v newsums="$(makepkg -g)" '
BEGIN {
  if (!newsums) exit 1
}

/^[[:blank:]]*(md|sha)[[:digit:]]+sums=/,/\)[[:blank:]]*$/ {
  if (!i) print newsums; i++
  next
}

1
' PKGBUILD > PKGBUILD.new && mv PKGBUILD{.new,}

3.1.2 Option 2

setconf PKGBUILD $(makepkg -g 2>/dev/null | pee "head -1 | cut -d= -f1" "cut -d= -f2") ')'

This works fine, but has the disadvantage that it requires the setconf package (just 364 KiB) and doesn't work with multiple sum (both md5sums and sha256sums in the same PKGBUILD, for example).

The speed is as good or better than scripts that uses awk, even though makepkg is called twice.

3.2 WARNING: Referencing $srcdir in PKGBUILD

Somehow, $srcdir of $pkgdir ended up in one of the installed files in your package.

To identify which files, run the following from the makepkg build directory:

grep -R "$(pwd)/src" pkg/

Link to discussion thread.

4 Acknowledgement

This wiki article is based on ArchWiki. We may have removed non-FSDG bits from it.