summaryrefslogtreecommitdiffstats
path: root/dev/packaging/tutorials/basic.mdwn
blob: b5e0e0e72b556261bcdaf1367e18245c50dc8bc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
[[!meta title="Basic Packaging Tutorial"]]

In this introduction to software packaging, we will package the Expat XML parser
library.  This is a pretty simple but complete package, consisting of a shared
library and its development files plus an executable utility and some
documentation.

We will use [opkbuild 3.0.x][opkbuild] to build a package in [Source Package
Format 2.0][spf] (SPF 2.0) with the assistance of [opkhelper 3.0.x][opkhelper].

This tutorial assumes some knowledge of the UNIX shell command language and
utilities (see the "Shell and Utilities" volume of [POSIX.1-2008][posix]) and at
least basic familiarity with [makefile syntax][posix-makefile].

This tutorial presents one possible packaging workflow that seems to work well.
There is no mandatory workflow to packaging.  The only requirements are those
made by the source package format and any build helper utilities that are used.


Getting Started
===============

Source Package Directory
------------------------

First, make a *source package directory*.  This is the directory that will
contain all of our source package files.  SPF 2.0 makes no requirements on the
name of this directory, but using the name of the source package is recommended.

    $ mkdir expat
    $ cd expat

We need [a file called `format`][spf-format] to identify the format of our
source package.  For SPF 2.0, it should simply contain the string `2.0`.

    $ echo '2.0' >format

Upstream Source Archive
-----------------------

Obviously we need the source code of the software to be packaged.  Go to
[Expat's Web site][expat], find the expat 2.1.0 archive, and download it into
the source package directory.

    $ wget 'http://downloads.sourceforge.net/project/expat/expat/2.1.0/expat-2.1.0.tar.gz'

SPF 2.0 requires that an [upstream source archive][spf-upstream-source] be named
`<pkgname>-<pkgver>.orig.tar<ext>`, where `<pkgname>` is the name of the source
package, `<pkgver>` is the upstream version of the source package, and `<ext>`
is an optional file extension to indicate compression.  So, rename the archive
accordingly.

    $ mv 'expat-2.1.0.tar.gz' 'expat-2.1.0.orig.tar.gz'


Source Package Metadata
=======================

Now we need some metadata for our source package.

Control File
------------

First we'll make a `control` file.  The format of this file is not yet
documented in the SPF 2.0 specification, but it is documented [in the Debian
Policy Manual][dpm-control].  The [source package fields][spf-fields-src] are
`Maintainer` (required), `Build-Depends` (optional), and `Homepage` (optional).
We'll fill in the fields whose values we know right now: `Maintainer` and
`Homepage`.

`Maintainer` is the name and e-mail address of the person or team responsible
for the package (i.e. usually you when you are making a package).  The value
must follow the syntax of the `mailbox` symbol in [RFC 5322 section
3.4][rfc-5322-3.4].  That is, the value must be of the form `name <address>`.
If `name` contains any of the following characters, it must be in double quotes:

    ( ) < > [ ] : ; @ \ , .

`Homepage` is the URL of the Web site for the package, if such a site exists.

Our expat `control` file looks like this:

    Maintainer: "J. Random Hacker" <jrandom@example.com>
    Homepage: http://expat.sourceforge.net/

Change Log
----------

Now we'll make a `changelog` file.  The format of this file is documented [in
the SPF 2.0 specification][spf-changelog].  We're making version "2.1.0-1" of
the "expat" source package for the "trunk" distribution.  We can get the current
date and time in the RFC 5322 format using the **date**(1) command:

    $ date '+%a, %d %b %Y %H:%M:%S %z'

Our expat `changelog` file looks like this:

    expat (2.1.0-1) trunk
    
      * Initial release.
    
     -- "J. Random Hacker" <jrandom@example.com>  Sun, 18 Nov 2012 11:58:19 -0500


Building the Software
=====================

We can now write our `build` makefile to try to get the Expat software to build.
[The `build` makefile][spf-build] "directs the process of building and
installing data files to be provided by binary packages".

Looking Through the Source
--------------------------

With a ["no-op"][no-op] target in `build`, we can make **opkbuild**(1) prepare a
[work area][spf-work-area] with the unpacked source code and stop.  This target
isn't required by SPF 2.0, but it seems to facilitate a nice workflow.  So begin
writing `build` as follows:

    #!/usr/bin/make -f
    
    nop:
    	@:

Note that, due to makefile syntax, the line after `nop:` must begin with a tab
character.  This line is called a "command line" in makefile syntax.  The [`:`
utility][posix-colon] is a "null utility" that returns an exit status of zero.
A command prefix of `@` tells make to not write the command to standard output
before executing it.

The `build` makefile must be executable, so set its file mode:

    $ chmod 755 build

We can now make **opkbuild**(1) prepare our build work area.

    $ opkbuild -b -c -T nop

The options are explained in the help output of opkbuild, obtained by running
`opkbuild -h`.  The `-b` option tells **opkbuild**(1) to build only binary
packages (no source package).  The `-c` option tells it to not clean up the work
area after building packages.  The `-T` option specifies a target to be built
instead of the standard `build` and `install` targets.

Now look in `tmp/src/`, the location of the source code within the build work
area.

    $ ls tmp/src/

Look for some documentation file that might tell us how to build Expat.  This
kind of information is usually kept in a file called `INSTALL` or `README`.
Expat's `README` file says to run `./configure`, then `make` and `make install`.

Looking at `tmp/src/configure`, we see that it is "[g]enerated by GNU Autoconf
2.68 for expat 2.1.0".  The `tmp/src/README` file reports that the makefile
supports the use of either the `DESTDIR` or `INSTALL_ROOT` macro to install
Expat somewhere other than in the root of the filesystem.  So, we should be able
to use opkhelper's buildsystem utilities to automatically configure, build, and
install Expat for us.

Building
--------

So let's add a `build` target to our `build` makefile.  The makefile should now
look like this:

    #!/usr/bin/make -f
    
    nop:
    	@:
    
    build:
    	oh-autoconfigure
    	oh-autobuild
    	touch $@
    
    install: build
    	oh-autoinstall

Read the manual pages and/or source code of **oh-autoconfigure**(1),
**oh-autobuild**(1), and **oh-autoinstall**(1) to learn more about what they do.

The `touch $@` command is recommended by SPF 2.0:

> The build target should create a file named build in the build work area to
> prevent configuration and compilation from being performed multiple times.

The `install` target is declared as depending on the `build` target:

    install: build

We can now build Expat.

    $ opkbuild -b -c -T build


TODO: Finish.


[opkbuild]: http://git.os.libiquity.com/opkbuild/opkbuild.git/
[spf]: http://specs.os.libiquity.com/spf-2.0/
[opkhelper]: http://git.os.libiquity.com/opkhelper/opkhelper.git/
[posix]: http://pubs.opengroup.org/onlinepubs/9699919799/
[posix-makefile]: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/make.html#tag_20_76_13
[spf-format]: http://specs.os.libiquity.com/spf-2.0/overview.html#files-format
[expat]: http://expat.sourceforge.net/
[spf-upstream-source]: http://specs.os.libiquity.com/spf-2.0/overview.html#files-src-src-ver-tar-ext
[dpm-control]: http://www.debian.org/doc/debian-policy/ch-controlfields.html#s-controlsyntax
[spf-fields-src]: http://specs.os.libiquity.com/spf-2.0/fields.html#fields-src
[rfc-5322-3.4]: https://tools.ietf.org/html/rfc5322#section-3.4
[spf-changelog]: http://specs.os.libiquity.com/spf-2.0/metadata.html#changelog
[spf-build]: http://specs.os.libiquity.com/spf-2.0/buildsys.html#build
[no-op]: https://en.wiktionary.org/wiki/no-op
[spf-work-area]: http://specs.os.libiquity.com/spf-2.0/buildsys.html#work-area
[posix-colon]: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#colon