source: trunk/www/portfileHOWTO.php @ 907

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

Added Portfile HOWTO

  • Property svn:eol-style set to native
File size: 12.6 KB
Line 
1<?
2
3//
4// File     : portfileHOWTO.php
5// Version  : $Id: portfileHOWTO.php,v 1.1 2002/10/04 01:22:18 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>
42This will display useful debugging messages that are usually suppressed while running DarwinPorts.
43</p>
44<p>
45DarwinPorts performs several basic predefined tasks, these are:
46</p>
47<a name="basictoc"></a><h4>Basic Topics</h4>
48<ul>
49<li><a href="#fetch">Fetching the Sources</a></li>
50<li><a href="#checksum">Verifying the Downloaded File</a></li>
51<li><a href="#extract">Extracting the Sources into a Working Directory</a></li>
52<li><a href="#configure">Running a Configure Script</a></li>
53<li><a href="#build">Building the Sources</a><li>
54<li><a href="#install">Installing the Finished Product on the System</a></li>
55</ul>
56<a name="advancedtoc"></a><h4>Advanced Topics</h4>
57<ul>
58<li><a href="#targets">Overridding Targets</a></li>
59<li><a href="#variants">Portfile Variants</a></li>
60</ul>
61<a name="appendixtoc"></a><h4>Appendix</h4>
62<ul>
63<li><a href="#portfilelist">Portfile Listing</a></li>
64<li><a href="#contentslist">Contents Listing</a></li>
65</ul>
66
67<h3>
68<a name="fetch"></a>Fetching the Sources
69</h3>
70<p>
71The 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:
72</p>
73<pre><tt>
74PortSystem 1.0
75portname        ircii
76portversion     20020912
77categories      irc
78maintainers     kevin@opendarwin.org
79master_sites    ftp://ircftp.au.eterna.com.au/pub/ircII/
80</tt></pre>
81<p>
82The <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.
83</p>
84<p>
85At 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:
86</p>
87<pre><tt>
88% port checksum
89</tt></pre>
90<p>
91Which should give the following output:
92</p>
93<!--
94.........|.........|.........|.........|.........|.........|.........|.........|
95-->
96<pre><tt>
97DEBUG: Executing com.apple.main (ircii)
98DEBUG: Executing com.apple.fetch (ircii)
99--->  ircii-20020912.tar.gz doesn't seem to exist in /opt/local/var/db/dports/
100distfiles
101--->  Attempting to fetch ircii-20020912.tar.gz from ftp://
102ircftp.au.eterna.com.au/pub/ircII/
103DEBUG: Executing com.apple.checksum (ircii)
104Error: No checksums statement in Portfile.  File checksums are:
105ircii-20020912.tar.gz md5 2ae68c015698f58763a113e9bc6852cc
106Error: Target error: com.apple.checksum returned: No checksums statement in
107Portfile.
108</tt></pre>
109
110<h3>
111<a name="checksum"></a>Verifying the Downloaded File
112</h3>
113<p>
114Notice 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:
115</p>
116<pre><tt>
117checksums       md5 2ae68c015698f58763a113e9bc6852cc
118</tt></pre>
119
120<h3>
121<a name="extract"></a>Extracting the Sources into a Working Directory
122</h3>
123<p>
124Now that we have a checksum and can verify our sources, we can proceed to extracting the sources into our working directory.  Execute the following:
125</p>
126<pre><tt>
127% port extract
128</pre></tt>
129<p>
130Which should display the following output:
131</p>
132<!--
133.........|.........|.........|.........|.........|.........|.........|.........|
134-->
135<pre><tt>
136DEBUG: Skipping completed com.apple.main (ircii)
137DEBUG: Skipping completed com.apple.fetch (ircii)
138DEBUG: Skipping completed com.apple.checksum (ircii)
139DEBUG: Executing com.apple.extract (ircii)
140--->  Extracting for ircii-20020912
141--->  Extracting ircii-20020912.tar.gz ... DEBUG: Assembled command: 'cd /Users/
142kevin/opendarwin/proj/darwinports/dports/irc/ircii/work && gzip -dc /opt/local/
143var/db/dports/distfiles/ircii-20020912.tar.gz | tar -xf -'
144Done
145</tt></pre>
146<h3>
147<a name="configure"></a>Running a Configure Script
148</h3>
149<p>
150Now 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.
151</p>
152<p>
153ircII's standard set of options looks fine for a base install on Darwin, so we'll move on to the build phase. 
154</p>
155
156<h3>
157<a name="build"></a>Building the Sources
158</h3>
159To build, type the following:
160</p>
161<pre><tt>
162% port build
163</tt></pre>
164<p>
165By 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. 
166
167
168<h3>
169<a name="install"></a>Installing the Finished Product on the System
170</h3>
171Portfiles 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.
172</p>
173<p>
174Using 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:
175</p>
176<!--
177.........|.........|.........|.........|.........|.........|.........|.........|
178-->
179<pre><tt>
180% find /opt/local > /tmp/existing.files
181% sudo port install
182% find /opt/local > /tmp/more.files
183% diff -u /tmp/existing.files /tmp/more.files | grep ^\+\/ | \
184  sed -e 's|^\+/opt/local/||g' > contents
185</tt></pre>
186<p>
187Now 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:
188<pre><tt>
189include contents
190</tt></pre>
191<p>
192Now we have a complete portfile.  Re-run the installation step to add your port to your own registry:
193</p>
194<pre><tt>
195% sudo port install
196</tt></pre>
197Which will output the following:
198<pre><tt>
199DEBUG: Skipping completed com.apple.main (ircii)
200DEBUG: Skipping completed com.apple.fetch (ircii)
201DEBUG: Skipping completed com.apple.checksum (ircii)
202DEBUG: Skipping completed com.apple.extract (ircii)
203DEBUG: Skipping completed com.apple.patch (ircii)
204DEBUG: Skipping completed com.apple.configure (ircii)
205DEBUG: Skipping completed com.apple.build (ircii)
206DEBUG: Skipping completed com.apple.install (ircii)
207DEBUG: Executing com.apple.registry (ircii)
208--->  Adding ircii to registry, this may take a moment...
209</tt></pre>
210
211<h2>
212Advanced Topics
213</h2>
214
215<h3>
216<a name="targets"></a>Overriding Targets
217</h3>
218<p>
219It'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:
220</p>
221<pre><tt>
222configure {}
223</tt></pre>
224<p>
225In 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:
226</p>
227<!--
228.........|.........|.........|.........|.........|.........|.........|.........|
229-->
230<pre><tt>
231post-configure {
232    reinplace "s|change.this.to.a.server|irc.openprojects.net|g" \
233        "${workdir}/${worksrcdir}/config.h"
234}
235</tt></pre>
236<p>
237This 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.
238</p>
239
240<h3>
241<a name="variants"></a>Portfile Variants
242</h3>
243<p>
244Since 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:
245</p>
246<pre><tt>
247configure.args      --disable-ipv6
248
249variant ipv6 {
250    configure.args  --enable-ipv6
251}
252</tt></pre>
253<p>
254Now the default build will not include ipv6 support, but if the ipv6 variant is requested, ircII will have it.  You can specify the ipv6 variant in the following way:
255</p>
256<pre><tt>
257% port build +ipv6
258</tt></pre>
259
260<h2>
261Appendix
262</h2>
263
264<h3>
265<a name="portfilelist"></a>Portfile Listing
266</h3>
267<p>
268The following is a complete listing of the ircII Portfile:
269</p>
270<pre><tt>
271PortSystem 1.0
272portname        ircii
273portversion     20020912
274categories      irc
275maintainers     kevin@opendarwin.org
276master_sites    ftp://ircftp.au.eterna.com.au/pub/ircII/
277checksums       md5 2ae68c015698f58763a113e9bc6852cc
278configure.args  --disable-ipv6
279include         contents
280
281post-configure {
282        reinplace "s|change.this.to.a.server|irc.openprojects.net|g" \
283                  "${workdir}/${worksrcdir}/config.h"
284}
285
286variant ipv6 {
287        configure.args-append --enable-ipv6
288}
289</tt></pre>
290
291<h3>
292<a name="contentslist"></a>Contents Listing
293</h3>
294<p>
295The following is a partial listing of the ircII contents file:
296</p>
297<pre><tt>
298contents {
299/bin/irc
300/bin/irc-20020912
301... omitted ...
302/man/man1/irc.1
303/man/man1/ircbug.1
304/man/man1/ircII.1
305/share/irc
306/share/irc/help
307... omitted ...
308}
309</tt></pre>
310
311
312<? 
313        od_print_footer("en"); 
314?>
Note: See TracBrowser for help on using the repository browser.