Convert FLAC to MP3

From ArchWiki
Revision as of 07:59, 19 October 2007 by Fluomole (talk | contribs)
Jump to: navigation, search

Template:I18n links start Template:I18n entry Template:I18n entry Template:I18n entry Template:I18n links end

这个文档 ABS - The Arch Build System 提供了一份良好的用于制作和修改 Arch Linux 软件包所需工具和文件的总览。如果你只想要定制或重新编译一个已经存在的软件包,这里应该已经提供了足够的信息。但是,如果你想要制作一个新的软件包,你还需要一些额外的指南。这个文档假定你已经事先阅读并掌握了 ABS 的描述。


所有用于制作一个软件包所需的信息放在PKGBUILD 文件中。当你运行makepkg命令,它将在当前工作目录中寻找PKGBUILD文件,然后根据PKGBUILD文件中的指令编译软件的源代码。当编译成功后,得到的二进制代码,包括诸如包版本和依赖等meta-信息将封装在名叫name.pkg.tar.gz的软件包中,你可以使用pacman -Up <package file>命令将其安装。

The PKGBUILD file contains all instructions for creating the package, in a form that is directly interpretable by bash (don't worry if that little tidbit of clue doesn't help you). The variables used here are described in the ABS article, but the most important/confusing ones are recapped here anyway. To start out with a new package, you should first create an empty working directory, preferably named /var/abs/local/<PKGNAME>. That way it's nicely integrated into the normal ABS-tree, but not touched by cvsup whenever you synchronize the tree. Change into this directory, and create a PKGBUILD file to work with either by copying the dummy prototype from /var/abs/PKGBUILD.proto into the working directory, or by copying the PKGBUILD from another package. The latter is quite useful if you only want to modify compilation options of a package instead of creating an entirely new one.

However you do it, you need a PKGBUILD file to work with here.

Editing of Variables

Now open it, and set each of these these variable values depending to the package you are building:

  • pkgname: Set this to a name for the package. Convention requests that you use all lower case letters for the package name. It's arbitrary, but it helps if a package's name is equal to the name of the working directory you're in, as well as the filename of the tar.gz that's containing the source of the program you're going to download.
  • pkgver: Set the version of the package. This can contain letters, numbers and periods, but CANNOT contain a hyphen. It depends on the versioning system (major.minor.bugfix,, etc) that the program you are packaging uses. Again, in most cases you should stick to the version that's part of the sourcepackage filename to make later steps easier and more flexible. Also note: if the package writer uses a dash in their version numbering scheme, replace it with an underscore. ('0.99-10' => '0.99_10')
  • pkgrel: This should be incremented each time you release a package, starting with 1. Its purpose is to differentiate consecutive builds of the same version of a package. Occasionally the first release of a package contains a problem or misfeature. When you make the second release, you increment the pkgrel variable so that pacman knows it needs to be reinstalled. When a new version of the package is released, you reset the pkgrel variable to 1.
  • pkgdesc: This should contain a short, usually less than 76 characters, description of the package. Usually it is not needed repeating the program name. OpenGL accelerated X server is better than xgl is a OpenGL....
  • arch: This should contain an array of architectures, usually 'i686', that describes where the PKGBUILD file can be used. You can access this value with the variable $arch during the build.
  • url: This should contain the address of the official site of the program where who is interested can find more informations.
  • license: The type of license, if you do not know it please write down 'unknow.'
  • depends: This should contain an array of package names that need to be installed before this program can be run, separated by spaces. The names can optionally be enclosed in single quotes (apostrophes) to prevent possible shell quoting problems, and the array should be enclosed in round brackets. Sometimes a program requires a minimum version of a dependency; In that case, you might want to use the mathematical "larger or equal than" operator, and enclose the whole construct in quotes. Here's an example to add a dependency on the glibc package, and the slang library of at least version 1.8.0: depends('glibc' 'slang>=1.8.0')
  • makedepends: This should contain an array of package names that are needed only during the build, but that are unneeded for *using* the package after install. Example: unarj used in a build to unpack some patches.
  • provides: This should contain an array of package names that become unneeded with described one since it gives at least the same features.
  • conflicts: This should be an array of package names that if installed with the described one will give problems.
  • replaces: This should be an array of obsolete package names that are replaced by the described one.
  • source: This must be an array of files which are needed to build the package, containing at least the location of the program source, which is in most cases a full HTTP or FTP URL enclosed in double quotes. The prototype PKGBUILD shows how you can use the previously set variables for package name and version effectively here. If you find you need to supply files which are not downloadable on the fly, for example self-made patches, you simply put those into the same directory where your PKGBUILD is in, and add the filename to this source array. Any paths you add here are resolved relative to the directory where the PKGBUILD lies. Before the actual build process is started, all of the files referenced here will be downloaded or checked for existence, and makepkg will not proceed if any are missing.
  • md5sums: An array of md5 checksums for the source files, space seperated and enclosed in quotes. Once all files in the source array are available, an md5 hash of each file will be automatically generated and compared with the values of this array, in the same order they appear in the source array. Whilst the order of the source files itself does not matter, it's important that it's coherent with the order of the md5sums, as makepkg won't guess which md5sum belongs to what source file, and will happily start spewing errors if they don't match to prevent download errors or manipulations. You can generate the md5sums array quickly and easily using the command makepkg -g (after the source array has been properly set up) in the directory that contains the PKGBUILD. makepkg -g >>PKGBUILD will generate the sums and append them to the end of the PKGBUILD, from whence you can move the line(s) into the proper position of the file.

