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 &filedoublearray
15 &GetPackages &basename &dirname &xargs %dh &compat &addsubstvar
16 &delsubstvar &excludefile);
21 # If DH_OPTIONS is set, prepend it @ARGV.
22 if (defined($ENV{DH_OPTIONS})) {
23 # Ignore leading/trailing whitespace.
24 $ENV{DH_OPTIONS}=~s/^\s+//;
25 $ENV{DH_OPTIONS}=~s/\s+$//;
26 unshift @ARGV,split(/\s+/,$ENV{DH_OPTIONS});
29 # Check to see if an argument on the command line starts with a dash.
30 # if so, we need to pass this off to the resource intensive
31 # Getopt::Long, which I'd prefer to avoid loading at all if possible.
34 foreach $arg (@ARGV) {
41 eval "use Debian::Debhelper::Dh_Getopt";
43 %dh=Debian::Debhelper::Dh_Getopt::parseopts();
46 # Another way to set excludes.
47 if (exists $ENV{DH_ALWAYS_EXCLUDE} && length $ENV{DH_ALWAYS_EXCLUDE}) {
48 push @{$dh{EXCLUDE}}, $ENV{DH_ALWAYS_EXCLUDE};
51 # Generate EXCLUDE_FIND.
54 foreach (@{$dh{EXCLUDE}}) {
58 $dh{EXCLUDE_FIND}.="-regex .\\*$x.\\* -or ";
60 $dh{EXCLUDE_FIND}=~s/ -or $//;
63 # Check to see if DH_VERBOSE environment variable was set, if so,
64 # make sure verbose is on.
65 if (defined $ENV{DH_VERBOSE} && $ENV{DH_VERBOSE} ne "") {
69 # Check to see if DH_NO_ACT environment variable was set, if so,
70 # make sure no act mode is on.
71 if (defined $ENV{DH_NO_ACT} && $ENV{DH_NO_ACT} ne "") {
75 # Get the name of the main binary package (first one listed in
77 my @allpackages=GetPackages();
78 $dh{MAINPACKAGE}=$allpackages[0];
80 # Check if packages to build have been specified, if not, fall back to
81 # the default, doing them all.
82 if (! defined $dh{DOPACKAGES} || ! @{$dh{DOPACKAGES}}) {
83 if ($dh{DOINDEP} || $dh{DOARCH} || $dh{DOSAME}) {
84 error("You asked that all arch in(dep) packages be built, but there are none of that type.");
86 push @{$dh{DOPACKAGES}},@allpackages;
89 # Check to see if -P was specified. If so, we can only act on a single
91 if ($dh{TMPDIR} && $#{$dh{DOPACKAGES}} > 0) {
92 error("-P was specified, but multiple packages would be acted on (".join(",",@{$dh{DOPACKAGES}}).").");
95 # Figure out which package is the first one we were instructed to build.
96 # This package gets special treatement: files and directories specified on
97 # the command line may affect it.
98 $dh{FIRSTPACKAGE}=${$dh{DOPACKAGES}}[0];
101 # Pass it an array containing the arguments of a shell command like would
102 # be run by exec(). It turns that into a line like you might enter at the
103 # shell, escaping metacharacters and quoting arguments that contain spaces.
108 foreach my $word (@args) {
110 # Escape only a few things since it will be quoted.
111 # Note we use double quotes because you cannot
112 # escape ' in single quotes, while " can be escaped
114 # This does make -V"foo bar" turn into "-Vfoo bar",
115 # but that will be parsed identically by the shell
117 $word=~s/([\n`\$"\\])/\$1/g;
118 push @ret, "\"$word\"";
121 # This list is from _Unix in a Nutshell_. (except '#')
122 $word=~s/([\s!"\$()*+#;<>?@\[\]\\`|~])/\\$1/g;
126 return join(' ', @ret);
129 # Run a command, and display the command to stdout if verbose mode is on.
130 # All commands that modifiy files in $TMP should be ran via this
133 # Note that this cannot handle complex commands, especially anything
134 # involving redirection. Use complex_doit instead.
136 verbose_print(escape_shell(@_));
139 system(@_) == 0 || error("command returned error code");
143 # Run a command and display the command to stdout if verbose mode is on.
144 # Use doit() if you can, instead of this function, because this function
145 # forks a shell. However, this function can handle more complicated stuff
148 verbose_print(join(" ",@_));
151 # The join makes system get a scalar so it forks off a shell.
152 system(join(" ",@_)) == 0
153 || error("command returned error code");
157 # Run a command that may have a huge number of arguments, like xargs does.
158 # Pass in a reference to an array containing the arguments, and then other
159 # parameters that are the command and any parameters that should be passed to
164 # The kernel can accept command lines up to 20k worth of characters.
165 my $command_max=20000; # LINUX SPECIFIC!!
166 # I could use POSIX::ARG_MAX, but that would be slow.
168 # Figure out length of static portion of command.
171 $static_length+=length($_)+1;
175 my $length=$static_length;
177 if (length($_) + 1 + $static_length > $command_max) {
178 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? \"@_ $_\"");
180 $length+=length($_) + 1;
181 if ($length < $command_max) {
185 doit(@_,@collect) if $#collect > -1;
187 $length=$static_length + length($_) + 1;
190 doit(@_,@collect) if $#collect > -1;
193 # Print something if the verbose flag is on.
198 print "\t$message\n";
202 # Output an error message and exit.
214 print STDERR basename($0).": $message\n";
217 # Returns the basename of the argument passed to it.
221 $fn=~s/\/$//g; # ignore trailing slashes
222 $fn=~s:^.*/(.*?)$:$1:;
226 # Returns the directory name of the argument passed to it.
230 $fn=~s/\/$//g; # ignore trailing slashes
231 $fn=~s:^(.*)/.*?$:$1:;
235 # Pass in a number, will return true iff the current compatibility level
236 # is less than or equal to that number.
241 if (defined $ENV{DH_COMPAT}) {
244 elsif (-e 'debian/compat') {
246 open (COMPAT_IN, "debian/compat") || die "debian/compat: $!";
251 if ($c > $max_compat) {
252 error("Sorry, but $max_compat is the highest compatibility level of debhelper currently supported.");
258 # Pass it a name of a binary package, it returns the name of the tmp dir to
259 # use, for that package.
266 elsif (compat(1) && $package eq $dh{MAINPACKAGE}) {
267 # This is for back-compatibility with the debian/tmp tradition.
271 return "debian/$package";
275 # Pass this the name of a binary package, and the name of the file wanted
276 # for the package, and it will return the actual existing filename to use.
278 # It tries several filenames:
279 # * debian/package.filename.buildarch
280 # * debian/package.filename
281 # * debian/file (if the package is the main package)
286 if (-f "debian/$package.$filename.".buildarch()) {
287 return "debian/$package.$filename.".buildarch();
289 elsif (-f "debian/$package.$filename") {
290 return "debian/$package.$filename";
292 elsif ($package eq $dh{MAINPACKAGE} && -f "debian/$filename") {
293 return "debian/$filename";
300 # Pass it a name of a binary package, it returns the name to prefix to files
301 # in debian for this package.
305 if (compat(1) and $package eq $dh{MAINPACKAGE}) {
311 # Returns 1 if the package is a native debian package, null otherwise.
312 # As a side effect, sets $dh{VERSION} to the version of this package.
314 # Caches return code so it only needs to run dpkg-parsechangelog once.
320 return $isnative_cache{$package} if defined $isnative_cache{$package};
322 # Make sure we look at the correct changelog.
323 my $isnative_changelog=pkgfile($package,"changelog");
324 if (! $isnative_changelog) {
325 $isnative_changelog="debian/changelog";
327 # Get the package version.
328 my $version=`dpkg-parsechangelog -l$isnative_changelog`;
329 ($dh{VERSION})=$version=~m/Version:\s*(.*)/m;
330 # Did the changelog parse fail?
331 if (! defined $dh{VERSION}) {
332 error("changelog parse failure");
335 # Is this a native Debian package?
336 if ($dh{VERSION}=~m/.*-/) {
337 return $isnative_cache{$package}=0;
340 return $isnative_cache{$package}=1;
345 # Automatically add a shell script snippet to a debian script.
346 # Only works if the script has #DEBHELPER# in it.
350 # 2: script to add to
351 # 3: filename of snippet
352 # 4: sed to run on the snippet. Ie, s/#PACKAGE#/$PACKAGE/
359 # This is the file we will append to.
360 my $outfile="debian/".pkgext($package)."$script.debhelper";
362 # Figure out what shell script snippet to use.
364 if (defined($ENV{DH_AUTOSCRIPTDIR}) &&
365 -e "$ENV{DH_AUTOSCRIPTDIR}/$filename") {
366 $infile="$ENV{DH_AUTOSCRIPTDIR}/$filename";
369 if (-e "/usr/share/debhelper/autoscripts/$filename") {
370 $infile="/usr/share/debhelper/autoscripts/$filename";
373 error("/usr/share/debhelper/autoscripts/$filename does not exist");
377 complex_doit("echo \"# Automatically added by ".basename($0)."\">> $outfile");
378 complex_doit("sed \"$sed\" $infile >> $outfile");
379 complex_doit("echo '# End automatically added section' >> $outfile");
382 # Removes a whole substvar line.
387 my $ext=pkgext($package);
388 my $substvarfile="debian/${ext}substvars";
390 if (-e $substvarfile) {
391 complex_doit("grep -v '^${substvar}=' $substvarfile > $substvarfile.new || true");
392 doit("mv", "$substvarfile.new","$substvarfile");
396 # Adds a dependency on some package to the specified
397 # substvar in a package's substvar's file.
401 my $deppackage=shift;
405 my $ext=pkgext($package);
406 my $substvarfile="debian/${ext}substvars";
408 $str.=" ($verinfo)" if defined $verinfo && length $verinfo;
410 # Figure out what the line will look like, based on what's there
411 # now, and what we're to add or remove.
413 if (-e $substvarfile) {
415 open(SUBSTVARS_IN, "$substvarfile") || die "read $substvarfile: $!";
416 while (<SUBSTVARS_IN>) {
418 if (/^\Q$substvar\E=(.*)/) {
419 %items = map { $_ => 1} split(", ", $1);
431 $line=join(", ", keys %items);
438 complex_doit("echo '${substvar}=$line' >> $substvarfile");
442 # Reads in the specified file, one line at a time. splits on words,
443 # and returns an array of arrays of the contents.
444 # If a value is passed in as the second parameter, then glob
445 # expansion is done in the directory specified by the parameter ("." is
446 # frequently a good choice).
447 sub filedoublearray {
452 open (DH_FARRAY_IN, $file) || error("cannot read $file: $1");
453 while (<DH_FARRAY_IN>) {
455 # Only do glob expansion in v3 mode.
457 # The tricky bit is that the glob expansion is done
458 # as if we were in the specified directory, so the
459 # filenames that come out are relative to it.
460 if (defined $globdir && ! compat(2)) {
461 for (map { glob "$globdir/$_" } split) {
476 # Reads in the specified file, one word at a time, and returns an array of
477 # the result. Can do globbing as does filedoublearray.
479 return map { @$_ } filedoublearray(@_);
482 # Passed a filename, returns true if -X says that file should be excluded.
484 my $filename = shift;
485 foreach my $f (@{$dh{EXCLUDE}}) {
486 return 1 if $filename =~ /\Q$f\E/;
491 # Returns the build architecture. (Memoized)
496 return $arch if defined $arch;
498 $arch=`dpkg --print-architecture` || error($!);
504 # Returns a list of packages in the control file.
505 # Must pass "arch" or "indep" or "same" to specify arch-dependant or
506 # -independant or same arch packages. If nothing is specified, returns all
511 $type="" if ! defined $type;
513 # Look up the build arch if we need to.
515 if ($type eq 'same') {
516 $buildarch=buildarch();
523 open (CONTROL, 'debian/control') ||
524 error("cannot read debian/control: $!\n");
528 if (/^Package:\s*(.*)/) {
530 # Detect duplicate package names in the same control file.
531 if (! $seen{$package}) {
535 error("debian/control has a duplicate entry for $package");
538 if (/^Architecture:\s*(.*)/) {
542 if (!$_ or eof) { # end of stanza.
544 (($type eq 'indep' && $arch eq 'all') ||
545 ($type eq 'arch' && $arch ne 'all') ||
546 ($type eq 'same' && ($arch eq 'any' || $arch =~ /\b$buildarch\b/)) ||
548 push @list, $package;