Reword the copyright notices to match what's suggested in GPLv3.
[autoconf.git] / lib / Autom4te / General.pm
blobb79467fe1ce15f743e3b32744b3c6c5da44cb494
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;
19 =head1 NAME
21 Autom4te::General - general support functions for Autoconf and Automake
23 =head1 SYNOPSIS
25 use Autom4te::General
27 =head1 DESCRIPTION
29 This perl module provides various general purpose support functions
30 used in several executables of the Autoconf and Automake packages.
32 =cut
34 use 5.005_03;
35 use Exporter;
36 use Autom4te::ChannelDefs;
37 use Autom4te::Channels;
38 use File::Basename;
39 use File::Path ();
40 use File::stat;
41 use IO::File;
42 use Carp;
43 use strict;
45 use vars qw (@ISA @EXPORT);
47 @ISA = qw (Exporter);
49 # Variables we define and export.
50 my @export_vars =
51 qw ($debug $force $help $me $tmp $verbose $version);
53 # Functions we define and export.
54 my @export_subs =
55 qw (&debug
56 &getopt &mktmpdir
57 &uniq);
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
72 =over 4
74 =item C<$debug>
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
78 incorrect execution.
80 =cut
82 use vars qw ($debug);
83 $debug = 0;
85 =item C<$force>
87 Set this variable to 1 to recreate all the files, or to consider all
88 the output files are obsolete.
90 =cut
92 use vars qw ($force);
93 $force = undef;
95 =item C<$help>
97 Set to the help message associated to the option C<--help>.
99 =cut
101 use vars qw ($help);
102 $help = undef;
104 =item C<$me>
106 The name of this application, as should be used in diagostic messages.
108 =cut
110 use vars qw ($me);
111 $me = basename ($0);
113 =item C<$tmp>
115 The name of the temporary directory created by C<mktmpdir>. Left
116 C<undef> otherwise.
118 =cut
120 # Our tmp dir.
121 use vars qw ($tmp);
122 $tmp = undef;
124 =item C<$verbose>
126 Enable verbosity messages. These messages are meant for ordinary
127 users, and typically make explicit the steps being performed.
129 =cut
131 use vars qw ($verbose);
132 $verbose = 0;
134 =item C<$version>
136 Set to the version message associated to the option C<--version>.
138 =cut
140 use vars qw ($version);
141 $version = undef;
143 =back
145 =cut
149 ## ----- ##
150 ## END. ##
151 ## ----- ##
153 =head2 Functions
155 =over 4
157 =item C<END>
159 Filter Perl's exit codes, delete any temporary directory (unless
160 C<$debug>), and exit nonzero whenever closing C<STDOUT> fails.
162 =cut
164 # END
165 # ---
166 sub END
168 # $? contains the exit status we will return.
169 # It was set using one of the following ways:
171 # 1) normal termination
172 # this sets $? = 0
173 # 2) calling `exit (n)'
174 # this sets $? = n
175 # 3) calling die or friends (croak, confess...):
176 # a) when $! is non-0
177 # this set $? = $!
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
182 # this sets $? = 255
184 # Cases 1), 2), and 3b) are fine, but we prefer $? = 1 for 3a) and 3c).
185 my $status = $?;
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.
199 if (! close STDOUT)
201 print STDERR "$me: closing standard output: $!\n";
202 $? = 1;
203 return;
206 $? = $status;
210 ## ----------- ##
211 ## Functions. ##
212 ## ----------- ##
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.
220 =cut
222 # &debug(@MESSAGE)
223 # ----------------
224 # Messages displayed only if $DEBUG and $VERBOSE.
225 sub debug (@)
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.
241 =cut
243 # getopt (%OPTION)
244 # ----------------
245 # Handle the %OPTION, plus all the common options.
246 # Work around Getopt bugs wrt `-'.
247 sub getopt (%)
249 my (%option) = @_;
250 use Getopt::Long;
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.
264 %option);
265 Getopt::Long::Configure ("bundling", "pass_through");
266 GetOptions (%option)
267 or exit 1;
269 foreach (grep { /^-./ } @ARGV)
271 print STDERR "$0: unrecognized option `$_'\n";
272 print STDERR "Try `$0 --help' for more information.\n";
273 exit (1);
276 push @ARGV, '-'
277 if $stdin;
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
288 C<$debug>.
290 =cut
292 # mktmpdir ($SIGNATURE)
293 # ---------------------
294 sub mktmpdir ($)
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`;
302 chomp $tmp;
304 if (!$tmp || ! -d $tmp)
306 $tmp = "$TMPDIR/$signature" . int (rand 10000) . ".$$";
307 mkdir $tmp, 0700
308 or croak "$me: cannot create $tmp: $!\n";
311 print STDERR "$me:$$: working in $tmp\n"
312 if $debug;
316 =item C<uniq (@list)>
318 Return C<@list> with no duplicates, keeping only the first
319 occurrences.
321 =cut
323 # @RES
324 # uniq (@LIST)
325 # ------------
326 sub uniq (@)
328 my @res = ();
329 my %seen = ();
330 foreach my $item (@_)
332 if (! exists $seen{$item})
334 $seen{$item} = 1;
335 push (@res, $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
345 C<$?> and C<$!>.
347 =cut
350 # handle_exec_errors ($COMMAND)
351 # -----------------------------
352 sub handle_exec_errors ($)
354 my ($command) = @_;
356 $command = (split (' ', $command))[0];
357 if ($!)
359 error "failed to run $command: $!";
361 else
363 use POSIX qw (WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG);
365 if (WIFEXITED ($?))
367 my $status = WEXITSTATUS ($?);
368 # WIFEXITED and WEXITSTATUS can alter $!, reset it so that
369 # error() actually propagates the command's exit status, not $!.
370 $! = 0;
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.
377 $! = 1;
378 error "$command terminated by signal: $signal";
380 else
382 error "$command exited abnormally";
387 =back
389 =head1 SEE ALSO
391 L<Autom4te::XFile>
393 =head1 HISTORY
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>.
398 =cut
402 1; # for require
404 ### Setup "GNU" style for perl-mode and cperl-mode.
405 ## Local Variables:
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
419 ## End: