1 | # Copyright (C) 2003, 2004 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, write to the Free Software
|
---|
15 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
---|
16 | # 02111-1307, USA.
|
---|
17 |
|
---|
18 | package Automake::Variable;
|
---|
19 | use strict;
|
---|
20 | use Carp;
|
---|
21 |
|
---|
22 | use Automake::Channels;
|
---|
23 | use Automake::ChannelDefs;
|
---|
24 | use Automake::Configure_ac;
|
---|
25 | use Automake::Item;
|
---|
26 | use Automake::VarDef;
|
---|
27 | use Automake::Condition qw (TRUE FALSE);
|
---|
28 | use Automake::DisjConditions;
|
---|
29 | use Automake::General 'uniq';
|
---|
30 | use Automake::Wrap 'makefile_wrap';
|
---|
31 |
|
---|
32 | require Exporter;
|
---|
33 | use vars '@ISA', '@EXPORT', '@EXPORT_OK';
|
---|
34 | @ISA = qw/Automake::Item Exporter/;
|
---|
35 | @EXPORT = qw (err_var msg_var msg_cond_var reject_var
|
---|
36 | var rvar vardef rvardef
|
---|
37 | variables
|
---|
38 | scan_variable_expansions check_variable_expansions
|
---|
39 | variable_delete
|
---|
40 | variables_dump
|
---|
41 | set_seen
|
---|
42 | require_variables
|
---|
43 | variable_value
|
---|
44 | output_variables
|
---|
45 | transform_variable_recursively);
|
---|
46 |
|
---|
47 | =head1 NAME
|
---|
48 |
|
---|
49 | Automake::Variable - support for variable definitions
|
---|
50 |
|
---|
51 | =head1 SYNOPSIS
|
---|
52 |
|
---|
53 | use Automake::Variable;
|
---|
54 | use Automake::VarDef;
|
---|
55 |
|
---|
56 | # Defining a variable.
|
---|
57 | Automake::Variable::define($varname, $owner, $type,
|
---|
58 | $cond, $value, $comment,
|
---|
59 | $where, $pretty)
|
---|
60 |
|
---|
61 | # Looking up a variable.
|
---|
62 | my $var = var $varname;
|
---|
63 | if ($var)
|
---|
64 | {
|
---|
65 | ...
|
---|
66 | }
|
---|
67 |
|
---|
68 | # Looking up a variable that is assumed to exist.
|
---|
69 | my $var = rvar $varname;
|
---|
70 |
|
---|
71 | # The list of conditions where $var has been defined.
|
---|
72 | # ($var->conditions is an Automake::DisjConditions,
|
---|
73 | # $var->conditions->conds is a list of Automake::Condition.)
|
---|
74 | my @conds = $var->conditions->conds
|
---|
75 |
|
---|
76 | # Accessing to the definition in Condition $cond.
|
---|
77 | # $def is an Automake::VarDef.
|
---|
78 | my $def = $var->def ($cond);
|
---|
79 | if ($def)
|
---|
80 | {
|
---|
81 | ...
|
---|
82 | }
|
---|
83 |
|
---|
84 | # When the conditional definition is assumed to exist, use
|
---|
85 | my $def = $var->rdef ($cond);
|
---|
86 |
|
---|
87 |
|
---|
88 | =head1 DESCRIPTION
|
---|
89 |
|
---|
90 | This package provides support for Makefile variable definitions.
|
---|
91 |
|
---|
92 | An C<Automake::Variable> is a variable name associated to possibly
|
---|
93 | many conditional definitions. These definitions are instances
|
---|
94 | of C<Automake::VarDef>.
|
---|
95 |
|
---|
96 | Therefore obtaining the value of a variable under a given
|
---|
97 | condition involves two lookups. One to look up the variable,
|
---|
98 | and one to look up the conditional definition:
|
---|
99 |
|
---|
100 | my $var = var $name;
|
---|
101 | if ($var)
|
---|
102 | {
|
---|
103 | my $def = $var->def ($cond);
|
---|
104 | if ($def)
|
---|
105 | {
|
---|
106 | return $def->value;
|
---|
107 | }
|
---|
108 | ...
|
---|
109 | }
|
---|
110 | ...
|
---|
111 |
|
---|
112 | When it is known that the variable and the definition
|
---|
113 | being looked up exist, the above can be simplified to
|
---|
114 |
|
---|
115 | return var ($name)->def ($cond)->value; # Do not write this.
|
---|
116 |
|
---|
117 | but is better written
|
---|
118 |
|
---|
119 | return rvar ($name)->rdef ($cond)->value;
|
---|
120 |
|
---|
121 | or even
|
---|
122 |
|
---|
123 | return rvardef ($name, $cond)->value;
|
---|
124 |
|
---|
125 | The I<r> variants of the C<var>, C<def>, and C<vardef> methods add an
|
---|
126 | extra test to ensure that the lookup succeeded, and will diagnose
|
---|
127 | failures as internal errors (with a message which is much more
|
---|
128 | informative than Perl's warning about calling a method on a
|
---|
129 | non-object).
|
---|
130 |
|
---|
131 | =cut
|
---|
132 |
|
---|
133 | my $_VARIABLE_PATTERN = '^[.A-Za-z0-9_@]+' . "\$";
|
---|
134 |
|
---|
135 | # The order in which variables should be output. (May contain
|
---|
136 | # duplicates -- only the first occurrence matters.)
|
---|
137 | my @_var_order;
|
---|
138 |
|
---|
139 | # This keeps track of all variables defined by &_gen_varname.
|
---|
140 | # $_gen_varname{$base} is a hash for all variable defined with
|
---|
141 | # prefix `$base'. Values stored this this hash are the variable names.
|
---|
142 | # Keys have the form "(COND1)VAL1(COND2)VAL2..." where VAL1 and VAL2
|
---|
143 | # are the values of the variable for condition COND1 and COND2.
|
---|
144 | my %_gen_varname = ();
|
---|
145 |
|
---|
146 | # Declare the macros that define known variables, so we can
|
---|
147 | # hint the user if she try to use one of these variables.
|
---|
148 |
|
---|
149 | # Macros accessible via aclocal.
|
---|
150 | my %_am_macro_for_var =
|
---|
151 | (
|
---|
152 | ANSI2KNR => 'AM_C_PROTOTYPES',
|
---|
153 | CCAS => 'AM_PROG_AS',
|
---|
154 | CCASFLAGS => 'AM_PROG_AS',
|
---|
155 | EMACS => 'AM_PATH_LISPDIR',
|
---|
156 | GCJ => 'AM_PROG_GCJ',
|
---|
157 | LEX => 'AM_PROG_LEX',
|
---|
158 | LIBTOOL => 'AC_PROG_LIBTOOL',
|
---|
159 | lispdir => 'AM_PATH_LISPDIR',
|
---|
160 | pkgpyexecdir => 'AM_PATH_PYTHON',
|
---|
161 | pkgpythondir => 'AM_PATH_PYTHON',
|
---|
162 | pyexecdir => 'AM_PATH_PYTHON',
|
---|
163 | PYTHON => 'AM_PATH_PYTHON',
|
---|
164 | pythondir => 'AM_PATH_PYTHON',
|
---|
165 | U => 'AM_C_PROTOTYPES',
|
---|
166 | );
|
---|
167 |
|
---|
168 | # Macros shipped with Autoconf.
|
---|
169 | my %_ac_macro_for_var =
|
---|
170 | (
|
---|
171 | ALLOCA => 'AC_FUNC_ALLOCA',
|
---|
172 | CC => 'AC_PROG_CC',
|
---|
173 | CFLAGS => 'AC_PROG_CC',
|
---|
174 | CXX => 'AC_PROG_CXX',
|
---|
175 | CXXFLAGS => 'AC_PROG_CXX',
|
---|
176 | F77 => 'AC_PROG_F77',
|
---|
177 | F77FLAGS => 'AC_PROG_F77',
|
---|
178 | RANLIB => 'AC_PROG_RANLIB',
|
---|
179 | YACC => 'AC_PROG_YACC',
|
---|
180 | );
|
---|
181 |
|
---|
182 | # The name of the configure.ac file.
|
---|
183 | my $configure_ac = find_configure_ac;
|
---|
184 |
|
---|
185 | # Variables that can be overriden without complaint from -Woverride
|
---|
186 | my %_silent_variable_override =
|
---|
187 | (AM_MAKEINFOHTMLFLAGS => 1,
|
---|
188 | AR => 1,
|
---|
189 | ARFLAGS => 1,
|
---|
190 | DEJATOOL => 1,
|
---|
191 | JAVAC => 1,
|
---|
192 | JAVAROOT => 1);
|
---|
193 |
|
---|
194 | # This hash records helper variables used to implement conditional '+='.
|
---|
195 | # Keys have the form "VAR:CONDITIONS". The value associated to a key is
|
---|
196 | # the named of the helper variable used to append to VAR in CONDITIONS.
|
---|
197 | my %_appendvar = ();
|
---|
198 |
|
---|
199 | # Each call to C<Automake::Variable::traverse_recursively> gets an
|
---|
200 | # unique label. This is used to detect recursively defined variables.
|
---|
201 | my $_traversal = 0;
|
---|
202 |
|
---|
203 |
|
---|
204 | =head2 Error reporting functions
|
---|
205 |
|
---|
206 | In these functions, C<$var> can be either a variable name, or
|
---|
207 | an instance of C<Automake::Variable>.
|
---|
208 |
|
---|
209 | =over 4
|
---|
210 |
|
---|
211 | =item C<err_var ($var, $message, [%options])>
|
---|
212 |
|
---|
213 | Uncategorized errors about variables.
|
---|
214 |
|
---|
215 | =cut
|
---|
216 |
|
---|
217 | sub err_var ($$;%)
|
---|
218 | {
|
---|
219 | msg_var ('error', @_);
|
---|
220 | }
|
---|
221 |
|
---|
222 | =item C<msg_cond_var ($channel, $cond, $var, $message, [%options])>
|
---|
223 |
|
---|
224 | Messages about conditional variable.
|
---|
225 |
|
---|
226 | =cut
|
---|
227 |
|
---|
228 | sub msg_cond_var ($$$$;%)
|
---|
229 | {
|
---|
230 | my ($channel, $cond, $var, $msg, %opts) = @_;
|
---|
231 | my $v = ref ($var) ? $var : rvar ($var);
|
---|
232 | msg $channel, $v->rdef ($cond)->location, $msg, %opts;
|
---|
233 | }
|
---|
234 |
|
---|
235 | =item C<msg_var ($channel, $var, $message, [%options])>
|
---|
236 |
|
---|
237 | Messages about variables.
|
---|
238 |
|
---|
239 | =cut
|
---|
240 |
|
---|
241 | sub msg_var ($$$;%)
|
---|
242 | {
|
---|
243 | my ($channel, $var, $msg, %opts) = @_;
|
---|
244 | my $v = ref ($var) ? $var : rvar ($var);
|
---|
245 | # Don't know which condition is concerned. Pick any.
|
---|
246 | my $cond = $v->conditions->one_cond;
|
---|
247 | msg_cond_var $channel, $cond, $v, $msg, %opts;
|
---|
248 | }
|
---|
249 |
|
---|
250 | =item C<$bool = reject_var ($varname, $error_msg)>
|
---|
251 |
|
---|
252 | Bail out with C<$error_msg> if a variable with name C<$varname> has
|
---|
253 | been defined.
|
---|
254 |
|
---|
255 | Return true iff C<$varname> is defined.
|
---|
256 |
|
---|
257 | =cut
|
---|
258 |
|
---|
259 | sub reject_var ($$)
|
---|
260 | {
|
---|
261 | my ($var, $msg) = @_;
|
---|
262 | my $v = var ($var);
|
---|
263 | if ($v)
|
---|
264 | {
|
---|
265 | err_var $v, $msg;
|
---|
266 | return 1;
|
---|
267 | }
|
---|
268 | return 0;
|
---|
269 | }
|
---|
270 |
|
---|
271 | =back
|
---|
272 |
|
---|
273 | =head2 Administrative functions
|
---|
274 |
|
---|
275 | =over 4
|
---|
276 |
|
---|
277 | =item C<Automake::Variable::hook ($varname, $fun)>
|
---|
278 |
|
---|
279 | Declare a function to be called whenever a variable
|
---|
280 | named C<$varname> is defined or redefined.
|
---|
281 |
|
---|
282 | C<$fun> should take two arguments: C<$type> and C<$value>.
|
---|
283 | When type is C<''> or <':'>, C<$value> is the value being
|
---|
284 | assigned to C<$varname>. When C<$type> is C<'+'>, C<$value>
|
---|
285 | is the value being appended to C<$varname>.
|
---|
286 |
|
---|
287 | =cut
|
---|
288 |
|
---|
289 | use vars '%_hooks';
|
---|
290 | sub hook ($$)
|
---|
291 | {
|
---|
292 | my ($var, $fun) = @_;
|
---|
293 | $_hooks{$var} = $fun;
|
---|
294 | }
|
---|
295 |
|
---|
296 | =item C<variables>
|
---|
297 |
|
---|
298 | Returns the list of all L<Automake::Variable> instances. (I.e., all
|
---|
299 | variables defined so far.)
|
---|
300 |
|
---|
301 | =cut
|
---|
302 |
|
---|
303 | use vars '%_variable_dict';
|
---|
304 | sub variables ()
|
---|
305 | {
|
---|
306 | return values %_variable_dict;
|
---|
307 | }
|
---|
308 |
|
---|
309 | =item C<Automake::Variable::reset>
|
---|
310 |
|
---|
311 | The I<forget all> function. Clears all know variables and reset some
|
---|
312 | other internal data.
|
---|
313 |
|
---|
314 | =cut
|
---|
315 |
|
---|
316 | sub reset ()
|
---|
317 | {
|
---|
318 | %_variable_dict = ();
|
---|
319 | %_appendvar = ();
|
---|
320 | @_var_order = ();
|
---|
321 | %_gen_varname = ();
|
---|
322 | $_traversal = 0;
|
---|
323 | }
|
---|
324 |
|
---|
325 | =item C<var ($varname)>
|
---|
326 |
|
---|
327 | Return the C<Automake::Variable> object for the variable
|
---|
328 | named C<$varname> if defined. Return 0 otherwise.
|
---|
329 |
|
---|
330 | =cut
|
---|
331 |
|
---|
332 | sub var ($)
|
---|
333 | {
|
---|
334 | my ($name) = @_;
|
---|
335 | return $_variable_dict{$name} if exists $_variable_dict{$name};
|
---|
336 | return 0;
|
---|
337 | }
|
---|
338 |
|
---|
339 | =item C<vardef ($varname, $cond)>
|
---|
340 |
|
---|
341 | Return the C<Automake::VarDef> object for the variable named
|
---|
342 | C<$varname> if defined in condition C<$cond>. Return false
|
---|
343 | if the condition or the variable does not exist.
|
---|
344 |
|
---|
345 | =cut
|
---|
346 |
|
---|
347 | sub vardef ($$)
|
---|
348 | {
|
---|
349 | my ($name, $cond) = @_;
|
---|
350 | my $var = var $name;
|
---|
351 | return $var && $var->def ($cond);
|
---|
352 | }
|
---|
353 |
|
---|
354 | # Create the variable if it does not exist.
|
---|
355 | # This is used only by other functions in this package.
|
---|
356 | sub _cvar ($)
|
---|
357 | {
|
---|
358 | my ($name) = @_;
|
---|
359 | my $v = var $name;
|
---|
360 | return $v if $v;
|
---|
361 | return _new Automake::Variable $name;
|
---|
362 | }
|
---|
363 |
|
---|
364 | =item C<rvar ($varname)>
|
---|
365 |
|
---|
366 | Return the C<Automake::Variable> object for the variable named
|
---|
367 | C<$varname>. Abort with an internal error if the variable was not
|
---|
368 | defined.
|
---|
369 |
|
---|
370 | The I<r> in front of C<var> stands for I<required>. One
|
---|
371 | should call C<rvar> to assert the variable's existence.
|
---|
372 |
|
---|
373 | =cut
|
---|
374 |
|
---|
375 | sub rvar ($)
|
---|
376 | {
|
---|
377 | my ($name) = @_;
|
---|
378 | my $v = var $name;
|
---|
379 | prog_error ("undefined variable $name\n" . &variables_dump)
|
---|
380 | unless $v;
|
---|
381 | return $v;
|
---|
382 | }
|
---|
383 |
|
---|
384 | =item C<rvardef ($varname, $cond)>
|
---|
385 |
|
---|
386 | Return the C<Automake::VarDef> object for the variable named
|
---|
387 | C<$varname> if defined in condition C<$cond>. Abort with an internal
|
---|
388 | error if the condition or the variable does not exist.
|
---|
389 |
|
---|
390 | =cut
|
---|
391 |
|
---|
392 | sub rvardef ($$)
|
---|
393 | {
|
---|
394 | my ($name, $cond) = @_;
|
---|
395 | return rvar ($name)->rdef ($cond);
|
---|
396 | }
|
---|
397 |
|
---|
398 | =back
|
---|
399 |
|
---|
400 | =head2 Methods
|
---|
401 |
|
---|
402 | C<Automake::Variable> is a subclass of C<Automake::Item>. See
|
---|
403 | that package for inherited methods.
|
---|
404 |
|
---|
405 | Here are the methods specific to the C<Automake::Variable> instances.
|
---|
406 | Use the C<define> function, described latter, to create such objects.
|
---|
407 |
|
---|
408 | =over 4
|
---|
409 |
|
---|
410 | =cut
|
---|
411 |
|
---|
412 | # Create Automake::Variable objects. This is used
|
---|
413 | # only in this file. Other users should use
|
---|
414 | # the "define" function.
|
---|
415 | sub _new ($$)
|
---|
416 | {
|
---|
417 | my ($class, $name) = @_;
|
---|
418 | my $self = Automake::Item::new ($class, $name);
|
---|
419 | $self->{'scanned'} = 0;
|
---|
420 | $_variable_dict{$name} = $self;
|
---|
421 | return $self;
|
---|
422 | }
|
---|
423 |
|
---|
424 | # _check_ambiguous_condition ($SELF, $COND, $WHERE)
|
---|
425 | # -------------------------------------------------
|
---|
426 | # Check for an ambiguous conditional. This is called when a variable
|
---|
427 | # is being defined conditionally. If we already know about a
|
---|
428 | # definition that is true under the same conditions, then we have an
|
---|
429 | # ambiguity.
|
---|
430 | sub _check_ambiguous_condition ($$$)
|
---|
431 | {
|
---|
432 | my ($self, $cond, $where) = @_;
|
---|
433 | my $var = $self->name;
|
---|
434 | my ($message, $ambig_cond) = $self->conditions->ambiguous_p ($var, $cond);
|
---|
435 |
|
---|
436 | # We allow silent variables to be overridden silently.
|
---|
437 | my $def = $self->def ($cond);
|
---|
438 | if ($message && !($def && $def->pretty == VAR_SILENT))
|
---|
439 | {
|
---|
440 | msg 'syntax', $where, "$message ...", partial => 1;
|
---|
441 | msg_var ('syntax', $var, "... `$var' previously defined here");
|
---|
442 | verb ($self->dump);
|
---|
443 | }
|
---|
444 | }
|
---|
445 |
|
---|
446 | =item C<$bool = $var-E<gt>check_defined_unconditionally ([$parent, $parent_cond])>
|
---|
447 |
|
---|
448 | Warn if the variable is conditionally defined. C<$parent> is the name
|
---|
449 | of the parent variable, and C<$parent_cond> the condition of the parent
|
---|
450 | definition. These two variables are used to display diagnostics.
|
---|
451 |
|
---|
452 | =cut
|
---|
453 |
|
---|
454 | sub check_defined_unconditionally ($;$$)
|
---|
455 | {
|
---|
456 | my ($self, $parent, $parent_cond) = @_;
|
---|
457 |
|
---|
458 | if (!$self->conditions->true)
|
---|
459 | {
|
---|
460 | if ($parent)
|
---|
461 | {
|
---|
462 | msg_cond_var ('unsupported', $parent_cond, $parent,
|
---|
463 | "automake does not support conditional definition of "
|
---|
464 | . $self->name . " in $parent");
|
---|
465 | }
|
---|
466 | else
|
---|
467 | {
|
---|
468 | msg_var ('unsupported', $self,
|
---|
469 | "automake does not support " . $self->name
|
---|
470 | . " being defined conditionally");
|
---|
471 | }
|
---|
472 | }
|
---|
473 | }
|
---|
474 |
|
---|
475 | =item C<$str = $var-E<gt>output ([@conds])>
|
---|
476 |
|
---|
477 | Format all the definitions of C<$var> if C<@cond> is not specified,
|
---|
478 | else only that corresponding to C<@cond>.
|
---|
479 |
|
---|
480 | =cut
|
---|
481 |
|
---|
482 | sub output ($@)
|
---|
483 | {
|
---|
484 | my ($self, @conds) = @_;
|
---|
485 |
|
---|
486 | @conds = $self->conditions->conds
|
---|
487 | unless @conds;
|
---|
488 |
|
---|
489 | my $res = '';
|
---|
490 | my $name = $self->name;
|
---|
491 |
|
---|
492 | foreach my $cond (@conds)
|
---|
493 | {
|
---|
494 | my $def = $self->def ($cond);
|
---|
495 | prog_error ("unknown condition `" . $cond->human . "' for `"
|
---|
496 | . $self->name . "'")
|
---|
497 | unless $def;
|
---|
498 |
|
---|
499 | next
|
---|
500 | if $def->pretty == VAR_SILENT;
|
---|
501 |
|
---|
502 | $res .= $def->comment;
|
---|
503 |
|
---|
504 | my $val = $def->raw_value;
|
---|
505 | my $equals = $def->type eq ':' ? ':=' : '=';
|
---|
506 | my $str = $cond->subst_string;
|
---|
507 |
|
---|
508 |
|
---|
509 | if ($def->pretty == VAR_ASIS)
|
---|
510 | {
|
---|
511 | my $output_var = "$name $equals $val";
|
---|
512 | $output_var =~ s/^/$str/meg;
|
---|
513 | $res .= "$output_var\n";
|
---|
514 | }
|
---|
515 | elsif ($def->pretty == VAR_PRETTY)
|
---|
516 | {
|
---|
517 | # Suppress escaped new lines. &makefile_wrap will
|
---|
518 | # add them back, maybe at other places.
|
---|
519 | $val =~ s/\\$//mg;
|
---|
520 | my $wrap = makefile_wrap ("$str$name $equals", "$str\t",
|
---|
521 | split (' ', $val));
|
---|
522 |
|
---|
523 | # If the last line of the definition is made only of
|
---|
524 | # @substitutions@, append an empty variable to make sure it
|
---|
525 | # cannot be substituted as a blank line (that would confuse
|
---|
526 | # HP-UX Make).
|
---|
527 | $wrap = makefile_wrap ("$str$name $equals", "$str\t",
|
---|
528 | split (' ', $val), '$(am__empty)')
|
---|
529 | if $wrap =~ /\n(\s*@\w+@)+\s*$/;
|
---|
530 |
|
---|
531 | $res .= $wrap;
|
---|
532 | }
|
---|
533 | else # ($def->pretty == VAR_SORTED)
|
---|
534 | {
|
---|
535 | # Suppress escaped new lines. &makefile_wrap will
|
---|
536 | # add them back, maybe at other places.
|
---|
537 | $val =~ s/\\$//mg;
|
---|
538 | $res .= makefile_wrap ("$str$name $equals", "$str\t",
|
---|
539 | sort (split (' ' , $val)));
|
---|
540 | }
|
---|
541 | }
|
---|
542 | return $res;
|
---|
543 | }
|
---|
544 |
|
---|
545 | =item C<@values = $var-E<gt>value_as_list ($cond, [$parent, $parent_cond])>
|
---|
546 |
|
---|
547 | Get the value of C<$var> as a list, given a specified condition,
|
---|
548 | without recursing through any subvariables.
|
---|
549 |
|
---|
550 | C<$cond> is the condition of interest. C<$var> does not need
|
---|
551 | to be defined for condition C<$cond> exactly, but it needs
|
---|
552 | to be defined for at most one condition implied by C<$cond>.
|
---|
553 |
|
---|
554 | C<$parent> and C<$parent_cond> designate the name and the condition
|
---|
555 | of the parent variable, i.e., the variable in which C<$var> is
|
---|
556 | being expanded. These are used in diagnostics.
|
---|
557 |
|
---|
558 | For example, if C<A> is defined as "C<foo $(B) bar>" in condition
|
---|
559 | C<TRUE>, calling C<rvar ('A')->value_as_list (TRUE)> will return
|
---|
560 | C<("foo", "$(B)", "bar")>.
|
---|
561 |
|
---|
562 | =cut
|
---|
563 |
|
---|
564 | sub value_as_list ($$;$$)
|
---|
565 | {
|
---|
566 | my ($self, $cond, $parent, $parent_cond) = @_;
|
---|
567 | my @result;
|
---|
568 |
|
---|
569 | # Get value for given condition
|
---|
570 | my $onceflag;
|
---|
571 | foreach my $vcond ($self->conditions->conds)
|
---|
572 | {
|
---|
573 | if ($vcond->true_when ($cond))
|
---|
574 | {
|
---|
575 | # If there is more than one definitions of $var matching
|
---|
576 | # $cond then we are in trouble: tell the user we need a
|
---|
577 | # paddle. Continue by merging results from all conditions,
|
---|
578 | # although it doesn't make much sense.
|
---|
579 | $self->check_defined_unconditionally ($parent, $parent_cond)
|
---|
580 | if $onceflag;
|
---|
581 | $onceflag = 1;
|
---|
582 |
|
---|
583 | my $val = $self->rdef ($vcond)->value;
|
---|
584 | push @result, split (' ', $val);
|
---|
585 | }
|
---|
586 | }
|
---|
587 | return @result;
|
---|
588 | }
|
---|
589 |
|
---|
590 | =item C<@values = $var-E<gt>value_as_list_recursive ([%options])>
|
---|
591 |
|
---|
592 | Return the contents of C<$var> as a list, split on whitespace. This
|
---|
593 | will recursively follow C<$(...)> and C<${...}> inclusions. It
|
---|
594 | preserves C<@...@> substitutions.
|
---|
595 |
|
---|
596 | C<%options> is a list of option for C<Variable::traverse_recursively>
|
---|
597 | (see this method). The most useful is C<cond_filter>:
|
---|
598 |
|
---|
599 | $var->value_as_list_recursive (cond_filter => $cond)
|
---|
600 |
|
---|
601 | will return the contents of C<$var> and any subvariable in all
|
---|
602 | conditions implied by C<$cond>.
|
---|
603 |
|
---|
604 | C<%options> can also carry options specific to C<value_as_list_recursive>.
|
---|
605 | Presently, the only such option is C<location =E<gt> 1> which instructs
|
---|
606 | C<value_as_list_recursive> to return a list of C<[$location, @values]> pairs.
|
---|
607 |
|
---|
608 | =cut
|
---|
609 |
|
---|
610 | sub value_as_list_recursive ($;%)
|
---|
611 | {
|
---|
612 | my ($var, %options) = @_;
|
---|
613 |
|
---|
614 | return $var->traverse_recursively
|
---|
615 | (# Construct [$location, $value] pairs if requested.
|
---|
616 | sub {
|
---|
617 | my ($var, $val, $cond, $full_cond) = @_;
|
---|
618 | return [$var->rdef ($cond)->location, $val] if $options{'location'};
|
---|
619 | return $val;
|
---|
620 | },
|
---|
621 | # Collect results.
|
---|
622 | sub {
|
---|
623 | my ($var, $parent_cond, @allresults) = @_;
|
---|
624 | return map { my ($cond, @vals) = @$_; @vals } @allresults;
|
---|
625 | },
|
---|
626 | %options);
|
---|
627 | }
|
---|
628 |
|
---|
629 |
|
---|
630 | =item C<$bool = $var-E<gt>has_conditional_contents>
|
---|
631 |
|
---|
632 | Return 1 if C<$var> or one of its subvariable was conditionally
|
---|
633 | defined. Return 0 otherwise.
|
---|
634 |
|
---|
635 | =cut
|
---|
636 |
|
---|
637 | sub has_conditional_contents ($)
|
---|
638 | {
|
---|
639 | my ($self) = @_;
|
---|
640 |
|
---|
641 | # Traverse the variable recursively until we
|
---|
642 | # find a variable defined conditionally.
|
---|
643 | # Use `die' to abort the traversal, and pass it `$full_cond'
|
---|
644 | # to we can find easily whether the `eval' block aborted
|
---|
645 | # because we found a condition, or for some other error.
|
---|
646 | eval
|
---|
647 | {
|
---|
648 | $self->traverse_recursively
|
---|
649 | (sub
|
---|
650 | {
|
---|
651 | my ($subvar, $val, $cond, $full_cond) = @_;
|
---|
652 | die $full_cond if ! $full_cond->true;
|
---|
653 | return ();
|
---|
654 | },
|
---|
655 | sub { return (); });
|
---|
656 | };
|
---|
657 | if ($@)
|
---|
658 | {
|
---|
659 | return 1 if ref ($@) && $@->isa ("Automake::Condition");
|
---|
660 | # Propagate other errors.
|
---|
661 | die;
|
---|
662 | }
|
---|
663 | return 0;
|
---|
664 | }
|
---|
665 |
|
---|
666 |
|
---|
667 | =item C<$string = $var-E<gt>dump>
|
---|
668 |
|
---|
669 | Return a string describing all we know about C<$var>.
|
---|
670 | For debugging.
|
---|
671 |
|
---|
672 | =cut
|
---|
673 |
|
---|
674 | sub dump ($)
|
---|
675 | {
|
---|
676 | my ($self) = @_;
|
---|
677 |
|
---|
678 | my $text = $self->name . ": \n {\n";
|
---|
679 | foreach my $vcond ($self->conditions->conds)
|
---|
680 | {
|
---|
681 | $text .= " " . $vcond->human . " => " . $self->rdef ($vcond)->dump;
|
---|
682 | }
|
---|
683 | $text .= " }\n";
|
---|
684 | return $text;
|
---|
685 | }
|
---|
686 |
|
---|
687 |
|
---|
688 | =back
|
---|
689 |
|
---|
690 | =head2 Utility functions
|
---|
691 |
|
---|
692 | =over 4
|
---|
693 |
|
---|
694 | =item C<@list = scan_variable_expansions ($text)>
|
---|
695 |
|
---|
696 | Return the list of variable names expanded in C<$text>. Note that
|
---|
697 | unlike some other functions, C<$text> is not split on spaces before we
|
---|
698 | check for subvariables.
|
---|
699 |
|
---|
700 | =cut
|
---|
701 |
|
---|
702 | sub scan_variable_expansions ($)
|
---|
703 | {
|
---|
704 | my ($text) = @_;
|
---|
705 | my @result = ();
|
---|
706 |
|
---|
707 | # Strip comments.
|
---|
708 | $text =~ s/#.*$//;
|
---|
709 |
|
---|
710 | # Record each use of ${stuff} or $(stuff) that does not follow a $.
|
---|
711 | while ($text =~ /(?<!\$)\$(?:\{([^\}]*)\}|\(([^\)]*)\))/g)
|
---|
712 | {
|
---|
713 | my $var = $1 || $2;
|
---|
714 | # The occurence may look like $(string1[:subst1=[subst2]]) but
|
---|
715 | # we want only `string1'.
|
---|
716 | $var =~ s/:[^:=]*=[^=]*$//;
|
---|
717 | push @result, $var;
|
---|
718 | }
|
---|
719 |
|
---|
720 | return @result;
|
---|
721 | }
|
---|
722 |
|
---|
723 | =item C<check_variable_expansions ($text, $where)>
|
---|
724 |
|
---|
725 | Check variable expansions in C<$text> and warn about any name that
|
---|
726 | does not conform to POSIX. C<$where> is the location of C<$text>
|
---|
727 | for the error message.
|
---|
728 |
|
---|
729 | =cut
|
---|
730 |
|
---|
731 | sub check_variable_expansions ($$)
|
---|
732 | {
|
---|
733 | my ($text, $where) = @_;
|
---|
734 | # Catch expansion of variables whose name does not conform to POSIX.
|
---|
735 | foreach my $var (scan_variable_expansions ($text))
|
---|
736 | {
|
---|
737 | if ($var !~ /$_VARIABLE_PATTERN/o)
|
---|
738 | {
|
---|
739 | # If the variable name contains a space, it's likely
|
---|
740 | # to be a GNU make extension (such as $(addsuffix ...)).
|
---|
741 | # Mention this in the diagnostic.
|
---|
742 | my $gnuext = "";
|
---|
743 | $gnuext = "\n(probably a GNU make extension)" if $var =~ / /;
|
---|
744 | msg ('portability', $where,
|
---|
745 | "$var: non-POSIX variable name$gnuext");
|
---|
746 | }
|
---|
747 | }
|
---|
748 | }
|
---|
749 |
|
---|
750 |
|
---|
751 |
|
---|
752 | =item C<Automake::Variable::define($varname, $owner, $type, $cond, $value, $comment, $where, $pretty)>
|
---|
753 |
|
---|
754 | Define or append to a new variable.
|
---|
755 |
|
---|
756 | C<$varname>: the name of the variable being defined.
|
---|
757 |
|
---|
758 | C<$owner>: owner of the variable (one of C<VAR_MAKEFILE>,
|
---|
759 | C<VAR_CONFIGURE>, or C<VAR_AUTOMAKE>, defined by L<Automake::VarDef>).
|
---|
760 | Variables can be overriden, provided the new owner is not weaker
|
---|
761 | (C<VAR_AUTOMAKE> < C<VAR_CONFIGURE> < C<VAR_MAKEFILE>).
|
---|
762 |
|
---|
763 | C<$type>: the type of the assignment (C<''> for C<FOO = bar>,
|
---|
764 | C<':'> for C<FOO := bar>, and C<'+'> for C<'FOO += bar'>).
|
---|
765 |
|
---|
766 | C<$cond>: the C<Condition> in which C<$var> is being defined.
|
---|
767 |
|
---|
768 | C<$value>: the value assigned to C<$var> in condition C<$cond>.
|
---|
769 |
|
---|
770 | C<$comment>: any comment (C<'# bla.'>) associated with the assignment.
|
---|
771 | Comments from C<+=> assignments stack with comments from the last C<=>
|
---|
772 | assignment.
|
---|
773 |
|
---|
774 | C<$where>: the C<Location> of the assignment.
|
---|
775 |
|
---|
776 | C<$pretty>: whether C<$value> should be pretty printed (one of
|
---|
777 | C<VAR_ASIS>, C<VAR_PRETTY>, C<VAR_SILENT>, or C<VAR_SORTED>, defined
|
---|
778 | by by L<Automake::VarDef>). C<$pretty> applies only to real
|
---|
779 | assignments. I.e., it does not apply to a C<+=> assignment (except
|
---|
780 | when part of it is being done as a conditional C<=> assignment).
|
---|
781 |
|
---|
782 | This function will all run any hook registered with the C<hook>
|
---|
783 | function.
|
---|
784 |
|
---|
785 | =cut
|
---|
786 |
|
---|
787 | sub define ($$$$$$$$)
|
---|
788 | {
|
---|
789 | my ($var, $owner, $type, $cond, $value, $comment, $where, $pretty) = @_;
|
---|
790 |
|
---|
791 | prog_error "$cond is not a reference"
|
---|
792 | unless ref $where;
|
---|
793 |
|
---|
794 | prog_error "$where is not a reference"
|
---|
795 | unless ref $where;
|
---|
796 |
|
---|
797 | prog_error "pretty argument missing"
|
---|
798 | unless defined $pretty && ($pretty == VAR_ASIS
|
---|
799 | || $pretty == VAR_PRETTY
|
---|
800 | || $pretty == VAR_SILENT
|
---|
801 | || $pretty == VAR_SORTED);
|
---|
802 |
|
---|
803 | error $where, "bad characters in variable name `$var'"
|
---|
804 | if $var !~ /$_VARIABLE_PATTERN/o;
|
---|
805 |
|
---|
806 | # `:='-style assignments are not acknowledged by POSIX. Moreover it
|
---|
807 | # has multiple meanings. In GNU make or BSD make it means "assign
|
---|
808 | # with immediate expansion", while in OSF make it is used for
|
---|
809 | # conditional assignments.
|
---|
810 | msg ('portability', $where, "`:='-style assignments are not portable")
|
---|
811 | if $type eq ':';
|
---|
812 |
|
---|
813 | check_variable_expansions ($value, $where);
|
---|
814 |
|
---|
815 | # If there's a comment, make sure it is \n-terminated.
|
---|
816 | if ($comment)
|
---|
817 | {
|
---|
818 | chomp $comment;
|
---|
819 | $comment .= "\n";
|
---|
820 | }
|
---|
821 | else
|
---|
822 | {
|
---|
823 | $comment = '';
|
---|
824 | }
|
---|
825 |
|
---|
826 | my $self = _cvar $var;
|
---|
827 |
|
---|
828 | my $def = $self->def ($cond);
|
---|
829 | my $new_var = $def ? 0 : 1;
|
---|
830 |
|
---|
831 | # Additional checks for Automake definitions.
|
---|
832 | if ($owner == VAR_AUTOMAKE && ! $new_var)
|
---|
833 | {
|
---|
834 | # An Automake variable must be consistently defined with the same
|
---|
835 | # sign by Automake.
|
---|
836 | if ($def->type ne $type && $def->owner == VAR_AUTOMAKE)
|
---|
837 | {
|
---|
838 | error ($def->location,
|
---|
839 | "Automake variable `$var' was set with `"
|
---|
840 | . $def->type . "=' here...", partial => 1);
|
---|
841 | error ($where, "... and is now set with `$type=' here.");
|
---|
842 | prog_error ("Automake variable assignments should be consistently\n"
|
---|
843 | . "defined with the same sign.");
|
---|
844 | }
|
---|
845 |
|
---|
846 | # If Automake tries to override a value specified by the user,
|
---|
847 | # just don't let it do.
|
---|
848 | if ($def->owner != VAR_AUTOMAKE)
|
---|
849 | {
|
---|
850 | if (! exists $_silent_variable_override{$var})
|
---|
851 | {
|
---|
852 | my $condmsg = ($cond == TRUE
|
---|
853 | ? '' : (" in condition `" . $cond->human . "'"));
|
---|
854 | msg_cond_var ('override', $cond, $var,
|
---|
855 | "user variable `$var' defined here$condmsg...",
|
---|
856 | partial => 1);
|
---|
857 | msg ('override', $where,
|
---|
858 | "... overrides Automake variable `$var' defined here");
|
---|
859 | }
|
---|
860 | verb ("refusing to override the user definition of:\n"
|
---|
861 | . $self->dump ."with `" . $cond->human . "' => `$value'");
|
---|
862 | return;
|
---|
863 | }
|
---|
864 | }
|
---|
865 |
|
---|
866 | # Differentiate assignment types.
|
---|
867 |
|
---|
868 | # 1. append (+=) to a variable defined for current condition
|
---|
869 | if ($type eq '+' && ! $new_var)
|
---|
870 | {
|
---|
871 | $def->append ($value, $comment);
|
---|
872 |
|
---|
873 | # Only increase owners. A VAR_CONFIGURE variable augmented in a
|
---|
874 | # Makefile.am becomes a VAR_MAKEFILE variable.
|
---|
875 | $def->set_owner ($owner, $where->clone)
|
---|
876 | if $owner > $def->owner;
|
---|
877 | }
|
---|
878 | # 2. append (+=) to a variable defined for *another* condition
|
---|
879 | elsif ($type eq '+' && ! $self->conditions->false)
|
---|
880 | {
|
---|
881 | # * Generally, $cond is not TRUE. For instance:
|
---|
882 | # FOO = foo
|
---|
883 | # if COND
|
---|
884 | # FOO += bar
|
---|
885 | # endif
|
---|
886 | # In this case, we declare an helper variable conditionally,
|
---|
887 | # and append it to FOO:
|
---|
888 | # FOO = foo $(am__append_1)
|
---|
889 | # @COND_TRUE@am__append_1 = bar
|
---|
890 | # Of course if FOO is defined under several conditions, we add
|
---|
891 | # $(am__append_1) to each definitions.
|
---|
892 | #
|
---|
893 | # * If $cond is TRUE, we don't need the helper variable. E.g., in
|
---|
894 | # if COND1
|
---|
895 | # FOO = foo1
|
---|
896 | # else
|
---|
897 | # FOO = foo2
|
---|
898 | # endif
|
---|
899 | # FOO += bar
|
---|
900 | # we can add bar directly to all definition of FOO, and output
|
---|
901 | # @COND_TRUE@FOO = foo1 bar
|
---|
902 | # @COND_FALSE@FOO = foo2 bar
|
---|
903 |
|
---|
904 | # Do we need an helper variable?
|
---|
905 | if ($cond != TRUE)
|
---|
906 | {
|
---|
907 | # Does the helper variable already exists?
|
---|
908 | my $key = "$var:" . $cond->string;
|
---|
909 | if (exists $_appendvar{$key})
|
---|
910 | {
|
---|
911 | # Yes, let's simply append to it.
|
---|
912 | $var = $_appendvar{$key};
|
---|
913 | $owner = VAR_AUTOMAKE;
|
---|
914 | $self = var ($var);
|
---|
915 | $def = $self->rdef ($cond);
|
---|
916 | $new_var = 0;
|
---|
917 | }
|
---|
918 | else
|
---|
919 | {
|
---|
920 | # No, create it.
|
---|
921 | my $num = 1 + keys (%_appendvar);
|
---|
922 | my $hvar = "am__append_$num";
|
---|
923 | $_appendvar{$key} = $hvar;
|
---|
924 | &define ($hvar, VAR_AUTOMAKE, '+',
|
---|
925 | $cond, $value, $comment, $where, $pretty);
|
---|
926 | # Now HVAR is to be added to VAR.
|
---|
927 | $comment = '';
|
---|
928 | $value = "\$($hvar)";
|
---|
929 | }
|
---|
930 | }
|
---|
931 |
|
---|
932 | # Add VALUE to all definitions of SELF.
|
---|
933 | foreach my $vcond ($self->conditions->conds)
|
---|
934 | {
|
---|
935 | # We have a bit of error detection to do here.
|
---|
936 | # This:
|
---|
937 | # if COND1
|
---|
938 | # X = Y
|
---|
939 | # endif
|
---|
940 | # X += Z
|
---|
941 | # should be rejected because X is not defined for all conditions
|
---|
942 | # where `+=' applies.
|
---|
943 | my $undef_cond = $self->not_always_defined_in_cond ($cond);
|
---|
944 | if (! $undef_cond->false)
|
---|
945 | {
|
---|
946 | error ($where,
|
---|
947 | "Cannot apply `+=' because `$var' is not defined "
|
---|
948 | . "in\nthe following conditions:\n "
|
---|
949 | . join ("\n ", map { $_->human } $undef_cond->conds)
|
---|
950 | . "\nEither define `$var' in these conditions,"
|
---|
951 | . " or use\n`+=' in the same conditions as"
|
---|
952 | . " the definitions.");
|
---|
953 | }
|
---|
954 | else
|
---|
955 | {
|
---|
956 | &define ($var, $owner, '+', $vcond, $value, $comment,
|
---|
957 | $where, $pretty);
|
---|
958 | }
|
---|
959 | }
|
---|
960 | }
|
---|
961 | # 3. first assignment (=, :=, or +=)
|
---|
962 | else
|
---|
963 | {
|
---|
964 | # There must be no previous value unless the user is redefining
|
---|
965 | # an Automake variable or an AC_SUBST variable for an existing
|
---|
966 | # condition.
|
---|
967 | _check_ambiguous_condition ($self, $cond, $where)
|
---|
968 | unless (!$new_var
|
---|
969 | && (($def->owner == VAR_AUTOMAKE && $owner != VAR_AUTOMAKE)
|
---|
970 | || $def->owner == VAR_CONFIGURE));
|
---|
971 |
|
---|
972 | # Never decrease an owner.
|
---|
973 | $owner = $def->owner
|
---|
974 | if ! $new_var && $owner < $def->owner;
|
---|
975 |
|
---|
976 | # Assignments to a macro set its location. We don't adjust
|
---|
977 | # locations for `+='. Ideally I suppose we would associate
|
---|
978 | # line numbers with random bits of text.
|
---|
979 | $def = new Automake::VarDef ($var, $value, $comment, $where->clone,
|
---|
980 | $type, $owner, $pretty);
|
---|
981 | $self->set ($cond, $def);
|
---|
982 | push @_var_order, $var;
|
---|
983 | }
|
---|
984 |
|
---|
985 | # Call any defined hook. This helps to update some internal state
|
---|
986 | # *while* parsing the file. For instance the handling of SUFFIXES
|
---|
987 | # requires this (see var_SUFFIXES_trigger).
|
---|
988 | &{$_hooks{$var}}($type, $value) if exists $_hooks{$var};
|
---|
989 | }
|
---|
990 |
|
---|
991 | =item C<variable_delete ($varname, [@conds])>
|
---|
992 |
|
---|
993 | Forget about C<$varname> under the conditions C<@conds>, or completely
|
---|
994 | if C<@conds> is empty.
|
---|
995 |
|
---|
996 | =cut
|
---|
997 |
|
---|
998 | sub variable_delete ($@)
|
---|
999 | {
|
---|
1000 | my ($var, @conds) = @_;
|
---|
1001 |
|
---|
1002 | if (!@conds)
|
---|
1003 | {
|
---|
1004 | delete $_variable_dict{$var};
|
---|
1005 | }
|
---|
1006 | else
|
---|
1007 | {
|
---|
1008 | for my $cond (@conds)
|
---|
1009 | {
|
---|
1010 | delete $_variable_dict{$var}{'defs'}{$cond};
|
---|
1011 | }
|
---|
1012 | }
|
---|
1013 | }
|
---|
1014 |
|
---|
1015 | =item C<$str = variables_dump>
|
---|
1016 |
|
---|
1017 | Return a string describing all we know about all variables.
|
---|
1018 | For debugging.
|
---|
1019 |
|
---|
1020 | =cut
|
---|
1021 |
|
---|
1022 | sub variables_dump ()
|
---|
1023 | {
|
---|
1024 | my $text = "All variables:\n{\n";
|
---|
1025 | foreach my $var (sort { $a->name cmp $b->name } variables)
|
---|
1026 | {
|
---|
1027 | $text .= $var->dump;
|
---|
1028 | }
|
---|
1029 | $text .= "}\n";
|
---|
1030 | return $text;
|
---|
1031 | }
|
---|
1032 |
|
---|
1033 |
|
---|
1034 | =item C<$var = set_seen ($varname)>
|
---|
1035 |
|
---|
1036 | =item C<$var = $var-E<gt>set_seen>
|
---|
1037 |
|
---|
1038 | Mark all definitions of this variable as examined, if the variable
|
---|
1039 | exists. See L<Automake::VarDef::set_seen>.
|
---|
1040 |
|
---|
1041 | Return the C<Variable> object if the variable exists, or 0
|
---|
1042 | otherwise (i.e., as the C<var> function).
|
---|
1043 |
|
---|
1044 | =cut
|
---|
1045 |
|
---|
1046 | sub set_seen ($)
|
---|
1047 | {
|
---|
1048 | my ($self) = @_;
|
---|
1049 | $self = ref $self ? $self : var $self;
|
---|
1050 |
|
---|
1051 | return 0 unless $self;
|
---|
1052 |
|
---|
1053 | for my $c ($self->conditions->conds)
|
---|
1054 | {
|
---|
1055 | $self->rdef ($c)->set_seen;
|
---|
1056 | }
|
---|
1057 |
|
---|
1058 | return $self;
|
---|
1059 | }
|
---|
1060 |
|
---|
1061 |
|
---|
1062 | =item C<$count = require_variables ($where, $reason, $cond, @variables)>
|
---|
1063 |
|
---|
1064 | Make sure that each supplied variable is defined in C<$cond>.
|
---|
1065 | Otherwise, issue a warning showing C<$reason> (C<$reason> should be
|
---|
1066 | the reason why these variable are required, for instance C<'option foo
|
---|
1067 | used'>). If we know which macro can define this variable, hint the
|
---|
1068 | user. Return the number of undefined variables.
|
---|
1069 |
|
---|
1070 | =cut
|
---|
1071 |
|
---|
1072 | sub require_variables ($$$@)
|
---|
1073 | {
|
---|
1074 | my ($where, $reason, $cond, @vars) = @_;
|
---|
1075 | my $res = 0;
|
---|
1076 | $reason .= ' but ' unless $reason eq '';
|
---|
1077 |
|
---|
1078 | VARIABLE:
|
---|
1079 | foreach my $var (@vars)
|
---|
1080 | {
|
---|
1081 | # Nothing to do if the variable exists.
|
---|
1082 | next VARIABLE
|
---|
1083 | if vardef ($var, $cond);
|
---|
1084 |
|
---|
1085 | my $text = "$reason`$var' is undefined\n";
|
---|
1086 | my $v = var $var;
|
---|
1087 | if ($v)
|
---|
1088 | {
|
---|
1089 | my $undef_cond = $v->not_always_defined_in_cond ($cond);
|
---|
1090 | next VARIABLE
|
---|
1091 | if $undef_cond->false;
|
---|
1092 | $text .= ("in the following conditions:\n "
|
---|
1093 | . join ("\n ", map { $_->human } $undef_cond->conds));
|
---|
1094 | }
|
---|
1095 |
|
---|
1096 | ++$res;
|
---|
1097 |
|
---|
1098 | if (exists $_am_macro_for_var{$var})
|
---|
1099 | {
|
---|
1100 | $text .= "\nThe usual way to define `$var' is to add "
|
---|
1101 | . "`$_am_macro_for_var{$var}'\nto `$configure_ac' and "
|
---|
1102 | . "run `aclocal' and `autoconf' again.";
|
---|
1103 | }
|
---|
1104 | elsif (exists $_ac_macro_for_var{$var})
|
---|
1105 | {
|
---|
1106 | $text .= "\nThe usual way to define `$var' is to add "
|
---|
1107 | . "`$_ac_macro_for_var{$var}'\nto `$configure_ac' and "
|
---|
1108 | . "run `autoconf' again.";
|
---|
1109 | }
|
---|
1110 |
|
---|
1111 | error $where, $text, uniq_scope => US_GLOBAL;
|
---|
1112 | }
|
---|
1113 | return $res;
|
---|
1114 | }
|
---|
1115 |
|
---|
1116 | =item C<$count = $var->requires_variables ($reason, @variables)>
|
---|
1117 |
|
---|
1118 | Same as C<require_variables>, but a method of Automake::Variable.
|
---|
1119 | C<@variables> should be defined in the same conditions as C<$var> is
|
---|
1120 | defined.
|
---|
1121 |
|
---|
1122 | =cut
|
---|
1123 |
|
---|
1124 | sub requires_variables ($$@)
|
---|
1125 | {
|
---|
1126 | my ($var, $reason, @args) = @_;
|
---|
1127 | my $res = 0;
|
---|
1128 | for my $cond ($var->conditions->conds)
|
---|
1129 | {
|
---|
1130 | $res += require_variables ($var->rdef ($cond)->location, $reason,
|
---|
1131 | $cond, @args);
|
---|
1132 | }
|
---|
1133 | return $res;
|
---|
1134 | }
|
---|
1135 |
|
---|
1136 |
|
---|
1137 | =item C<variable_value ($var)>
|
---|
1138 |
|
---|
1139 | Get the C<TRUE> value of a variable, warn if the variable is
|
---|
1140 | conditionally defined. C<$var> can be either a variable name
|
---|
1141 | or a C<Automake::Variable> instance (this allows to calls sucha
|
---|
1142 | as C<$var-E<gt>variable_value>).
|
---|
1143 |
|
---|
1144 | =cut
|
---|
1145 |
|
---|
1146 | sub variable_value ($)
|
---|
1147 | {
|
---|
1148 | my ($var) = @_;
|
---|
1149 | my $v = ref ($var) ? $var : var ($var);
|
---|
1150 | return () unless $v;
|
---|
1151 | $v->check_defined_unconditionally;
|
---|
1152 | return $v->rdef (TRUE)->value;
|
---|
1153 | }
|
---|
1154 |
|
---|
1155 | =item C<$str = output_variables>
|
---|
1156 |
|
---|
1157 | Format definitions for all variables.
|
---|
1158 |
|
---|
1159 | =cut
|
---|
1160 |
|
---|
1161 | sub output_variables ()
|
---|
1162 | {
|
---|
1163 | my $res = '';
|
---|
1164 | # We output variables it in the same order in which they were
|
---|
1165 | # defined (skipping duplicates).
|
---|
1166 | my @vars = uniq @_var_order;
|
---|
1167 |
|
---|
1168 | # Output all the Automake variables. If the user changed one,
|
---|
1169 | # then it is now marked as VAR_CONFIGURE or VAR_MAKEFILE.
|
---|
1170 | foreach my $var (@vars)
|
---|
1171 | {
|
---|
1172 | my $v = rvar $var;
|
---|
1173 | foreach my $cond ($v->conditions->conds)
|
---|
1174 | {
|
---|
1175 | $res .= $v->output ($cond)
|
---|
1176 | if $v->rdef ($cond)->owner == VAR_AUTOMAKE;
|
---|
1177 | }
|
---|
1178 | }
|
---|
1179 |
|
---|
1180 | # Now dump the user variables that were defined.
|
---|
1181 | foreach my $var (@vars)
|
---|
1182 | {
|
---|
1183 | my $v = rvar $var;
|
---|
1184 | foreach my $cond ($v->conditions->conds)
|
---|
1185 | {
|
---|
1186 | $res .= $v->output ($cond)
|
---|
1187 | if $v->rdef ($cond)->owner != VAR_AUTOMAKE;
|
---|
1188 | }
|
---|
1189 | }
|
---|
1190 | return $res;
|
---|
1191 | }
|
---|
1192 |
|
---|
1193 | =item C<$var-E<gt>traverse_recursively (&fun_item, &fun_collect, [cond_filter =E<gt> $cond_filter], [inner_expand =E<gt> 1])>
|
---|
1194 |
|
---|
1195 | Split the value of the Automake::Variable C<$var> on space, and
|
---|
1196 | traverse its components recursively.
|
---|
1197 |
|
---|
1198 | If C<$cond_filter> is an C<Automake::Condition>, process any
|
---|
1199 | conditions which are true when C<$cond_filter> is true. Otherwise,
|
---|
1200 | process all conditions.
|
---|
1201 |
|
---|
1202 | We distinguish two kinds of items in the content of C<$var>.
|
---|
1203 | Terms that look like C<$(foo)> or C<${foo}> are subvariables
|
---|
1204 | and cause recursion. Other terms are assumed to be filenames.
|
---|
1205 |
|
---|
1206 | Each time a filename is encountered, C<&fun_item> is called with the
|
---|
1207 | following arguments:
|
---|
1208 |
|
---|
1209 | ($var, -- the Automake::Variable we are currently
|
---|
1210 | traversing
|
---|
1211 | $val, -- the item (i.e., filename) to process
|
---|
1212 | $cond, -- the Condition for the $var definition we are
|
---|
1213 | examinating (ignoring the recursion context)
|
---|
1214 | $full_cond) -- the full Condition, taking into account
|
---|
1215 | conditions inherited from parent variables
|
---|
1216 | during recursion
|
---|
1217 |
|
---|
1218 | If C<inner_expand> is set, variable references occuring in filename
|
---|
1219 | (as in C<$(BASE).ext>) are expansed before the filename is passed to
|
---|
1220 | C<&fun_item>.
|
---|
1221 |
|
---|
1222 | C<&fun_item> may return a list of items, they will be passed to
|
---|
1223 | C<&fun_store> later on. Define C<&fun_item> as C<undef> when it serve
|
---|
1224 | no purpose, this will speed things up.
|
---|
1225 |
|
---|
1226 | Once all items of a variable have been processed, the result (of the
|
---|
1227 | calls to C<&fun_items>, or of recursive traversals of subvariables)
|
---|
1228 | are passed to C<&fun_collect>. C<&fun_collect> receives three
|
---|
1229 | arguments:
|
---|
1230 |
|
---|
1231 | ($var, -- the variable being traversed
|
---|
1232 | $parent_cond, -- the Condition inherited from parent
|
---|
1233 | variables during recursion
|
---|
1234 | @condlist) -- a list of [$cond, @results] pairs
|
---|
1235 | where each $cond appear only once, and @result
|
---|
1236 | are all the results for this condition.
|
---|
1237 |
|
---|
1238 | Typically you should do C<$cond->merge ($parent_cond)> to recompute
|
---|
1239 | the C<$full_cond> associated to C<@result>. C<&fun_collect> may
|
---|
1240 | return a list of items, that will be used as the result of
|
---|
1241 | C<Automake::Variable::traverse_recursively> (the top-level, or its
|
---|
1242 | recursive calls).
|
---|
1243 |
|
---|
1244 | =cut
|
---|
1245 |
|
---|
1246 | # Contains a stack of `from' and `to' parts of variable
|
---|
1247 | # substitutions currently in force.
|
---|
1248 | my @_substfroms;
|
---|
1249 | my @_substtos;
|
---|
1250 | sub traverse_recursively ($&&;%)
|
---|
1251 | {
|
---|
1252 | ++$_traversal;
|
---|
1253 | @_substfroms = ();
|
---|
1254 | @_substtos = ();
|
---|
1255 | my ($var, $fun_item, $fun_collect, %options) = @_;
|
---|
1256 | my $cond_filter = $options{'cond_filter'};
|
---|
1257 | my $inner_expand = $options{'inner_expand'};
|
---|
1258 | return $var->_do_recursive_traversal ($var,
|
---|
1259 | $fun_item, $fun_collect,
|
---|
1260 | $cond_filter, TRUE, $inner_expand)
|
---|
1261 | }
|
---|
1262 |
|
---|
1263 | # The guts of Automake::Variable::traverse_recursively.
|
---|
1264 | sub _do_recursive_traversal ($$&&$$$)
|
---|
1265 | {
|
---|
1266 | my ($var, $parent, $fun_item, $fun_collect, $cond_filter, $parent_cond,
|
---|
1267 | $inner_expand) = @_;
|
---|
1268 |
|
---|
1269 | $var->set_seen;
|
---|
1270 |
|
---|
1271 | if ($var->{'scanned'} == $_traversal)
|
---|
1272 | {
|
---|
1273 | err_var $var, "variable `" . $var->name() . "' recursively defined";
|
---|
1274 | return ();
|
---|
1275 | }
|
---|
1276 | $var->{'scanned'} = $_traversal;
|
---|
1277 |
|
---|
1278 | my @allresults = ();
|
---|
1279 | my $cond_once = 0;
|
---|
1280 | foreach my $cond ($var->conditions->conds)
|
---|
1281 | {
|
---|
1282 | if (ref $cond_filter)
|
---|
1283 | {
|
---|
1284 | # Ignore conditions that don't match $cond_filter.
|
---|
1285 | next if ! $cond->true_when ($cond_filter);
|
---|
1286 | # If we found out several definitions of $var
|
---|
1287 | # match $cond_filter then we are in trouble.
|
---|
1288 | # Tell the user we don't support this.
|
---|
1289 | $var->check_defined_unconditionally ($parent, $parent_cond)
|
---|
1290 | if $cond_once;
|
---|
1291 | $cond_once = 1;
|
---|
1292 | }
|
---|
1293 | my @result = ();
|
---|
1294 | my $full_cond = $cond->merge ($parent_cond);
|
---|
1295 |
|
---|
1296 | my @to_process = $var->value_as_list ($cond, $parent, $parent_cond);
|
---|
1297 | while (@to_process)
|
---|
1298 | {
|
---|
1299 | my $val = shift @to_process;
|
---|
1300 | # If $val is a variable (i.e. ${foo} or $(bar), not a filename),
|
---|
1301 | # handle the sub variable recursively.
|
---|
1302 | # (Backslashes before `}' and `)' within brackets are here to
|
---|
1303 | # please Emacs's indentation.)
|
---|
1304 | if ($val =~ /^\$\{([^\}]*)\}$/ || $val =~ /^\$\(([^\)]*)\)$/)
|
---|
1305 | {
|
---|
1306 | my $subvarname = $1;
|
---|
1307 |
|
---|
1308 | # If the user uses a losing variable name, just ignore it.
|
---|
1309 | # This isn't ideal, but people have requested it.
|
---|
1310 | next if ($subvarname =~ /\@.*\@/);
|
---|
1311 |
|
---|
1312 | # See if the variable is actually a substitution reference
|
---|
1313 | my ($from, $to);
|
---|
1314 | # This handles substitution references like ${foo:.a=.b}.
|
---|
1315 | if ($subvarname =~ /^([^:]*):([^=]*)=(.*)$/o)
|
---|
1316 | {
|
---|
1317 | $subvarname = $1;
|
---|
1318 | $to = $3;
|
---|
1319 | $from = quotemeta $2;
|
---|
1320 | }
|
---|
1321 |
|
---|
1322 | my $subvar = var ($subvarname);
|
---|
1323 | # Don't recurse into undefined variables.
|
---|
1324 | next unless $subvar;
|
---|
1325 |
|
---|
1326 | push @_substfroms, $from;
|
---|
1327 | push @_substtos, $to;
|
---|
1328 |
|
---|
1329 | my @res = $subvar->_do_recursive_traversal ($parent,
|
---|
1330 | $fun_item,
|
---|
1331 | $fun_collect,
|
---|
1332 | $cond_filter,
|
---|
1333 | $full_cond,
|
---|
1334 | $inner_expand);
|
---|
1335 | push (@result, @res);
|
---|
1336 |
|
---|
1337 | pop @_substfroms;
|
---|
1338 | pop @_substtos;
|
---|
1339 |
|
---|
1340 | next;
|
---|
1341 | }
|
---|
1342 | # Try to expand variable references inside filenames such as
|
---|
1343 | # `$(NAME).txt'. We do not handle `:.foo=.bar'
|
---|
1344 | # substitutions, but it would make little sense to use this
|
---|
1345 | # here anyway.
|
---|
1346 | elsif ($inner_expand
|
---|
1347 | && ($val =~ /\$\{([^\}]*)\}/ || $val =~ /\$\(([^\)]*)\)/))
|
---|
1348 | {
|
---|
1349 | my $subvarname = $1;
|
---|
1350 | my $subvar = var $subvarname;
|
---|
1351 | if ($subvar)
|
---|
1352 | {
|
---|
1353 | # Replace the reference by its value, and reschedule
|
---|
1354 | # for expansion.
|
---|
1355 | foreach my $c ($subvar->conditions->conds)
|
---|
1356 | {
|
---|
1357 | if (ref $cond_filter)
|
---|
1358 | {
|
---|
1359 | # Ignore conditions that don't match $cond_filter.
|
---|
1360 | next if ! $c->true_when ($cond_filter);
|
---|
1361 | # If we found out several definitions of $var
|
---|
1362 | # match $cond_filter then we are in trouble.
|
---|
1363 | # Tell the user we don't support this.
|
---|
1364 | $subvar->check_defined_unconditionally ($var,
|
---|
1365 | $full_cond)
|
---|
1366 | if $cond_once;
|
---|
1367 | $cond_once = 1;
|
---|
1368 | }
|
---|
1369 | my $subval = $subvar->rdef ($c)->value;
|
---|
1370 | $val =~ s/\$\{$subvarname\}/$subval/g;
|
---|
1371 | $val =~ s/\$\($subvarname\)/$subval/g;
|
---|
1372 | unshift @to_process, split (' ', $val);
|
---|
1373 | }
|
---|
1374 | next;
|
---|
1375 | }
|
---|
1376 | # We do not know any variable with this name. Fall through
|
---|
1377 | # to filename processing.
|
---|
1378 | }
|
---|
1379 |
|
---|
1380 | if ($fun_item) # $var is a filename we must process
|
---|
1381 | {
|
---|
1382 | my $substnum=$#_substfroms;
|
---|
1383 | while ($substnum >= 0)
|
---|
1384 | {
|
---|
1385 | $val =~ s/$_substfroms[$substnum]$/$_substtos[$substnum]/
|
---|
1386 | if defined $_substfroms[$substnum];
|
---|
1387 | $substnum -= 1;
|
---|
1388 | }
|
---|
1389 |
|
---|
1390 | # Make sure you update the doc of
|
---|
1391 | # Automake::Variable::traverse_recursively
|
---|
1392 | # if you change the prototype of &fun_item.
|
---|
1393 | my @transformed = &$fun_item ($var, $val, $cond, $full_cond);
|
---|
1394 | push (@result, @transformed);
|
---|
1395 | }
|
---|
1396 | }
|
---|
1397 | push (@allresults, [$cond, @result]) if @result;
|
---|
1398 | }
|
---|
1399 |
|
---|
1400 | # We only care about _recursive_ variable definitions. The user
|
---|
1401 | # is free to use the same variable several times in the same definition.
|
---|
1402 | $var->{'scanned'} = -1;
|
---|
1403 |
|
---|
1404 | # Make sure you update the doc of Automake::Variable::traverse_recursively
|
---|
1405 | # if you change the prototype of &fun_collect.
|
---|
1406 | return &$fun_collect ($var, $parent_cond, @allresults);
|
---|
1407 | }
|
---|
1408 |
|
---|
1409 | # $VARNAME
|
---|
1410 | # _gen_varname ($BASE, @DEFINITIONS)
|
---|
1411 | # ---------------------------------
|
---|
1412 | # Return a variable name starting with $BASE, that will be
|
---|
1413 | # used to store definitions @DEFINITIONS.
|
---|
1414 | # @DEFINITIONS is a list of pair [$COND, @OBJECTS].
|
---|
1415 | #
|
---|
1416 | # If we already have a $BASE-variable containing @DEFINITIONS, reuse it.
|
---|
1417 | # This way, we avoid combinatorial explosion of the generated
|
---|
1418 | # variables. Especially, in a Makefile such as:
|
---|
1419 | #
|
---|
1420 | # | if FOO1
|
---|
1421 | # | A1=1
|
---|
1422 | # | endif
|
---|
1423 | # |
|
---|
1424 | # | if FOO2
|
---|
1425 | # | A2=2
|
---|
1426 | # | endif
|
---|
1427 | # |
|
---|
1428 | # | ...
|
---|
1429 | # |
|
---|
1430 | # | if FOON
|
---|
1431 | # | AN=N
|
---|
1432 | # | endif
|
---|
1433 | # |
|
---|
1434 | # | B=$(A1) $(A2) ... $(AN)
|
---|
1435 | # |
|
---|
1436 | # | c_SOURCES=$(B)
|
---|
1437 | # | d_SOURCES=$(B)
|
---|
1438 | #
|
---|
1439 | # The generated c_OBJECTS and d_OBJECTS will share the same variable
|
---|
1440 | # definitions.
|
---|
1441 | #
|
---|
1442 | # This setup can be the case of a testsuite containing lots (>100) of
|
---|
1443 | # small C programs, all testing the same set of source files.
|
---|
1444 | sub _gen_varname ($@)
|
---|
1445 | {
|
---|
1446 | my $base = shift;
|
---|
1447 | my $key = '';
|
---|
1448 | foreach my $pair (@_)
|
---|
1449 | {
|
---|
1450 | my ($cond, @values) = @$pair;
|
---|
1451 | $key .= "($cond)@values";
|
---|
1452 | }
|
---|
1453 |
|
---|
1454 | return $_gen_varname{$base}{$key} if exists $_gen_varname{$base}{$key};
|
---|
1455 |
|
---|
1456 | my $num = 1 + keys (%{$_gen_varname{$base}});
|
---|
1457 | my $name = "${base}_${num}";
|
---|
1458 | $_gen_varname{$base}{$key} = $name;
|
---|
1459 | return $name;
|
---|
1460 | }
|
---|
1461 |
|
---|
1462 | =item C<$resvar = transform_variable_recursively ($var, $resvar, $base, $nodefine, $where, &fun_item, [%options])>
|
---|
1463 |
|
---|
1464 | =item C<$resvar = $var-E<gt>transform_variable_recursively ($resvar, $base, $nodefine, $where, &fun_item, [%options])>
|
---|
1465 |
|
---|
1466 | Traverse C<$var> recursively, and create a C<$resvar> variable in
|
---|
1467 | which each filename in C<$var> have been transformed using
|
---|
1468 | C<&fun_item>. (C<$var> may be a variable name in the first syntax.
|
---|
1469 | It must be an C<Automake::Variable> otherwise.)
|
---|
1470 |
|
---|
1471 | Helper variables (corresponding to sub-variables of C<$var>) are
|
---|
1472 | created as needed, using C<$base> as prefix.
|
---|
1473 |
|
---|
1474 | Arguments are:
|
---|
1475 | $var source variable to traverse
|
---|
1476 | $resvar resulting variable to define
|
---|
1477 | $base prefix to use when naming subvariables of $resvar
|
---|
1478 | $nodefine if true, traverse $var but do not define any variable
|
---|
1479 | (this assumes &fun_item has some useful side-effect)
|
---|
1480 | $where context into which variable definitions are done
|
---|
1481 | &fun_item a transformation function -- see the documentation
|
---|
1482 | of &fun_item in Automake::Variable::traverse_recursively.
|
---|
1483 |
|
---|
1484 | This returns the string C<"\$($RESVAR)">.
|
---|
1485 |
|
---|
1486 | C<%options> is a list of options to pass to
|
---|
1487 | C<Variable::traverse_recursively> (see this method).
|
---|
1488 |
|
---|
1489 | =cut
|
---|
1490 |
|
---|
1491 | sub transform_variable_recursively ($$$$$&;%)
|
---|
1492 | {
|
---|
1493 | my ($var, $resvar, $base, $nodefine, $where, $fun_item, %options) = @_;
|
---|
1494 |
|
---|
1495 | $var = ref $var ? $var : rvar $var;
|
---|
1496 |
|
---|
1497 | my $res = $var->traverse_recursively
|
---|
1498 | ($fun_item,
|
---|
1499 | # The code that define the variable holding the result
|
---|
1500 | # of the recursive transformation of a subvariable.
|
---|
1501 | sub {
|
---|
1502 | my ($subvar, $parent_cond, @allresults) = @_;
|
---|
1503 | # Find a name for the variable, unless this is the top-variable
|
---|
1504 | # for which we want to use $resvar.
|
---|
1505 | my $varname =
|
---|
1506 | ($var != $subvar) ? _gen_varname ($base, @allresults) : $resvar;
|
---|
1507 | # Define the variable if required.
|
---|
1508 | unless ($nodefine)
|
---|
1509 | {
|
---|
1510 | # If the new variable is the source variable, we assume
|
---|
1511 | # we are trying to override a user variable. Delete
|
---|
1512 | # the old variable first.
|
---|
1513 | variable_delete ($varname) if $varname eq $var->name;
|
---|
1514 | # Define an empty variable in condition TRUE if there is no
|
---|
1515 | # result.
|
---|
1516 | @allresults = ([TRUE, '']) unless @allresults;
|
---|
1517 | # Define the rewritten variable in all conditions not
|
---|
1518 | # already covered by user definitions.
|
---|
1519 | foreach my $pair (@allresults)
|
---|
1520 | {
|
---|
1521 | my ($cond, @result) = @$pair;
|
---|
1522 | my $var = var $varname;
|
---|
1523 | my @conds = ($var
|
---|
1524 | ? $var->not_always_defined_in_cond ($cond)->conds
|
---|
1525 | : $cond);
|
---|
1526 |
|
---|
1527 | foreach (@conds)
|
---|
1528 | {
|
---|
1529 | define ($varname, VAR_AUTOMAKE, '', $_, "@result",
|
---|
1530 | '', $where, VAR_PRETTY);
|
---|
1531 | }
|
---|
1532 | }
|
---|
1533 | set_seen $varname;
|
---|
1534 | }
|
---|
1535 | return "\$($varname)";
|
---|
1536 | },
|
---|
1537 | %options);
|
---|
1538 | return $res;
|
---|
1539 | }
|
---|
1540 |
|
---|
1541 |
|
---|
1542 | =back
|
---|
1543 |
|
---|
1544 | =head1 SEE ALSO
|
---|
1545 |
|
---|
1546 | L<Automake::VarDef>, L<Automake::Condition>,
|
---|
1547 | L<Automake::DisjConditions>, L<Automake::Location>.
|
---|
1548 |
|
---|
1549 | =cut
|
---|
1550 |
|
---|
1551 | 1;
|
---|
1552 |
|
---|
1553 | ### Setup "GNU" style for perl-mode and cperl-mode.
|
---|
1554 | ## Local Variables:
|
---|
1555 | ## perl-indent-level: 2
|
---|
1556 | ## perl-continued-statement-offset: 2
|
---|
1557 | ## perl-continued-brace-offset: 0
|
---|
1558 | ## perl-brace-offset: 0
|
---|
1559 | ## perl-brace-imaginary-offset: 0
|
---|
1560 | ## perl-label-offset: -2
|
---|
1561 | ## cperl-indent-level: 2
|
---|
1562 | ## cperl-brace-offset: 0
|
---|
1563 | ## cperl-continued-brace-offset: 0
|
---|
1564 | ## cperl-label-offset: -2
|
---|
1565 | ## cperl-extra-newline-before-brace: t
|
---|
1566 | ## cperl-merge-trailing-else: nil
|
---|
1567 | ## cperl-continued-statement-offset: 2
|
---|
1568 | ## End:
|
---|