summaryrefslogtreecommitdiff
path: root/lib/Automake/ChannelDefs.pm
blob: bfe5ba5482de0d6538b1474a99e05208f2314d11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
# Copyright (C) 2002-2023 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

package Automake::ChannelDefs;

=head1 NAME

Automake::ChannelDefs - channel definitions for Automake and helper functions

=head1 SYNOPSIS

  use Automake::ChannelDefs;

  print Automake::ChannelDefs::usage (), "\n";
  prog_error ($MESSAGE, [%OPTIONS]);
  error ($WHERE, $MESSAGE, [%OPTIONS]);
  error ($MESSAGE);
  fatal ($WHERE, $MESSAGE, [%OPTIONS]);
  fatal ($MESSAGE);
  verb ($MESSAGE, [%OPTIONS]);
  switch_warning ($CATEGORY);
  parse_WARNINGS ();
  parse_warnings ($OPTION, @ARGUMENT);
  Automake::ChannelDefs::set_strictness ($STRICTNESS_NAME);

=head1 DESCRIPTION

This package defines channels that can be used in Automake to
output diagnostics and other messages (via C<msg()>).  It also defines
some helper function to enable or disable these channels, and some
shorthand function to output on specific channels.

=cut

use 5.010;
use strict;
use warnings FATAL => 'all';

use Exporter;

use Automake::Channels;
use Automake::Config;
BEGIN
{
  if ($perl_threads)
    {
      require threads;
      import threads;
    }
}

our @ISA = qw (Exporter);
our @EXPORT = qw (&prog_error &error &fatal &verb
		  &switch_warning &parse_WARNINGS &parse_warnings
		  &merge_WARNINGS);

=head2 CHANNELS

The following channels can be used as the first argument of
C<Automake::Channel::msg>.  For some of them we list a shorthand
function that makes the code more readable.

=over 4

=item C<fatal>

Fatal errors.  Use C<&fatal> to send messages over this channel.

=item C<error>

Common errors.  Use C<&error> to send messages over this channel.

=item C<error-gnu>

Errors related to GNU Standards.

=item C<error-gnu/warn>

Errors related to GNU Standards that should be warnings in 'foreign' mode.

=item C<error-gnits>

Errors related to GNITS Standards (silent by default).

=item C<automake>

Internal errors.  Use C<&prog_error> to send messages over this channel.

=item C<cross>

Constructs compromising the cross-compilation of the package.

=item C<gnu>

Warnings related to GNU Coding Standards.

=item C<obsolete>

Warnings about obsolete features.

=item C<override>

Warnings about user redefinitions of Automake rules or
variables (silent by default).

=item C<portability>

Warnings about non-portable constructs.

=item C<portability-recursive>

Warnings about recursive variable expansions (C<$(foo$(x))>).
These are not universally supported, but are more portable than
the other non-portable constructs diagnosed by C<-Wportability>.
These warnings are turned on by C<-Wportability> but can then be
turned off separately by C<-Wno-portability-recursive>.

=item C<extra-portability>

Extra warnings about non-portable constructs covering obscure tools.

=item C<syntax>

Warnings about weird syntax, unused variables, typos...

=item C<unsupported>

Warnings about unsupported (or mis-supported) features.

=item C<verb>

Messages output in C<--verbose> mode.  Use C<&verb> to send such messages.

=item C<note>

Informative messages.

=back

=cut

# Initialize our list of error/warning channels.
# Do not forget to update &usage and the manual
# if you add or change a warning channel.

register_channel 'fatal', type => 'fatal', uniq_part => UP_NONE, ordered => 0;
register_channel 'error', type => 'error';
register_channel 'error-gnu', type => 'error';
register_channel 'error-gnu/warn', type => 'error';
register_channel 'error-gnits', type => 'error', silent => 1;
register_channel 'automake', type => 'fatal', backtrace => 1,
  header => ("####################\n" .
	     "## Internal Error ##\n" .
	     "####################\n"),
  footer => "\nPlease contact <$PACKAGE_BUGREPORT>.",
  uniq_part => UP_NONE, ordered => 0;

register_channel 'cross', type => 'warning', silent => 1;
register_channel 'gnu', type => 'warning';
register_channel 'obsolete', type => 'warning';
register_channel 'override', type => 'warning', silent => 1;
register_channel 'portability', type => 'warning', silent => 1;
register_channel 'extra-portability', type => 'warning', silent => 1;
register_channel 'portability-recursive', type => 'warning', silent => 1;
register_channel 'syntax', type => 'warning';
register_channel 'unsupported', type => 'warning';

register_channel 'verb', type => 'debug', silent => 1, uniq_part => UP_NONE,
  ordered => 0;
register_channel 'note', type => 'debug', silent => 0;

setup_channel_type 'warning', header => 'warning: ';
setup_channel_type 'error', header => 'error: ';
setup_channel_type 'fatal', header => 'error: ';

=head2 FUNCTIONS

=over 4

=item C<usage ()>

Return the warning category descriptions.

=cut

sub usage ()
{
  return "Warning categories include:
  cross                  cross compilation issues
  gnu                    GNU coding standards (default in gnu and gnits modes)
  obsolete               obsolete features or constructions (default)
  override               user redefinitions of Automake rules or variables
  portability            portability issues (default in gnu and gnits modes)
  portability-recursive  nested Make variables (default with -Wportability)
  extra-portability      extra portability issues related to obscure tools
  syntax                 dubious syntactic constructs (default)
  unsupported            unsupported or incomplete features (default)
  all                    all the warnings
  no-CATEGORY            turn off warnings in CATEGORY
  none                   turn off all the warnings
  error                  treat warnings as errors";
}

=item C<prog_error ($MESSAGE, [%OPTIONS])>

Signal a programming error (on channel C<automake>),
display C<$MESSAGE>, and exit 1.

=cut

sub prog_error ($;%)
{
  my ($msg, %opts) = @_;
  msg 'automake', '', $msg, %opts;
}

=item C<error ($WHERE, $MESSAGE, [%OPTIONS])>

=item C<error ($MESSAGE)>

Uncategorized errors.

=cut

sub error ($;$%)
{
  my ($where, $msg, %opts) = @_;
  msg ('error', $where, $msg, %opts);
}

=item C<fatal ($WHERE, $MESSAGE, [%OPTIONS])>

=item C<fatal ($MESSAGE)>

Fatal errors.

=cut

sub fatal ($;$%)
{
  my ($where, $msg, %opts) = @_;
  msg ('fatal', $where, $msg, %opts);
}

=item C<verb ($MESSAGE, [%OPTIONS])>

C<--verbose> messages.

=cut

sub verb ($;%)
{
  my ($msg, %opts) = @_;
  $msg = "thread " . threads->tid . ": " . $msg
    if $perl_threads;
  msg 'verb', '', $msg, %opts;
}

=item C<switch_warning ($CATEGORY)>

If C<$CATEGORY> is C<mumble>, turn on channel C<mumble>.
If it is C<no-mumble>, turn C<mumble> off.
Else handle C<all> and C<none> for completeness.

=cut

sub switch_warning ($)
{
  my ($cat) = @_;
  my $has_no = 0;

  if ($cat =~ /^no-(.*)$/)
    {
      $cat = $1;
      $has_no = 1;
    }

  if ($cat eq 'all')
    {
      setup_channel_type 'warning', silent => $has_no;
    }
  elsif ($cat eq 'none')
    {
      setup_channel_type 'warning', silent => ! $has_no;
    }
  elsif ($cat eq 'error')
    {
      $warnings_are_errors = ! $has_no;
      # Set exit code if Perl warns about something
      # (like uninitialized variables).
      $SIG{"__WARN__"} =
	$has_no ? 'DEFAULT' : sub { print STDERR @_; $exit_code = 1; };
    }
  elsif (channel_type ($cat) eq 'warning')
    {
      setup_channel $cat, silent => $has_no;
      #
      # Handling of portability warnings is trickier.  For relevant tests,
      # see 'dollarvar2', 'extra-portability' and 'extra-portability3'.
      #
      # -Wportability-recursive and -Wno-portability-recursive should not
      # have any effect on other 'portability' or 'extra-portability'
      # warnings, so there's no need to handle them separately or ad-hoc.
      #
      if ($cat eq 'extra-portability' && ! $has_no) # -Wextra-portability
        {
          # -Wextra-portability must enable 'portability' and
          # 'portability-recursive' warnings.
          setup_channel 'portability', silent => 0;
          setup_channel 'portability-recursive', silent => 0;
        }
      if ($cat eq 'portability') # -Wportability or -Wno-portability
        {
          if ($has_no) # -Wno-portability
            {
              # -Wno-portability must disable 'extra-portability' and
              # 'portability-recursive' warnings.
              setup_channel 'portability-recursive', silent => 1;
              setup_channel 'extra-portability', silent => 1;
            }
          else # -Wportability
            {
              # -Wportability must enable 'portability-recursive'
              # warnings.  But it should have no influence over the
              # 'extra-portability' warnings.
              setup_channel 'portability-recursive', silent => 0;
            }
        }
    }
  else
    {
      return 1;
    }
  return 0;
}

=item C<parse_WARNINGS ()>

Parse the WARNINGS environment variable.

=cut

# Used to communicate from parse_WARNINGS to parse_warnings.
our $_werror = 0;

