Warning:This article is out of date and should not be used for creating new packages. Please refer to the Debian New Maintainers Guide instead.

Making a Debian Package

Let's take a little detour from the debian directory and cd up one directory to take a look at Empires' Makefile. This is the script make(1) will use to automate building this program.

1  # %W% %G% %U% - (c) Copyright 1987, 1988 Chuck Simmons
2  
3  #
4  # Copyright (C) 1987, 1988 Chuck Simmons
5  #
6  # See the file COPYING, distributed with empire, for restriction
7  # and warranty information.
8  
9  # Change the line below for your system. If you are on a Sun or Vax,
10 # you may want BSD.
11 
12 SYS = BSD
13 #SYS = SYSV
14 
15 # Use -g to compile the program for debugging.
16 
17 DEBUG = -g -DDEBUG
18 #DEBUG = -O
19 
20 # Use -p to profile the program.
21 #PROFILE = -p -DPROFILE
22 PROFILE =
23 
24 # Define all necessary libraries. 'curses' is necessary. 'termcap'
25 # is needed on BSD systems.
26 LIBS = -ldcurses
27 #LIBS = -lcurses -ltermcap
28 
29 # You shouldn't have to modify anything below this line.
30 
31 CFLAGS = $(DEBUG) $(PROFILE) -D$(SYS)
32 INS = /etc/install
33 
34 FILES = \
35     attack.c \
36     compmove.c \
37     data.c \
38     display.c \
39     edit.c \
40     empire.c \
41     game.c \
42     main.c \
43     map.c \
44     math.c \
45     object.c \
46     term.c \
47     usermove.c \
48     util.c
49 
50 HEADERS = empire.h extern.h
51 
52 OFILES = \
53     attack.o \
54     compmove.o \
55     data.o \
56     display.o \
57     edit.o \
58     empire.o \
59     game.o \
60     main.o \
61     map.o \
62     math.o \
63     object.o \
64     term.o \
65     usermove.o \
66     util.o
67 
68 all: empire
69 
70 empire: $(OFILES)
71     $(CC) $(PROFILE) -o empire $(OFILES) $(LIBS)
72 
73 TAGS: $(HEADERS) $(FILES)
74     etags $(HEADERS) $(FILES)
75 
76 lint: $(FILES)
77     lint -u -D$(SYS) $(FILES) -lcurses
78 
79 clean:
80     rm -f *.o TAGS
81 
82 clobber: clean
83     rm -f empire empire.tar*
84 
85 install: empire
86     $(INS) -o -f /usr/local/games empire
87 
88 installman: empire.6
89     $(INS) -f /usr/local/man/man6 empire.6
90 
91 SOURCES = READ.ME empire.6 COPYING Makefile BUGS $(FILES) $(HEADERS) MANIFEST empire.lsm
92 
93 empire.tar: $(SOURCES)
94     tar -cvf empire.tar $(SOURCES)
95 empire.tar.gz: empire.tar
96     gzip empire.tar
97 
98 empire.shar: $(SOURCES)
99     shar $(SOURCES) >empire.shar

Hopefully, in your package you will have a nice, well-created makefile and you won't have to do anything but my experience is you always have to tweak them a bit to get them working right.  There isn't space here to go into all the details of fixing makefiles but here are a few problems you often run accross.

Take a look at lines 12 and 13.  There are two main variants of Unix, System V and BSD.  Linux has elements of both but in general you want to choose System 5.  So we'll comment out line 12 and uncomment line 13.  We also don't want the debug options.

12 #SYS = BSD
13 SYS = SYSV
17 #DEBUG = -g -DDEBUG
18 DEBUG = -O

The biggest one is forgetting that lines within make targets such as 35-48 have to be indented with a tab.  Not spaces.  Often when you cut and paste tabs get replaced by spaces and this will give you strange error messages.

Libraries are often different from platform to platform.  Line 26 contains a reference to dcurses which doesn't exist on Debian.  We need to change this to ncurses which does.

26 LIBS = -lncurses
27 #LIBS = -lcurses -ltermcap

Another is program locations.  For example line 32 specifies install(1) is in /etc.  In Debian, that program is in /usr/bin.so we need to edit that line.

32  INS = /usr/bin/install

Debian has strict guidelines on where programs are to be installed.  This is specified in /usr/doc/debian-policy/fsstnd.  To follow guidelines we should install the binary in /usr/games instead of /usr/local/games, and the man page in /usr/man/man6 instead of /usr/local/man/man6.  there's going to be another problem too.   Debians' install has different options to the one on the system Empire's author was using.  Briefly, the -o option specifies the owner, -g the group and -m the permissions  for each file.  The policy manual specifies what they should be.   On binary files we also use the -s option to strip them.  This makes them smaller so they will use less memory and disk space. Look at the man page for install for full details.

We change lines 85-89 as follows (the reason for the debian/tmp prefix to paths will be described below):

85 install: empire
86     $(INS) -o root -g root -m 0755 -s empire debian/tmp/usr/games/empire
87 
88 installman: empire.6
89     $(INS) -o root -g root -m 0644 empire.6 debian/tmp/usr/man/man6/empire.6

Many makefiles have a macro called DESTDIR which specifies the directory to install to.   If it's available, you should set it to debian/tmp.

If you're really unlucky you'll have to patch the source itself.  When you make changes like this, do it in a non-destructive way so someone on another platform doesn't have to undo your changes.   Protect your changes with something like this:

#ifdef DEBIAN...#endif

to mark off your changes.   Whenever you make changes, be sure to send them to the original author or maintainer so they can be included as a standard part of the next revision.

rules
Now we go back to the debian directory to take a look at rules which dpkg-dev will use to actually create the package. This is what debmake gives us as a default rules file.

1  #!/usr/bin/make -f
2  # Made with the aid of debmake, by Christoph Lameter,
3  # based on the sample debian/rules file for GNU hello by Ian Jackson.
4 
5  package=empire
6
7  build:
8     $(checkdir)
9     
10    make CFLAGS="-O2 -g -Wall"
11 
12    touch build
13
14 clean:
15    $(checkdir)
16    -rm -f build
17    -make clean
18    -rm -f `find . -name "*~"`
19    -rm -rf debian/tmp debian/files* core debian/substvars
20
21 binary-indep:    checkroot build
22    $(checkdir)
23 # There are no architecture-independent files to be uploaded
24 # generated by this package. If there were any they would be
25 # made here.
26 
27 binary-arch:    checkroot build
28    $(checkdir)
29    -rm -rf debian/tmp
30    install -d debian/tmp
31    cd debian/tmp && install -d `cat ../dirs`
32    make install DESTDIR=`pwd`/debian/tmp
33 # Must have debmake installed for this to work. Otherwise please copy
34 # /usr/bin/debstd into the debian directory and change debstd to debian/debstd
35    debstd BUGS 
36    dpkg-gencontrol
37    chown -R root.root debian/tmp
38    chmod -R go=rX debian/tmp
39    dpkg --build debian/tmp ..
40 
41 define checkdir
42    test -f debian/rules
43 endef
44 
45 # Below here is fairly generic really
46 
47 binary:        binary-indep binary-arch
48 
49 checkroot:
50    $(checkdir)
51    test root = "`whoami`"
52
53 .PHONY: binary binary-arch binary-indep clean checkroot

You are probably familiar with lines like line 1 from shell and perl scripts. It   means this file is to be run through make.  Line 10 runs the applications own makefile.  The CFLAGS portion of this line gives default compiler options just in case  the applications makefile is deficient.  We don't have to worry about that (See line 31 of Empires' makefile) so we'll just comment that out

10    make

Things rarely work perfectly the first time so lines 14-20 clean up any unneeded junk left over from previous failed attempts. 

As the comments suggest, lines 20-26 are used to build architecture independant files.

Lines 27-40 are the heart of the rules file.  A directory called tmp is created in the debian directory.  (if it already exists it will be removed first so you can start afresh.)  The package will be assembled here.  This is why in empires makefile we specified debian/tmp as the root installation directory.  In line 31 the directories we will be installing into will be created.  They are taken from the dirs file which will be discussed below.  Line 32 runs the install target from Empires' makefile.  Line 35 runs debstd(1) which comes with debmake.  debstd is a "black box" which will do things to your package to make it conform to debian policy.  If you want the details, read the man page or take a look at the source (it's just a shell script) I encourage you to do so.  But the advantage of the black box approach is you don't have to know what it does, you can just count on it doing the right things.  When Debian policies change,  it will be transparent to you.   Just install  an updated version of debmake that supports the new policies and recompile and you will be compliant.  (For the most part.  Not all policy changes are easy to handle automatically so it pays to understand the issues thouroughly before relying on a piece of software.) Line 36 runs dpkg-gencontrol which creates the binary packages' control  file.  See deb(5) for details of the Debian package format.  Finally line 39 will actually create the package.

Lines 41 onward are just macros used by the rules file and not important to know about.

The important part to know about the rules file created by debmake is that it is just a suggestion.   It will work for simple packages but for more complicated ones, don't be afraid to add and subtract from it to fit your needs.

dirs
This file specifies the directories which our package will create.  By default, it looks like this:

1 usr/bin
2 usr/sbin

Note the preceding slash is not included.  We will change it to look like this

1 usr/games
2 usr/man/man6

Now we just need to create a few more files, and our package will be complete.


Previous page
Next Page

By Jaldhar Vyas
November 11, 1997