File Coverage

File:/usr/local/share/automake-1.11/Automake/VarDef.pm
Coverage:42.9%

linestmtbrancondsubpodtimecode
1# Copyright (C) 2003, 2004, 2006 Free Software Foundation, Inc.
2
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.
7
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.
12
13# You should have received a copy of the GNU General Public License
14# along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16package Automake::VarDef;
17
1
1
1
8
1
4
use strict;
18
1
1
1
5
7
2
use Carp;
19
1
1
1
4
1
2
use Automake::ChannelDefs;
20
1
1
1
11
1
8
use Automake::ItemDef;
21
22require Exporter;
23
1
1
1
4
1
3
use vars '@ISA', '@EXPORT';
24@ISA = qw/Automake::ItemDef Exporter/;
25@EXPORT = qw (&VAR_AUTOMAKE &VAR_CONFIGURE &VAR_MAKEFILE
26              &VAR_ASIS &VAR_PRETTY &VAR_SILENT &VAR_SORTED);
27
28 - 84
=head1 NAME

Automake::VarDef - a class for variable definitions

=head1 SYNOPSIS

  use Automake::VarDef;
  use Automake::Location;

  # Create a VarDef for a definition such as
  # | # any comment
  # | foo = bar # more comment
  # in Makefile.am
  my $loc = new Automake::Location 'Makefile.am:2';
  my $def = new Automake::VarDef ('foo', 'bar # more comment',
                                  '# any comment',
                                  $loc, '', VAR_MAKEFILE, VAR_ASIS);

  # Appending to a definition.
  $def->append ('value to append', 'comment to append');

  # Accessors.
  my $value    = $def->value;  # with trailing `#' comments and
                               # continuation ("\\\n") omitted.
  my $value    = $def->raw_value; # the real value, as passed to new().
  my $comment  = $def->comment;
  my $location = $def->location;
  my $type     = $def->type;
  my $owner    = $def->owner;
  my $pretty   = $def->pretty;

  # Changing owner.
  $def->set_owner (VAR_CONFIGURE,
                   new Automake::Location 'configure.ac:15');

  # Marking examined definitions.
  $def->set_seen;
  my $seen_p = $def->seen;

  # Printing a variable for debugging.
  print STDERR $def->dump;

=head1 DESCRIPTION

This class gathers data related to one Makefile-variable definition.

=head2 Constants

=over 4

=item C<VAR_AUTOMAKE>, C<VAR_CONFIGURE>, C<VAR_MAKEFILE>

Possible owners for variables.  A variable can be defined
by Automake, in F<configure.ac> (using C<AC_SUBST>), or in
the user's F<Makefile.am>.

=cut
85
86# Defined so that the owner of a variable can only be increased (e.g
87# Automake should not override a configure or Makefile variable).
88
1
1
1
4
1
3
use constant VAR_AUTOMAKE => 0; # Variable defined by Automake.
89
1
1
1
3
1
3
use constant VAR_CONFIGURE => 1;# Variable defined in configure.ac.
90
1
1
1
4
1
2
use constant VAR_MAKEFILE => 2; # Variable defined in Makefile.am.
91
92 - 104
=item C<VAR_ASIS>, C<VAR_PRETTY>, C<VAR_SILENT>, C<VAR_SORTED>

Possible print styles.  C<VAR_ASIS> variables should be output as-is.
C<VAR_PRETTY> variables are wrapped on multiple lines if they cannot
fit on one.  C<VAR_SILENT> variables are not output at all.  Finally,
C<VAR_SORTED> variables should be sorted and then handled as
C<VAR_PRETTY> variables.

C<VAR_SILENT> variables can also be overridden silently (unlike the
other kinds of variables whose overriding may sometimes produce
warnings).

=cut
105
106# Possible values for pretty.
107
1
1
1
3
1
7
use constant VAR_ASIS => 0; # Output as-is.
108
1
1
1
4
1
2
use constant VAR_PRETTY => 1; # Pretty printed on output.
109
1
1
1
3
1
2
use constant VAR_SILENT => 2; # Not output. (Can also be
110                                # overridden silently.)
111
1
1
1
4
1
3
use constant VAR_SORTED => 3; # Sorted and pretty-printed.
112
113=back
114
115 - 145
=head2 Methods

C<VarDef> defines the following methods in addition to those inherited
from L<Automake::ItemDef>.

=over 4

=item C<my $def = new Automake::VarDef ($varname, $value, $comment, $location, $type, $owner, $pretty)>

Create a new Makefile-variable definition.  C<$varname> is the name of
the variable being defined and C<$value> its value.

C<$comment> is any comment preceding the definition.  (Because
Automake reorders variable definitions in the output, it also tries to
carry comments around.)

C<$location> is the place where the definition occurred, it should be
an instance of L<Automake::Location>.

C<$type> should be C<''> for definitions made with C<=>, and C<':'>
for those made with C<:=>.

C<$owner> specifies who owns the variables, it can be one of
C<VAR_AUTOMAKE>, C<VAR_CONFIGURE>, or C<VAR_MAKEFILE> (see these
definitions).

Finally, C<$pretty> tells how the variable should be output, and can
be one of C<VAR_ASIS>, C<VAR_PRETTY>, or C<VAR_SILENT>, or
C<VAR_SORTED> (see these definitions).