sub parse_WARNINGS ()
{
  if (exists $ENV{'WARNINGS'})
    {
      # Ignore unknown categories.  This is required because WARNINGS
      # should be honored by many tools.
      # For the same reason, do not turn on -Werror at this point, just
      # record that we saw it; parse_warnings will turn on -Werror after
      # the command line has been processed.
      foreach (split (',', $ENV{'WARNINGS'}))
        {
          if (/^(no-)?error$/)
            {
              $_werror = !defined $1;
            }
          else
            {
              switch_warning $_;
            }
        }
    }
}

=item C<parse_warnings (@CATEGORIES)>

Parse the argument of C<--warning=CATEGORY> or C<-WCATEGORY>.
C<@CATEGORIES> is the accumulated set of warnings categories.
Use like this:

    Automake::GetOpt::parse_options (
        # ...
        'W|warnings=s' => \@warnings,
    )
    # possibly call set_strictness here
    parse_warnings @warnings;

=cut

sub parse_warnings (@)
{
  foreach my $cat (map { split ',' } @_)
    {
      if ($cat =~ /^(no-)?error$/)
        {
          $_werror = !defined $1;
        }
      elsif (switch_warning $cat)
        {
          msg 'unsupported', "unknown warning category '$cat'";
        }
    }

  switch_warning ($_werror ? 'error' : 'no-error');
}

=item C<merge_WARNINGS (@CATEGORIES)>

Merge the warnings categories in the environment variable C<WARNINGS>
with the warnings categories in C<@CATEGORIES>, and return a new
value for C<WARNINGS>.  Values in C<@CATEGORIES> take precedence.
Use like this:

    local $ENV{WARNINGS} = merge_WARNINGS @additional_warnings;

=cut

sub merge_WARNINGS (@)
{
  my $werror = '';
  my $all_or_none = '';
  my %warnings;

  my @categories = split /,/, $ENV{WARNINGS} || '';
  push @categories, @_;

  foreach (@categories)
    {
      if (/^(?:no-)?error$/)
        {
          $werror = $_;
        }
      elsif (/^(?:all|none)$/)
        {
          $all_or_none = $_;
        }
      else
        {
          # The character class in the second match group is ASCII \S minus
          # comma.  We are generous with this because category values may come
          # from WARNINGS and we don't want to assume what other programs'
          # syntaxes for warnings categories are.
          /^(no-|)([\w\[\]\/\\!"#$%&'()*+-.:;<=>?@^`{|}~]+)$/
            or die "Invalid warnings category: $_";
          $warnings{$2} = $1;
        }
    }

  my @final_warnings;
  if ($all_or_none)
    {
      push @final_warnings, $all_or_none;
    }
  else
    {
      foreach (sort keys %warnings)
        {
          push @final_warnings, $warnings{$_} . $_;
        }
    }
  if ($werror)
    {
      push @final_warnings, $werror;
    }

  return join (',', @final_warnings);
}

=item C<set_strictness ($STRICTNESS_NAME)>

Configure channels for strictness C<$STRICTNESS_NAME>.

=cut

sub set_strictness ($)
{
  my ($name) = @_;

  if ($name eq 'gnu')
    {
      setup_channel 'error-gnu', silent => 0;
      setup_channel 'error-gnu/warn', silent => 0, type => 'error';
      setup_channel 'error-gnits', silent => 1;
      setup_channel 'portability', silent => 0;
      setup_channel 'extra-portability', silent => 1;
      setup_channel 'gnu', silent => 0;
    }
  elsif ($name eq 'gnits')
    {
      setup_channel 'error-gnu', silent => 0;
      setup_channel 'error-gnu/warn', silent => 0, type => 'error';
      setup_channel 'error-gnits', silent => 0;
      setup_channel 'portability', silent => 0;
      setup_channel 'extra-portability', silent => 1;
      setup_channel 'gnu', silent => 0;
    }
  elsif ($name eq 'foreign')
    {
      setup_channel 'error-gnu', silent => 1;
      setup_channel 'error-gnu/warn', silent => 0, type => 'warning';
      setup_channel 'error-gnits', silent => 1;
      setup_channel 'portability', silent => 1;
      setup_channel 'extra-portability', silent => 1;
      setup_channel 'gnu', silent => 1;
    }
  else
    {
      prog_error "level '$name' not recognized";
    }
}

=back

=head1 SEE ALSO

L<Automake::Channels>

=head1 HISTORY

Written by Alexandre Duret-Lutz E<lt>F<adl@gnu.org>E<gt>.

=cut

1;

### Setup "GNU" style for perl-mode and cperl-mode.
## Local Variables:
## perl-indent-level: 2
## perl-continued-statement-offset: 2
## perl-continued-brace-offset: 0
## perl-brace-offset: 0
## perl-brace-imaginary-offset: 0
## perl-label-offset: -2
## cperl-indent-level: 2
## cperl-brace-offset: 0
## cperl-continued-brace-offset: 0
## cperl-label-offset: -2
## cperl-extra-newline-before-brace: t
## cperl-merge-trailing-else: nil
## cperl-continued-statement-offset: 2
## End: