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 &xargs %dh &compat);
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 error("You asked that all arch in(dep) packages be built, but there are none of that type.");
65 push @{$dh{DOPACKAGES}},@allpackages;
68 # Check to see if -P was specified. If so, we can only act on a single
70 if ($dh{TMPDIR} && $#{$dh{DOPACKAGES}} > 0) {
71 error("-P was specified, but multiple packages would be acted on (".join(",",@{$dh{DOPACKAGES}}).").");
74 # Figure out which package is the first one we were instructed to build.
75 # This package gets special treatement: files and directories specified on
76 # the command line may affect it.
77 $dh{FIRSTPACKAGE}=${$dh{DOPACKAGES}}[0];
80 # Pass it an array containing the arguments of a shell command like would
81 # be run by exec(). It turns that into a line like you might enter at the
82 # shell, escaping metacharacters and quoting qrguments that contain spaces.
87 foreach my $word (@args) {
89 # Escape only a few things since it will be quoted.
90 # Note we use double quotes because you cannot
91 # escape ' in qingle quotes, while " can be escaped
93 # This does make -V"foo bar" turn into "-Vfoo bar",
94 # but that will be parsed identically by the shell
96 $word=~s/([\n`\$"\\])/\$1/g;
97 push @ret, "\"$word\"";
100 # This list is from _Unix in a Nutshell_. (except '#')
101 $word=~s/([\s!"\$()*+#;<>?@\[\]\\`|~])/\\$1/g;
105 return join(' ', @ret);
108 # Run a command, and display the command to stdout if verbose mode is on.
109 # All commands that modifiy files in $TMP should be ran via this
112 # Note that this cannot handle complex commands, especially anything
113 # involving redirection. Use complex_doit instead.
115 verbose_print(escape_shell(@_));
118 system(@_) == 0 || error("command returned error code");
122 # Run a command and display the command to stdout if verbose mode is on.
123 # Use doit() if you can, instead of this function, because this function
124 # forks a shell. However, this function can handle more complicated stuff
127 verbose_print(join(" ",@_));
130 # The join makes system get a scalar so it forks off a shell.
131 system(join(" ",@_)) == 0
132 || error("command returned error code");
136 # Run a command that may have a huge number of arguments, like xargs does.
137 # Pass in a reference to an array containing the arguments, and then other
138 # parameters that are the command and any parameters that should be passed to
143 # The kernel can accept command lines up to 20k worth of characters.
144 my $command_max=20000; # LINUX SPECIFIC!!
145 # I could use POSIX::ARG_MAX, but that would be slow.
147 # Figure out length of static portion of command.
150 $static_length+=length($_)+1;
154 my $length=$static_length;
156 if (length($_) + 1 + $static_length > $command_max) {
157 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? \"@_ $_\"");
159 $length+=length($_) + 1;
160 if ($length < $command_max) {
164 doit(@_,@collect) if $#collect > -1;
166 $length=$static_length + length($_) + 1;
169 doit(@_,@collect) if $#collect > -1;
172 # Print something if the verbose flag is on.
177 print "\t$message\n";
181 # Output an error message and exit.
193 print STDERR basename($0).": $message\n";
196 # Returns the basename of the argument passed to it.
200 $fn=~s:^.*/(.*?)$:$1:;
204 # Returns the directory name of the argument passed to it.
208 $fn=~s:^(.*)/.*?$:$1:;
212 # Pass in a number, will return true iff the current compatability level
213 # is less than or equal to that number.
218 if (defined $ENV{DH_COMPAT}) {
222 if ($c > $max_compat) {
223 error("Sorry, but $max_compat is the highest compatability level of debhelper currently supported.");
229 # Pass it a name of a binary package, it returns the name of the tmp dir to
230 # use, for that package.
237 elsif (compat(1) && $package eq $dh{MAINPACKAGE}) {
238 # This is for back-compatability with the debian/tmp tradition.
242 return "debian/$package";
246 # Pass this the name of a binary package, and the name of the file wanted
247 # for the package, and it will return the actual existing filename to use.
249 # It tries several filenames:
250 # * debian/package.filename.buildarch
251 # * debian/package.filename
252 # * debian/file (if the package is the main package)
257 if (-f "debian/$package.$filename.".buildarch()) {
258 return "debian/$package.$filename.".buildarch();
260 elsif (-f "debian/$package.$filename") {
261 return "debian/$package.$filename";
263 elsif ($package eq $dh{MAINPACKAGE} && -f "debian/$filename") {
264 return "debian/$filename";
271 # Pass it a name of a binary package, it returns the name to prefix to files
272 # in debian for this package.
276 if (compat(1) and $package eq $dh{MAINPACKAGE}) {
282 # Returns 1 if the package is a native debian package, null otherwise.
283 # As a side effect, sets $dh{VERSION} to the version of this package.
285 # Caches return code so it only needs to run dpkg-parsechangelog once.
291 return $isnative_cache{$package} if defined $isnative_cache{$package};
293 # Make sure we look at the correct changelog.
294 my $isnative_changelog=pkgfile($package,"changelog");
295 if (! $isnative_changelog) {
296 $isnative_changelog="debian/changelog";
298 # Get the package version.
299 my $version=`dpkg-parsechangelog -l$isnative_changelog`;
300 ($dh{VERSION})=$version=~m/Version:\s*(.*)/m;
301 # Did the changelog parse fail?
302 if (! defined $dh{VERSION}) {
303 error("changelog parse failure");
306 # Is this a native Debian package?
307 if ($dh{VERSION}=~m/.*-/) {
308 return $isnative_cache{$package}=0;
311 return $isnative_cache{$package}=1;
316 # Automatically add a shell script snippet to a debian script.
317 # Only works if the script has #DEBHELPER# in it.
321 # 2: script to add to
322 # 3: filename of snippet
323 # 4: sed to run on the snippet. Ie, s/#PACKAGE#/$PACKAGE/
330 # This is the file we will append to.
331 my $outfile="debian/".pkgext($package)."$script.debhelper";
333 # Figure out what shell script snippet to use.
335 if (defined($ENV{DH_AUTOSCRIPTDIR}) &&
336 -e "$ENV{DH_AUTOSCRIPTDIR}/$filename") {
337 $infile="$ENV{DH_AUTOSCRIPTDIR}/$filename";
340 if (-e "/usr/share/debhelper/autoscripts/$filename") {
341 $infile="/usr/share/debhelper/autoscripts/$filename";
344 error("/usr/share/debhelper/autoscripts/$filename does not exist");
348 complex_doit("echo \"# Automatically added by ".basename($0)."\">> $outfile");
349 complex_doit("sed \"$sed\" $infile >> $outfile");
350 complex_doit("echo '# End automatically added section' >> $outfile");
353 # Reads in the specified file, one word at a time, and returns an array of
354 # the result. If a value is passed in as the second parameter, then glob
355 # expansion is done in the directory specified by the parameter ("." is
356 # frequently a good choice).
362 open (DH_FARRAY_IN, $file) || error("cannot read $file: $1");
363 while (<DH_FARRAY_IN>) {
364 # Only do glob expansion in v3 mode.
366 # The tricky bit is that the glob expansion is done
367 # as if we were in the specified directory, so the
368 # filenames that come out are relative to it.
369 if (defined $globdir && ! compat(2)) {
370 for (map { glob "$globdir/$_" } split) {
384 # Returns the build architecture. (Memoized)
389 return $arch if defined $arch;
391 $arch=`dpkg --print-architecture` || error($!);
397 # Returns a list of packages in the control file.
398 # Must pass "arch" or "indep" or "same" to specify arch-dependant or
399 # -independant or same arch packages. If nothing is specified, returns all
404 $type="" if ! defined $type;
406 # Look up the build arch if we need to.
408 if ($type eq 'same') {
409 $buildarch=buildarch();
415 open (CONTROL, 'debian/control') ||
416 error("cannot read debian/control: $!\n");
420 if (/^Package:\s*(.*)/) {
423 if (/^Architecture:\s*(.*)/) {
426 if (!$_ or eof) { # end of stanza.
428 (($type eq 'indep' && $arch eq 'all') ||
429 ($type eq 'arch' && $arch ne 'all') ||
430 ($type eq 'same' && ($arch eq 'any' || $arch =~ /\b$buildarch\b/)) ||
432 push @list, $package;