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
15 &basename &dirname &xargs %dh &compat &addsubstvar &delsubstvar);
20 # If DH_OPTIONS is set, prepend it @ARGV.
21 if (defined($ENV{DH_OPTIONS})) {
22 # Ignore leading/trailing whitespace.
23 $ENV{DH_OPTIONS}=~s/^\s+//;
24 $ENV{DH_OPTIONS}=~s/\s+$//;
25 unshift @ARGV,split(/\s+/,$ENV{DH_OPTIONS});
28 # Check to see if an argument on the command line starts with a dash.
29 # if so, we need to pass this off to the resource intensive
30 # Getopt::Long, which I'd prefer to avoid loading at all if possible.
33 foreach $arg (@ARGV) {
40 eval "use Debian::Debhelper::Dh_Getopt";
42 %dh=Debian::Debhelper::Dh_Getopt::parseopts();
45 # Check to see if DH_VERBOSE environment variable was set, if so,
46 # make sure verbose is on.
47 if (defined $ENV{DH_VERBOSE} && $ENV{DH_VERBOSE} ne "") {
51 # Check to see if DH_NO_ACT environment variable was set, if so,
52 # make sure no act mode is on.
53 if (defined $ENV{DH_NO_ACT} && $ENV{DH_NO_ACT} ne "") {
57 # Get the name of the main binary package (first one listed in
59 my @allpackages=GetPackages();
60 $dh{MAINPACKAGE}=$allpackages[0];
62 # Check if packages to build have been specified, if not, fall back to
63 # the default, doing them all.
64 if (! defined $dh{DOPACKAGES} || ! @{$dh{DOPACKAGES}}) {
65 if ($dh{DOINDEP} || $dh{DOARCH} || $dh{DOSAME}) {
66 error("You asked that all arch in(dep) packages be built, but there are none of that type.");
68 push @{$dh{DOPACKAGES}},@allpackages;
71 # Check to see if -P was specified. If so, we can only act on a single
73 if ($dh{TMPDIR} && $#{$dh{DOPACKAGES}} > 0) {
74 error("-P was specified, but multiple packages would be acted on (".join(",",@{$dh{DOPACKAGES}}).").");
77 # Figure out which package is the first one we were instructed to build.
78 # This package gets special treatement: files and directories specified on
79 # the command line may affect it.
80 $dh{FIRSTPACKAGE}=${$dh{DOPACKAGES}}[0];
83 # Pass it an array containing the arguments of a shell command like would
84 # be run by exec(). It turns that into a line like you might enter at the
85 # shell, escaping metacharacters and quoting qrguments that contain spaces.
90 foreach my $word (@args) {
92 # Escape only a few things since it will be quoted.
93 # Note we use double quotes because you cannot
94 # escape ' in qingle quotes, while " can be escaped
96 # This does make -V"foo bar" turn into "-Vfoo bar",
97 # but that will be parsed identically by the shell
99 $word=~s/([\n`\$"\\])/\$1/g;
100 push @ret, "\"$word\"";
103 # This list is from _Unix in a Nutshell_. (except '#')
104 $word=~s/([\s!"\$()*+#;<>?@\[\]\\`|~])/\\$1/g;
108 return join(' ', @ret);
111 # Run a command, and display the command to stdout if verbose mode is on.
112 # All commands that modifiy files in $TMP should be ran via this
115 # Note that this cannot handle complex commands, especially anything
116 # involving redirection. Use complex_doit instead.
118 verbose_print(escape_shell(@_));
121 system(@_) == 0 || error("command returned error code");
125 # Run a command and display the command to stdout if verbose mode is on.
126 # Use doit() if you can, instead of this function, because this function
127 # forks a shell. However, this function can handle more complicated stuff
130 verbose_print(join(" ",@_));
133 # The join makes system get a scalar so it forks off a shell.
134 system(join(" ",@_)) == 0
135 || error("command returned error code");
139 # Run a command that may have a huge number of arguments, like xargs does.
140 # Pass in a reference to an array containing the arguments, and then other
141 # parameters that are the command and any parameters that should be passed to
146 # The kernel can accept command lines up to 20k worth of characters.
147 my $command_max=20000; # LINUX SPECIFIC!!
148 # I could use POSIX::ARG_MAX, but that would be slow.
150 # Figure out length of static portion of command.
153 $static_length+=length($_)+1;
157 my $length=$static_length;
159 if (length($_) + 1 + $static_length > $command_max) {
160 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? \"@_ $_\"");
162 $length+=length($_) + 1;
163 if ($length < $command_max) {
167 doit(@_,@collect) if $#collect > -1;
169 $length=$static_length + length($_) + 1;
172 doit(@_,@collect) if $#collect > -1;
175 # Print something if the verbose flag is on.
180 print "\t$message\n";
184 # Output an error message and exit.
196 print STDERR basename($0).": $message\n";
199 # Returns the basename of the argument passed to it.
203 $fn=~s:^.*/(.*?)$:$1:;
207 # Returns the directory name of the argument passed to it.
211 $fn=~s:^(.*)/.*?$:$1:;
215 # Pass in a number, will return true iff the current compatibility level
216 # is less than or equal to that number.
221 if (defined $ENV{DH_COMPAT}) {
224 elsif (-e 'debian/compat') {
226 open (COMPAT_IN, "debian/compat") || die "debian/compat: $!";
231 if ($c > $max_compat) {
232 error("Sorry, but $max_compat is the highest compatibility level of debhelper currently supported.");
238 # Pass it a name of a binary package, it returns the name of the tmp dir to
239 # use, for that package.
246 elsif (compat(1) && $package eq $dh{MAINPACKAGE}) {
247 # This is for back-compatibility with the debian/tmp tradition.
251 return "debian/$package";
255 # Pass this the name of a binary package, and the name of the file wanted
256 # for the package, and it will return the actual existing filename to use.
258 # It tries several filenames:
259 # * debian/package.filename.buildarch
260 # * debian/package.filename
261 # * debian/file (if the package is the main package)
266 if (-f "debian/$package.$filename.".buildarch()) {
267 return "debian/$package.$filename.".buildarch();
269 elsif (-f "debian/$package.$filename") {
270 return "debian/$package.$filename";
272 elsif ($package eq $dh{MAINPACKAGE} && -f "debian/$filename") {
273 return "debian/$filename";
280 # Pass it a name of a binary package, it returns the name to prefix to files
281 # in debian for this package.
285 if (compat(1) and $package eq $dh{MAINPACKAGE}) {
291 # Returns 1 if the package is a native debian package, null otherwise.
292 # As a side effect, sets $dh{VERSION} to the version of this package.
294 # Caches return code so it only needs to run dpkg-parsechangelog once.
300 return $isnative_cache{$package} if defined $isnative_cache{$package};
302 # Make sure we look at the correct changelog.
303 my $isnative_changelog=pkgfile($package,"changelog");
304 if (! $isnative_changelog) {
305 $isnative_changelog="debian/changelog";
307 # Get the package version.
308 my $version=`dpkg-parsechangelog -l$isnative_changelog`;
309 ($dh{VERSION})=$version=~m/Version:\s*(.*)/m;
310 # Did the changelog parse fail?
311 if (! defined $dh{VERSION}) {
312 error("changelog parse failure");
315 # Is this a native Debian package?
316 if ($dh{VERSION}=~m/.*-/) {
317 return $isnative_cache{$package}=0;
320 return $isnative_cache{$package}=1;
325 # Automatically add a shell script snippet to a debian script.
326 # Only works if the script has #DEBHELPER# in it.
330 # 2: script to add to
331 # 3: filename of snippet
332 # 4: sed to run on the snippet. Ie, s/#PACKAGE#/$PACKAGE/
339 # This is the file we will append to.
340 my $outfile="debian/".pkgext($package)."$script.debhelper";
342 # Figure out what shell script snippet to use.
344 if (defined($ENV{DH_AUTOSCRIPTDIR}) &&
345 -e "$ENV{DH_AUTOSCRIPTDIR}/$filename") {
346 $infile="$ENV{DH_AUTOSCRIPTDIR}/$filename";
349 if (-e "/usr/share/debhelper/autoscripts/$filename") {
350 $infile="/usr/share/debhelper/autoscripts/$filename";
353 error("/usr/share/debhelper/autoscripts/$filename does not exist");
357 complex_doit("echo \"# Automatically added by ".basename($0)."\">> $outfile");
358 complex_doit("sed \"$sed\" $infile >> $outfile");
359 complex_doit("echo '# End automatically added section' >> $outfile");
362 # Removes a whole substvar line.
367 my $ext=pkgext($package);
368 my $substvarfile="debian/${ext}substvars";
370 complex_doit("grep -v '^${substvar}=' $substvarfile > $substvarfile.new || true");
371 doit("mv", "$substvarfile.new","$substvarfile");
374 # Adds a dependency on some package to the specified
375 # substvar in a package's substvar's file.
379 my $deppackage=shift;
383 my $ext=pkgext($package);
384 my $substvarfile="debian/${ext}substvars";
386 $str.=" ($verinfo)" if length $verinfo;
388 # Figure out what the line will look like, based on what's there
389 # now, and what we're to add or remove.
391 if (-e $substvarfile) {
393 open(SUBSTVARS_IN, "$substvarfile") || die "read $substvarfile: $!";
394 while (<SUBSTVARS_IN>) {
396 if (/^\Q$substvar\E=(.*)/) {
397 %items = map { $_ => 1} split(", ", $1);
409 $line=join(", ", keys %items);
416 complex_doit("echo '${substvar}=$line' >> $substvarfile");
420 # Reads in the specified file, one word at a time, and returns an array of
421 # the result. If a value is passed in as the second parameter, then glob
422 # expansion is done in the directory specified by the parameter ("." is
423 # frequently a good choice).
429 open (DH_FARRAY_IN, $file) || error("cannot read $file: $1");
430 while (<DH_FARRAY_IN>) {
431 # Only do glob expansion in v3 mode.
433 # The tricky bit is that the glob expansion is done
434 # as if we were in the specified directory, so the
435 # filenames that come out are relative to it.
436 if (defined $globdir && ! compat(2)) {
437 for (map { glob "$globdir/$_" } split) {
451 # Returns the build architecture. (Memoized)
456 return $arch if defined $arch;
458 $arch=`dpkg --print-architecture` || error($!);
464 # Returns a list of packages in the control file.
465 # Must pass "arch" or "indep" or "same" to specify arch-dependant or
466 # -independant or same arch packages. If nothing is specified, returns all
471 $type="" if ! defined $type;
473 # Look up the build arch if we need to.
475 if ($type eq 'same') {
476 $buildarch=buildarch();
483 open (CONTROL, 'debian/control') ||
484 error("cannot read debian/control: $!\n");
488 if (/^Package:\s*(.*)/) {
490 # Detect duplicate package names in the same control file.
491 if (! $seen{$package}) {
495 error("debian/control has a duplicate entry for $package");
498 if (/^Architecture:\s*(.*)/) {
502 if (!$_ or eof) { # end of stanza.
504 (($type eq 'indep' && $arch eq 'all') ||
505 ($type eq 'arch' && $arch ne 'all') ||
506 ($type eq 'same' && ($arch eq 'any' || $arch =~ /\b$buildarch\b/)) ||
508 push @list, $package;