1 # autoconf -- create 'configure' using m4 macros
2 # Copyright (C) 2001-2004, 2006-2007, 2009-2017, 2020-2024 Free Software
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
;
22 Autom4te::General - general support functions for Autoconf
30 This perl module provides various general purpose support functions
31 used in several executables of the Autoconf package.
37 use warnings FATAL
=> 'all';
47 use Autom4te
::ChannelDefs
;
48 use Autom4te
::Channels
;
49 use Autom4te
::Getopt
();
51 our @ISA = qw
(Exporter
);
53 # Variables we define and export.
55 qw
($debug $force $help $me $tmp $verbose $version);
57 # Functions we define and export.
60 &getopt
&shell_quote
&mktmpdir
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
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
90 Set this variable to 1 to recreate all the files, or to consider all
91 the output files are obsolete.
99 Set to the help message associated with the option C<--help>.
107 The name of this application, for diagnostic messages.
111 our $me = basename
($0);
115 The name of the temporary directory created by C<mktmpdir>. Left
125 Enable verbosity messages. These messages are meant for ordinary
126 users, and typically make explicit the steps being performed.
134 Set to the version message associated to the option C<--version>.
138 our $version = undef;
156 Filter Perl's exit codes and exit nonzero whenever closing C<STDOUT> fails.
164 # $? contains the exit status we will return.
165 # It was set using one of the following ways:
167 # 1) normal termination
169 # 2) calling 'exit (n)'
171 # 3) calling die or friends (croak, confess...):
172 # a) when $! is non-0
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
180 # Cases 1), 2), and 3b) are fine, but we prefer $? = 1 for 3a) and 3c).
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.
191 print STDERR
"$me: closing standard output: $!\n";
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.
214 # Messages displayed only if $DEBUG and $VERBOSE.
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.
233 # Handle the %OPTION, plus all the common options.
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.
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.
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
268 if ($s =~ m![^\w+/.,-]!)
270 # Convert each single quote to '\''
271 $s =~ s/\'/\'\\\'\'/g;
272 # Then single quote the string.
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.
286 # mktmpdir ($SIGNATURE)
287 # ---------------------
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",
303 print STDERR
"$me:$$: working in $tmp\n"
310 =item C<uniq (@list)>
312 Return C<@list> with no duplicates, keeping only the first
324 foreach my $item (@_)
326 if (! exists $seen{$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
344 # handle_exec_errors ($COMMAND)
345 # -----------------------------
346 sub handle_exec_errors
($)
350 $command = (split (' ', $command))[0];
353 error
"failed to run $command: $!";
357 use POSIX qw
(WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG
);
361 my $status = WEXITSTATUS
($?
);
362 # WIFEXITED and WEXITSTATUS can alter $!, reset it so that
363 # error() actually propagates the command's exit status, not $!.
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.
372 error
"$command terminated by signal: $signal";
376 error
"$command exited abnormally";
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>.
398 ### Setup "GNU" style for perl-mode and cperl-mode.
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