1 # Copyright (C) 2003, 2004, 2005, 2006, 2008, 2009 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)
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 <http://www.gnu.org/licenses/>.
16 package Automake
::Variable
;
20 use Automake
::Channels
;
21 use Automake
::ChannelDefs
;
22 use Automake
::Configure_ac
;
25 use Automake
::Condition qw
(TRUE FALSE
);
26 use Automake
::DisjConditions
;
27 use Automake
::General
'uniq';
28 use Automake
::Wrap
'makefile_wrap';
31 use vars
'@ISA', '@EXPORT', '@EXPORT_OK';
32 @ISA = qw
/Automake::Item Exporter/;
33 @EXPORT = qw
(err_var msg_var msg_cond_var reject_var
34 var rvar vardef rvardef
36 scan_variable_expansions check_variable_expansions
43 transform_variable_recursively
);
47 Automake::Variable - support for variable definitions
51 use Automake::Variable;
54 # Defining a variable.
55 Automake::Variable::define($varname, $owner, $type,
56 $cond, $value, $comment,
59 # Looking up a variable.
60 my $var = var $varname;
66 # Looking up a variable that is assumed to exist.
67 my $var = rvar $varname;
69 # The list of conditions where $var has been defined.
70 # ($var->conditions is an Automake::DisjConditions,
71 # $var->conditions->conds is a list of Automake::Condition.)
72 my @conds = $var->conditions->conds
74 # Access to the definition in Condition $cond.
75 # $def is an Automake::VarDef.
76 my $def = $var->def ($cond);
82 # When the conditional definition is assumed to exist, use
83 my $def = $var->rdef ($cond);
88 This package provides support for Makefile variable definitions.
90 An C<Automake::Variable> is a variable name associated to possibly
91 many conditional definitions. These definitions are instances
92 of C<Automake::VarDef>.
94 Therefore obtaining the value of a variable under a given
95 condition involves two lookups. One to look up the variable,
96 and one to look up the conditional definition:
101 my $def = $var->def ($cond);
110 When it is known that the variable and the definition
111 being looked up exist, the above can be simplified to
113 return var ($name)->def ($cond)->value; # Do not write this.
115 but is better written
117 return rvar ($name)->rdef ($cond)->value;
121 return rvardef ($name, $cond)->value;
123 The I<r> variants of the C<var>, C<def>, and C<vardef> methods add an
124 extra test to ensure that the lookup succeeded, and will diagnose
125 failures as internal errors (with a message which is much more
126 informative than Perl's warning about calling a method on a
131 my $_VARIABLE_CHARACTERS = '[.A-Za-z0-9_@]+';
132 my $_VARIABLE_PATTERN = '^' . $_VARIABLE_CHARACTERS . "\$";
133 my $_VARIABLE_RECURSIVE_PATTERN =
134 '^([.A-Za-z0-9_@]|\$[({]' . $_VARIABLE_CHARACTERS . '[})]?)+' . "\$";
136 # The order in which variables should be output. (May contain
137 # duplicates -- only the first occurrence matters.)
140 # This keeps track of all variables defined by &_gen_varname.
141 # $_gen_varname{$base} is a hash for all variables defined with
142 # prefix `$base'. Values stored in this hash are the variable names.
143 # Keys have the form "(COND1)VAL1(COND2)VAL2..." where VAL1 and VAL2
144 # are the values of the variable for condition COND1 and COND2.
145 my %_gen_varname = ();
146 # $_gen_varname_n{$base} is the number of variables generated by
147 # _gen_varname() for $base. This is not the same as keys
148 # %{$_gen_varname{$base}} because %_gen_varname may also contain
149 # variables not generated by _gen_varname.
150 my %_gen_varname_n = ();
152 # Declare the macros that define known variables, so we can
153 # hint the user if she try to use one of these variables.
155 # Macros accessible via aclocal.
156 my %_am_macro_for_var =
158 ANSI2KNR
=> 'AM_C_PROTOTYPES',
159 CCAS
=> 'AM_PROG_AS',
160 CCASFLAGS
=> 'AM_PROG_AS',
161 EMACS
=> 'AM_PATH_LISPDIR',
162 GCJ
=> 'AM_PROG_GCJ',
163 LEX
=> 'AM_PROG_LEX',
164 LIBTOOL
=> 'AC_PROG_LIBTOOL',
165 lispdir
=> 'AM_PATH_LISPDIR',
166 pkgpyexecdir
=> 'AM_PATH_PYTHON',
167 pkgpythondir
=> 'AM_PATH_PYTHON',
168 pyexecdir
=> 'AM_PATH_PYTHON',
169 PYTHON
=> 'AM_PATH_PYTHON',
170 pythondir
=> 'AM_PATH_PYTHON',
171 U
=> 'AM_C_PROTOTYPES',
174 # Macros shipped with Autoconf.
175 my %_ac_macro_for_var =
177 ALLOCA
=> 'AC_FUNC_ALLOCA',
179 CFLAGS
=> 'AC_PROG_CC',
180 CXX
=> 'AC_PROG_CXX',
181 CXXFLAGS
=> 'AC_PROG_CXX',
182 F77
=> 'AC_PROG_F77',
183 F77FLAGS
=> 'AC_PROG_F77',
185 FCFLAGS
=> 'AC_PROG_FC',
186 OBJC
=> 'AC_PROG_OBJC',
187 OBJCFLAGS
=> 'AC_PROG_OBJC',
188 RANLIB
=> 'AC_PROG_RANLIB',
189 UPC
=> 'AM_PROG_UPC',
190 UPCFLAGS
=> 'AM_PROG_UPC',
191 YACC
=> 'AC_PROG_YACC',
194 # The name of the configure.ac file.
195 my $configure_ac = find_configure_ac
;
197 # Variables that can be overridden without complaint from -Woverride
198 my %_silent_variable_override =
199 (AM_MAKEINFOHTMLFLAGS
=> 1,
206 # Count of helper variables used to implement conditional '+='.
209 # Each call to C<Automake::Variable::traverse_recursively> gets an
210 # unique label. This is used to detect recursively defined variables.
214 =head2 Error reporting functions
216 In these functions, C<$var> can be either a variable name, or
217 an instance of C<Automake::Variable>.
221 =item C<err_var ($var, $message, [%options])>
223 Uncategorized errors about variables.
229 msg_var
('error', @_);
232 =item C<msg_cond_var ($channel, $cond, $var, $message, [%options])>
234 Messages about conditional variable.
238 sub msg_cond_var
($$$$;%)
240 my ($channel, $cond, $var, $msg, %opts) = @_;
241 my $v = ref ($var) ?
$var : rvar
($var);
242 msg
$channel, $v->rdef ($cond)->location, $msg, %opts;
245 =item C<msg_var ($channel, $var, $message, [%options])>
247 Messages about variables.
253 my ($channel, $var, $msg, %opts) = @_;
254 my $v = ref ($var) ?
$var : rvar
($var);
255 # Don't know which condition is concerned. Pick any.
256 my $cond = $v->conditions->one_cond;
257 msg_cond_var
$channel, $cond, $v, $msg, %opts;
260 =item C<$bool = reject_var ($varname, $error_msg)>
262 Bail out with C<$error_msg> if a variable with name C<$varname> has
265 Return true iff C<$varname> is defined.
271 my ($var, $msg) = @_;
283 =head2 Administrative functions
287 =item C<Automake::Variable::hook ($varname, $fun)>
289 Declare a function to be called whenever a variable
290 named C<$varname> is defined or redefined.
292 C<$fun> should take two arguments: C<$type> and C<$value>.
293 When type is C<''> or <':'>, C<$value> is the value being
294 assigned to C<$varname>. When C<$type> is C<'+'>, C<$value>
295 is the value being appended to C<$varname>.
302 my ($var, $fun) = @_;
303 $_hooks{$var} = $fun;
306 =item C<variables ([$suffix])>
308 Returns the list of all L<Automake::Variable> instances. (I.e., all
309 variables defined so far.) If C<$suffix> is supplied, return only
310 the L<Automake::Variable> instances that ends with C<_$suffix>.
314 use vars
'%_variable_dict', '%_primary_dict';
320 if (exists $_primary_dict{$suffix})
322 return values %{$_primary_dict{$suffix}};
331 return values %_variable_dict;
335 =item C<Automake::Variable::reset>
337 The I<forget all> function. Clears all know variables and reset some
344 %_variable_dict = ();
349 %_gen_varname_n = ();
353 =item C<var ($varname)>
355 Return the C<Automake::Variable> object for the variable
356 named C<$varname> if defined. Return 0 otherwise.
363 return $_variable_dict{$name} if exists $_variable_dict{$name};
367 =item C<vardef ($varname, $cond)>
369 Return the C<Automake::VarDef> object for the variable named
370 C<$varname> if defined in condition C<$cond>. Return false
371 if the condition or the variable does not exist.
377 my ($name, $cond) = @_;
379 return $var && $var->def ($cond);
382 # Create the variable if it does not exist.
383 # This is used only by other functions in this package.
389 return _new Automake
::Variable
$name;
392 =item C<rvar ($varname)>
394 Return the C<Automake::Variable> object for the variable named
395 C<$varname>. Abort with an internal error if the variable was not
398 The I<r> in front of C<var> stands for I<required>. One
399 should call C<rvar> to assert the variable's existence.
407 prog_error
("undefined variable $name\n" . &variables_dump
)
412 =item C<rvardef ($varname, $cond)>
414 Return the C<Automake::VarDef> object for the variable named
415 C<$varname> if defined in condition C<$cond>. Abort with an internal
416 error if the condition or the variable does not exist.
422 my ($name, $cond) = @_;
423 return rvar
($name)->rdef ($cond);
430 C<Automake::Variable> is a subclass of C<Automake::Item>. See
431 that package for inherited methods.
433 Here are the methods specific to the C<Automake::Variable> instances.
434 Use the C<define> function, described latter, to create such objects.
440 # Create Automake::Variable objects. This is used
441 # only in this file. Other users should use
442 # the "define" function.
445 my ($class, $name) = @_;
446 my $self = Automake
::Item
::new
($class, $name);
447 $self->{'scanned'} = 0;
448 $self->{'last-append'} = []; # helper variable for last conditional append.
449 $_variable_dict{$name} = $self;
450 if ($name =~ /_([[:alnum:]]+)$/)
452 $_primary_dict{$1}{$name} = $self;
457 # _check_ambiguous_condition ($SELF, $COND, $WHERE)
458 # -------------------------------------------------
459 # Check for an ambiguous conditional. This is called when a variable
460 # is being defined conditionally. If we already know about a
461 # definition that is true under the same conditions, then we have an
463 sub _check_ambiguous_condition
($$$)
465 my ($self, $cond, $where) = @_;
466 my $var = $self->name;
467 my ($message, $ambig_cond) = $self->conditions->ambiguous_p ($var, $cond);
469 # We allow silent variables to be overridden silently.
470 my $def = $self->def ($cond);
471 if ($message && !($def && $def->pretty == VAR_SILENT
))
473 msg
'syntax', $where, "$message ...", partial
=> 1;
474 msg_var
('syntax', $var, "... `$var' previously defined here");
479 =item C<$bool = $var-E<gt>check_defined_unconditionally ([$parent, $parent_cond])>
481 Warn if the variable is conditionally defined. C<$parent> is the name
482 of the parent variable, and C<$parent_cond> the condition of the parent
483 definition. These two variables are used to display diagnostics.
487 sub check_defined_unconditionally
($;$$)
489 my ($self, $parent, $parent_cond) = @_;
491 if (!$self->conditions->true)
495 msg_cond_var
('unsupported', $parent_cond, $parent,
496 "automake does not support conditional definition of "
497 . $self->name . " in $parent");
501 msg_var
('unsupported', $self,
502 "automake does not support " . $self->name
503 . " being defined conditionally");
508 =item C<$str = $var-E<gt>output ([@conds])>
510 Format all the definitions of C<$var> if C<@cond> is not specified,
511 else only that corresponding to C<@cond>.
517 my ($self, @conds) = @_;
519 @conds = $self->conditions->conds
523 my $name = $self->name;
525 foreach my $cond (@conds)
527 my $def = $self->def ($cond);
528 prog_error
("unknown condition `" . $cond->human . "' for `"
533 if $def->pretty == VAR_SILENT
;
535 $res .= $def->comment;
537 my $val = $def->raw_value;
538 my $equals = $def->type eq ':' ?
':=' : '=';
539 my $str = $cond->subst_string;
542 if ($def->pretty == VAR_ASIS
)
544 my $output_var = "$name $equals $val";
545 $output_var =~ s/^/$str/meg;
546 $res .= "$output_var\n";
548 elsif ($def->pretty == VAR_PRETTY
)
550 # Suppress escaped new lines. &makefile_wrap will
551 # add them back, maybe at other places.
553 my $wrap = makefile_wrap
("$str$name $equals", "$str\t",
556 # If the last line of the definition is made only of
557 # @substitutions@, append an empty variable to make sure it
558 # cannot be substituted as a blank line (that would confuse
560 $wrap = makefile_wrap
("$str$name $equals", "$str\t",
561 split (' ', $val), '$(am__empty)')
562 if $wrap =~ /\n(\s*@\w+@)+\s*$/;
566 else # ($def->pretty == VAR_SORTED)
568 # Suppress escaped new lines. &makefile_wrap will
569 # add them back, maybe at other places.
571 $res .= makefile_wrap
("$str$name $equals", "$str\t",
572 sort (split (' ' , $val)));
578 =item C<@values = $var-E<gt>value_as_list ($cond, [$parent, $parent_cond])>
580 Get the value of C<$var> as a list, given a specified condition,
581 without recursing through any subvariables.
583 C<$cond> is the condition of interest. C<$var> does not need
584 to be defined for condition C<$cond> exactly, but it needs
585 to be defined for at most one condition implied by C<$cond>.
587 C<$parent> and C<$parent_cond> designate the name and the condition
588 of the parent variable, i.e., the variable in which C<$var> is
589 being expanded. These are used in diagnostics.
591 For example, if C<A> is defined as "C<foo $(B) bar>" in condition
592 C<TRUE>, calling C<rvar ('A')->value_as_list (TRUE)> will return
593 C<("foo", "$(B)", "bar")>.
597 sub value_as_list
($$;$$)
599 my ($self, $cond, $parent, $parent_cond) = @_;
602 # Get value for given condition
604 foreach my $vcond ($self->conditions->conds)
606 if ($vcond->true_when ($cond))
608 # If there is more than one definitions of $var matching
609 # $cond then we are in trouble: tell the user we need a
610 # paddle. Continue by merging results from all conditions,
611 # although it doesn't make much sense.
612 $self->check_defined_unconditionally ($parent, $parent_cond)
616 my $val = $self->rdef ($vcond)->value;
617 push @result, split (' ', $val);
623 =item C<@values = $var-E<gt>value_as_list_recursive ([%options])>
625 Return the contents of C<$var> as a list, split on whitespace. This
626 will recursively follow C<$(...)> and C<${...}> inclusions. It
627 preserves C<@...@> substitutions.
629 C<%options> is a list of option for C<Variable::traverse_recursively>
630 (see this method). The most useful is C<cond_filter>:
632 $var->value_as_list_recursive (cond_filter => $cond)
634 will return the contents of C<$var> and any subvariable in all
635 conditions implied by C<$cond>.
637 C<%options> can also carry options specific to C<value_as_list_recursive>.
638 Presently, the only such option is C<location =E<gt> 1> which instructs
639 C<value_as_list_recursive> to return a list of C<[$location, @values]> pairs.
643 sub value_as_list_recursive
($;%)
645 my ($var, %options) = @_;
647 return $var->traverse_recursively
648 (# Construct [$location, $value] pairs if requested.
650 my ($var, $val, $cond, $full_cond) = @_;
651 return [$var->rdef ($cond)->location, $val] if $options{'location'};
656 my ($var, $parent_cond, @allresults) = @_;
657 return map { my ($cond, @vals) = @
$_; @vals } @allresults;
663 =item C<$bool = $var-E<gt>has_conditional_contents>
665 Return 1 if C<$var> or one of its subvariable was conditionally
666 defined. Return 0 otherwise.
670 sub has_conditional_contents
($)
674 # Traverse the variable recursively until we
675 # find a variable defined conditionally.
676 # Use `die' to abort the traversal, and pass it `$full_cond'
677 # to we can find easily whether the `eval' block aborted
678 # because we found a condition, or for some other error.
681 $self->traverse_recursively
684 my ($subvar, $val, $cond, $full_cond) = @_;
685 die $full_cond if ! $full_cond->true;
692 return 1 if ref ($@
) && $@
->isa ("Automake::Condition");
693 # Propagate other errors.
700 =item C<$string = $var-E<gt>dump>
702 Return a string describing all we know about C<$var>.
711 my $text = $self->name . ": \n {\n";
712 foreach my $vcond ($self->conditions->conds)
714 $text .= " " . $vcond->human . " => " . $self->rdef ($vcond)->dump;
723 =head2 Utility functions
727 =item C<@list = scan_variable_expansions ($text)>
729 Return the list of variable names expanded in C<$text>. Note that
730 unlike some other functions, C<$text> is not split on spaces before we
731 check for subvariables.
735 sub scan_variable_expansions
($)
743 # Record each use of ${stuff} or $(stuff) that does not follow a $.
744 while ($text =~ /(?<!\$)\$(?:\{([^\}]*)\}|\(([^\)]*)\))/g)
747 # The occurrence may look like $(string1[:subst1=[subst2]]) but
748 # we want only `string1'.
749 $var =~ s/:[^:=]*=[^=]*$//;
756 =item C<check_variable_expansions ($text, $where)>
758 Check variable expansions in C<$text> and warn about any name that
759 does not conform to POSIX. C<$where> is the location of C<$text>
760 for the error message.
764 sub check_variable_expansions
($$)
766 my ($text, $where) = @_;
767 # Catch expansion of variables whose name does not conform to POSIX.
768 foreach my $var (scan_variable_expansions
($text))
770 if ($var !~ /$_VARIABLE_PATTERN/o)
772 # If the variable name contains a space, it's likely
773 # to be a GNU make extension (such as $(addsuffix ...)).
774 # Mention this in the diagnostic.
776 $gnuext = "\n(probably a GNU make extension)" if $var =~ / /;
777 # Accept recursive variable expansions if so desired
778 # (we hope they are rather portable in practice).
779 if ($var =~ /$_VARIABLE_RECURSIVE_PATTERN/o)
781 msg
('portability-recursive', $where,
782 "$var: non-POSIX recursive variable expansion$gnuext");
786 msg
('portability', $where, "$var: non-POSIX variable name$gnuext");
794 =item C<Automake::Variable::define($varname, $owner, $type, $cond, $value, $comment, $where, $pretty)>
796 Define or append to a new variable.
798 C<$varname>: the name of the variable being defined.
800 C<$owner>: owner of the variable (one of C<VAR_MAKEFILE>,
801 C<VAR_CONFIGURE>, or C<VAR_AUTOMAKE>, defined by L<Automake::VarDef>).
802 Variables can be overridden, provided the new owner is not weaker
803 (C<VAR_AUTOMAKE> < C<VAR_CONFIGURE> < C<VAR_MAKEFILE>).
805 C<$type>: the type of the assignment (C<''> for C<FOO = bar>,
806 C<':'> for C<FOO := bar>, and C<'+'> for C<'FOO += bar'>).
808 C<$cond>: the C<Condition> in which C<$var> is being defined.
810 C<$value>: the value assigned to C<$var> in condition C<$cond>.
812 C<$comment>: any comment (C<'# bla.'>) associated with the assignment.
813 Comments from C<+=> assignments stack with comments from the last C<=>
816 C<$where>: the C<Location> of the assignment.
818 C<$pretty>: whether C<$value> should be pretty printed (one of
819 C<VAR_ASIS>, C<VAR_PRETTY>, C<VAR_SILENT>, or C<VAR_SORTED>, defined
820 by by L<Automake::VarDef>). C<$pretty> applies only to real
821 assignments. I.e., it does not apply to a C<+=> assignment (except
822 when part of it is being done as a conditional C<=> assignment).
824 This function will all run any hook registered with the C<hook>
829 sub define
($$$$$$$$)
831 my ($var, $owner, $type, $cond, $value, $comment, $where, $pretty) = @_;
833 prog_error
"$cond is not a reference"
836 prog_error
"$where is not a reference"
839 prog_error
"pretty argument missing"
840 unless defined $pretty && ($pretty == VAR_ASIS
841 || $pretty == VAR_PRETTY
842 || $pretty == VAR_SILENT
843 || $pretty == VAR_SORTED
);
845 error
$where, "bad characters in variable name `$var'"
846 if $var !~ /$_VARIABLE_PATTERN/o;
848 # `:='-style assignments are not acknowledged by POSIX. Moreover it
849 # has multiple meanings. In GNU make or BSD make it means "assign
850 # with immediate expansion", while in OSF make it is used for
851 # conditional assignments.
852 msg
('portability', $where, "`:='-style assignments are not portable")
855 check_variable_expansions
($value, $where);
857 # If there's a comment, make sure it is \n-terminated.
868 my $self = _cvar
$var;
870 my $def = $self->def ($cond);
871 my $new_var = $def ?
0 : 1;
873 # Additional checks for Automake definitions.
874 if ($owner == VAR_AUTOMAKE
&& ! $new_var)
876 # An Automake variable must be consistently defined with the same
878 if ($def->type ne $type && $def->owner == VAR_AUTOMAKE
)
880 error
($def->location,
881 "Automake variable `$var' was set with `"
882 . $def->type . "=' here...", partial
=> 1);
883 error
($where, "... and is now set with `$type=' here.");
884 prog_error
("Automake variable assignments should be consistently\n"
885 . "defined with the same sign.");
888 # If Automake tries to override a value specified by the user,
889 # just don't let it do.
890 if ($def->owner != VAR_AUTOMAKE
)
892 if (! exists $_silent_variable_override{$var})
894 my $condmsg = ($cond == TRUE
895 ?
'' : (" in condition `" . $cond->human . "'"));
896 msg_cond_var
('override', $cond, $var,
897 "user variable `$var' defined here$condmsg...",
899 msg
('override', $where,
900 "... overrides Automake variable `$var' defined here");
902 verb
("refusing to override the user definition of:\n"
903 . $self->dump ."with `" . $cond->human . "' => `$value'");
908 # Differentiate assignment types.
910 # 1. append (+=) to a variable defined for current condition
911 if ($type eq '+' && ! $new_var)
913 $def->append ($value, $comment);
914 $self->{'last-append'} = [];
916 # Only increase owners. A VAR_CONFIGURE variable augmented in a
917 # Makefile.am becomes a VAR_MAKEFILE variable.
918 $def->set_owner ($owner, $where->clone)
919 if $owner > $def->owner;
921 # 2. append (+=) to a variable defined for *another* condition
922 elsif ($type eq '+' && ! $self->conditions->false)
924 # * Generally, $cond is not TRUE. For instance:
929 # In this case, we declare an helper variable conditionally,
930 # and append it to FOO:
931 # FOO = foo $(am__append_1)
932 # @COND_TRUE@am__append_1 = bar
933 # Of course if FOO is defined under several conditions, we add
934 # $(am__append_1) to each definitions.
936 # * If $cond is TRUE, we don't need the helper variable. E.g., in
943 # we can add bar directly to all definition of FOO, and output
944 # @COND_TRUE@FOO = foo1 bar
945 # @COND_FALSE@FOO = foo2 bar
948 # Do we need an helper variable?
951 # Can we reuse the helper variable created for the previous
952 # append? (We cannot reuse older helper variables because
953 # we must preserve the order of items appended to the
955 my $condstr = $cond->string;
956 my $key = "$var:$condstr";
957 my ($appendvar, $appendvarcond) = @
{$self->{'last-append'}};
958 if ($appendvar && $condstr eq $appendvarcond)
960 # Yes, let's simply append to it.
962 $owner = VAR_AUTOMAKE
;
964 $def = $self->rdef ($cond);
970 my $num = ++$_appendvar;
971 my $hvar = "am__append_$num";
972 $lastappend = [$hvar, $condstr];
973 &define
($hvar, VAR_AUTOMAKE
, '+',
974 $cond, $value, $comment, $where, $pretty);
976 # Now HVAR is to be added to VAR.
978 $value = "\$($hvar)";
982 # Add VALUE to all definitions of SELF.
983 foreach my $vcond ($self->conditions->conds)
985 # We have a bit of error detection to do here.
991 # should be rejected because X is not defined for all conditions
992 # where `+=' applies.
993 my $undef_cond = $self->not_always_defined_in_cond ($cond);
994 if (! $undef_cond->false)
997 "Cannot apply `+=' because `$var' is not defined "
998 . "in\nthe following conditions:\n "
999 . join ("\n ", map { $_->human } $undef_cond->conds)
1000 . "\nEither define `$var' in these conditions,"
1001 . " or use\n`+=' in the same conditions as"
1002 . " the definitions.");
1006 &define
($var, $owner, '+', $vcond, $value, $comment,
1010 $self->{'last-append'} = $lastappend;
1012 # 3. first assignment (=, :=, or +=)
1015 # There must be no previous value unless the user is redefining
1016 # an Automake variable or an AC_SUBST variable for an existing
1018 _check_ambiguous_condition
($self, $cond, $where)
1020 && (($def->owner == VAR_AUTOMAKE
&& $owner != VAR_AUTOMAKE
)
1021 || $def->owner == VAR_CONFIGURE
));
1023 # Never decrease an owner.
1024 $owner = $def->owner
1025 if ! $new_var && $owner < $def->owner;
1027 # Assignments to a macro set its location. We don't adjust
1028 # locations for `+='. Ideally I suppose we would associate
1029 # line numbers with random bits of text.
1030 $def = new Automake
::VarDef
($var, $value, $comment, $where->clone,
1031 $type, $owner, $pretty);
1032 $self->set ($cond, $def);
1033 push @_var_order, $var;
1036 # Call any defined hook. This helps to update some internal state
1037 # *while* parsing the file. For instance the handling of SUFFIXES
1038 # requires this (see var_SUFFIXES_trigger).
1039 &{$_hooks{$var}}($type, $value) if exists $_hooks{$var};
1042 =item C<variable_delete ($varname, [@conds])>
1044 Forget about C<$varname> under the conditions C<@conds>, or completely
1045 if C<@conds> is empty.
1049 sub variable_delete
($@
)
1051 my ($var, @conds) = @_;
1055 delete $_variable_dict{$var};
1059 for my $cond (@conds)
1061 delete $_variable_dict{$var}{'defs'}{$cond};
1064 if ($var =~ /_([[:alnum:]]+)$/)
1066 delete $_primary_dict{$1}{$var};
1070 =item C<$str = variables_dump>
1072 Return a string describing all we know about all variables.
1077 sub variables_dump
()
1079 my $text = "All variables:\n{\n";
1080 foreach my $var (sort { $a->name cmp $b->name } variables
)
1082 $text .= $var->dump;
1089 =item C<$var = set_seen ($varname)>
1091 =item C<$var = $var-E<gt>set_seen>
1093 Mark all definitions of this variable as examined, if the variable
1094 exists. See L<Automake::VarDef::set_seen>.
1096 Return the C<Variable> object if the variable exists, or 0
1097 otherwise (i.e., as the C<var> function).
1104 $self = ref $self ?
$self : var
$self;
1106 return 0 unless $self;
1108 for my $c ($self->conditions->conds)
1110 $self->rdef ($c)->set_seen;
1117 =item C<$count = require_variables ($where, $reason, $cond, @variables)>
1119 Make sure that each supplied variable is defined in C<$cond>.
1120 Otherwise, issue a warning showing C<$reason> (C<$reason> should be
1121 the reason why these variables are required, for instance C<'option foo
1122 used'>). If we know which macro can define this variable, hint the
1123 user. Return the number of undefined variables.
1127 sub require_variables
($$$@
)
1129 my ($where, $reason, $cond, @vars) = @_;
1131 $reason .= ' but ' unless $reason eq '';
1134 foreach my $var (@vars)
1136 # Nothing to do if the variable exists.
1138 if vardef
($var, $cond);
1140 my $text = "$reason`$var' is undefined\n";
1144 my $undef_cond = $v->not_always_defined_in_cond ($cond);
1146 if $undef_cond->false;
1147 $text .= ("in the following conditions:\n "
1148 . join ("\n ", map { $_->human } $undef_cond->conds)
1154 if (exists $_am_macro_for_var{$var})
1156 my $mac = $_am_macro_for_var{$var};
1157 $text .= " The usual way to define `$var' is to add "
1158 . "`$mac'\n to `$configure_ac' and run `aclocal' and "
1159 . "`autoconf' again.";
1160 # aclocal will not warn about undefined macros unless it
1162 $text .= "\n If `$mac' is in `$configure_ac', make sure\n"
1163 . " its definition is in aclocal's search path."
1164 unless $mac =~ /^AM_/;
1166 elsif (exists $_ac_macro_for_var{$var})
1168 $text .= " The usual way to define `$var' is to add "
1169 . "`$_ac_macro_for_var{$var}'\n to `$configure_ac' and "
1170 . "run `autoconf' again.";
1173 error
$where, $text, uniq_scope
=> US_GLOBAL
;
1178 =item C<$count = $var->requires_variables ($reason, @variables)>
1180 Same as C<require_variables>, but a method of Automake::Variable.
1181 C<@variables> should be defined in the same conditions as C<$var> is
1186 sub requires_variables
($$@
)
1188 my ($var, $reason, @args) = @_;
1190 for my $cond ($var->conditions->conds)
1192 $res += require_variables
($var->rdef ($cond)->location, $reason,
1199 =item C<variable_value ($var)>
1201 Get the C<TRUE> value of a variable, warn if the variable is
1202 conditionally defined. C<$var> can be either a variable name
1203 or a C<Automake::Variable> instance (this allows calls such
1204 as C<$var-E<gt>variable_value>).
1208 sub variable_value
($)
1211 my $v = ref ($var) ?
$var : var
($var);
1212 return () unless $v;
1213 $v->check_defined_unconditionally;
1214 my $d = $v->def (TRUE
);
1215 return $d ?
$d->value : "";
1218 =item C<$str = output_variables>
1220 Format definitions for all variables.
1224 sub output_variables
()
1227 # We output variables it in the same order in which they were
1228 # defined (skipping duplicates).
1229 my @vars = uniq
@_var_order;
1231 # Output all the Automake variables. If the user changed one,
1232 # then it is now marked as VAR_CONFIGURE or VAR_MAKEFILE.
1233 foreach my $var (@vars)
1236 foreach my $cond ($v->conditions->conds)
1238 $res .= $v->output ($cond)
1239 if $v->rdef ($cond)->owner == VAR_AUTOMAKE
;
1243 # Now dump the user variables that were defined.
1244 foreach my $var (@vars)
1247 foreach my $cond ($v->conditions->conds)
1249 $res .= $v->output ($cond)
1250 if $v->rdef ($cond)->owner != VAR_AUTOMAKE
;
1256 =item C<$var-E<gt>traverse_recursively (&fun_item, &fun_collect, [cond_filter =E<gt> $cond_filter], [inner_expand =E<gt> 1], [skip_ac_subst =E<gt> 1])>
1258 Split the value of the Automake::Variable C<$var> on space, and
1259 traverse its components recursively.
1261 If C<$cond_filter> is an C<Automake::Condition>, process any
1262 conditions which are true when C<$cond_filter> is true. Otherwise,
1263 process all conditions.
1265 We distinguish two kinds of items in the content of C<$var>.
1266 Terms that look like C<$(foo)> or C<${foo}> are subvariables
1267 and cause recursion. Other terms are assumed to be filenames.
1269 Each time a filename is encountered, C<&fun_item> is called with the
1270 following arguments:
1272 ($var, -- the Automake::Variable we are currently
1274 $val, -- the item (i.e., filename) to process
1275 $cond, -- the Condition for the $var definition we are
1276 examining (ignoring the recursion context)
1277 $full_cond) -- the full Condition, taking into account
1278 conditions inherited from parent variables
1281 If C<inner_expand> is set, variable references occurring in filename
1282 (as in C<$(BASE).ext>) are expanded before the filename is passed to
1285 If C<skip_ac_subst> is set, Autoconf @substitutions@ will be skipped,
1286 i.e., C<&fun_item> will never be called for them.
1288 C<&fun_item> may return a list of items, they will be passed to
1289 C<&fun_store> later on. Define C<&fun_item> or @<&fun_store> as
1290 C<undef> when they serve no purpose.
1292 Once all items of a variable have been processed, the result (of the
1293 calls to C<&fun_items>, or of recursive traversals of subvariables)
1294 are passed to C<&fun_collect>. C<&fun_collect> receives three
1297 ($var, -- the variable being traversed
1298 $parent_cond, -- the Condition inherited from parent
1299 variables during recursion
1300 @condlist) -- a list of [$cond, @results] pairs
1301 where each $cond appear only once, and @result
1302 are all the results for this condition.
1304 Typically you should do C<$cond->merge ($parent_cond)> to recompute
1305 the C<$full_cond> associated to C<@result>. C<&fun_collect> may
1306 return a list of items, that will be used as the result of
1307 C<Automake::Variable::traverse_recursively> (the top-level, or its
1312 # Contains a stack of `from' and `to' parts of variable
1313 # substitutions currently in force.
1316 sub traverse_recursively
($&&;%)
1321 my ($var, $fun_item, $fun_collect, %options) = @_;
1322 my $cond_filter = $options{'cond_filter'};
1323 my $inner_expand = $options{'inner_expand'};
1324 my $skip_ac_subst = $options{'skip_ac_subst'};
1325 return $var->_do_recursive_traversal ($var,
1326 $fun_item, $fun_collect,
1327 $cond_filter, TRUE
, $inner_expand,
1331 # The guts of Automake::Variable::traverse_recursively.
1332 sub _do_recursive_traversal
($$&&$$$$)
1334 my ($var, $parent, $fun_item, $fun_collect, $cond_filter, $parent_cond,
1335 $inner_expand, $skip_ac_subst) = @_;
1339 if ($var->{'scanned'} == $_traversal)
1341 err_var
$var, "variable `" . $var->name() . "' recursively defined";
1344 $var->{'scanned'} = $_traversal;
1346 my @allresults = ();
1348 foreach my $cond ($var->conditions->conds)
1350 if (ref $cond_filter)
1352 # Ignore conditions that don't match $cond_filter.
1353 next if ! $cond->true_when ($cond_filter);
1354 # If we found out several definitions of $var
1355 # match $cond_filter then we are in trouble.
1356 # Tell the user we don't support this.
1357 $var->check_defined_unconditionally ($parent, $parent_cond)
1362 my $full_cond = $cond->merge ($parent_cond);
1364 my @to_process = $var->value_as_list ($cond, $parent, $parent_cond);
1367 my $val = shift @to_process;
1368 # If $val is a variable (i.e. ${foo} or $(bar), not a filename),
1369 # handle the sub variable recursively.
1370 # (Backslashes before `}' and `)' within brackets are here to
1371 # please Emacs's indentation.)
1372 if ($val =~ /^\$\{([^\}]*)\}$/ || $val =~ /^\$\(([^\)]*)\)$/)
1374 my $subvarname = $1;
1376 # If the user uses a losing variable name, just ignore it.
1377 # This isn't ideal, but people have requested it.
1378 next if ($subvarname =~ /\@.*\@/);
1380 # See if the variable is actually a substitution reference
1382 # This handles substitution references like ${foo:.a=.b}.
1383 if ($subvarname =~ /^([^:]*):([^=]*)=(.*)$/o)
1387 $from = quotemeta $2;
1390 my $subvar = var
($subvarname);
1391 # Don't recurse into undefined variables.
1392 next unless $subvar;
1394 push @_substfroms, $from;
1395 push @_substtos, $to;
1397 my @res = $subvar->_do_recursive_traversal ($parent,
1404 push (@result, @res);
1411 # Try to expand variable references inside filenames such as
1412 # `$(NAME).txt'. We do not handle `:.foo=.bar'
1413 # substitutions, but it would make little sense to use this
1415 elsif ($inner_expand
1416 && ($val =~ /\$\{([^\}]*)\}/ || $val =~ /\$\(([^\)]*)\)/))
1418 my $subvarname = $1;
1419 my $subvar = var
$subvarname;
1422 # Replace the reference by its value, and reschedule
1424 foreach my $c ($subvar->conditions->conds)
1426 if (ref $cond_filter)
1428 # Ignore conditions that don't match $cond_filter.
1429 next if ! $c->true_when ($cond_filter);
1430 # If we found out several definitions of $var
1431 # match $cond_filter then we are in trouble.
1432 # Tell the user we don't support this.
1433 $subvar->check_defined_unconditionally ($var,
1438 my $subval = $subvar->rdef ($c)->value;
1439 $val =~ s/\$\{$subvarname\}/$subval/g;
1440 $val =~ s/\$\($subvarname\)/$subval/g;
1441 unshift @to_process, split (' ', $val);
1445 # We do not know any variable with this name. Fall through
1446 # to filename processing.
1448 elsif ($skip_ac_subst && $val =~ /^\@.+\@$/)
1453 if ($fun_item) # $var is a filename we must process
1455 my $substnum=$#_substfroms;
1456 while ($substnum >= 0)
1458 $val =~ s/$_substfroms[$substnum]$/$_substtos[$substnum]/
1459 if defined $_substfroms[$substnum];
1463 # Make sure you update the doc of
1464 # Automake::Variable::traverse_recursively
1465 # if you change the prototype of &fun_item.
1466 my @transformed = &$fun_item ($var, $val, $cond, $full_cond);
1467 push (@result, @transformed);
1470 push (@allresults, [$cond, @result]) if @result;
1473 # We only care about _recursive_ variable definitions. The user
1474 # is free to use the same variable several times in the same definition.
1475 $var->{'scanned'} = -1;
1478 unless $fun_collect;
1479 # Make sure you update the doc of Automake::Variable::traverse_recursively
1480 # if you change the prototype of &fun_collect.
1481 return &$fun_collect ($var, $parent_cond, @allresults);
1484 # _hash_varname ($VAR)
1485 # --------------------
1486 # Compute the key associated $VAR in %_gen_varname.
1487 # See _gen_varname() below.
1488 sub _hash_varname
($)
1492 foreach my $cond ($var->conditions->conds)
1494 my @values = $var->value_as_list ($cond);
1495 $key .= "($cond)@values";
1500 # _hash_values (@VALUES)
1501 # ----------------------
1502 # Hash @VALUES for %_gen_varname. @VALUES should be a list
1503 # of pairs: ([$cond, @values], [$cond, @values], ...).
1504 # See _gen_varname() below.
1505 sub _hash_values
(@
)
1508 foreach my $pair (@_)
1510 my ($cond, @values) = @
$pair;
1511 $key .= "($cond)@values";
1515 # ($VARNAME, $GENERATED)
1516 # _gen_varname ($BASE, @DEFINITIONS)
1517 # ---------------------------------
1518 # Return a variable name starting with $BASE, that will be
1519 # used to store definitions @DEFINITIONS.
1520 # @DEFINITIONS is a list of pair [$COND, @OBJECTS].
1522 # If we already have a $BASE-variable containing @DEFINITIONS, reuse
1523 # it and set $GENERATED to 0. Otherwise construct a new name and set
1526 # This way, we avoid combinatorial explosion of the generated
1527 # variables. Especially, in a Makefile such as:
1543 # | B=$(A1) $(A2) ... $(AN)
1548 # The generated c_OBJECTS and d_OBJECTS will share the same variable
1551 # This setup can be the case of a testsuite containing lots (>100) of
1552 # small C programs, all testing the same set of source files.
1553 sub _gen_varname
($@
)
1556 my $key = _hash_values
@_;
1558 return ($_gen_varname{$base}{$key}, 0)
1559 if exists $_gen_varname{$base}{$key};
1561 my $num = 1 + ($_gen_varname_n{$base} || 0);
1562 $_gen_varname_n{$base} = $num;
1563 my $name = "${base}_${num}";
1564 $_gen_varname{$base}{$key} = $name;
1569 =item C<$resvar = transform_variable_recursively ($var, $resvar, $base, $nodefine, $where, &fun_item, [%options])>
1571 =item C<$resvar = $var-E<gt>transform_variable_recursively ($resvar, $base, $nodefine, $where, &fun_item, [%options])>
1573 Traverse C<$var> recursively, and create a C<$resvar> variable in
1574 which each filename in C<$var> have been transformed using
1575 C<&fun_item>. (C<$var> may be a variable name in the first syntax.
1576 It must be an C<Automake::Variable> otherwise.)
1578 Helper variables (corresponding to sub-variables of C<$var>) are
1579 created as needed, using C<$base> as prefix.
1582 $var source variable to traverse
1583 $resvar resulting variable to define
1584 $base prefix to use when naming subvariables of $resvar
1585 $nodefine if true, traverse $var but do not define any variable
1586 (this assumes &fun_item has some useful side-effect)
1587 $where context into which variable definitions are done
1588 &fun_item a transformation function -- see the documentation
1589 of &fun_item in Automake::Variable::traverse_recursively.
1591 This returns the string C<"\$($RESVAR)">.
1593 C<%options> is a list of options to pass to
1594 C<Variable::traverse_recursively> (see this method).
1598 sub transform_variable_recursively
($$$$$&;%)
1600 my ($var, $resvar, $base, $nodefine, $where, $fun_item, %options) = @_;
1602 $var = ref $var ?
$var : rvar
$var;
1604 my $res = $var->traverse_recursively
1606 # The code that defines the variable holding the result
1607 # of the recursive transformation of a subvariable.
1609 my ($subvar, $parent_cond, @allresults) = @_;
1610 # If no definition is required, return anything: the result is
1611 # not expected to be used, only the side effect of $fun_item
1613 return 'report-me' if $nodefine;
1614 # Cache $subvar, so that we reuse it if @allresults is the same.
1615 my $key = _hash_varname
$subvar;
1616 $_gen_varname{$base}{$key} = $subvar->name;
1618 # Find a name for the variable, unless this is the top-variable
1619 # for which we want to use $resvar.
1620 my ($varname, $generated) =
1621 ($var != $subvar) ? _gen_varname
($base, @allresults) : ($resvar, 1);
1623 # Define the variable if we are not reusing a previously
1624 # defined variable. At the top-level, we can also avoid redefining
1625 # the variable if it already contains the same values.
1627 && !($varname eq $var->name && $key eq _hash_values
@allresults))
1629 # If the new variable is the source variable, we assume
1630 # we are trying to override a user variable. Delete
1631 # the old variable first.
1632 variable_delete
($varname) if $varname eq $var->name;
1633 # Define an empty variable in condition TRUE if there is no
1635 @allresults = ([TRUE
, '']) unless @allresults;
1636 # Define the rewritten variable in all conditions not
1637 # already covered by user definitions.
1638 foreach my $pair (@allresults)
1640 my ($cond, @result) = @
$pair;
1641 my $var = var
$varname;
1643 ?
$var->not_always_defined_in_cond ($cond)->conds
1648 define
($varname, VAR_AUTOMAKE
, '', $_, "@result",
1649 '', $where, VAR_PRETTY
);
1654 return "\$($varname)";
1665 L<Automake::VarDef>, L<Automake::Condition>,
1666 L<Automake::DisjConditions>, L<Automake::Location>.
1672 ### Setup "GNU" style for perl-mode and cperl-mode.
1674 ## perl-indent-level: 2
1675 ## perl-continued-statement-offset: 2
1676 ## perl-continued-brace-offset: 0
1677 ## perl-brace-offset: 0
1678 ## perl-brace-imaginary-offset: 0
1679 ## perl-label-offset: -2
1680 ## cperl-indent-level: 2
1681 ## cperl-brace-offset: 0
1682 ## cperl-continued-brace-offset: 0
1683 ## cperl-label-offset: -2
1684 ## cperl-extra-newline-before-brace: t
1685 ## cperl-merge-trailing-else: nil
1686 ## cperl-continued-statement-offset: 2