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
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 # Check to see if DH_VERBOSE environment variable was set, if so,
47 # make sure verbose is on.
48 if (defined $ENV{DH_VERBOSE} && $ENV{DH_VERBOSE} ne "") {
52 # Check to see if DH_NO_ACT environment variable was set, if so,
53 # make sure no act mode is on.
54 if (defined $ENV{DH_NO_ACT} && $ENV{DH_NO_ACT} ne "") {
58 # Get the name of the main binary package (first one listed in
60 my @allpackages=GetPackages();
61 $dh{MAINPACKAGE}=$allpackages[0];
63 # Check if packages to build have been specified, if not, fall back to
64 # the default, doing them all.
65 if (! defined $dh{DOPACKAGES} || ! @{$dh{DOPACKAGES}}) {
66 if ($dh{DOINDEP} || $dh{DOARCH} || $dh{DOSAME}) {
67 error("You asked that all arch in(dep) packages be built, but there are none of that type.");
69 push @{$dh{DOPACKAGES}},@allpackages;
72 # Check to see if -P was specified. If so, we can only act on a single
74 if ($dh{TMPDIR} && $#{$dh{DOPACKAGES}} > 0) {
75 error("-P was specified, but multiple packages would be acted on (".join(",",@{$dh{DOPACKAGES}}).").");
78 # Figure out which package is the first one we were instructed to build.
79 # This package gets special treatement: files and directories specified on
80 # the command line may affect it.
81 $dh{FIRSTPACKAGE}=${$dh{DOPACKAGES}}[0];
84 # Pass it an array containing the arguments of a shell command like would
85 # be run by exec(). It turns that into a line like you might enter at the
86 # shell, escaping metacharacters and quoting arguments that contain spaces.
91 foreach my $word (@args) {
93 # Escape only a few things since it will be quoted.
94 # Note we use double quotes because you cannot
95 # escape ' in qingle quotes, while " can be escaped
97 # This does make -V"foo bar" turn into "-Vfoo bar",
98 # but that will be parsed identically by the shell
100 $word=~s/([\n`\$"\\])/\$1/g;
101 push @ret, "\"$word\"";
104 # This list is from _Unix in a Nutshell_. (except '#')
105 $word=~s/([\s!"\$()*+#;<>?@\[\]\\`|~])/\\$1/g;
109 return join(' ', @ret);
112 # Run a command, and display the command to stdout if verbose mode is on.
113 # All commands that modifiy files in $TMP should be ran via this
116 # Note that this cannot handle complex commands, especially anything
117 # involving redirection. Use complex_doit instead.
119 verbose_print(escape_shell(@_));
122 system(@_) == 0 || error("command returned error code");
126 # Run a command and display the command to stdout if verbose mode is on.
127 # Use doit() if you can, instead of this function, because this function
128 # forks a shell. However, this function can handle more complicated stuff
131 verbose_print(join(" ",@_));
134 # The join makes system get a scalar so it forks off a shell.
135 system(join(" ",@_)) == 0
136 || error("command returned error code");
140 # Run a command that may have a huge number of arguments, like xargs does.
141 # Pass in a reference to an array containing the arguments, and then other
142 # parameters that are the command and any parameters that should be passed to
147 # The kernel can accept command lines up to 20k worth of characters.
148 my $command_max=20000; # LINUX SPECIFIC!!
149 # I could use POSIX::ARG_MAX, but that would be slow.
151 # Figure out length of static portion of command.
154 $static_length+=length($_)+1;
158 my $length=$static_length;
160 if (length($_) + 1 + $static_length > $command_max) {
161 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? \"@_ $_\"");
163 $length+=length($_) + 1;
164 if ($length < $command_max) {
168 doit(@_,@collect) if $#collect > -1;
170 $length=$static_length + length($_) + 1;
173 doit(@_,@collect) if $#collect > -1;
176 # Print something if the verbose flag is on.
181 print "\t$message\n";
185 # Output an error message and exit.
197 print STDERR basename($0).": $message\n";
200 # Returns the basename of the argument passed to it.
204 $fn=~s/\/$//g; # ignore trailing slashes
205 $fn=~s:^.*/(.*?)$:$1:;
209 # Returns the directory name of the argument passed to it.
213 $fn=~s/\/$//g; # ignore trailing slashes
214 $fn=~s:^(.*)/.*?$:$1:;
218 # Pass in a number, will return true iff the current compatibility level
219 # is less than or equal to that number.
224 if (defined $ENV{DH_COMPAT}) {
227 elsif (-e 'debian/compat') {
229 open (COMPAT_IN, "debian/compat") || die "debian/compat: $!";
234 if ($c > $max_compat) {
235 error("Sorry, but $max_compat is the highest compatibility level of debhelper currently supported.");
241 # Pass it a name of a binary package, it returns the name of the tmp dir to
242 # use, for that package.
249 elsif (compat(1) && $package eq $dh{MAINPACKAGE}) {
250 # This is for back-compatibility with the debian/tmp tradition.
254 return "debian/$package";
258 # Pass this the name of a binary package, and the name of the file wanted
259 # for the package, and it will return the actual existing filename to use.
261 # It tries several filenames:
262 # * debian/package.filename.buildarch
263 # * debian/package.filename
264 # * debian/file (if the package is the main package)
269 if (-f "debian/$package.$filename.".buildarch()) {
270 return "debian/$package.$filename.".buildarch();
272 elsif (-f "debian/$package.$filename") {
273 return "debian/$package.$filename";
275 elsif ($package eq $dh{MAINPACKAGE} && -f "debian/$filename") {
276 return "debian/$filename";
283 # Pass it a name of a binary package, it returns the name to prefix to files
284 # in debian for this package.
288 if (compat(1) and $package eq $dh{MAINPACKAGE}) {
294 # Returns 1 if the package is a native debian package, null otherwise.
295 # As a side effect, sets $dh{VERSION} to the version of this package.
297 # Caches return code so it only needs to run dpkg-parsechangelog once.
303 return $isnative_cache{$package} if defined $isnative_cache{$package};
305 # Make sure we look at the correct changelog.
306 my $isnative_changelog=pkgfile($package,"changelog");
307 if (! $isnative_changelog) {
308 $isnative_changelog="debian/changelog";
310 # Get the package version.
311 my $version=`dpkg-parsechangelog -l$isnative_changelog`;
312 ($dh{VERSION})=$version=~m/Version:\s*(.*)/m;
313 # Did the changelog parse fail?
314 if (! defined $dh{VERSION}) {
315 error("changelog parse failure");
318 # Is this a native Debian package?
319 if ($dh{VERSION}=~m/.*-/) {
320 return $isnative_cache{$package}=0;
323 return $isnative_cache{$package}=1;
328 # Automatically add a shell script snippet to a debian script.
329 # Only works if the script has #DEBHELPER# in it.
333 # 2: script to add to
334 # 3: filename of snippet
335 # 4: sed to run on the snippet. Ie, s/#PACKAGE#/$PACKAGE/
342 # This is the file we will append to.
343 my $outfile="debian/".pkgext($package)."$script.debhelper";
345 # Figure out what shell script snippet to use.
347 if (defined($ENV{DH_AUTOSCRIPTDIR}) &&
348 -e "$ENV{DH_AUTOSCRIPTDIR}/$filename") {
349 $infile="$ENV{DH_AUTOSCRIPTDIR}/$filename";
352 if (-e "/usr/share/debhelper/autoscripts/$filename") {
353 $infile="/usr/share/debhelper/autoscripts/$filename";
356 error("/usr/share/debhelper/autoscripts/$filename does not exist");
360 complex_doit("echo \"# Automatically added by ".basename($0)."\">> $outfile");
361 complex_doit("sed \"$sed\" $infile >> $outfile");
362 complex_doit("echo '# End automatically added section' >> $outfile");
365 # Removes a whole substvar line.
370 my $ext=pkgext($package);
371 my $substvarfile="debian/${ext}substvars";
373 if (-e $substvarfile) {
374 complex_doit("grep -v '^${substvar}=' $substvarfile > $substvarfile.new || true");
375 doit("mv", "$substvarfile.new","$substvarfile");
379 # Adds a dependency on some package to the specified
380 # substvar in a package's substvar's file.
384 my $deppackage=shift;
388 my $ext=pkgext($package);
389 my $substvarfile="debian/${ext}substvars";
391 $str.=" ($verinfo)" if length $verinfo;
393 # Figure out what the line will look like, based on what's there
394 # now, and what we're to add or remove.
396 if (-e $substvarfile) {
398 open(SUBSTVARS_IN, "$substvarfile") || die "read $substvarfile: $!";
399 while (<SUBSTVARS_IN>) {
401 if (/^\Q$substvar\E=(.*)/) {
402 %items = map { $_ => 1} split(", ", $1);
414 $line=join(", ", keys %items);
421 complex_doit("echo '${substvar}=$line' >> $substvarfile");
425 # Reads in the specified file, one word at a time, and returns an array of
426 # the result. If a value is passed in as the second parameter, then glob
427 # expansion is done in the directory specified by the parameter ("." is
428 # frequently a good choice).
434 open (DH_FARRAY_IN, $file) || error("cannot read $file: $1");
435 while (<DH_FARRAY_IN>) {
436 # Only do glob expansion in v3 mode.
438 # The tricky bit is that the glob expansion is done
439 # as if we were in the specified directory, so the
440 # filenames that come out are relative to it.
441 if (defined $globdir && ! compat(2)) {
442 for (map { glob "$globdir/$_" } split) {
456 # Passed a filename, returns true if -X says that file should be excluded.
458 my $filename = shift;
459 foreach my $f (@{$dh{EXCLUDE}}) {
460 return 1 if $filename =~ /\Q$f\E/;
465 # Returns the build architecture. (Memoized)
470 return $arch if defined $arch;
472 $arch=`dpkg --print-architecture` || error($!);
478 # Returns a list of packages in the control file.
479 # Must pass "arch" or "indep" or "same" to specify arch-dependant or
480 # -independant or same arch packages. If nothing is specified, returns all
485 $type="" if ! defined $type;
487 # Look up the build arch if we need to.
489 if ($type eq 'same') {
490 $buildarch=buildarch();
497 open (CONTROL, 'debian/control') ||
498 error("cannot read debian/control: $!\n");
502 if (/^Package:\s*(.*)/) {
504 # Detect duplicate package names in the same control file.
505 if (! $seen{$package}) {
509 error("debian/control has a duplicate entry for $package");
512 if (/^Architecture:\s*(.*)/) {
516 if (!$_ or eof) { # end of stanza.
518 (($type eq 'indep' && $arch eq 'all') ||
519 ($type eq 'arch' && $arch ne 'all') ||
520 ($type eq 'same' && ($arch eq 'any' || $arch =~ /\b$buildarch\b/)) ||
522 push @list, $package;