fortran: clean up core files after AC_FC_CHECK_BOUNDS
[autoconf.git] / lib / Autom4te / General.pm
blob02e3c87f78013271548acbf9354143c1cf3cc721
1 # autoconf -- create `configure' using m4 macros
2 # Copyright (C) 2001-2004, 2006-2007, 2009-2012 Free Software
3 # 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;
20 =head1 NAME
22 Autom4te::General - general support functions for Autoconf and Automake
24 =head1 SYNOPSIS
26 use Autom4te::General
28 =head1 DESCRIPTION
30 This perl module provides various general purpose support functions
31 used in several executables of the Autoconf and Automake packages.
33 =cut
35 use 5.006_002;
36 use Exporter;
37 use Autom4te::ChannelDefs;
38 use Autom4te::Channels;
39 use Autom4te::Getopt ();
40 use File::Basename;
41 use File::Path ();
42 use File::stat;
43 use IO::File;
44 use Carp;
45 use strict;
47 use vars qw (@ISA @EXPORT);
49 @ISA = qw (Exporter);
51 # Variables we define and export.
52 my @export_vars =
53 qw ($debug $force $help $me $tmp $verbose $version);
55 # Functions we define and export.
56 my @export_subs =
57 qw (&debug
58 &getopt &shell_quote &mktmpdir
59 &uniq);
61 # Functions we forward (coming from modules we use).
62 my @export_forward_subs =
63 qw (&basename &dirname &fileparse);
65 @EXPORT = (@export_vars, @export_subs, @export_forward_subs);
68 # Variable we share with the main package. Be sure to have a single
69 # copy of them: using `my' together with multiple inclusion of this
70 # package would introduce several copies.
72 =head2 Global Variables
74 =over 4
76 =item C<$debug>
78 Set this variable to 1 if debug messages should be enabled. Debug
79 messages are meant for developers only, or when tracking down an
80 incorrect execution.
82 =cut
84 use vars qw ($debug);
85 $debug = 0;
87 =item C<$force>
89 Set this variable to 1 to recreate all the files, or to consider all
90 the output files are obsolete.
92 =cut
94 use vars qw ($force);
95 $force = undef;
97 =item C<$help>
99 Set to the help message associated with the option C<--help>.
101 =cut
103 use vars qw ($help);
104 $help = undef;
106 =item C<$me>
108 The name of this application, for diagnostic messages.
110 =cut
112 use vars qw ($me);
113 $me = basename ($0);
115 =item C<$tmp>
117 The name of the temporary directory created by C<mktmpdir>. Left
118 C<undef> otherwise.
120 =cut
122 # Our tmp dir.
123 use vars qw ($tmp);
124 $tmp = undef;
126 =item C<$verbose>
128 Enable verbosity messages. These messages are meant for ordinary
129 users, and typically make explicit the steps being performed.
131 =cut
133 use vars qw ($verbose);
134 $verbose = 0;
136 =item C<$version>
138 Set to the version message associated to the option C<--version>.
140 =cut
142 use vars qw ($version);
143 $version = undef;
145 =back
147 =cut
151 ## ----- ##
152 ## END. ##
153 ## ----- ##
155 =head2 Functions
157 =over 4
159 =item C<END>
161 Filter Perl's exit codes, delete any temporary directory (unless
162 C<$debug>), and exit nonzero whenever closing C<STDOUT> fails.
164 =cut
166 # END
167 # ---
168 sub END
170 # $? contains the exit status we will return.
171 # It was set using one of the following ways:
173 # 1) normal termination
174 # this sets $? = 0
175 # 2) calling `exit (n)'
176 # this sets $? = n
177 # 3) calling die or friends (croak, confess...):
178 # a) when $! is non-0
179 # this set $? = $!
180 # b) when $! is 0 but $? is not
181 # this sets $? = ($? >> 8) (i.e., the exit code of the
182 # last program executed)
183 # c) when both $! and $? are 0
184 # this sets $? = 255
186 # Cases 1), 2), and 3b) are fine, but we prefer $? = 1 for 3a) and 3c).
187 my $status = $?;
188 $status = 1 if ($! && $! == $?) || $? == 255;
189 # (Note that we cannot safely distinguish calls to `exit (n)'
190 # from calls to die when `$! = n'. It's not big deal because
191 # we only call `exit (0)' or `exit (1)'.)
193 if (!$debug && defined $tmp && -d $tmp)
195 local $SIG{__WARN__} = sub { $status = 1; warn $_[0] };
196 File::Path::rmtree $tmp;
199 # This is required if the code might send any output to stdout
200 # E.g., even --version or --help. So it's best to do it unconditionally.
201 if (! close STDOUT)
203 print STDERR "$me: closing standard output: $!\n";
204 $? = 1;
205 return;
208 $? = $status;
212 ## ----------- ##
213 ## Functions. ##
214 ## ----------- ##
217 =item C<debug (@message)>
219 If the debug mode is enabled (C<$debug> and C<$verbose>), report the
220 C<@message> on C<STDERR>, signed with the name of the program.
222 =cut
224 # &debug(@MESSAGE)
225 # ----------------
226 # Messages displayed only if $DEBUG and $VERBOSE.
227 sub debug (@)
229 print STDERR "$me: ", @_, "\n"
230 if $verbose && $debug;
234 =item C<getopt (%option)>
236 Wrapper around C<Autom4te::Getopt::parse_options>. In addition to
237 the user C<option>s, support C<-h>/C<--help>, C<-V>/C<--version>,
238 C<-v>/C<--verbose>, C<-d>/C<--debug>, C<-f>/C<--force>. Conform to
239 the GNU Coding Standards for error messages.
241 =cut
243 # getopt (%OPTION)
244 # ----------------
245 # Handle the %OPTION, plus all the common options.
246 sub getopt (%)
248 my (%option) = @_;
249 %option = ("h|help" => sub { print $help; exit 0 },
250 "V|version" => sub { print $version; exit 0 },
252 "v|verbose" => sub { ++$verbose },
253 "d|debug" => sub { ++$debug },
254 'f|force' => \$force,
256 # User options last, so that they have precedence.
257 %option);
258 Autom4te::Getopt::parse_options (%option);
260 setup_channel 'note', silent => !$verbose;
261 setup_channel 'verb', silent => !$verbose;
265 =item C<shell_quote ($file_name)>
267 Quote C<$file_name> for the shell.
269 =cut
271 # $FILE_NAME
272 # shell_quote ($FILE_NAME)
273 # ------------------------
274 # If the string $S is a well-behaved file name, simply return it.
275 # If it contains white space, quotes, etc., quote it, and return
276 # the new string.
277 sub shell_quote($)
279 my ($s) = @_;
280 if ($s =~ m![^\w+/.,-]!)
282 # Convert each single quote to '\''
283 $s =~ s/\'/\'\\\'\'/g;
284 # Then single quote the string.
285 $s = "'$s'";
287 return $s;
290 =item C<mktmpdir ($signature)>
292 Create a temporary directory which name is based on C<$signature>.
293 Store its name in C<$tmp>. C<END> is in charge of removing it, unless
294 C<$debug>.
296 =cut
298 # mktmpdir ($SIGNATURE)
299 # ---------------------
300 sub mktmpdir ($)
302 my ($signature) = @_;
303 my $TMPDIR = $ENV{'TMPDIR'} || '/tmp';
304 my $quoted_tmpdir = shell_quote ($TMPDIR);
306 # If mktemp supports dirs, use it.
307 $tmp = `(umask 077 &&
308 mktemp -d $quoted_tmpdir/"${signature}XXXXXX") 2>/dev/null`;
309 chomp $tmp;
311 if (!$tmp || ! -d $tmp)
313 $tmp = "$TMPDIR/$signature" . int (rand 10000) . ".$$";
314 mkdir $tmp, 0700
315 or croak "$me: cannot create $tmp: $!\n";
318 print STDERR "$me:$$: working in $tmp\n"
319 if $debug;
323 =item C<uniq (@list)>
325 Return C<@list> with no duplicates, keeping only the first
326 occurrences.
328 =cut
330 # @RES
331 # uniq (@LIST)
332 # ------------
333 sub uniq (@)
335 my @res = ();
336 my %seen = ();
337 foreach my $item (@_)
339 if (! exists $seen{$item})
341 $seen{$item} = 1;
342 push (@res, $item);
345 return wantarray ? @res : "@res";
349 =item C<handle_exec_errors ($command)>
351 Display an error message for C<$command>, based on the content of
352 C<$?> and C<$!>.
354 =cut
357 # handle_exec_errors ($COMMAND)
358 # -----------------------------
359 sub handle_exec_errors ($)
361 my ($command) = @_;
363 $command = (split (' ', $command))[0];
364 if ($!)
366 error "failed to run $command: $!";
368 else
370 use POSIX qw (WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG);
372 if (WIFEXITED ($?))
374 my $status = WEXITSTATUS ($?);
375 # WIFEXITED and WEXITSTATUS can alter $!, reset it so that
376 # error() actually propagates the command's exit status, not $!.
377 $! = 0;
378 error "$command failed with exit status: $status";
380 elsif (WIFSIGNALED ($?))
382 my $signal = WTERMSIG ($?);
383 # In this case we prefer to exit with status 1.
384 $! = 1;
385 error "$command terminated by signal: $signal";
387 else
389 error "$command exited abnormally";
394 =back
396 =head1 SEE ALSO
398 L<Autom4te::XFile>
400 =head1 HISTORY
402 Written by Alexandre Duret-Lutz E<lt>F<adl@gnu.org>E<gt> and Akim
403 Demaille E<lt>F<akim@freefriends.org>E<gt>.
405 =cut
409 1; # for require
411 ### Setup "GNU" style for perl-mode and cperl-mode.
412 ## Local Variables:
413 ## perl-indent-level: 2
414 ## perl-continued-statement-offset: 2
415 ## perl-continued-brace-offset: 0
416 ## perl-brace-offset: 0
417 ## perl-brace-imaginary-offset: 0
418 ## perl-label-offset: -2
419 ## cperl-indent-level: 2
420 ## cperl-brace-offset: 0
421 ## cperl-continued-brace-offset: 0
422 ## cperl-label-offset: -2
423 ## cperl-extra-newline-before-brace: t
424 ## cperl-merge-trailing-else: nil
425 ## cperl-continued-statement-offset: 2
426 ## End: