]> git.donarmstrong.com Git - debhelper.git/blob - doc/PROGRAMMING
r1750: document dh_clean removal
[debhelper.git] / doc / PROGRAMMING
1 This file documents things you should know to write a new debhelper program.
2 Any program with a name that begins with dh_ should conform to these
3 guidelines (with the historical exception of dh_make).
4
5 Standardization:
6 ---------------
7
8 There are lots of debhelper commands. To make the learning curve shallower,
9 I want them all to behave in a standard manner:
10
11 All debhelper programs have names beginning with "dh_". This is so we don't
12 pollute the name space too much.
13
14 Debhelper programs should never output anything to standard output except
15 error messages, important warnings, and the actual commands they run that
16 modify files under debian/ (this last only if they are passed -v, and if you
17 output the commands, you should indent them with 1 tab). This is so we don't
18 have a lot of noise output when all the debhelper commands in a debian/rules
19 are run, so the important stuff is clearly visible.
20
21 Debhelper programs should accept all options listed in the "SHARED
22 DEBHELPER OPTIONS" section of debhelper(7), including any long forms of
23 these options, like --verbose . If necessary, the options may be ignored.
24
25 If debhelper commands need config files, they should use
26 debian/package.filename as the name of the config file (replace filename
27 with whatever your command wants), and debian/filename should also be
28 checked for config information for the first binary package in
29 debian/control. Also, debhelper commands should accept the same sort of
30 information that appears in the config files, on their command lines, if
31 possible, and apply that information to the first package they act on.
32 The config file format should be as simple as possible, generally just a
33 list of files to act on.
34
35 Debhelper programs should never modify the debian/postinst, debian/prerm,
36 etc scripts. Instead, they can add lines to debian/postinst.debhelper, etc. 
37 The autoscript() function (see below) is one easy way to do this.
38 dh_installdeb is an exception, it will run after the other commands and
39 merge these modifications into the actual postinst scripts.
40
41 In general, files named debian/*.debhelper are internal to debhelper, and
42 their existence or use should not be relied on by external programs such as
43 the build process of a package. These files will be deleted by dh_clean.
44
45 Debhelper programs should default to doing exactly what policy says to do.
46
47 There are always exceptions. Just ask me.
48
49 Introducing Dh_Lib.pm:
50 ---------------------
51
52 Dh_Lib.pm is the library used by all debhelper programs to parse their
53 arguments and set some useful variables. It's not mandatory that your
54 program use Dh_Lib.pm, but it will make it a lot easier to keep it in sync
55 with the rest of debhelper if it does, so this is highly encouraged.
56
57 (There used to be a version of Dh_lib.pm that was a library of functions for
58 shell scripts. If you want to write a debhelper command that is a shell
59 script, I can dig up that old library for you. Only the perl one is
60 supported now, though.)
61
62 Use Dh_Lib.pm like this:
63
64 use Debian::Debhelper::Dh_Lib
65 init();
66
67 The BEGIN block is there to make perl look for the module in all the right
68 places.
69
70 The init() function causes Dh_lib to parse the command line and do some other
71 initialization tasks.
72
73 Argument processing:
74 -------------------
75
76 All debhelper programs should respond to certain arguments, such as -v, -i,
77 -a, and -p. To help you make this work right, Dh_Lib.pm handles argument
78 processing. Just call init().
79
80 After argument processing, some global variables are used to hold the
81 results; programs can use them later. These variables are elements of the
82 %dh hash.
83
84 switch          variable        description
85 -v              VERBOSE         should the program verbosely output what it is
86                                 doing?
87 --no-act        NO_ACT          should the program not actually do anything?
88 -i,-a,-p,-N     DOPACKAGES      a space delimited list of the binary packages
89                                 to act on (in Dh_Lib.pm, this is an array)
90 -i              DOINDEP         set if we're acting on binary independent
91                                 packages
92 -a              DOARCH          set if we're acting on binary dependent
93                                 packages
94 -n              NOSCRIPTS       if set, do not make any modifications to the 
95                                 package's postinst, postrm, etc scripts.
96 -X              EXCLUDE         exclude a something from processing (you
97                                 decide what this means for your program)
98                                 (This is an array)
99                 EXCLUDE_FIND    same as DH_EXCLUDE, except all items are put
100                                 into a string in a way that they will make
101                                 find find them. (Use ! in front to negate
102                                 that, of course)
103 -x              INCLUDE_CONFFILES
104                                 include conffiles. It's -x for obscure
105                                 historical reasons.
106 -d              D_FLAG          you decide what this means to your program
107 -r              R_FLAG          you decide what this means to your program
108 -k              K_FLAG          you decide what this means to your program
109 -P              TMPDIR          package build directory (implies only one
110                                 package is being acted on)
111 -u              U_PARAMS        will be set to a string, that is typically
112                                 parameters your program passes on to some
113                                 other program. (This is an array)
114 -m              M_PARAMS        will be set to a string, you decide what it
115                                 means to your program
116 -l              L_PARAMS        will be set to a string, you decide what it
117                                 means to your program
118 -V              V_FLAG          will be set to a string, you decide what it
119                                 means to your program
120 -V              V_FLAG_SET      will be 1 if -V was specified, even if no
121                                 parameters were passed along with the -V
122 -A              PARAMS_ALL      generally means that additional command line
123                                 parameters passed to the program (other than
124                                 those processed here), will apply to all 
125                                 binary packages the program acts on, not just
126                                 the first
127 --init-script   INIT_SCRIPT     will be set to a string, which specifies an
128                                 init script name (probably only
129                                 dh_installinit will ever use this)
130 --sourcedir     SOURCEDIR       will be set to a string (probably only
131                                 dh_movefiles will ever use this)
132 --destdir       DESTDIR         will be set to a string (probably only
133                                 dh_builddeb will ever use this)
134 --filename      FILENAME        will be set to a string
135 --flavor        FLAVOR          will be set to a string (probably only
136                                 dh_installemacsen will ever use this)
137 --number        PRIORITY        will be set to a number (deprecated)
138 --priority      PRIORITY        will be set to a number
139 --name          NAME            a name to use for installed files, instead of
140                                 the package name
141 --error-handler ERROR_HANDLER   a function to call on error
142
143 Any additional command line parameters that do not start with "-" will be 
144 ignored, and you can access them later just as you normally would.
145
146 If you need a new command line option, just ask me, and I will add it.
147
148 Global variables:
149 ----------------
150
151 The following keys are also set in the %dh hash when you call init():
152
153 MAINPACKAGE     the name of the first binary package listed in
154                 debian/control
155 FIRSTPACKAGE    the first package we were instructed to act on. This package
156                 typically gets special treatment; additional arguments
157                 specified on the command line may effect it.
158
159 Functions:
160 ---------
161
162 Dh_Lib.pm also contains a number of functions you may find useful.
163
164 doit(@command)
165         Pass this function an array that is a 
166         shell command. It will run the command (unless $dh{NO_ACT} is set), and
167         if $dh{VERBOSE} is set, it will also output the command to stdout. You
168         should use this function for almost all commands your program performs
169         that manipulate files in the package build directories.
170 complex_doit($command)
171         Pass this function a string that is a shell command, it will run it
172         similarly to how doit() does. You can pass more complicated commands 
173         to this (ie, commands involving piping redirection), however, you 
174         have to worry about things like escaping shell metacharacters.
175 verbose_print($message)
176         Pass this command a string, and it will echo it if $dh{VERBOSE} is set.
177 error($errormsg)
178         Pass this command a string, it will output it to standard error and
179         exit.
180 warning($message)
181         Pass this command a string, and it will output it to standard error
182         as a warning message.
183 tmpdir($dir)
184         Pass this command the name of a binary package, it will return the
185         name of the tmp directory that will be used as this package's
186         package build directory. Typically, this will be "debian/package".
187 compat($num)
188         Pass this command a number, and if the current compatibility level
189         is less than or equal to that number, it will return true.
190         Looks at DH_COMPAT to get the compatibility level.
191 pkgfile($package, $basename)
192         Pass this command the name of a binary package, and the base name of a
193         file, and it will return the actual filename to use. This is used
194         for allowing debhelper programs to have configuration files in the
195         debian/ directory, so there can be one config file per binary
196         package. The convention is that the files are named
197         debian/package.filename, and debian/filename is also allowable for
198         the $dh{MAINPACKAGE}. If the file does not exist, nothing is returned.
199 pkgext($package)
200         Pass this command the name of a binary package, and it will return
201         the name to prefix to files in debian/ for this package. For the
202         $dh{MAINPACKAGE}, it returns nothing (there is no prefix), for the other
203         packages, it returns "package.".
204 isnative($package)
205         Pass this command the name of a package, it returns 1 if the package
206         is a native debian package.
207         As a side effect, $dh{VERSION} is set to the version number of the
208         package.
209 autoscript($package, $scriptname, $snippetname, $sedcommands)
210         Pass parameters:
211          - binary package to be affected
212          - script to add to
213          - filename of snippet
214          - sed commands to run on the snippet. Ie, s/#PACKAGE#/$PACKAGE/
215            (optional)
216         This command automatically adds shell script snippets to a debian
217         maintainer script (like the postinst or prerm).
218 dirname($pathname)
219         Return directory part of pathname.
220 basename($pathname)
221         Return base of pathname,
222 addsubstvar($package, $substvar, $deppackage, $verinfo, $remove)
223         This function adds a dependency on some package to the specified
224         substvar in a package's substvar's file. It needs all these
225         parameters:
226         - binary package that gets the item
227         - name of the substvar to add the item to
228         - the package that will be depended on
229         - version info for the package (optional) (ie: ">= 1.1")
230         - if this last parameter is passed, the thing that would be added
231           is removed instead. This can be useful to ensure that a debhelper
232           command is idempotent. Note that without this parameter, if you
233           call the function twice with the same values it will only add one
234           item to the substvars file.
235 delsubstvar($package, $substvar)
236         This function removes the entire line for the substvar from the
237         package's shlibs file.
238 excludefile($filename)
239         This function returns true if -X has been used to ask for the file
240         to be excluded.
241 is_udeb($package)
242         Returns true if the package is marked as a udeb in the control
243         file.
244 udeb_filename($package)
245         Returns the filename of the udeb package.
246
247 -- Joey Hess <joeyh@debian.org>