So far you've only been setting up meta-information about the package; Where to get the sources, what the name of the package shall be, etc. The next step is supplying instructions on how to actually compile and install the program you're intending to pack up.

Using the source

Now you should download the source tarball, extract it, and note all commands needed to compile and install it. The contents of the build() function in your PKGBUILD will do nothing but run exactly these steps again, with a little glue to pack everything up once compilation is done.

Now you probably need to edit the contents of the build() function in the PKGBUILD. This function uses common shell commands in the bash syntax. The basic purpose of this function is to automatically compile the programs and create a pkg directory to install the program to, allowing makepkg to pack it all up easily without having to pick all interesting files from your "live" filesystem.

The build() function

Usually the first step in the build function is to change into one of the directories created by uncompressing the source files. You can use the $startdir variable to do this (it refers to the directory that contains the PKGBUILD). You may also use the $pkgname and $pkgver variables that you set earlier. For example, depending on the name of the directory that was uncompressed by makepkg, the first command in your build function might be cd $startdir/src/$pkgname-$pkgver, which happens to be a very common case unless the program's author is a very, very evil person.

Compiling the programs is the more difficult part. I will assume you managed to compile the program successfully "by hand" here, as all imaginable steps to do this cannot possibly be covered here. That's what the program's author is supposed to write README and INSTALL files for after all.

Now that you are in that directory, you need to issue whatever commands it takes to compile the files. In simple cases, you may simply use ./configure; make, although there are dozens of variations including ant build or issuing the actual gcc commands to compile the packages.

Good thing is, if you already managed to compile the package manually, you basically only need to list the commands you used here, and things should work out just fine. Since many packages like to install their files relative to the /usr/local directory, but Arch Linux prefers using just /usr, you probably want to supply a parameter to the configure script or the make command to take care of this. The prototype PKGBUILD serves as an example for that. It might work differently, though; Again, your mileage may vary.

The next step in the build() function is to put the compiled files in a place where makepkg can scoop them up to create a package. This directory is the pkg directory. It is supposed to imitate the root of your filesystem to the program's installation procedure. Any files that should be installed in a directory in the root of your filesystem should go in the pkg directory under the same directory structure (ie. if you want to install the file myprog in /usr/bin, it should be placed in $startdir/pkg/usr/bin). Fortunately, only a few programs require the user to copy dozens of files manually, but they supply some kind of installation procedure instead which is supposed to do that automatically, often invoked by running "make install". It's critical, however, that you find out how to tell this installation procedure that it's supposed to stuff all it's nifty files not into your /, but into $startdir/pkg/ instead! Otherwise you'll end up with an empty package file, and the binaries of the program you installed "correctly" added to your system already. Most of the time you'll have to supply the prefix parameter to the "make install" call as shown in the prototype, but it's very well possible that the program you're packaging expects an altogether different approach, but here are some hints:

  • Sometimes the configure script accepts a prefix property that tells where the files should be installed. You might use ./configure --prefix=$startdir/pkg/usr in such configuration, for example.
  • Sometimes there is a PREFIX option to append to a make install command. This is sometimes set as a variable, and sometimes set in the command. In worse cases you have to edit the Makefile(s) (or ant build/properties files if the project uses ant) with sed or a patch you'd have to create yourself.
  • There might be other sorts of install scripts that allow you to specify where the program should be installed.
  • In some cases, the program expects to be run from a single directory. Often it is wise to simply copy these to $startdir/pkg/opt.

As you might have guessed already, that's the part where actual knowledge and experience becomes a necessity. It helps a lot if you browse over the PKGBUILD files in the ABS tree, as those are tested and contain a few tricks that might prove valuable.

