1 # autoconf -- create `configure' using m4 macros
2 # Copyright (C) 2001, 2002, 2003, 2004, 2006 Free Software Foundation, Inc.
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package Autom4te
::General
;
21 Autom4te::General - general support functions for Autoconf and Automake
29 This perl module provides various general purpose support functions
30 used in several executables of the Autoconf and Automake packages.
36 use Autom4te
::ChannelDefs
;
37 use Autom4te
::Channels
;
45 use vars qw
(@ISA @EXPORT);
49 # Variables we define and export.
51 qw
($debug $force $help $me $tmp $verbose $version);
53 # Functions we define and export.
59 # Functions we forward (coming from modules we use).
60 my @export_forward_subs =
61 qw
(&basename
&dirname
&fileparse
);
63 @EXPORT = (@export_vars, @export_subs, @export_forward_subs);
66 # Variable we share with the main package. Be sure to have a single
67 # copy of them: using `my' together with multiple inclusion of this
68 # package would introduce several copies.
70 =head2 Global Variables
76 Set this variable to 1 if debug messages should be enabled. Debug
77 messages are meant for developpers only, or when tracking down an
87 Set this variable to 1 to recreate all the files, or to consider all
88 the output files are obsolete.
97 Set to the help message associated to the option C<--help>.
106 The name of this application, as should be used in diagostic messages.
115 The name of the temporary directory created by C<mktmpdir>. Left
126 Enable verbosity messages. These messages are meant for ordinary
127 users, and typically make explicit the steps being performed.
131 use vars qw
($verbose);
136 Set to the version message associated to the option C<--version>.
140 use vars qw
($version);
159 Filter Perl's exit codes, delete any temporary directory (unless
160 C<$debug>), and exit nonzero whenever closing C<STDOUT> fails.
168 # $? contains the exit status we will return.
169 # It was set using one of the following ways:
171 # 1) normal termination
173 # 2) calling `exit (n)'
175 # 3) calling die or friends (croak, confess...):
176 # a) when $! is non-0
178 # b) when $! is 0 but $? is not
179 # this sets $? = ($? >> 8) (i.e., the exit code of the
180 # last program executed)
181 # c) when both $! and $? are 0
184 # Cases 1), 2), and 3b) are fine, but we prefer $? = 1 for 3a) and 3c).
186 $status = 1 if ($! && $! == $?
) || $?
== 255;
187 # (Note that we cannot safely distinguish calls to `exit (n)'
188 # from calls to die when `$! = n'. It's not big deal because
189 # we only call `exit (0)' or `exit (1)'.)
191 if (!$debug && defined $tmp && -d
$tmp)
193 local $SIG{__WARN__
} = sub { $status = 1; warn $_[0] };
194 File
::Path
::rmtree
$tmp;
197 # This is required if the code might send any output to stdout
198 # E.g., even --version or --help. So it's best to do it unconditionally.
201 print STDERR
"$me: closing standard output: $!\n";
215 =item C<debug (@message)>
217 If the debug mode is enabled (C<$debug> and C<$verbose>), report the
218 C<@message> on C<STDERR>, signed with the name of the program.
224 # Messages displayed only if $DEBUG and $VERBOSE.
227 print STDERR
"$me: ", @_, "\n"
228 if $verbose && $debug;
232 =item C<getopt (%option)>
234 Wrapper around C<Getopt::Long>. In addition to the user C<option>s,
235 support C<-h>/C<--help>, C<-V>/C<--version>, C<-v>/C<--verbose>,
236 C<-d>/C<--debug>, C<-f>/C<--force>. Conform to the GNU Coding
237 Standards for error messages. Try to work around a weird behavior
238 from C<Getopt::Long> to preserve C<-> as an C<@ARGV> instead of
239 rejecting it as a broken option.
245 # Handle the %OPTION, plus all the common options.
246 # Work around Getopt bugs wrt `-'.
252 # F*k. Getopt seems bogus and dies when given `-' with `bundling'.
253 # If fixed some day, use this: '' => sub { push @ARGV, "-" }
254 my $stdin = grep /^-$/, @ARGV;
255 @ARGV = grep !/^-$/, @ARGV;
256 %option = ("h|help" => sub { print $help; exit 0 },
257 "V|version" => sub { print $version; exit 0 },
259 "v|verbose" => sub { ++$verbose },
260 "d|debug" => sub { ++$debug },
261 'f|force' => \
$force,
263 # User options last, so that they have precedence.
265 Getopt
::Long
::Configure
("bundling", "pass_through");
269 foreach (grep { /^-./ } @ARGV)
271 print STDERR
"$0: unrecognized option `$_'\n";
272 print STDERR
"Try `$0 --help' for more information.\n";
279 setup_channel
'note', silent
=> !$verbose;
280 setup_channel
'verb', silent
=> !$verbose;
284 =item C<mktmpdir ($signature)>
286 Create a temporary directory which name is based on C<$signature>.
287 Store its name in C<$tmp>. C<END> is in charge of removing it, unless
292 # mktmpdir ($SIGNATURE)
293 # ---------------------
296 my ($signature) = @_;
297 my $TMPDIR = $ENV{'TMPDIR'} || '/tmp';
299 # If mktemp supports dirs, use it.
300 $tmp = `(umask 077 &&
301 mktemp -d "$TMPDIR/${signature}XXXXXX") 2>/dev/null`;
304 if (!$tmp || ! -d
$tmp)
306 $tmp = "$TMPDIR/$signature" . int (rand 10000) . ".$$";
308 or croak
"$me: cannot create $tmp: $!\n";
311 print STDERR
"$me:$$: working in $tmp\n"
316 =item C<uniq (@list)>
318 Return C<@list> with no duplicates, keeping only the first
330 foreach my $item (@_)
332 if (! exists $seen{$item})
338 return wantarray ?
@res : "@res";
342 =item C<handle_exec_errors ($command)>
344 Display an error message for C<$command>, based on the content of
350 # handle_exec_errors ($COMMAND)
351 # -----------------------------
352 sub handle_exec_errors
($)
356 $command = (split (' ', $command))[0];
359 error
"failed to run $command: $!";
363 use POSIX qw
(WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG
);
367 my $status = WEXITSTATUS
($?
);
368 # WIFEXITED and WEXITSTATUS can alter $!, reset it so that
369 # error() actually propagates the command's exit status, not $!.
371 error
"$command failed with exit status: $status";
373 elsif (WIFSIGNALED
($?
))
375 my $signal = WTERMSIG
($?
);
376 # In this case we prefer to exit with status 1.
378 error
"$command terminated by signal: $signal";
382 error
"$command exited abnormally";
395 Written by Alexandre Duret-Lutz E<lt>F<adl@gnu.org>E<gt> and Akim
396 Demaille E<lt>F<akim@freefriends.org>E<gt>.
404 ### Setup "GNU" style for perl-mode and cperl-mode.
406 ## perl-indent-level: 2
407 ## perl-continued-statement-offset: 2
408 ## perl-continued-brace-offset: 0
409 ## perl-brace-offset: 0
410 ## perl-brace-imaginary-offset: 0
411 ## perl-label-offset: -2
412 ## cperl-indent-level: 2
413 ## cperl-brace-offset: 0
414 ## cperl-continued-brace-offset: 0
415 ## cperl-label-offset: -2
416 ## cperl-extra-newline-before-brace: t
417 ## cperl-merge-trailing-else: nil
418 ## cperl-continued-statement-offset: 2