=cut
146
147sub new ($$$$$$$$)
148{
149
0
1
  my ($class, $var, $value, $comment, $location, $type, $owner, $pretty) = @_;
150
151  # A user variable must be set by either `=' or `:=', and later
152  # promoted to `+='.
153
0
  if ($owner != VAR_AUTOMAKE && $type eq '+')
154    {
155
0
      error $location, "$var must be set with `=' before using `+='";
156    }
157
158
0
  my $self = Automake::ItemDef::new ($class, $comment, $location, $owner);
159
0
  $self->{'value'} = $value;
160
0
  $self->{'type'} = $type;
161
0
  $self->{'pretty'} = $pretty;
162
0
  $self->{'seen'} = 0;
163
0
  return $self;
164}
165
166 - 171
=item C<$def-E<gt>append ($value, $comment)>

Append C<$value> and <$comment> to the existing value and comment of
C<$def>.  This is normally called on C<+=> definitions.

=cut
172
173sub append ($$$)
174{
175
0
1
  my ($self, $value, $comment) = @_;
176
0
  $self->{'comment'} .= $comment;
177
178
0
  my $val = $self->{'value'};
179
180  # Strip comments from augmented variables. This is so that
181  # VAR = foo # com
182  # VAR += bar
183  # does not become
184  # VAR = foo # com bar
185  # Furthermore keeping `#' would not be portable if the variable is
186  # output on multiple lines.
187
0
  $val =~ s/ ?#.*//;
188
189
0
  if (chomp $val)
190    {
191      # Insert a backslash before a trailing newline.
192
0
      $val .= "\\\n";
193    }
194  elsif ($val)
195    {
196      # Insert a separator.
197
0
      $val .= ' ';
198    }
199
0
  $self->{'value'} = $val . $value;
200  # Turn ASIS appended variables into PRETTY variables. This is to
201  # cope with `make' implementation that cannot read very long lines.
202
0
  $self->{'pretty'} = VAR_PRETTY if $self->{'pretty'} == VAR_ASIS;
203}
204
205 - 214
=item C<$def-E<gt>value>

=item C<$def-E<gt>type>

=item C<$def-E<gt>pretty>

Accessors to the various constituents of a C<VarDef>.  See the
documentation of C<new>'s arguments for a description of these.

=cut
215
216sub value ($)
217{
218
0
1
  my ($self) = @_;
219
0
  my $val = $self->raw_value;
220  # Strip anything past `#'. `#' characters cannot be escaped
221  # in Makefiles, so we don't have to be smart.
222
0
  $val =~ s/#.*$//s;
223  # Strip backslashes.
224
0
  $val =~ s/\\$/ /mg;
225
0
  return $val;
226}
227
228sub raw_value ($)
229{
230
0
0
  my ($self) = @_;
231
0
  return $self->{'value'};
232}
233
234sub type ($)
235{
236
0
1
  my ($self) = @_;
237
0
  return $self->{'type'};
238}
239
240sub pretty ($)
241{
242
0
1
  my ($self) = @_;
243
0
  return $self->{'pretty'};
244}
245
246 - 253
=item C<$def-E<gt>set_owner ($owner, $location)>

Change the owner of a definition.  This usually happens because
the user used C<+=> on an Automake variable, so (s)he now owns
the content.  C<$location> should be an instance of L<Automake::Location>
indicating where the change took place.

=cut
254
255sub set_owner ($$$)
256{
257
0
1
  my ($self, $owner, $location) = @_;
258  # We always adjust the location when the owner changes (even for
259  # `+=' statements). The risk otherwise is to warn about
260  # a VAR_MAKEFILE variable and locate it in configure.ac...
261
0
  $self->{'owner'} = $owner;
262
0
  $self->{'location'} = $location;
263}
264
265 - 273
=item C<$def-E<gt>set_seen>

=item C<$bool = $def-E<gt>seen>

These function allows Automake to mark (C<set_seen>) variable that
it has examined in some way, and latter check (using C<seen>) for
unused variables.  Unused variables usually indicate typos.

=cut
274
275sub set_seen ($)
276{
277
0
1
  my ($self) = @_;
278
0
  $self->{'seen'} = 1;
279}
280
281sub seen ($)
282{
283
0
1
  my ($self) = @_;
284
0
  return $self->{'seen'};
285}
286
287 - 292
=item C<$str = $def-E<gt>dump>

Format the contents of C<$def> as a human-readable string,
for debugging.

=cut
293
294sub dump ($)
295{
296
0
1
  my ($self) = @_;
297
0
  my $owner = $self->owner;
298
299
0
  if ($owner == VAR_AUTOMAKE)
300    {
301
0
      $owner = 'Automake';
302    }
303  elsif ($owner == VAR_CONFIGURE)
304    {
305
0
      $owner = 'Configure';
306    }
307  elsif ($owner == VAR_MAKEFILE)
308    {
309
0
      $owner = 'Makefile';
310    }
311  else
312    {
313
0
      prog_error ("unexpected owner");
314    }
315
316
0
  my $where = $self->location->dump;
317
0
  my $comment = $self->comment;
318
0
  my $value = $self->raw_value;
319
0
  my $type = $self->type;
320
321
0
  return "{
322      type: $type=
323      where: $where comment: $comment
324      value: $value
325      owner: $owner
326    }\n";
327}
328
329=back
330
331 - 335
=head1 SEE ALSO

L<Automake::Variable>, L<Automake::ItemDef>.

=cut
336
3371;
338
339### Setup "GNU" style for perl-mode and cperl-mode.
340## Local Variables:
341## perl-indent-level: 2
342## perl-continued-statement-offset: 2
343## perl-continued-brace-offset: 0
344## perl-brace-offset: 0
345## perl-brace-imaginary-offset: 0
346## perl-label-offset: -2
347## cperl-indent-level: 2
348## cperl-brace-offset: 0
349## cperl-continued-brace-offset: 0
350## cperl-label-offset: -2
351## cperl-extra-newline-before-brace: t
352## cperl-merge-trailing-else: nil
353## cperl-continued-statement-offset: 2
354## End: