source: trunk/www/portfileHOWTO.php @ 912

Last change on this file since 912 was 912, checked in by kevin, 18 years ago

xhtml bug fixes
correct output from port checksum
explain -append and -delete

  • Property svn:eol-style set to native
File size: 13.0 KB
Line 
1<?
2
3//
4// File     : portfileHOWTO.php
5// Version  : $Id: portfileHOWTO.php,v 1.2 2002/10/04 01:48:58 kevin Exp $
6// Location : /projects/darwinports/portfileHOWTO.php
7//
8
9        include_once("$DOCUMENT_ROOT/includes/od_lib.inc.php");
10        od_print_header("How to Write a DarwinPorts Portfile", "en", "iso-8859-1", "", 0);
11?>
12
13<h2>
14How to Write a DarwinPorts Portfile
15</h2>
16<pre><tt>
17Kevin Van Vechten | <a href="mailto:kevin@opendarwin.org">kevin@opendarwin.org</a>
183-Oct-2002
19</tt></pre>
20<h3>
21Abstract
22</h3>
23<p>
24DarwinPorts 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.
25</p>
26<p>
27This article describes the construction of a simple Portfile, and explores a few of DarwinPorts' most common features.
28</p>
29<h3>
30Getting Started
31</h3>
32<p>
33In order to work with DarwinPorts, you'll need to download and install it on your system.  The DarwinPorts project <a href="http://opendarwin.org/projects/darwinports/">homepage</a> describes how to get and install it.
34</p>
35<p>
36Since you're interested in writing a Portfile, let's change some configuration options that will help in debugging as we go.  Edit the file <tt>/etc/ports/ports.conf</tt> to contain the following:
37</p>
38<pre><tt>
39ports_debug     yes
40ports_verbose   yes
41</tt></pre>
42<p>
43This will display useful debugging messages that are usually suppressed while running DarwinPorts.
44</p>
45<p>
46DarwinPorts performs several basic predefined tasks, these are:
47</p>
48<a name="basictoc"></a><h4>Basic Topics</h4>
49<ul>
50<li><a href="#fetch">Fetching the Sources</a></li>
51<li><a href="#checksum">Verifying the Downloaded File</a></li>
52<li><a href="#extract">Extracting the Sources into a Working Directory</a></li>
53<li><a href="#configure">Running a Configure Script</a></li>
54<li><a href="#build">Building the Sources</a></li>
55<li><a href="#install">Installing the Finished Product on the System</a></li>
56</ul>
57<a name="advancedtoc"></a><h4>Advanced Topics</h4>
58<ul>
59<li><a href="#targets">Overridding Targets</a></li>
60<li><a href="#variants">Portfile Variants</a></li>
61</ul>
62<a name="appendixtoc"></a><h4>Appendix</h4>
63<ul>
64<li><a href="#portfilelist">Portfile Listing</a></li>
65<li><a href="#contentslist">Contents Listing</a></li>
66</ul>
67
68<h3>
69<a name="fetch"></a>Fetching the Sources
70</h3>
71<p>
72The 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 <tt>ircii</tt> and inside it create a file named <tt>Portfile</tt> with the following contents:
73</p>
74<pre><tt>
75PortSystem 1.0
76portname        ircii
77portversion     20020912
78categories      irc
79maintainers     kevin@opendarwin.org
80master_sites    ftp://ircftp.au.eterna.com.au/pub/ircII/
81</tt></pre>
82<p>
83The <tt>portname</tt> and <tt>portversion</tt> options describe the name and version of the software, the <tt>categories</tt> option is a list of the logical categories to which the software belongs, this is used for organizational purposes.  The first entry in <tt>categories</tt> should match the directory in which the port's directory resides in the port tree.  The <tt>maintainers</tt> option should contain your email address, and the <tt>master_sites</tt> option should contain a list of sites where the distribution sources may be downloaded.
84</p>
85<p>
86At 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 <tt>.tar.gz</tt> format.  From your working directory, execute the following command:
87</p>
88<pre><tt>
89% port checksum
90</tt></pre>
91<p>
92Which should give the following output:
93</p>
94<!--
95.........|.........|.........|.........|.........|.........|.........|.........|
96-->
97<pre><tt>
98DEBUG: Executing com.apple.main (ircii)
99DEBUG: Executing com.apple.fetch (ircii)
100--->  ircii-20020912.tar.gz doesn't seem to exist in /opt/local/var/db/dports/
101distfiles
102--->  Attempting to fetch ircii-20020912.tar.gz from ftp://
103ircftp.au.eterna.com.au/pub/ircII/
104DEBUG: Executing com.apple.checksum (ircii)
105Error: No checksums statement in Portfile.  File checksums are:
106ircii-20020912.tar.gz md5 2ae68c015698f58763a113e9bc6852cc
107Error: Target error: com.apple.checksum returned: No checksums statement in
108Portfile.
109</tt></pre>
110
111<h3>
112<a name="checksum"></a>Verifying the Downloaded File
113</h3>
114<p>
115Notice that DarwinPorts first checks for a local copy of <tt>ircii-20020912.tar.gz</tt> 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 <tt>checksums</tt> argument is not specified.  Go back and add the following to your Portfile:
116</p>
117<pre><tt>
118checksums       md5 2ae68c015698f58763a113e9bc6852cc
119</tt></pre>
120
121<h3>
122<a name="extract"></a>Extracting the Sources into a Working Directory
123</h3>
124<p>
125Now that we have a checksum and can verify our sources, we can proceed to extracting the sources into our working directory.  Execute the following:
126</p>
127<pre><tt>
128% port extract
129</pre></tt>
130<p>
131Which should display the following output:
132</p>
133<!--
134.........|.........|.........|.........|.........|.........|.........|.........|
135-->
136<pre><tt>
137DEBUG: Skipping completed com.apple.main (ircii)
138DEBUG: Skipping completed com.apple.fetch (ircii)
139DEBUG: Executing com.apple.checksum (ircii)
140--->  Checksum OK for ircii-20020912.tar.gz
141DEBUG: Executing com.apple.extract (ircii)
142--->  Extracting for ircii-20020912
143--->  Extracting ircii-20020912.tar.gz ... DEBUG: Assembled command: 'cd /Users/
144kevin/opendarwin/proj/darwinports/dports/irc/ircii/work &amp;&amp; gzip -dc /opt/local/
145var/db/dports/distfiles/ircii-20020912.tar.gz | tar -xf -'
146Done
147</tt></pre>
148<h3>
149<a name="configure"></a>Running a Configure Script
150</h3>
151<p>
152Now that the sources have been extracted into a <tt>work</tt> 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 <tt>--prefix=${prefix}</tt> argument to configure, specifying that the software should be installed in the directory tree used by DarwinPorts.
153</p>
154<p>
155ircII's standard set of options looks fine for a base install on Darwin, so we'll move on to the build phase. 
156</p>
157
158<h3>
159<a name="build"></a>Building the Sources
160</h3>
161To build, type the following:
162</p>
163<pre><tt>
164% port build
165</tt></pre>
166<p>
167By default, the build phase executes the makefile using GNU make.  (This can be changed with the <tt>make/build.type</tt> option.)  The above step has starting compiling the sources, when it finishes we'll be ready to install the software. 
168
169
170<h3>
171<a name="install"></a>Installing the Finished Product on the System
172</h3>
173Portfiles are required to have a <tt>contents</tt> 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 <tt>contents</tt> is a path to a file.  All paths are relative to the <tt>${prefix}</tt> 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 <tt>${prefix}</tt> tree.  After installing we'll re-run the find command, and use the differences to generate our contents list.
174</p>
175<p>
176Using 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 <tt>${prefix}</tt>, we'll pipe through <tt>sed</tt> and delete the prefix (<tt>/opt/local/</tt>), storing the result in a file named <tt>contents</tt> in our port directory.  We can do this with the following commands:
177</p>
178<!--
179.........|.........|.........|.........|.........|.........|.........|.........|
180-->
181<pre><tt>
182% find /opt/local > /tmp/existing.files
183% sudo port install
184% find /opt/local > /tmp/more.files
185% diff -u /tmp/existing.files /tmp/more.files | grep ^\+\/ | \
186  sed -e 's|^\+/opt/local/||g' > contents
187</tt></pre>
188<p>
189Now that we have a contents file in our port directory, we should edit it to begin with <tt>contents {</tt> and end with a closing <tt>}</tt>.  (It is important to note that any other process using the <tt>${prefix}</tt> tree may interfere with the accuracy of the <tt>find</tt> command.  You should audit the resulting <tt>contents</tt> file to look for any files that appear out of place, specifically some DarwinPorts temporary files such as <tt>/var/db/dports/receipts/ircii-20020912.tmp</tt>.)  Next we should edit the Portfile to include our contents file:
190<pre><tt>
191include contents
192</tt></pre>
193<p>
194Now we have a complete portfile.  Re-run the installation step to add your port to your own registry:
195</p>
196<pre><tt>
197% sudo port install
198</tt></pre>
199Which will output the following:
200<pre><tt>
201DEBUG: Skipping completed com.apple.main (ircii)
202DEBUG: Skipping completed com.apple.fetch (ircii)
203DEBUG: Skipping completed com.apple.checksum (ircii)
204DEBUG: Skipping completed com.apple.extract (ircii)
205DEBUG: Skipping completed com.apple.patch (ircii)
206DEBUG: Skipping completed com.apple.configure (ircii)
207DEBUG: Skipping completed com.apple.build (ircii)
208DEBUG: Skipping completed com.apple.install (ircii)
209DEBUG: Executing com.apple.registry (ircii)
210--->  Adding ircii to registry, this may take a moment...
211</tt></pre>
212
213<h2>
214Advanced Topics
215</h2>
216
217<h3>
218<a name="targets"></a>Overriding Targets
219</h3>
220<p>
221It'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:
222</p>
223<pre><tt>
224configure {}
225</tt></pre>
226<p>
227In 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:
228</p>
229<!--
230.........|.........|.........|.........|.........|.........|.........|.........|
231-->
232<pre><tt>
233post-configure {
234    reinplace "s|change.this.to.a.server|irc.openprojects.net|g" \
235        "${workdir}/${worksrcdir}/config.h"
236}
237</tt></pre>
238<p>
239This example replaces the occurance of <tt>change.this.to.a.server</tt> with <tt>irc.openprojects.net</tt> in the config.h file that was generated during the preceding <tt>configure</tt> phase.  Note this is a somewhat contrived example, since the same could have been accomplished by specifying <tt>--with-default-server=irc.openprojects.net</tt> in <tt>configure.args</tt>, but the approach is generally useful when such configure arguments aren't present.
240</p>
241
242<h3>
243<a name="variants"></a>Portfile Variants
244</h3>
245<p>
246Since Darwin 6.0 has ipv6, it would be possible to configure with the <tt>--with-ipv6</tt> option.  This can be done by adding the following option to the Portfile:
247</p>
248<pre><tt>
249configure.args      --disable-ipv6
250
251variant ipv6 {
252    configure.args-append  --enable-ipv6
253}
254</tt></pre>
255<p>
256Now 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 <tt>-append</tt> or <tt>-delete</tt> to append or delete one term from the list.  You can specify building with the ipv6 variant in the following way:
257</p>
258<pre><tt>
259% port build +ipv6
260</tt></pre>
261
262<h2>
263Appendix
264</h2>
265
266<h3>
267<a name="portfilelist"></a>Portfile Listing
268</h3>
269<p>
270The following is a complete listing of the ircII Portfile:
271</p>
272<pre><tt>
273PortSystem 1.0
274portname        ircii
275portversion     20020912
276categories      irc
277maintainers     kevin@opendarwin.org
278master_sites    ftp://ircftp.au.eterna.com.au/pub/ircII/
279checksums       md5 2ae68c015698f58763a113e9bc6852cc
280configure.args  --disable-ipv6
281include         contents
282
283post-configure {
284        reinplace "s|change.this.to.a.server|irc.openprojects.net|g" \
285                  "${workdir}/${worksrcdir}/config.h"
286}
287
288variant ipv6 {
289        configure.args-append --enable-ipv6
290}
291</tt></pre>
292
293<h3>
294<a name="contentslist"></a>Contents Listing
295</h3>
296<p>
297The following is a partial listing of the ircII contents file:
298</p>
299<pre><tt>
300contents {
301/bin/irc
302/bin/irc-20020912
303... omitted ...
304/man/man1/irc.1
305/man/man1/ircbug.1
306/man/man1/ircII.1
307/share/irc
308/share/irc/help
309... omitted ...
310}
311</tt></pre>
312
313
314<? 
315        od_print_footer("en"); 
316?>
Note: See TracBrowser for help on using the repository browser.