How to Write a DarwinPorts Portfile


Kevin Van Vechten | kevin@opendarwin.org
3-Oct-2002

Abstract

DarwinPorts automates the common tasks needed to port software to Darwin. Portfiles provide the information necessary for building and installing a particular software title correctly on Darwin. The goal of DarwinPorts is to keep Portfile syntax as simple as possible, while still supporting all the special cases that many software titles require to build and install successfully.

This article describes the construction of a simple Portfile, and explores a few of DarwinPorts' most common features.

Getting Started

In order to work with DarwinPorts, you'll need to download and install it on your system. The DarwinPorts project homepage describes how to get and install it.

Since you're interested in writing a Portfile, let's change some configuration options that will help in debugging as we go. Edit the file /etc/ports/ports.conf to contain the following:


ports_debug     yes
ports_verbose   yes

This will display useful debugging messages that are usually suppressed while running DarwinPorts.

DarwinPorts performs several basic predefined tasks, these are:

Basic Topics

Advanced Topics

Appendix

Fetching the Sources

The first step is to choose a piece of software to bring to port. For this example, we'll be porting ircII, a popular internet relay chat client. We can start with a simple Portfile describing the basic attributes of ircII such as its name, version, and the site where we can download the sources. Create a working directory named ircii and inside it create a file named Portfile with the following contents:


PortSystem 1.0
portname        ircii
portversion     20020912
categories      irc
maintainers     kevin@opendarwin.org
master_sites    ftp://ircftp.au.eterna.com.au/pub/ircII/

The portname and portversion options describe the name and version of the software, the categories option is a list of the logical categories to which the software belongs, this is used for organizational purposes. The first entry in categories should match the directory in which the port's directory resides in the port tree. The maintainers option should contain your email address, and the master_sites option should contain a list of sites where the distribution sources may be downloaded.

At this point, the Portfile is complete enough to download ircII. By default, DarwinPorts will append the port version to the portname and assume sources are in .tar.gz format. From your working directory, execute the following command:


% port checksum

Which should give the following output:


DEBUG: Executing com.apple.main (ircii)
DEBUG: Executing com.apple.fetch (ircii)
--->  ircii-20020912.tar.gz doesn't seem to exist in /opt/local/var/db/dports/
distfiles
--->  Attempting to fetch ircii-20020912.tar.gz from ftp://
ircftp.au.eterna.com.au/pub/ircII/
DEBUG: Executing com.apple.checksum (ircii)
Error: No checksums statement in Portfile.  File checksums are:
ircii-20020912.tar.gz md5 2ae68c015698f58763a113e9bc6852cc
Error: Target error: com.apple.checksum returned: No checksums statement in 
Portfile.

Verifying the Downloaded File

Notice that DarwinPorts first checks for a local copy of ircii-20020912.tar.gz and doesn't find it, so it then downloads from the remote site. The port doesn't finish because of an error: "No checksums statement in Portfile." Portfiles must contain an md5 checksum of all distribution files--this allows DarwinPorts to verify the accuracy and authenticity of the sources. For convenience, an md5 checksum of the downladed files is printed when the checksums argument is not specified. Go back and add the following to your Portfile:


checksums       md5 2ae68c015698f58763a113e9bc6852cc

Extracting the Sources into a Working Directory

Now that we have a checksum and can verify our sources, we can proceed to extracting the sources into our working directory. Execute the following:


% port extract

Which should display the following output:


DEBUG: Skipping completed com.apple.main (ircii)
DEBUG: Skipping completed com.apple.fetch (ircii)
DEBUG: Executing com.apple.checksum (ircii)
--->  Checksum OK for ircii-20020912.tar.gz
DEBUG: Executing com.apple.extract (ircii)
--->  Extracting for ircii-20020912
--->  Extracting ircii-20020912.tar.gz ... DEBUG: Assembled command: 'cd /Users/
kevin/opendarwin/proj/darwinports/dports/irc/ircii/work && gzip -dc /opt/local/
var/db/dports/distfiles/ircii-20020912.tar.gz | tar -xf -'
Done

Running a Configure Script

Now that the sources have been extracted into a work directory in the current working directory, we can configure the sources to compile with the desired options. By default DarwinPorts assumes the software you're porting uses an autoconf configure script, also by default, DarwinPorts will pass the --prefix=${prefix} argument to configure, specifying that the software should be installed in the directory tree used by DarwinPorts.

ircII's standard set of options looks fine for a base install on Darwin, so we'll move on to the build phase.

Building the Sources

To build, type the following:


% port build

By default, the build phase executes the makefile using GNU make. (This can be changed with the make.type option.) The above step has starting compiling the sources, when it finishes we'll be ready to install the software.

Installing the Finished Product on the System

Portfiles are required to have a contents option which specifies which files are installed. DarwinPorts uses this information to catalog what files belong to which piece of software so they may be later uninstalled. Each parameter to contents is a path to a file. All paths are relative to the ${prefix} variable. As a convenient way to determine exactly what files are installed as part of ircII, let's use the find command to compose a manifest of the files in the ${prefix} tree. After installing we'll re-run the find command, and use the differences to generate our contents list.

Using the unidiff format, we'll compare the list of existing files with the new list of files, only paying attention to the lines that were added. Since the contents paths are supposed to be relative to ${prefix}, we'll pipe through sed and delete the prefix (/opt/local/), storing the result in a file named contents in our port directory. We can do this with the following commands:


% find /opt/local > /tmp/existing.files
% sudo port install
% find /opt/local > /tmp/more.files
% diff -u /tmp/existing.files /tmp/more.files | grep ^\+\/ | \
  sed -e 's|^\+/opt/local/||g' > contents

Now that we have a contents file in our port directory, we should edit it to begin with contents { and end with a closing }. (It is important to note that any other process using the ${prefix} tree may interfere with the accuracy of the find command. You should audit the resulting contents file to look for any files that appear out of place, specifically some DarwinPorts temporary files such as /var/db/dports/receipts/ircii-20020912.tmp.) Next we should edit the Portfile to include our contents file:


include contents

Now we have a complete portfile. Re-run the installation step to add your port to your own registry:


% sudo port install
Which will output the following:

DEBUG: Skipping completed com.apple.main (ircii)
DEBUG: Skipping completed com.apple.fetch (ircii)
DEBUG: Skipping completed com.apple.checksum (ircii)
DEBUG: Skipping completed com.apple.extract (ircii)
DEBUG: Skipping completed com.apple.patch (ircii)
DEBUG: Skipping completed com.apple.configure (ircii)
DEBUG: Skipping completed com.apple.build (ircii)
DEBUG: Skipping completed com.apple.install (ircii)
DEBUG: Executing com.apple.registry (ircii)
--->  Adding ircii to registry, this may take a moment...

Advanced Topics

Overriding Targets

It's possible to override the functionality of any build target with Tcl code. A common example is the following, which might be useful for a script without an autoconf configure script:


configure {}

In the Portfile, this will replace the functionality of the configure target, thus skipping that step. It is also possible to execute Tcl code immediately before or after any of the standard targets. This can be accomplished in the following manner:


post-configure {
    reinplace "s|change.this.to.a.server|irc.openprojects.net|g" \
        "${workdir}/${worksrcdir}/config.h"
}

This example replaces the occurance of change.this.to.a.server with irc.openprojects.net in the config.h file that was generated during the preceding configure phase. Note this is a somewhat contrived example, since the same could have been accomplished by specifying --with-default-server=irc.openprojects.net in configure.args, but the approach is generally useful when such configure arguments aren't present.

Portfile Variants

Since Darwin 6.0 has ipv6, it would be possible to configure with the --with-ipv6 option. This can be done by adding the following option to the Portfile:


configure.args      --disable-ipv6

variant ipv6 {
    configure.args-append  --enable-ipv6
}

Now the default build will not include ipv6 support, but if the ipv6 variant is requested, ircII will have it. Options by themselves should be thought of as an assignment operator. Since variants may be used in combination with one another, it's good practice to only append to options instead of overwrite them. All options may be suffixed with -append or -delete to append or delete one term from the list. You can specify building with the ipv6 variant in the following way:


% port build +ipv6

Appendix

Portfile Listing

The following is a complete listing of the ircII Portfile:


PortSystem 1.0
portname        ircii
portversion     20020912
categories      irc
maintainers     kevin@opendarwin.org
master_sites    ftp://ircftp.au.eterna.com.au/pub/ircII/
checksums       md5 2ae68c015698f58763a113e9bc6852cc
configure.args  --disable-ipv6
include         contents

post-configure {
        reinplace "s|change.this.to.a.server|irc.openprojects.net|g" \
                  "${workdir}/${worksrcdir}/config.h"
}

variant ipv6 {
        configure.args-append --enable-ipv6
}

Contents Listing

The following is a partial listing of the ircII contents file:


contents {
/bin/irc
/bin/irc-20020912
... omitted ...
/man/man1/irc.1
/man/man1/ircbug.1
/man/man1/ircII.1
/share/irc
/share/irc/help
... omitted ...
}