3 # Library functions for debhelper programs, perl version.
5 # Joey Hess, GPL copyright 1997-2000.
7 package Debian::Debhelper::Dh_Lib;
11 use vars qw(@ISA @EXPORT %dh);
13 @EXPORT=qw(&init &doit &complex_doit &verbose_print &error &warning &tmpdir
14 &pkgfile &pkgext &isnative &autoscript &filearray &GetPackages
20 # If DH_OPTIONS is set, prepend it @ARGV.
21 if (defined($ENV{DH_OPTIONS})) {
22 unshift @ARGV,split(/\s+/,$ENV{DH_OPTIONS});
25 # Check to see if an argument on the command line starts with a dash.
26 # if so, we need to pass this off to the resource intensive
27 # Getopt::Long, which I'd prefer to avoid loading at all if possible.
30 foreach $arg (@ARGV) {
37 eval "use Debian::Debhelper::Dh_Getopt";
39 %dh=Debian::Debhelper::Dh_Getopt::parseopts();
42 # Check to see if DH_VERBOSE environment variable was set, if so,
43 # make sure verbose is on.
44 if (defined $ENV{DH_VERBOSE} && $ENV{DH_VERBOSE} ne "") {
48 # Check to see if DH_NO_ACT environment variable was set, if so,
49 # make sure no act mode is on.
50 if (defined $ENV{DH_NO_ACT} && $ENV{DH_NO_ACT} ne "") {
54 # Get the name of the main binary package (first one listed in
56 my @allpackages=GetPackages();
57 $dh{MAINPACKAGE}=$allpackages[0];
59 # Check if packages to build have been specified, if not, fall back to
60 # the default, doing them all.
61 if (! defined $dh{DOPACKAGES} || ! @{$dh{DOPACKAGES}}) {
62 if ($dh{DOINDEP} || $dh{DOARCH} || $dh{DOSAME}) {
63 # User specified that all arch (in)dep package be
64 # built, and there are none of that type.
65 error("I have no package to act on");
67 push @{$dh{DOPACKAGES}},@allpackages;
70 # Check to see if -P was specified. If so, we can only act on a single
72 if ($dh{TMPDIR} && $#{$dh{DOPACKAGES}} > 0) {
73 error("-P was specified, but multiple packages would be acted on (".join(",",@{$dh{DOPACKAGES}}).").");
76 # Figure out which package is the first one we were instructed to build.
77 # This package gets special treatement: files and directories specified on
78 # the command line may affect it.
79 $dh{FIRSTPACKAGE}=${$dh{DOPACKAGES}}[0];
81 # Split the U_PARAMS up into an array.
85 push @{$dh{U_PARAMS}}, split(/\s+/,$u);
89 # Escapes out shell metacharacters in a line of shell script.
92 # This list is from _Unix in a Nutshell_. (except '#')
93 $line=~s/([\s!"\$()*+#;<>?@\[\]\\`|~])/\\$1/g;
97 # Run a command, and display the command to stdout if verbose mode is on.
98 # All commands that modifiy files in $TMP should be ran via this
101 # Note that this cannot handle complex commands, especially anything
102 # involving redirection. Use complex_doit instead.
104 verbose_print(join(" ",map { escape_shell($_) } @_));
107 system(@_) == 0 || error("command returned error code");
111 # Run a command and display the command to stdout if verbose mode is on.
112 # Use doit() if you can, instead of this function, because this function
113 # forks a shell. However, this function can handle more complicated stuff
116 verbose_print(join(" ",@_));
119 # The join makes system get a scalar so it forks off a shell.
120 system(join(" ",@_)) == 0
121 || error("command returned error code");
125 # Run a command that may have a huge number of arguments, like xargs does.
126 # Pass in a reference to an array containing the arguments, and then other
127 # parameters that are the command and any parameters that should be passed to
132 # The kernel can accept command lines up to 20k worth of characters.
133 my $command_max=20000; # LINUX SPECIFIC!!
135 # Figure out length of static portion of command.
138 $static_length+=length($_)+1;
142 my $length=$static_length;
144 if (length($_) + 1 + $static_length > $command_max) {
145 error("This command is greater than the maximum command size allowed by the kernel, and cannot be split up further. What on earth are you doing? \"@_ $_\"");
147 $length+=length($_) + 1;
148 if ($length < $command_max) {
152 doit(@_,@collect) if $#collect > -1;
154 $length=$static_length + length($_) + 1;
157 doit(@_,@collect) if $#collect > -1;
160 # Print something if the verbose flag is on.
165 print "\t$message\n";
169 # Output an error message and exit.
181 print STDERR basename($0).": $message\n";
184 # Returns the basename of the argument passed to it.
188 $fn=~s:^.*/(.*?)$:$1:;
192 # Returns the directory name of the argument passed to it.
196 $fn=~s:^(.*)/.*?$:$1:;
200 # Pass in a number, will return true iff the current compatability level
201 # is less than or equal to that number.
206 if (defined $ENV{DH_COMPAT}) {
210 if ($c > $max_compat) {
211 error("Sorry, but $max_compat is the highest compatability level of debhelper currently supported.");
217 # Pass it a name of a binary package, it returns the name of the tmp dir to
218 # use, for that package.
225 elsif (compat(1) && $package eq $dh{MAINPACKAGE}) {
226 # This is for back-compatability with the debian/tmp tradition.
230 return "debian/$package";
234 # Pass this the name of a binary package, and the name of the file wanted
235 # for the package, and it will return the actual existing filename to use.
237 # It tries several filenames:
238 # * debian/package.filename.buildarch
239 # * debian/package.filename
240 # * debian/file (if the package is the main package)
245 if (-f "debian/$package.$filename.".buildarch()) {
246 return "debian/$package.$filename.".buildarch();
248 elsif (-f "debian/$package.$filename") {
249 return "debian/$package.$filename";
251 elsif ($package eq $dh{MAINPACKAGE} && -f "debian/$filename") {
252 return "debian/$filename";
259 # Pass it a name of a binary package, it returns the name to prefix to files
260 # in debian for this package.
264 if ($package ne $dh{MAINPACKAGE}) {
270 # Returns 1 if the package is a native debian package, null otherwise.
271 # As a side effect, sets $dh{VERSION} to the version of this package.
273 # Caches return code so it only needs to run dpkg-parsechangelog once.
279 return $isnative_cache{$package} if defined $isnative_cache{$package};
281 # Make sure we look at the correct changelog.
282 my $isnative_changelog=pkgfile($package,"changelog");
283 if (! $isnative_changelog) {
284 $isnative_changelog="debian/changelog";
286 # Get the package version.
287 my $version=`dpkg-parsechangelog -l$isnative_changelog`;
288 ($dh{VERSION})=$version=~m/Version:\s*(.*)/m;
289 # Did the changelog parse fail?
290 if (! defined $dh{VERSION}) {
291 error("changelog parse failure");
294 # Is this a native Debian package?
295 if ($dh{VERSION}=~m/.*-/) {
296 return $isnative_cache{$package}=0;
299 return $isnative_cache{$package}=1;
304 # Automatically add a shell script snippet to a debian script.
305 # Only works if the script has #DEBHELPER# in it.
309 # 2: script to add to
310 # 3: filename of snippet
311 # 4: sed to run on the snippet. Ie, s/#PACKAGE#/$PACKAGE/
318 # This is the file we will append to.
319 my $outfile="debian/".pkgext($package)."$script.debhelper";
321 # Figure out what shell script snippet to use.
323 if (defined($ENV{DH_AUTOSCRIPTDIR}) &&
324 -e "$ENV{DH_AUTOSCRIPTDIR}/$filename") {
325 $infile="$ENV{DH_AUTOSCRIPTDIR}/$filename";
328 if (-e "/usr/share/debhelper/autoscripts/$filename") {
329 $infile="/usr/share/debhelper/autoscripts/$filename";
332 error("/usr/share/debhelper/autoscripts/$filename does not exist");
336 complex_doit("echo \"# Automatically added by ".basename($0)."\">> $outfile");
337 complex_doit("sed \"$sed\" $infile >> $outfile");
338 complex_doit("echo '# End automatically added section' >> $outfile");
341 # Reads in the specified file, one word at a time, and returns an array of
342 # the result. If a value is passed in as the second parameter, then glob
343 # expansion is done in the directory specified by the parameter ("." is
344 # frequently a good choice).
350 open (DH_FARRAY_IN, $file) || error("cannot read $file: $1");
351 while (<DH_FARRAY_IN>) {
352 # Only do glob expansion in v3 mode.
354 # The tricky bit is that the glob expansion is done
355 # as if we were in the specified directory, so the
356 # filenames that come out are relative to it.
357 if (defined $globdir && compat(3)) {
358 for (map { glob "$globdir/$_" } split) {
373 # Returns the build architecture. (Memoized)
378 return $arch if defined $arch;
380 $arch=`dpkg --print-architecture` || error($!);
386 # Returns a list of packages in the control file.
387 # Must pass "arch" or "indep" or "same" to specify arch-dependant or
388 # -independant or same arch packages. If nothing is specified, returns all
393 $type="" if ! defined $type;
395 # Look up the build arch if we need to.
397 if ($type eq 'same') {
398 $buildarch=buildarch();
404 open (CONTROL,"<debian/control") ||
405 error("cannot read debian/control: $!\n");
409 if (/^Package:\s*(.*)/) {
412 if (/^Architecture:\s*(.*)/) {
415 if (!$_ or eof) { # end of stanza.
417 (($type eq 'indep' && $arch eq 'all') ||
418 ($type eq 'arch' && $arch ne 'all') ||
419 ($type eq 'same' && ($arch eq 'any' || $arch =~ /\b$buildarch\b/)) ||
421 push @list, $package;