autoconf.texi: Don’t say that Darwin is “derived from FreeBSD.”
[autoconf.git] / lib / Autom4te / General.pm
blob5ea85ddcbc52464dbf0341aae563ba8264cb31f9
1 # autoconf -- create 'configure' using m4 macros
2 # Copyright (C) 2001-2004, 2006-2007, 2009-2017, 2020-2024 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 <https://www.gnu.org/licenses/>.
18 package Autom4te::General;
20 =head1 NAME
22 Autom4te::General - general support functions for Autoconf
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 package.
33 =cut
35 use 5.006;
36 use strict;
37 use warnings FATAL => 'all';
39 use Carp;
40 use Exporter;
41 use File::Basename;
42 use File::Spec ();
43 use File::stat;
44 use File::Temp ();
45 use IO::File;
47 use Autom4te::ChannelDefs;
48 use Autom4te::Channels;
49 use Autom4te::Getopt ();
51 our @ISA = qw (Exporter);
53 # Variables we define and export.
54 my @export_vars =
55 qw ($debug $force $help $me $tmp $verbose $version);
57 # Functions we define and export.
58 my @export_subs =
59 qw (&debug
60 &getopt &shell_quote &mktmpdir
61 &uniq);
63 # Functions we forward (coming from modules we use).
64 my @export_forward_subs =
65 qw (&basename &dirname &fileparse);
67 our @EXPORT = (@export_vars, @export_subs, @export_forward_subs);
70 # Variable we share with the main package. Be sure to have a single
71 # copy of them: using 'my' together with multiple inclusion of this
72 # package would introduce several copies.
74 =head2 Global Variables
76 =over 4
78 =item C<$debug>
80 Set this variable to 1 if debug messages should be enabled. Debug
81 messages are meant for developers only, or when tracking down an
82 incorrect execution.
84 =cut
86 our $debug = 0;
88 =item C<$force>
90 Set this variable to 1 to recreate all the files, or to consider all
91 the output files are obsolete.
93 =cut
95 our $force = undef;
97 =item C<$help>
99 Set to the help message associated with the option C<--help>.
101 =cut
103 our $help = undef;
105 =item C<$me>
107 The name of this application, for diagnostic messages.
109 =cut
111 our $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 our $tmp = undef;
123 =item C<$verbose>
125 Enable verbosity messages. These messages are meant for ordinary
126 users, and typically make explicit the steps being performed.
128 =cut
130 our $verbose = 0;
132 =item C<$version>
134 Set to the version message associated to the option C<--version>.
136 =cut
138 our $version = undef;
140 =back
142 =cut
146 ## ----- ##
147 ## END. ##
148 ## ----- ##
150 =head2 Functions
152 =over 4
154 =item C<END>
156 Filter Perl's exit codes and exit nonzero whenever closing C<STDOUT> fails.
158 =cut
160 # END
161 # ---
162 sub END
164 # $? contains the exit status we will return.
165 # It was set using one of the following ways:
167 # 1) normal termination
168 # this sets $? = 0
169 # 2) calling 'exit (n)'
170 # this sets $? = n
171 # 3) calling die or friends (croak, confess...):
172 # a) when $! is non-0
173 # this set $? = $!
174 # b) when $! is 0 but $? is not
175 # this sets $? = ($? >> 8) (i.e., the exit code of the
176 # last program executed)
177 # c) when both $! and $? are 0
178 # this sets $? = 255
180 # Cases 1), 2), and 3b) are fine, but we prefer $? = 1 for 3a) and 3c).
181 my $status = $?;
182 $status = 1 if ($! && $! == $?) || $? == 255;
183 # (Note that we cannot safely distinguish calls to 'exit (n)'
184 # from calls to die when '$! = n'. It's not big deal because
185 # we only call 'exit (0)' or 'exit (1)'.)
187 # This is required if the code might send any output to stdout
188 # E.g., even --version or --help. So it's best to do it unconditionally.
189 if (! close STDOUT)
191 print STDERR "$me: closing standard output: $!\n";
192 $? = 1;
193 return;
196 $? = $status;
200 ## ----------- ##
201 ## Functions. ##
202 ## ----------- ##
205 =item C<debug (@message)>
207 If the debug mode is enabled (C<$debug> and C<$verbose>), report the
208 C<@message> on C<STDERR>, signed with the name of the program.
210 =cut
212 # &debug(@MESSAGE)
213 # ----------------
214 # Messages displayed only if $DEBUG and $VERBOSE.
215 sub debug (@)
217 print STDERR "$me: ", @_, "\n"
218 if $verbose && $debug;
222 =item C<getopt (%option)>
224 Wrapper around C<Autom4te::Getopt::parse_options>. In addition to
225 the user C<option>s, support C<-h>/C<--help>, C<-V>/C<--version>,
226 C<-v>/C<--verbose>, C<-d>/C<--debug>, C<-f>/C<--force>. Conform to
227 the GNU Coding Standards for error messages.
229 =cut
231 # getopt (%OPTION)
232 # ----------------
233 # Handle the %OPTION, plus all the common options.
234 sub getopt (%)
236 my (%option) = @_;
237 %option = ("h|help" => sub { print $help; exit 0 },
238 "V|version" => sub { print $version; exit 0 },
240 "v|verbose" => sub { ++$verbose },
241 "d|debug" => sub { ++$debug },
242 'f|force' => \$force,
244 # User options last, so that they have precedence.
245 %option);
246 Autom4te::Getopt::parse_options (%option);
248 setup_channel 'note', silent => !$verbose;
249 setup_channel 'verb', silent => !$verbose;
253 =item C<shell_quote ($file_name)>
255 Quote C<$file_name> for the shell.
257 =cut
259 # $FILE_NAME
260 # shell_quote ($FILE_NAME)
261 # ------------------------
262 # If the string $S is a well-behaved file name, simply return it.
263 # If it contains white space, quotes, etc., quote it, and return
264 # the new string.
265 sub shell_quote($)
267 my ($s) = @_;
268 if ($s =~ m![^\w+/.,-]!)
270 # Convert each single quote to '\''
271 $s =~ s/\'/\'\\\'\'/g;
272 # Then single quote the string.
273 $s = "'$s'";
275 return $s;
278 =item C<mktmpdir ($signature)>
280 Create a temporary directory which name is based on C<$signature>.
281 Store its name in C<$tmp>. It will be removed at program exit,
282 unless C<$debug> is true.
284 =cut
286 # mktmpdir ($SIGNATURE)
287 # ---------------------
288 sub mktmpdir ($)
290 my ($signature) = @_;
292 # Ensure that we refer to the temporary directory by absolute
293 # pathname; most importantly, this ensures that C<do FILE> will
294 # work whenever FILE is in $tmp, even when '.' is not in @INC
295 # (perl 5.26 and later).
296 my $TMPDIR = File::Spec->rel2abs (File::Spec->tmpdir ());
297 $tmp = File::Temp::tempdir (
298 $signature . "XXXXXX",
299 DIR => $TMPDIR,
300 CLEANUP => !$debug
303 print STDERR "$me:$$: working in $tmp\n"
304 if $debug;
306 return $tmp;
310 =item C<uniq (@list)>
312 Return C<@list> with no duplicates, keeping only the first
313 occurrences.
315 =cut
317 # @RES
318 # uniq (@LIST)
319 # ------------
320 sub uniq (@)
322 my @res = ();
323 my %seen = ();
324 foreach my $item (@_)
326 if (! exists $seen{$item})
328 $seen{$item} = 1;
329 push (@res, $item);
332 return wantarray ? @res : "@res";
336 =item C<handle_exec_errors ($command)>
338 Display an error message for C<$command>, based on the content of
339 C<$?> and C<$!>.
341 =cut
344 # handle_exec_errors ($COMMAND)
345 # -----------------------------
346 sub handle_exec_errors ($)
348 my ($command) = @_;
350 $command = (split (' ', $command))[0];
351 if ($!)
353 error "failed to run $command: $!";
355 else
357 use POSIX qw (WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG);
359 if (WIFEXITED ($?))
361 my $status = WEXITSTATUS ($?);
362 # WIFEXITED and WEXITSTATUS can alter $!, reset it so that
363 # error() actually propagates the command's exit status, not $!.
364 $! = 0;
365 error "$command failed with exit status: $status";
367 elsif (WIFSIGNALED ($?))
369 my $signal = WTERMSIG ($?);
370 # In this case we prefer to exit with status 1.
371 $! = 1;
372 error "$command terminated by signal: $signal";
374 else
376 error "$command exited abnormally";
381 =back
383 =head1 SEE ALSO
385 L<Autom4te::XFile>
387 =head1 HISTORY
389 Written by Alexandre Duret-Lutz E<lt>F<adl@gnu.org>E<gt> and Akim
390 Demaille E<lt>F<akim@freefriends.org>E<gt>.
392 =cut
396 1; # for require
398 ### Setup "GNU" style for perl-mode and cperl-mode.
399 ## Local Variables:
400 ## perl-indent-level: 2
401 ## perl-continued-statement-offset: 2
402 ## perl-continued-brace-offset: 0
403 ## perl-brace-offset: 0
404 ## perl-brace-imaginary-offset: 0
405 ## perl-label-offset: -2
406 ## cperl-indent-level: 2
407 ## cperl-brace-offset: 0
408 ## cperl-continued-brace-offset: 0
409 ## cperl-label-offset: -2
410 ## cperl-extra-newline-before-brace: t
411 ## cperl-merge-trailing-else: nil
412 ## cperl-continued-statement-offset: 2
413 ## End: