]> git.donarmstrong.com Git - debhelper.git/blob - doc/PROGRAMMING
Make it possible to pass perl code to autoscript.
[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:
50 ------------------
51
52 Dh_Lib 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 Use Dh_Lib like this:
58
59 use Debian::Debhelper::Dh_Lib
60 init();
61
62 The init() function causes Dh_lib to parse the command line and do some other
63 initialization tasks.
64
65 Argument processing:
66 -------------------
67
68 All debhelper programs should respond to certain arguments, such as -v, -i,
69 -a, and -p. To help you make this work right, Dh_Lib.pm handles argument
70 processing. Just call init().
71
72 You can add support for additional options to your command by passing an
73 options hash to init(). The hash is then passed on the Getopt::Long to
74 parse the command line options. For example, to add a --foo option, which
75 sets $dh{FOO}:
76
77 init(options => { foo => \$dh{FOO} });
78
79 After argument processing, some global variables are used to hold the
80 results; programs can use them later. These variables are elements of the
81 %dh hash.
82
83 switch          variable        description
84 -v              VERBOSE         should the program verbosely output what it is
85                                 doing?
86 --no-act        NO_ACT          should the program not actually do anything?
87 -i,-a,-p,-N     DOPACKAGES      a space delimited list of the binary packages
88                                 to act on (in Dh_Lib.pm, this is an array)
89 -i              DOINDEP         set if we're acting on binary independent
90                                 packages
91 -a              DOARCH          set if we're acting on binary dependent
92                                 packages
93 -n              NOSCRIPTS       if set, do not make any modifications to the 
94                                 package's postinst, postrm, etc scripts.
95 -o              ONLYSCRIPTS     if set, only make modifications to the
96                                 package's scripts, but don't look for or
97                                 install associated files.
98 -X              EXCLUDE         exclude a something from processing (you
99                                 decide what this means for your program)
100                                 (This is an array)
101 -X              EXCLUDE_FIND    same as EXCLUDE, except all items are put
102                                 into a string in a way that they will make
103                                 find find them. (Use ! in front to negate
104                                 that, of course) Note that this should
105                                 only be used inside complex_doit(), not in
106                                 doit().
107 -d              D_FLAG          you decide what this means to your program
108 -k              K_FLAG          used to turn on keeping of something
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 -V              V_FLAG          will be set to a string, you decide what it
115                                 means to your program
116 -V              V_FLAG_SET      will be 1 if -V was specified, even if no
117                                 parameters were passed along with the -V
118 -A              PARAMS_ALL      generally means that additional command line
119                                 parameters passed to the program (other than
120                                 those processed here), will apply to all 
121                                 binary packages the program acts on, not just
122                                 the first
123 --priority      PRIORITY        will be set to a number
124 --mainpackage   MAINPACKAGE     controls which package is treated as the
125                                 main package to act on
126 --name          NAME            a name to use for installed files, instead of
127                                 the package name
128 --error-handler ERROR_HANDLER   a function to call on error
129
130 Any additional command line parameters that do not start with "-" will be 
131 ignored, and you can access them later just as you normally would.
132
133 Global variables:
134 ----------------
135
136 The following keys are also set in the %dh hash when you call init():
137
138 MAINPACKAGE     the name of the first binary package listed in
139                 debian/control
140 FIRSTPACKAGE    the first package we were instructed to act on. This package
141                 typically gets special treatment; additional arguments
142                 specified on the command line may effect it.
143
144 Functions:
145 ---------
146
147 Dh_Lib.pm also contains a number of functions you may find useful.
148
149 doit(@command)
150         Pass this function an array that is a 
151         shell command. It will run the command (unless $dh{NO_ACT} is set), and
152         if $dh{VERBOSE} is set, it will also output the command to stdout. You
153         should use this function for almost all commands your program performs
154         that manipulate files in the package build directories.
155 complex_doit($command)
156         Pass this function a string that is a shell command, it will run it
157         similarly to how doit() does. You can pass more complicated commands 
158         to this (ie, commands involving piping redirection), however, you 
159         have to worry about things like escaping shell metacharacters.
160 verbose_print($message)
161         Pass this command a string, and it will echo it if $dh{VERBOSE} is set.
162 error($errormsg)
163         Pass this command a string, it will output it to standard error and
164         exit.
165 warning($message)
166         Pass this command a string, and it will output it to standard error
167         as a warning message.
168 tmpdir($dir)
169         Pass this command the name of a binary package, it will return the
170         name of the tmp directory that will be used as this package's
171         package build directory. Typically, this will be "debian/package".
172 compat($num)
173         Pass this command a number, and if the current compatibility level
174         is less than or equal to that number, it will return true.
175         Looks at DH_COMPAT to get the compatibility level.
176 pkgfile($package, $basename)
177         Pass this command the name of a binary package, and the base name of a
178         file, and it will return the actual filename to use. This is used
179         for allowing debhelper programs to have configuration files in the
180         debian/ directory, so there can be one config file per binary
181         package. The convention is that the files are named
182         debian/package.filename, and debian/filename is also allowable for
183         the $dh{MAINPACKAGE}. If the file does not exist, nothing is returned.
184 pkgext($package)
185         Pass this command the name of a binary package, and it will return
186         the name to prefix to files in debian/ for this package. For the
187         $dh{MAINPACKAGE}, it returns nothing (there is no prefix), for the other
188         packages, it returns "package.".
189 isnative($package)
190         Pass this command the name of a package, it returns 1 if the package
191         is a native debian package.
192         As a side effect, $dh{VERSION} is set to the version number of the
193         package.
194 autoscript($package, $scriptname, $snippetname, $sedcommands || $sub)
195         Pass parameters:
196          - binary package to be affected
197          - script to add to
198          - filename of snippet
199          - (optional) EITHER sed commands to run on the snippet. Ie,
200            s/#PACKAGE#/$PACKAGE/ Note: Passed to the shell inside double
201            quotes.
202            OR a perl sub to invoke with $_ set to each line of the snippet in
203            turn.
204         This command automatically adds shell script snippets to a debian
205         maintainer script (like the postinst or prerm).
206         Note that in v6 mode and up, the snippets are added in reverse
207         order for the removal scripts.
208 dirname($pathname)
209         Return directory part of pathname.
210 basename($pathname)
211         Return base of pathname,
212 addsubstvar($package, $substvar, $deppackage, $verinfo, $remove)
213         This function adds a dependency on some package to the specified
214         substvar in a package's substvar's file. It needs all these
215         parameters:
216         - binary package that gets the item
217         - name of the substvar to add the item to
218         - the package that will be depended on
219         - version info for the package (optional) (ie: ">= 1.1")
220         - if this last parameter is passed, the thing that would be added
221           is removed instead. This can be useful to ensure that a debhelper
222           command is idempotent. (However, we generally don't bother,
223           and rely on the user calling dh_prep.) Note that without this
224           parameter, if you call the function twice with the same values it
225           will only add one item to the substvars file.
226 delsubstvar($package, $substvar)
227         This function removes the entire line for the substvar from the
228         package's shlibs file.
229 excludefile($filename)
230         This function returns true if -X has been used to ask for the file
231         to be excluded.
232 is_udeb($package)
233         Returns true if the package is marked as a udeb in the control
234         file.
235 udeb_filename($package)
236         Returns the filename of the udeb package.
237 getpackages($type)
238         Returns a list of packages in the control file.
239         Pass "arch" or "indep" to specify arch-dependent or
240         -independent. If nothing is specified, returns all
241         packages (including packages that are not built
242         for this architecture). Pass "both" to get the union
243         of "arch" and "indep" packages.
244         As a side effect, populates %package_arches and %package_types with
245         the types of all packages (not only those returned).
246 inhibit_log()
247         Prevent logging the program's successful finish to
248         debian/*debhelper.log
249 load_log($package, $hashref)
250         Loads the log file for the given package and returns a list of
251         logged commands.
252         (Passing a hashref also causes it to populate the hash.)
253 write_log($cmd, $package ...)
254         Writes the log files for the specified package(s), adding
255         the cmd to the end.
256
257 Sequence Addons:
258 ---------------
259
260 The dh(1) command has a --with <addon> parameter that ca be used to load
261 a sequence addon module named Debian::Debhelper::Sequence::<addon>. 
262 These modules can add/remove commands to the dh command sequences, by
263 calling some functions from Dh_Lib:
264
265 insert_before($existing_command, $new_command)
266         Insert $new_command in sequences before $existing_command
267
268 insert_after($existing_command, $new_command)
269         Insert $new_command in sequences after $existing_command
270
271 remove_command($existing_command)
272         Remove $existing_command from the list of commands to run
273         in all sequences.
274
275 add_command($new_command, $sequence)
276         Add $new_command to the beginning of the specified sequence.
277         If the sequence does not exist, it will be created.
278
279 add_command_options($command, $opt1, $opt2, ...)
280         Append $opt1, $opt2 etc. to the list of additional options which
281         dh passes when running the specified $command. These options are
282         not relayed to debhelper commands called via $command override.
283
284 remove_command_options($command)
285         Clear all additional $command options previously added with
286         add_command_options().
287
288 remove_command_options($command, $opt1, $opt2, ...)
289         Remove $opt1, $opt2 etc. from the list of additional options which
290         dh passes when running the specified $command.
291
292 Buildsystem Classes:
293 -------------------
294
295 The dh_auto_* commands are frontends that use debhelper buildsystem
296 classes. These classes have names like Debian::Debhelper::Buildsystem::foo,
297 and are derived from Debian::Debhelper::Buildsystem, or other, related
298 classes.
299
300 A buildsystem class needs to inherit or define these methods: DESCRIPTION,
301 check_auto_buildable, configure, build, test, install, clean. See the comments
302 inside Debian::Debhelper::Buildsystem for details. Note that this interface
303 is still subject to change.
304
305 Note that third-party buildsystems will not automatically be used by default,
306 but can be forced to be used via the --buildsystem parameter.
307
308 -- Joey Hess <joeyh@debian.org>