More often that not, the installation routine of the program will take care to create any subdirectories below the pkg/ directory. If it does not, however, you'll get a lot of errors during the install stage as files are copied to nonexistent subdirectories. In that case you'll have to create the needed subdirectories by adding the appropriate mkdir commands in the build() function before running the installation procedure. The actual directory structure is package dependent, of course; some programs need to place files in /etc or /usr while others might need to use /bin or /opt. Most will need to create several directories. You can do all of this with the mkdir -p $startdir/pkg/OTHER/DIRS/AS/NEEDED command, where OTHER/DIRS/AS/NEEDED represent directories at the root of the filesystem.

Testing the PKGBUILD

As you are writing the PKGBUILD's build() function, you will want to test your changes frequently to ensure there are no bugs. You can do this using the makepkg command in the directory containing the PKGBUILD. With a properly formatted PKGBUILD, this will create a package, but with a broken or unfinished one it will throw an error. Hopefully it will be a descriptive error!

If running makepkg finished successfully, it will place a shiny new file called $pkgname-$pkgver.pkg.tar.gz in your working directory. This is a pacman package and can be installed with the pacman -U and pacman -A options, or added to a local or web based repository. Note that just because a package file was built it doesn't mean it works! It might conceivably contain only the directory structure and no files whatsoever if, for example, you specified a prefix improperly. You can use pacman's query functions to display a list of files contained in the package and the dependencies it requires, and compare those with what you consider as correct. "pacman -Qlp <package file>" and "pacman -Qip <package file>" do the trick.

If the package looks sane, that's all you need to do. However, if you plan on releasing the package or PKGBUILD, it is imperative that you check and double check and re-double-check the contents of the depends array. This should contain a list of all packages that need to be installed in order for your package to work. You only need to list first level depends in the depends array. That is, you do not need to list packages that your program depends on if other packages that your program depends on are already listed.

For example, gtk2 depends on glib2. Like most open source C programs, it also requires glibc to be installed. However, glibc does not need to be listed as a dependency for gtk2 because it is a dependency for glib2, and glib2 is already listed in gtk2.

There are some tools you can use to check dependencies, including Jason Chu's famous namcap program (pacman -Sy namcap), and the more arcane ldd program. Check the man pages for these programs and the links at the end of this document for more information. You should also scour the program's documentation and website (some nice developers have a page called "dependencies" that helps a lot).

Testing the package

Also make sure that the package binaries actually run flawlessly! It's really annoying to release a package that contains all necessary files, but dumps core because of some obscure configuration option that doesn't quite work well with the rest of the system. If you're only going to compile packages for your own system, though, you don't need to worry too much about this quality assurance step, as you're the only person suffering from mistakes after all.

To sum it all up

  • Download the source tarball of the program you want to package up
  • Try compiling the package and installing it into an arbitrary directory
  • Copy over the prototype /var/abs/PKGBUILD.proto and rename it to PKGBUILD in a temporary working directory
  • Edit the PKGBUILD according to the needs of your package
  • Run makepkg and see whether the resulting package is built correctly
  • If not, repeat the last two steps

Useful links


  • Before you can automate the package building process, you should have done it manually at least once unless you know exactly what you're doing in advance, in which case you would not be reading this in the first place. Unfortunately, although a good bunch of program authors stick to the 3-step build cycle of "./configure; make; make install", this is not always the case, and things can get real ugly if you have to apply patches to make everything work at all. Rule of thumb: If you can't get the program to compile from the source tarball, and make it install itself to a defined, temporary subdirectory, you don't even need to try packaging it. There isn't any magic pixie dust in makepkg that makes source problems go away.
  • In a few cases, the packages are not even available as source and you have to use something like sh to get it to work. You will have to do quite a bit of research (read READMEs, INSTALL instructions, man pages, perhaps ebuilds from gentoo or other package installers, possibly even the MAKEFILEs or source code) to get it working. In some really bad cases, you have to edit the source files to get it to work at all. However, makepkg needs to be completely autonomous, with no user input. Therefore if you need to edit the Makefiles, you may have to bundle a custom patch with the PKGBUILD and install it from inside the build() function, or you might have to issue some sed commands from inside the build() function.
  • Note that just because a package file was built it doesn't mean it works! It might conceivably contain only the directory structure and no files whatsoever if, for example, you specified a prefix improperly. You can use pacman's query functions to display a list of files contained in the package and the dependencies it requires, and compare those with what you consider as correct. "pacman -Qlp <package file>" and "pacman -Qip <package file>" do the trick.