VarDef.pm 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. # Copyright (C) 2003-2017 Free Software Foundation, Inc.
  2. # This program is free software; you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation; either version 2, or (at your option)
  5. # any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License
  11. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. package Automake::VarDef;
  13. use 5.006;
  14. use strict;
  15. use Carp;
  16. use Automake::ChannelDefs;
  17. use Automake::ItemDef;
  18. require Exporter;
  19. use vars '@ISA', '@EXPORT';
  20. @ISA = qw/Automake::ItemDef Exporter/;
  21. @EXPORT = qw (&VAR_AUTOMAKE &VAR_CONFIGURE &VAR_MAKEFILE
  22. &VAR_ASIS &VAR_PRETTY &VAR_SILENT &VAR_SORTED);
  23. =head1 NAME
  24. Automake::VarDef - a class for variable definitions
  25. =head1 SYNOPSIS
  26. use Automake::VarDef;
  27. use Automake::Location;
  28. # Create a VarDef for a definition such as
  29. # | # any comment
  30. # | foo = bar # more comment
  31. # in Makefile.am
  32. my $loc = new Automake::Location 'Makefile.am:2';
  33. my $def = new Automake::VarDef ('foo', 'bar # more comment',
  34. '# any comment',
  35. $loc, '', VAR_MAKEFILE, VAR_ASIS);
  36. # Appending to a definition.
  37. $def->append ('value to append', 'comment to append');
  38. # Accessors.
  39. my $value = $def->value; # with trailing '#' comments and
  40. # continuation ("\\\n") omitted.
  41. my $value = $def->raw_value; # the real value, as passed to new().
  42. my $comment = $def->comment;
  43. my $location = $def->location;
  44. my $type = $def->type;
  45. my $owner = $def->owner;
  46. my $pretty = $def->pretty;
  47. # Changing owner.
  48. $def->set_owner (VAR_CONFIGURE,
  49. new Automake::Location 'configure.ac:15');
  50. # Marking examined definitions.
  51. $def->set_seen;
  52. my $seen_p = $def->seen;
  53. # Printing a variable for debugging.
  54. print STDERR $def->dump;
  55. =head1 DESCRIPTION
  56. This class gathers data related to one Makefile-variable definition.
  57. =head2 Constants
  58. =over 4
  59. =item C<VAR_AUTOMAKE>, C<VAR_CONFIGURE>, C<VAR_MAKEFILE>
  60. Possible owners for variables. A variable can be defined
  61. by Automake, in F<configure.ac> (using C<AC_SUBST>), or in
  62. the user's F<Makefile.am>.
  63. =cut
  64. # Defined so that the owner of a variable can only be increased (e.g
  65. # Automake should not override a configure or Makefile variable).
  66. use constant VAR_AUTOMAKE => 0; # Variable defined by Automake.
  67. use constant VAR_CONFIGURE => 1;# Variable defined in configure.ac.
  68. use constant VAR_MAKEFILE => 2; # Variable defined in Makefile.am.
  69. =item C<VAR_ASIS>, C<VAR_PRETTY>, C<VAR_SILENT>, C<VAR_SORTED>
  70. Possible print styles. C<VAR_ASIS> variables should be output as-is.
  71. C<VAR_PRETTY> variables are wrapped on multiple lines if they cannot
  72. fit on one. C<VAR_SILENT> variables are not output at all. Finally,
  73. C<VAR_SORTED> variables should be sorted and then handled as
  74. C<VAR_PRETTY> variables.
  75. C<VAR_SILENT> variables can also be overridden silently (unlike the
  76. other kinds of variables whose overriding may sometimes produce
  77. warnings).
  78. =cut
  79. # Possible values for pretty.
  80. use constant VAR_ASIS => 0; # Output as-is.
  81. use constant VAR_PRETTY => 1; # Pretty printed on output.
  82. use constant VAR_SILENT => 2; # Not output. (Can also be
  83. # overridden silently.)
  84. use constant VAR_SORTED => 3; # Sorted and pretty-printed.
  85. =back
  86. =head2 Methods
  87. C<VarDef> defines the following methods in addition to those inherited
  88. from L<Automake::ItemDef>.
  89. =over 4
  90. =item C<my $def = new Automake::VarDef ($varname, $value, $comment, $location, $type, $owner, $pretty)>
  91. Create a new Makefile-variable definition. C<$varname> is the name of
  92. the variable being defined and C<$value> its value.
  93. C<$comment> is any comment preceding the definition. (Because
  94. Automake reorders variable definitions in the output, it also tries to
  95. carry comments around.)
  96. C<$location> is the place where the definition occurred, it should be
  97. an instance of L<Automake::Location>.
  98. C<$type> should be C<''> for definitions made with C<=>, and C<':'>
  99. for those made with C<:=>.
  100. C<$owner> specifies who owns the variables, it can be one of
  101. C<VAR_AUTOMAKE>, C<VAR_CONFIGURE>, or C<VAR_MAKEFILE> (see these
  102. definitions).
  103. Finally, C<$pretty> tells how the variable should be output, and can
  104. be one of C<VAR_ASIS>, C<VAR_PRETTY>, or C<VAR_SILENT>, or
  105. C<VAR_SORTED> (see these definitions).
  106. =cut
  107. sub new ($$$$$$$$)
  108. {
  109. my ($class, $var, $value, $comment, $location, $type, $owner, $pretty) = @_;
  110. # A user variable must be set by either '=' or ':=', and later
  111. # promoted to '+='.
  112. if ($owner != VAR_AUTOMAKE && $type eq '+')
  113. {
  114. error $location, "$var must be set with '=' before using '+='";
  115. }
  116. my $self = Automake::ItemDef::new ($class, $comment, $location, $owner);
  117. $self->{'value'} = $value;
  118. $self->{'type'} = $type;
  119. $self->{'pretty'} = $pretty;
  120. $self->{'seen'} = 0;
  121. return $self;
  122. }
  123. =item C<$def-E<gt>append ($value, $comment)>
  124. Append C<$value> and <$comment> to the existing value and comment of
  125. C<$def>. This is normally called on C<+=> definitions.
  126. =cut
  127. sub append ($$$)
  128. {
  129. my ($self, $value, $comment) = @_;
  130. $self->{'comment'} .= $comment;
  131. my $val = $self->{'value'};
  132. # Strip comments from augmented variables. This is so that
  133. # VAR = foo # com
  134. # VAR += bar
  135. # does not become
  136. # VAR = foo # com bar
  137. # Furthermore keeping '#' would not be portable if the variable is
  138. # output on multiple lines.
  139. $val =~ s/ ?#.*//;
  140. # Insert a separator, if required.
  141. $val .= ' ' if $val;
  142. $self->{'value'} = $val . $value;
  143. # Turn ASIS appended variables into PRETTY variables. This is to
  144. # cope with 'make' implementation that cannot read very long lines.
  145. $self->{'pretty'} = VAR_PRETTY if $self->{'pretty'} == VAR_ASIS;
  146. }
  147. =item C<$def-E<gt>value>
  148. =item C<$def-E<gt>raw_value>
  149. =item C<$def-E<gt>type>
  150. =item C<$def-E<gt>pretty>
  151. Accessors to the various constituents of a C<VarDef>. See the
  152. documentation of C<new>'s arguments for a description of these.
  153. =cut
  154. sub value ($)
  155. {
  156. my ($self) = @_;
  157. my $val = $self->raw_value;
  158. # Strip anything past '#'. '#' characters cannot be escaped
  159. # in Makefiles, so we don't have to be smart.
  160. $val =~ s/#.*$//s;
  161. # Strip backslashes.
  162. $val =~ s/\\$/ /mg;
  163. return $val;
  164. }
  165. sub raw_value ($)
  166. {
  167. my ($self) = @_;
  168. return $self->{'value'};
  169. }
  170. sub type ($)
  171. {
  172. my ($self) = @_;
  173. return $self->{'type'};
  174. }
  175. sub pretty ($)
  176. {
  177. my ($self) = @_;
  178. return $self->{'pretty'};
  179. }
  180. =item C<$def-E<gt>set_owner ($owner, $location)>
  181. Change the owner of a definition. This usually happens because
  182. the user used C<+=> on an Automake variable, so (s)he now owns
  183. the content. C<$location> should be an instance of L<Automake::Location>
  184. indicating where the change took place.
  185. =cut
  186. sub set_owner ($$$)
  187. {
  188. my ($self, $owner, $location) = @_;
  189. # We always adjust the location when the owner changes (even for
  190. # '+=' statements). The risk otherwise is to warn about
  191. # a VAR_MAKEFILE variable and locate it in configure.ac...
  192. $self->{'owner'} = $owner;
  193. $self->{'location'} = $location;
  194. }
  195. =item C<$def-E<gt>set_seen>
  196. =item C<$bool = $def-E<gt>seen>
  197. These function allows Automake to mark (C<set_seen>) variable that
  198. it has examined in some way, and latter check (using C<seen>) for
  199. unused variables. Unused variables usually indicate typos.
  200. =cut
  201. sub set_seen ($)
  202. {
  203. my ($self) = @_;
  204. $self->{'seen'} = 1;
  205. }
  206. sub seen ($)
  207. {
  208. my ($self) = @_;
  209. return $self->{'seen'};
  210. }
  211. =item C<$str = $def-E<gt>dump>
  212. Format the contents of C<$def> as a human-readable string,
  213. for debugging.
  214. =cut
  215. sub dump ($)
  216. {
  217. my ($self) = @_;
  218. my $owner = $self->owner;
  219. if ($owner == VAR_AUTOMAKE)
  220. {
  221. $owner = 'Automake';
  222. }
  223. elsif ($owner == VAR_CONFIGURE)
  224. {
  225. $owner = 'Configure';
  226. }
  227. elsif ($owner == VAR_MAKEFILE)
  228. {
  229. $owner = 'Makefile';
  230. }
  231. else
  232. {
  233. prog_error ("unexpected owner");
  234. }
  235. my $where = $self->location->dump;
  236. my $comment = $self->comment;
  237. my $value = $self->raw_value;
  238. my $type = $self->type;
  239. return "{
  240. type: $type=
  241. where: $where comment: $comment
  242. value: $value
  243. owner: $owner
  244. }\n";
  245. }
  246. =back
  247. =head1 SEE ALSO
  248. L<Automake::Variable>, L<Automake::ItemDef>.
  249. =cut
  250. 1;
  251. ### Setup "GNU" style for perl-mode and cperl-mode.
  252. ## Local Variables:
  253. ## perl-indent-level: 2
  254. ## perl-continued-statement-offset: 2
  255. ## perl-continued-brace-offset: 0
  256. ## perl-brace-offset: 0
  257. ## perl-brace-imaginary-offset: 0
  258. ## perl-label-offset: -2
  259. ## cperl-indent-level: 2
  260. ## cperl-brace-offset: 0
  261. ## cperl-continued-brace-offset: 0
  262. ## cperl-label-offset: -2
  263. ## cperl-extra-newline-before-brace: t
  264. ## cperl-merge-trailing-else: nil
  265. ## cperl-continued-statement-offset: 2
  266. ## End: