1 # autoconf -- create `configure' using m4 macros
2 # Copyright (C) 2001, 2002, 2003, 2004, 2006, 2007, 2009, 2010 Free
3 # Software Foundation, Inc.
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package Autom4te
::General
;
22 Autom4te::General - general support functions for Autoconf and Automake
30 This perl module provides various general purpose support functions
31 used in several executables of the Autoconf and Automake packages.
37 use Autom4te
::ChannelDefs
;
38 use Autom4te
::Channels
;
46 use vars qw
(@ISA @EXPORT);
50 # Variables we define and export.
52 qw
($debug $force $help $me $tmp $verbose $version);
54 # Functions we define and export.
57 &getopt
&shell_quote
&mktmpdir
60 # Functions we forward (coming from modules we use).
61 my @export_forward_subs =
62 qw
(&basename
&dirname
&fileparse
);
64 @EXPORT = (@export_vars, @export_subs, @export_forward_subs);
67 # Variable we share with the main package. Be sure to have a single
68 # copy of them: using `my' together with multiple inclusion of this
69 # package would introduce several copies.
71 =head2 Global Variables
77 Set this variable to 1 if debug messages should be enabled. Debug
78 messages are meant for developpers only, or when tracking down an
88 Set this variable to 1 to recreate all the files, or to consider all
89 the output files are obsolete.
98 Set to the help message associated to the option C<--help>.
107 The name of this application, as should be used in diagostic messages.
116 The name of the temporary directory created by C<mktmpdir>. Left
127 Enable verbosity messages. These messages are meant for ordinary
128 users, and typically make explicit the steps being performed.
132 use vars qw
($verbose);
137 Set to the version message associated to the option C<--version>.
141 use vars qw
($version);
160 Filter Perl's exit codes, delete any temporary directory (unless
161 C<$debug>), and exit nonzero whenever closing C<STDOUT> fails.
169 # $? contains the exit status we will return.
170 # It was set using one of the following ways:
172 # 1) normal termination
174 # 2) calling `exit (n)'
176 # 3) calling die or friends (croak, confess...):
177 # a) when $! is non-0
179 # b) when $! is 0 but $? is not
180 # this sets $? = ($? >> 8) (i.e., the exit code of the
181 # last program executed)
182 # c) when both $! and $? are 0
185 # Cases 1), 2), and 3b) are fine, but we prefer $? = 1 for 3a) and 3c).
187 $status = 1 if ($! && $! == $?
) || $?
== 255;
188 # (Note that we cannot safely distinguish calls to `exit (n)'
189 # from calls to die when `$! = n'. It's not big deal because
190 # we only call `exit (0)' or `exit (1)'.)
192 if (!$debug && defined $tmp && -d
$tmp)
194 local $SIG{__WARN__
} = sub { $status = 1; warn $_[0] };
195 File
::Path
::rmtree
$tmp;
198 # This is required if the code might send any output to stdout
199 # E.g., even --version or --help. So it's best to do it unconditionally.
202 print STDERR
"$me: closing standard output: $!\n";
216 =item C<debug (@message)>
218 If the debug mode is enabled (C<$debug> and C<$verbose>), report the
219 C<@message> on C<STDERR>, signed with the name of the program.
225 # Messages displayed only if $DEBUG and $VERBOSE.
228 print STDERR
"$me: ", @_, "\n"
229 if $verbose && $debug;
233 =item C<getopt (%option)>
235 Wrapper around C<Getopt::Long>. In addition to the user C<option>s,
236 support C<-h>/C<--help>, C<-V>/C<--version>, C<-v>/C<--verbose>,
237 C<-d>/C<--debug>, C<-f>/C<--force>. Conform to the GNU Coding
238 Standards for error messages. Try to work around a weird behavior
239 from C<Getopt::Long> to preserve C<-> as an C<@ARGV> instead of
240 rejecting it as a broken option.
246 # Handle the %OPTION, plus all the common options.
247 # Work around Getopt bugs wrt `-'.
253 # F*k. Getopt seems bogus and dies when given `-' with `bundling'.
254 # If fixed some day, use this: '' => sub { push @ARGV, "-" }
255 my $stdin = grep /^-$/, @ARGV;
256 @ARGV = grep !/^-$/, @ARGV;
257 %option = ("h|help" => sub { print $help; exit 0 },
258 "V|version" => sub { print $version; exit 0 },
260 "v|verbose" => sub { ++$verbose },
261 "d|debug" => sub { ++$debug },
262 'f|force' => \
$force,
264 # User options last, so that they have precedence.
266 Getopt
::Long
::Configure
("bundling", "pass_through");
270 foreach (grep { /^-./ } @ARGV)
272 print STDERR
"$0: unrecognized option `$_'\n";
273 print STDERR
"Try `$0 --help' for more information.\n";
280 setup_channel
'note', silent
=> !$verbose;
281 setup_channel
'verb', silent
=> !$verbose;
285 =item C<shell_quote ($file_name)>
287 Quote C<$file_name> for the shell.
292 # shell_quote ($FILE_NAME)
293 # ------------------------
294 # If the string $S is a well-behaved file name, simply return it.
295 # If it contains white space, quotes, etc., quote it, and return
300 if ($s =~ m![^\w+/.,-]!)
302 # Convert each single quote to '\''
303 $s =~ s/\'/\'\\\'\'/g;
304 # Then single quote the string.
310 =item C<mktmpdir ($signature)>
312 Create a temporary directory which name is based on C<$signature>.
313 Store its name in C<$tmp>. C<END> is in charge of removing it, unless
318 # mktmpdir ($SIGNATURE)
319 # ---------------------
322 my ($signature) = @_;
323 my $TMPDIR = $ENV{'TMPDIR'} || '/tmp';
324 my $quoted_tmpdir = shell_quote
($TMPDIR);
326 # If mktemp supports dirs, use it.
327 $tmp = `(umask 077 &&
328 mktemp -d $quoted_tmpdir/"${signature}XXXXXX") 2>/dev/null`;
331 if (!$tmp || ! -d
$tmp)
333 $tmp = "$TMPDIR/$signature" . int (rand 10000) . ".$$";
335 or croak
"$me: cannot create $tmp: $!\n";
338 print STDERR
"$me:$$: working in $tmp\n"
343 =item C<uniq (@list)>
345 Return C<@list> with no duplicates, keeping only the first
357 foreach my $item (@_)
359 if (! exists $seen{$item})
365 return wantarray ?
@res : "@res";
369 =item C<handle_exec_errors ($command)>
371 Display an error message for C<$command>, based on the content of
377 # handle_exec_errors ($COMMAND)
378 # -----------------------------
379 sub handle_exec_errors
($)
383 $command = (split (' ', $command))[0];
386 error
"failed to run $command: $!";
390 use POSIX qw
(WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG
);
394 my $status = WEXITSTATUS
($?
);
395 # WIFEXITED and WEXITSTATUS can alter $!, reset it so that
396 # error() actually propagates the command's exit status, not $!.
398 error
"$command failed with exit status: $status";
400 elsif (WIFSIGNALED
($?
))
402 my $signal = WTERMSIG
($?
);
403 # In this case we prefer to exit with status 1.
405 error
"$command terminated by signal: $signal";
409 error
"$command exited abnormally";
422 Written by Alexandre Duret-Lutz E<lt>F<adl@gnu.org>E<gt> and Akim
423 Demaille E<lt>F<akim@freefriends.org>E<gt>.
431 ### Setup "GNU" style for perl-mode and cperl-mode.
433 ## perl-indent-level: 2
434 ## perl-continued-statement-offset: 2
435 ## perl-continued-brace-offset: 0
436 ## perl-brace-offset: 0
437 ## perl-brace-imaginary-offset: 0
438 ## perl-label-offset: -2
439 ## cperl-indent-level: 2
440 ## cperl-brace-offset: 0
441 ## cperl-continued-brace-offset: 0
442 ## cperl-label-offset: -2
443 ## cperl-extra-newline-before-brace: t
444 ## cperl-merge-trailing-else: nil
445 ## cperl-continued-statement-offset: 2