maint: Configure Emacs automatically with ".dir-locals.el"
[automake.git] / lib / Automake / ChannelDefs.pm
blob0d661bfab8a39e2bea8e1422cc7a6d5cdaf67341
1 # Copyright (C) 2002-2017 Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2, or (at your option)
6 # any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <https://www.gnu.org/licenses/>.
16 package Automake::ChannelDefs;
18 use Automake::Config;
19 BEGIN
21 if ($perl_threads)
23 require threads;
24 import threads;
27 use Automake::Channels;
29 =head1 NAME
31 Automake::ChannelDefs - channel definitions for Automake and helper functions
33 =head1 SYNOPSIS
35 use Automake::ChannelDefs;
37 Automake::ChannelDefs::usage ();
38 prog_error ($MESSAGE, [%OPTIONS]);
39 error ($WHERE, $MESSAGE, [%OPTIONS]);
40 error ($MESSAGE);
41 fatal ($WHERE, $MESSAGE, [%OPTIONS]);
42 fatal ($MESSAGE);
43 verb ($MESSAGE, [%OPTIONS]);
44 switch_warning ($CATEGORY);
45 parse_WARNINGS ();
46 parse_warnings ($OPTION, $ARGUMENT);
47 Automake::ChannelDefs::set_strictness ($STRICTNESS_NAME);
49 =head1 DESCRIPTION
51 This packages defines channels that can be used in Automake to
52 output diagnostics and other messages (via C<msg()>). It also defines
53 some helper function to enable or disable these channels, and some
54 shorthand function to output on specific channels.
56 =cut
58 use 5.006;
59 use strict;
60 use Exporter;
62 use vars qw (@ISA @EXPORT);
64 @ISA = qw (Exporter);
65 @EXPORT = qw (&prog_error &error &fatal &verb
66 &switch_warning &parse_WARNINGS &parse_warnings);
68 =head2 CHANNELS
70 The following channels can be used as the first argument of
71 C<Automake::Channel::msg>. For some of them we list a shorthand
72 function that makes the code more readable.
74 =over 4
76 =item C<fatal>
78 Fatal errors. Use C<&fatal> to send messages over this channel.
80 =item C<error>
82 Common errors. Use C<&error> to send messages over this channel.
84 =item C<error-gnu>
86 Errors related to GNU Standards.
88 =item C<error-gnu/warn>
90 Errors related to GNU Standards that should be warnings in 'foreign' mode.
92 =item C<error-gnits>
94 Errors related to GNITS Standards (silent by default).
96 =item C<automake>
98 Internal errors. Use C<&prog_error> to send messages over this channel.
100 =item C<gnu>
102 Warnings related to GNU Coding Standards.
104 =item C<obsolete>
106 Warnings about obsolete features (silent by default).
108 =item C<override>
110 Warnings about user redefinitions of Automake rules or
111 variables (silent by default).
113 =item C<portability>
115 Warnings about non-portable constructs.
117 =item C<extra-portability>
119 Extra warnings about non-portable constructs covering obscure tools.
121 =item C<syntax>
123 Warnings about weird syntax, unused variables, typos...
125 =item C<unsupported>
127 Warnings about unsupported (or mis-supported) features.
129 =item C<verb>
131 Messages output in C<--verbose> mode. Use C<&verb> to send such messages.
133 =item C<note>
135 Informative messages.
137 =back
139 =cut
141 # Initialize our list of error/warning channels.
142 # Do not forget to update &usage and the manual
143 # if you add or change a warning channel.
145 register_channel 'fatal', type => 'fatal', uniq_part => UP_NONE, ordered => 0;
146 register_channel 'error', type => 'error';
147 register_channel 'error-gnu', type => 'error';
148 register_channel 'error-gnu/warn', type => 'error';
149 register_channel 'error-gnits', type => 'error', silent => 1;
150 register_channel 'automake', type => 'fatal', backtrace => 1,
151 header => ("####################\n" .
152 "## Internal Error ##\n" .
153 "####################\n"),
154 footer => "\nPlease contact <$PACKAGE_BUGREPORT>.",
155 uniq_part => UP_NONE, ordered => 0;
157 register_channel 'extra-portability', type => 'warning', silent => 1;
158 register_channel 'gnu', type => 'warning';
159 register_channel 'obsolete', type => 'warning';
160 register_channel 'override', type => 'warning', silent => 1;
161 register_channel 'portability', type => 'warning', silent => 1;
162 register_channel 'portability-recursive', type => 'warning', silent => 1;
163 register_channel 'syntax', type => 'warning';
164 register_channel 'unsupported', type => 'warning';
166 register_channel 'verb', type => 'debug', silent => 1, uniq_part => UP_NONE,
167 ordered => 0;
168 register_channel 'note', type => 'debug', silent => 0;
170 setup_channel_type 'warning', header => 'warning: ';
171 setup_channel_type 'error', header => 'error: ';
172 setup_channel_type 'fatal', header => 'error: ';
174 =head2 FUNCTIONS
176 =over 4
178 =item C<usage ()>
180 Display warning categories.
182 =cut
184 sub usage ()
186 print <<EOF;
187 Warning categories include:
188 gnu GNU coding standards (default in gnu and gnits modes)
189 obsolete obsolete features or constructions
190 override user redefinitions of Automake rules or variables
191 portability portability issues (default in gnu and gnits modes)
192 extra-portability extra portability issues related to obscure tools
193 syntax dubious syntactic constructs (default)
194 unsupported unsupported or incomplete features (default)
195 all all the warnings
196 no-CATEGORY turn off warnings in CATEGORY
197 none turn off all the warnings
198 error treat warnings as errors
202 =item C<prog_error ($MESSAGE, [%OPTIONS])>
204 Signal a programming error (on channel C<automake>),
205 display C<$MESSAGE>, and exit 1.
207 =cut
209 sub prog_error ($;%)
211 my ($msg, %opts) = @_;
212 msg 'automake', '', $msg, %opts;
215 =item C<error ($WHERE, $MESSAGE, [%OPTIONS])>
217 =item C<error ($MESSAGE)>
219 Uncategorized errors.
221 =cut
223 sub error ($;$%)
225 my ($where, $msg, %opts) = @_;
226 msg ('error', $where, $msg, %opts);
229 =item C<fatal ($WHERE, $MESSAGE, [%OPTIONS])>
231 =item C<fatal ($MESSAGE)>
233 Fatal errors.
235 =cut
237 sub fatal ($;$%)
239 my ($where, $msg, %opts) = @_;
240 msg ('fatal', $where, $msg, %opts);
243 =item C<verb ($MESSAGE, [%OPTIONS])>
245 C<--verbose> messages.
247 =cut
249 sub verb ($;%)
251 my ($msg, %opts) = @_;
252 $msg = "thread " . threads->tid . ": " . $msg
253 if $perl_threads;
254 msg 'verb', '', $msg, %opts;
257 =item C<switch_warning ($CATEGORY)>
259 If C<$CATEGORY> is C<mumble>, turn on channel C<mumble>.
260 If it's C<no-mumble>, turn C<mumble> off.
261 Else handle C<all> and C<none> for completeness.
263 =cut
265 sub switch_warning ($)
267 my ($cat) = @_;
268 my $has_no = 0;
270 if ($cat =~ /^no-(.*)$/)
272 $cat = $1;
273 $has_no = 1;
276 if ($cat eq 'all')
278 setup_channel_type 'warning', silent => $has_no;
280 elsif ($cat eq 'none')
282 setup_channel_type 'warning', silent => ! $has_no;
284 elsif ($cat eq 'error')
286 $warnings_are_errors = ! $has_no;
287 # Set exit code if Perl warns about something
288 # (like uninitialized variables).
289 $SIG{"__WARN__"} =
290 $has_no ? 'DEFAULT' : sub { print STDERR @_; $exit_code = 1; };
292 elsif (channel_type ($cat) eq 'warning')
294 setup_channel $cat, silent => $has_no;
296 # Handling of portability warnings is trickier. For relevant tests,
297 # see 'dollarvar2', 'extra-portability' and 'extra-portability3'.
299 # -Wportability-recursive and -Wno-portability-recursive should not
300 # have any effect on other 'portability' or 'extra-portability'
301 # warnings, so there's no need to handle them separately or ad-hoc.
303 if ($cat eq 'extra-portability' && ! $has_no) # -Wextra-portability
305 # -Wextra-portability must enable 'portability' and
306 # 'portability-recursive' warnings.
307 setup_channel 'portability', silent => 0;
308 setup_channel 'portability-recursive', silent => 0;
310 if ($cat eq 'portability') # -Wportability or -Wno-portability
312 if ($has_no) # -Wno-portability
314 # -Wno-portability must disable 'extra-portability' and
315 # 'portability-recursive' warnings.
316 setup_channel 'portability-recursive', silent => 1;
317 setup_channel 'extra-portability', silent => 1;
319 else # -Wportability
321 # -Wportability must enable 'portability-recursive'
322 # warnings. But it should have no influence over the
323 # 'extra-portability' warnings.
324 setup_channel 'portability-recursive', silent => 0;
328 else
330 return 1;
332 return 0;
335 =item C<parse_WARNINGS ()>
337 Parse the WARNINGS environment variable.
339 =cut
341 sub parse_WARNINGS ()
343 if (exists $ENV{'WARNINGS'})
345 # Ignore unknown categories. This is required because WARNINGS
346 # should be honored by many tools.
347 switch_warning $_ foreach (split (',', $ENV{'WARNINGS'}));
351 =item C<parse_warnings ($OPTION, $ARGUMENT)>
353 Parse the argument of C<--warning=CATEGORY> or C<-WCATEGORY>.
355 C<$OPTIONS> is C<"--warning"> or C<"-W">, C<$ARGUMENT> is C<CATEGORY>.
357 This is meant to be used as an argument to C<Getopt>.
359 =cut
361 sub parse_warnings ($$)
363 my ($opt, $categories) = @_;
365 foreach my $cat (split (',', $categories))
367 msg 'unsupported', "unknown warning category '$cat'"
368 if switch_warning $cat;
372 =item C<set_strictness ($STRICTNESS_NAME)>
374 Configure channels for strictness C<$STRICTNESS_NAME>.
376 =cut
378 sub set_strictness ($)
380 my ($name) = @_;
382 if ($name eq 'gnu')
384 setup_channel 'error-gnu', silent => 0;
385 setup_channel 'error-gnu/warn', silent => 0, type => 'error';
386 setup_channel 'error-gnits', silent => 1;
387 setup_channel 'portability', silent => 0;
388 setup_channel 'extra-portability', silent => 1;
389 setup_channel 'gnu', silent => 0;
391 elsif ($name eq 'gnits')
393 setup_channel 'error-gnu', silent => 0;
394 setup_channel 'error-gnu/warn', silent => 0, type => 'error';
395 setup_channel 'error-gnits', silent => 0;
396 setup_channel 'portability', silent => 0;
397 setup_channel 'extra-portability', silent => 1;
398 setup_channel 'gnu', silent => 0;
400 elsif ($name eq 'foreign')
402 setup_channel 'error-gnu', silent => 1;
403 setup_channel 'error-gnu/warn', silent => 0, type => 'warning';
404 setup_channel 'error-gnits', silent => 1;
405 setup_channel 'portability', silent => 1;
406 setup_channel 'extra-portability', silent => 1;
407 setup_channel 'gnu', silent => 1;
409 else
411 prog_error "level '$name' not recognized";
415 =back
417 =head1 SEE ALSO
419 L<Automake::Channels>
421 =head1 HISTORY
423 Written by Alexandre Duret-Lutz E<lt>F<adl@gnu.org>E<gt>.
425 =cut