Merge branch 'minor'
[automake.git] / lib / Automake / VarDef.pm
blob5acdb729b8d876ceb1d3045cb4fee3c25550d7e0
1 # Copyright (C) 2003-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::VarDef;
18 use 5.006;
19 use strict;
20 use Carp;
21 use Automake::ChannelDefs;
22 use Automake::ItemDef;
24 require Exporter;
25 use vars '@ISA', '@EXPORT';
26 @ISA = qw/Automake::ItemDef Exporter/;
27 @EXPORT = qw (&VAR_AUTOMAKE &VAR_CONFIGURE &VAR_MAKEFILE
28 &VAR_ASIS &VAR_PRETTY &VAR_SILENT &VAR_SORTED);
30 =head1 NAME
32 Automake::VarDef - a class for variable definitions
34 =head1 SYNOPSIS
36 use Automake::VarDef;
37 use Automake::Location;
39 # Create a VarDef for a definition such as
40 # | # any comment
41 # | foo = bar # more comment
42 # in Makefile.am
43 my $loc = new Automake::Location 'Makefile.am:2';
44 my $def = new Automake::VarDef ('foo', 'bar # more comment',
45 '# any comment',
46 $loc, '', VAR_MAKEFILE, VAR_ASIS);
48 # Appending to a definition.
49 $def->append ('value to append', 'comment to append');
51 # Accessors.
52 my $value = $def->value; # with trailing '#' comments and
53 # continuation ("\\\n") omitted.
54 my $value = $def->raw_value; # the real value, as passed to new().
55 my $comment = $def->comment;
56 my $location = $def->location;
57 my $type = $def->type;
58 my $owner = $def->owner;
59 my $pretty = $def->pretty;
61 # Changing owner.
62 $def->set_owner (VAR_CONFIGURE,
63 new Automake::Location 'configure.ac:15');
65 # Marking examined definitions.
66 $def->set_seen;
67 my $seen_p = $def->seen;
69 # Printing a variable for debugging.
70 print STDERR $def->dump;
72 =head1 DESCRIPTION
74 This class gathers data related to one Makefile-variable definition.
76 =head2 Constants
78 =over 4
80 =item C<VAR_AUTOMAKE>, C<VAR_CONFIGURE>, C<VAR_MAKEFILE>
82 Possible owners for variables. A variable can be defined
83 by Automake, in F<configure.ac> (using C<AC_SUBST>), or in
84 the user's F<Makefile.am>.
86 =cut
88 # Defined so that the owner of a variable can only be increased (e.g
89 # Automake should not override a configure or Makefile variable).
90 use constant VAR_AUTOMAKE => 0; # Variable defined by Automake.
91 use constant VAR_CONFIGURE => 1;# Variable defined in configure.ac.
92 use constant VAR_MAKEFILE => 2; # Variable defined in Makefile.am.
94 =item C<VAR_ASIS>, C<VAR_PRETTY>, C<VAR_SILENT>, C<VAR_SORTED>
96 Possible print styles. C<VAR_ASIS> variables should be output as-is.
97 C<VAR_PRETTY> variables are wrapped on multiple lines if they cannot
98 fit on one. C<VAR_SILENT> variables are not output at all. Finally,
99 C<VAR_SORTED> variables should be sorted and then handled as
100 C<VAR_PRETTY> variables.
102 C<VAR_SILENT> variables can also be overridden silently (unlike the
103 other kinds of variables whose overriding may sometimes produce
104 warnings).
106 =cut
108 # Possible values for pretty.
109 use constant VAR_ASIS => 0; # Output as-is.
110 use constant VAR_PRETTY => 1; # Pretty printed on output.
111 use constant VAR_SILENT => 2; # Not output. (Can also be
112 # overridden silently.)
113 use constant VAR_SORTED => 3; # Sorted and pretty-printed.
115 =back
117 =head2 Methods
119 C<VarDef> defines the following methods in addition to those inherited
120 from L<Automake::ItemDef>.
122 =over 4
124 =item C<my $def = new Automake::VarDef ($varname, $value, $comment, $location, $type, $owner, $pretty)>
126 Create a new Makefile-variable definition. C<$varname> is the name of
127 the variable being defined and C<$value> its value.
129 C<$comment> is any comment preceding the definition. (Because
130 Automake reorders variable definitions in the output, it also tries to
131 carry comments around.)
133 C<$location> is the place where the definition occurred, it should be
134 an instance of L<Automake::Location>.
136 C<$type> should be C<''> for definitions made with C<=>, and C<':'>
137 for those made with C<:=>.
139 C<$owner> specifies who owns the variables, it can be one of
140 C<VAR_AUTOMAKE>, C<VAR_CONFIGURE>, or C<VAR_MAKEFILE> (see these
141 definitions).
143 Finally, C<$pretty> tells how the variable should be output, and can
144 be one of C<VAR_ASIS>, C<VAR_PRETTY>, or C<VAR_SILENT>, or
145 C<VAR_SORTED> (see these definitions).
147 =cut
149 sub new ($$$$$$$$)
151 my ($class, $var, $value, $comment, $location, $type, $owner, $pretty) = @_;
153 # A user variable must be set by either '=' or ':=', and later
154 # promoted to '+='.
155 if ($owner != VAR_AUTOMAKE && $type eq '+')
157 error $location, "$var must be set with '=' before using '+='";
160 my $self = Automake::ItemDef::new ($class, $comment, $location, $owner);
161 $self->{'value'} = $value;
162 $self->{'type'} = $type;
163 $self->{'pretty'} = $pretty;
164 $self->{'seen'} = 0;
165 return $self;
168 =item C<$def-E<gt>append ($value, $comment)>
170 Append C<$value> and <$comment> to the existing value and comment of
171 C<$def>. This is normally called on C<+=> definitions.
173 =cut
175 sub append ($$$)
177 my ($self, $value, $comment) = @_;
178 $self->{'comment'} .= $comment;
180 my $val = $self->{'value'};
182 # Strip comments from augmented variables. This is so that
183 # VAR = foo # com
184 # VAR += bar
185 # does not become
186 # VAR = foo # com bar
187 # Furthermore keeping '#' would not be portable if the variable is
188 # output on multiple lines.
189 $val =~ s/ ?#.*//;
190 # Insert a separator, if required.
191 $val .= ' ' if $val;
192 $self->{'value'} = $val . $value;
193 # Turn ASIS appended variables into PRETTY variables. This is to
194 # cope with 'make' implementation that cannot read very long lines.
195 $self->{'pretty'} = VAR_PRETTY if $self->{'pretty'} == VAR_ASIS;
198 =item C<$def-E<gt>value>
200 =item C<$def-E<gt>raw_value>
202 =item C<$def-E<gt>type>
204 =item C<$def-E<gt>pretty>
206 Accessors to the various constituents of a C<VarDef>. See the
207 documentation of C<new>'s arguments for a description of these.
209 =cut
211 sub value ($)
213 my ($self) = @_;
214 my $val = $self->raw_value;
215 # Strip anything past '#'. '#' characters cannot be escaped
216 # in Makefiles, so we don't have to be smart.
217 $val =~ s/#.*$//s;
218 # Strip backslashes.
219 $val =~ s/\\$/ /mg;
220 return $val;
223 sub raw_value ($)
225 my ($self) = @_;
226 return $self->{'value'};
229 sub type ($)
231 my ($self) = @_;
232 return $self->{'type'};
235 sub pretty ($)
237 my ($self) = @_;
238 return $self->{'pretty'};
241 =item C<$def-E<gt>set_owner ($owner, $location)>
243 Change the owner of a definition. This usually happens because
244 the user used C<+=> on an Automake variable, so (s)he now owns
245 the content. C<$location> should be an instance of L<Automake::Location>
246 indicating where the change took place.
248 =cut
250 sub set_owner ($$$)
252 my ($self, $owner, $location) = @_;
253 # We always adjust the location when the owner changes (even for
254 # '+=' statements). The risk otherwise is to warn about
255 # a VAR_MAKEFILE variable and locate it in configure.ac...
256 $self->{'owner'} = $owner;
257 $self->{'location'} = $location;
260 =item C<$def-E<gt>set_seen>
262 =item C<$bool = $def-E<gt>seen>
264 These function allows Automake to mark (C<set_seen>) variable that
265 it has examined in some way, and latter check (using C<seen>) for
266 unused variables. Unused variables usually indicate typos.
268 =cut
270 sub set_seen ($)
272 my ($self) = @_;
273 $self->{'seen'} = 1;
276 sub seen ($)
278 my ($self) = @_;
279 return $self->{'seen'};
282 =item C<$str = $def-E<gt>dump>
284 Format the contents of C<$def> as a human-readable string,
285 for debugging.
287 =cut
289 sub dump ($)
291 my ($self) = @_;
292 my $owner = $self->owner;
294 if ($owner == VAR_AUTOMAKE)
296 $owner = 'Automake';
298 elsif ($owner == VAR_CONFIGURE)
300 $owner = 'Configure';
302 elsif ($owner == VAR_MAKEFILE)
304 $owner = 'Makefile';
306 else
308 prog_error ("unexpected owner");
311 my $where = $self->location->dump;
312 my $comment = $self->comment;
313 my $value = $self->raw_value;
314 my $type = $self->type;
316 return "{
317 type: $type=
318 where: $where comment: $comment
319 value: $value
320 owner: $owner
321 }\n";
324 =back
326 =head1 SEE ALSO
328 L<Automake::Variable>, L<Automake::ItemDef>.
330 =cut
334 ### Setup "GNU" style for perl-mode and cperl-mode.
335 ## Local Variables:
336 ## perl-indent-level: 2
337 ## perl-continued-statement-offset: 2
338 ## perl-continued-brace-offset: 0
339 ## perl-brace-offset: 0
340 ## perl-brace-imaginary-offset: 0
341 ## perl-label-offset: -2
342 ## cperl-indent-level: 2
343 ## cperl-brace-offset: 0
344 ## cperl-continued-brace-offset: 0
345 ## cperl-label-offset: -2
346 ## cperl-extra-newline-before-brace: t
347 ## cperl-merge-trailing-else: nil
348 ## cperl-continued-statement-offset: 2
349 ## End: