1 # Copyright (C) 1997, 2001, 2002, 2003, 2006, 2008, 2009 Free Software
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2, or (at your option)
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package Automake::Condition;
22 use vars '@ISA', '@EXPORT_OK';
24 @EXPORT_OK = qw/TRUE FALSE reduce_and reduce_or/;
28 Automake::Condition - record a conjunction of conditionals
32 use Automake::Condition;
34 # Create a condition to represent "COND1 and not COND2".
35 my $cond = new Automake::Condition "COND1_TRUE", "COND2_FALSE";
36 # Create a condition to represent "not COND3".
37 my $other = new Automake::Condition "COND3_FALSE";
39 # Create a condition to represent
40 # "COND1 and not COND2 and not COND3".
41 my $both = $cond->merge ($other);
43 # Likewise, but using a list of conditional strings
44 my $both2 = $cond->merge_conds ("COND3_FALSE");
46 # Strip from $both any subconditions which are in $other.
47 # This is the opposite of merge.
48 $cond = $both->strip ($other);
50 # Return the list of conditions ("COND1_TRUE", "COND2_FALSE"):
51 my @conds = $cond->conds;
53 # Is $cond always true? (Not in this example)
54 if ($cond->true) { ... }
56 # Is $cond always false? (Not in this example)
57 if ($cond->false) { ... }
59 # Return the list of conditionals as a string:
60 # "COND1_TRUE COND2_FALSE"
61 my $str = $cond->string;
63 # Return the list of conditionals as a human readable string:
65 my $str = $cond->human;
67 # Return the list of conditionals as a AC_SUBST-style string:
68 # "@COND1_TRUE@@COND2_FALSE@"
69 my $subst = $cond->subst_string;
71 # Is $cond true when $both is true? (Yes in this example)
72 if ($cond->true_when ($both)) { ... }
74 # Is $cond redundant w.r.t. {$other, $both}?
75 # (Yes in this example)
76 if ($cond->redundant_wrt ($other, $both)) { ... }
78 # Does $cond imply any of {$other, $both}?
79 # (Not in this example)
80 if ($cond->implies_any ($other, $both)) { ... }
82 # Remove superfluous conditionals assuming they will eventually
83 # be multiplied together.
84 # (Returns @conds = ($both) in this example, because
85 # $other and $cond are implied by $both.)
86 @conds = Automake::Condition::reduce_and ($other, $both, $cond);
88 # Remove superfluous conditionals assuming they will eventually
90 # (Returns @conds = ($cond, $other) in this example, because
91 # $both is a subset condition of $cond: $cond is true whenever $both
93 @conds = Automake::Condition::reduce_or ($other, $both, $cond);
95 # Invert a Condition. This returns a list of Conditions.
100 A C<Condition> is a conjunction of conditionals (i.e., atomic conditions
101 defined in F<configure.ac> by C<AM_CONDITIONAL>. In Automake they
102 are used to represent the conditions into which F<Makefile> variables and
103 F<Makefile> rules are defined.
105 If the variable C<VAR> is defined as
113 then it will be associated a C<Condition> created with
114 the following statement.
116 new Automake::Condition "COND1_TRUE", "COND2_TRUE";
118 Remember that a C<Condition> is a I<conjunction> of conditionals, so
119 the above C<Condition> means C<VAR> is defined when C<COND1>
120 B<and> C<COND2> are true. There is no way to express disjunctions
121 (i.e., I<or>s) with this class (but see L<DisjConditions>).
123 Another point worth to mention is that each C<Condition> object is
124 unique with respect to its conditionals. Two C<Condition> objects
125 created for the same set of conditionals will have the same address.
126 This makes it easy to compare C<Condition>s: just compare the
129 my $c1 = new Automake::Condition "COND1_TRUE", "COND2_TRUE";
130 my $c2 = new Automake::Condition "COND1_TRUE", "COND2_TRUE";
137 =item C<$cond = new Automake::Condition [@conds]>
139 Return a C<Condition> objects for the conjunctions of conditionals
140 listed in C<@conds> as strings.
142 An item in C<@conds> should be either C<"FALSE">, C<"TRUE">, or have
143 the form C<"NAME_FALSE"> or C<"NAME_TRUE"> where C<NAME> can be
144 anything (in practice C<NAME> should be the name of a conditional
145 declared in F<configure.ac> with C<AM_CONDITIONAL>, but it's not
146 C<Automake::Condition>'s responsibility to ensure this).
148 An empty C<@conds> means C<"TRUE">.
150 As explained previously, the reference (object) returned is unique
151 with respect to C<@conds>. For this purpose, duplicate elements are
152 ignored, and C<@conds> is rewritten as C<("FALSE")> if it contains
153 C<"FALSE"> or two contradictory conditionals (such as C<"NAME_FALSE">
156 Therefore the following two statements create the same object (they
157 both create the C<"FALSE"> condition).
159 my $c3 = new Automake::Condition "COND1_TRUE", "COND1_FALSE";
160 my $c4 = new Automake::Condition "COND2_TRUE", "FALSE";
162 $c3 == FALSE; # True!
166 # Keys in this hash are conditional strings. Values are the
167 # associated object conditions. This is used by `new' to reuse
168 # Condition objects with identical conditionals.
169 use vars '%_condition_singletons';
170 # Do NOT reset this hash here. It's already empty by default,
171 # and any setting would otherwise occur AFTER the `TRUE' and `FALSE'
172 # constants definitions.
173 # %_condition_singletons = ();
177 my ($class, @conds) = @_;
183 for my $cond (@conds)
185 # Catch some common programming errors:
186 # - A Condition passed to new
187 confess "`$cond' is a reference, expected a string" if ref $cond;
188 # - A Condition passed as a string to new
189 confess "`$cond' does not look like a condition" if $cond =~ /::/;
192 # Accept strings like "FOO BAR" as shorthand for ("FOO", "BAR").
193 @conds = map { split (' ', $_) } @conds;
195 for my $cond (@conds)
197 next if $cond eq 'TRUE';
199 # Detect cases when @conds can be simplified to FALSE.
200 if (($cond eq 'FALSE' && $#conds > 0)
201 || ($cond =~ /^(.*)_TRUE$/ && exists $self->{'hash'}{"${1}_FALSE"})
202 || ($cond =~ /^(.*)_FALSE$/ && exists $self->{'hash'}{"${1}_TRUE"}))
207 $self->{'hash'}{$cond} = 1;
210 my $key = $self->string;
211 if (exists $_condition_singletons{$key})
213 return $_condition_singletons{$key};
215 $_condition_singletons{$key} = $self;
219 =item C<$newcond = $cond-E<gt>merge (@otherconds)>
221 Return a new condition which is the conjunction of
222 C<$cond> and C<@otherconds>.
228 my ($self, @otherconds) = @_;
229 new Automake::Condition (map { $_->conds } ($self, @otherconds));
232 =item C<$newcond = $cond-E<gt>merge_conds (@conds)>
234 Return a new condition which is the conjunction of C<$cond> and
235 C<@conds>, where C<@conds> is a list of conditional strings, as
242 my ($self, @conds) = @_;
243 new Automake::Condition $self->conds, @conds;
246 =item C<$newcond = $cond-E<gt>strip ($minuscond)>
248 Return a new condition which has all the conditionals of C<$cond>
249 except those of C<$minuscond>. This is the opposite of C<merge>.
255 my ($self, $minus) = @_;
256 my @res = grep { not $minus->_has ($_) } $self->conds;
257 return new Automake::Condition @res;
260 =item C<@list = $cond-E<gt>conds>
262 Return the set of conditionals defining C<$cond>, as strings. Note that
263 this might not be exactly the list passed to C<new> (or a
264 concatenation of such lists if C<merge> was used), because of the
265 cleanup mentioned in C<new>'s description.
267 For instance C<$c3-E<gt>conds> will simply return C<("FALSE")>.
274 my @conds = keys %{$self->{'hash'}};
275 return ("TRUE") unless @conds;
279 # Undocumented, shouldn't be needed outside of this class.
282 my ($self, $cond) = @_;
283 return exists $self->{'hash'}{$cond};
286 =item C<$cond-E<gt>false>
288 Return 1 iff this condition is always false.
295 return $self->_has ('FALSE');
298 =item C<$cond-E<gt>true>
300 Return 1 iff this condition is always true.
307 return 0 == keys %{$self->{'hash'}};
310 =item C<$cond-E<gt>string>
312 Build a string which denotes the condition.
314 For instance using the C<$cond> definition from L<SYNOPSYS>,
315 C<$cond-E<gt>string> will return C<"COND1_TRUE COND2_FALSE">.
323 return $self->{'string'} if defined $self->{'string'};
332 $res = join (' ', $self->conds);
334 $self->{'string'} = $res;
338 =item C<$cond-E<gt>human>
340 Build a human readable string which denotes the condition.
342 For instance using the C<$cond> definition from L<SYNOPSYS>,
343 C<$cond-E<gt>string> will return C<"COND1 and !COND2">.
350 if ($s =~ /^(.*)_(TRUE|FALSE)$/)
352 return (($2 eq 'FALSE') ? '!' : '') . $1;
364 return $self->{'human'} if defined $self->{'human'};
373 $res = join (' and ', map { _to_human $_ } $self->conds);
375 $self->{'human'} = $res;
379 =item C<$cond-E<gt>subst_string>
381 Build a C<AC_SUBST>-style string for output in F<Makefile.in>.
383 For instance using the C<$cond> definition from L<SYNOPSYS>,
384 C<$cond-E<gt>subst_string> will return C<"@COND1_TRUE@@COND2_FALSE@">.
388 sub subst_string ($ )
392 return $self->{'subst_string'} if defined $self->{'subst_string'};
399 elsif (! $self->true)
401 $res = '@' . join ('@@', sort $self->conds) . '@';
403 $self->{'subst_string'} = $res;
407 =item C<$cond-E<gt>true_when ($when)>
409 Return 1 iff C<$cond> is true when C<$when> is true.
412 Using the definitions from L<SYNOPSYS>, C<$cond> is true
413 when C<$both> is true, but the converse is wrong.
419 my ($self, $when) = @_;
421 # Nothing is true when FALSE (not even FALSE itself, but it
422 # shouldn't hurt if you decide to change that).
423 return 0 if $self->false || $when->false;
425 # If we are true, we stay true when $when is true :)
426 return 1 if $self->true;
428 # $SELF is true under $WHEN if each conditional component of $SELF
430 foreach my $cond ($self->conds)
432 return 0 unless $when->_has ($cond);
437 =item C<$cond-E<gt>redundant_wrt (@conds)>
439 Return 1 iff C<$cond> is true for any condition in C<@conds>.
440 If @conds is empty, return 1 iff C<$cond> is C<FALSE>.
445 sub redundant_wrt ($@)
447 my ($self, @conds) = @_;
449 foreach my $cond (@conds)
451 return 1 if $self->true_when ($cond);
456 =item C<$cond-E<gt>implies_any (@conds)>
458 Return 1 iff C<$cond> implies any of the conditions in C<@conds>.
465 my ($self, @conds) = @_;
467 foreach my $cond (@conds)
469 return 1 if $cond->true_when ($self);
474 =item C<$cond-E<gt>not>
476 Return a negation of C<$cond> as a list of C<Condition>s.
477 This list should be used to construct a C<DisjConditions>
478 (we cannot return a C<DisjConditions> from C<Automake::Condition>,
479 because that would make these two packages interdependent).
486 return @{$self->{'not'}} if defined $self->{'not'};
488 map { new Automake::Condition &conditional_negate ($_) } $self->conds;
489 $self->{'not'} = [@res];
493 =item C<$cond-E<gt>multiply (@conds)>
495 Assumption: C<@conds> represent a disjunction of conditions.
497 Return the result of multiplying C<$cond> with that disjunction.
498 The result will be a list of conditions suitable to construct a
505 my ($self, @set) = @_;
509 my $ans = $self->merge ($cond);
513 # FALSE can always be removed from a disjunction.
516 # Now, $self is a common factor of the remaining conditions.
517 # If one of the conditions is $self, we can discard the rest.
519 if exists $res{$self};
521 return (values %res);
526 =head2 Other helper functions
532 The C<"TRUE"> conditional.
536 The C<"FALSE"> conditional.
540 use constant TRUE => new Automake::Condition "TRUE";
541 use constant FALSE => new Automake::Condition "FALSE";
543 =item C<reduce_and (@conds)>
545 Return a subset of @conds with the property that the conjunction of
546 the subset is the same as the conjunction of @conds. For example, if
547 both C<COND1_TRUE COND2_TRUE> and C<COND1_TRUE> are in the list,
548 discard the latter. If the input list is empty, return C<(TRUE)>.
559 $cond = shift @conds;
561 # FALSE is absorbent.
565 if (! $cond->redundant_wrt (@ret, @conds))
571 return TRUE if @ret == 0;
575 =item C<reduce_or (@conds)>
577 Return a subset of @conds with the property that the disjunction of
578 the subset is equivalent to the disjunction of @conds. For example,
579 if both C<COND1_TRUE COND2_TRUE> and C<COND1_TRUE> are in the list,
580 discard the former. If the input list is empty, return C<(FALSE)>.
591 $cond = shift @conds;
599 unless $cond->implies_any (@ret, @conds);
602 return FALSE if @ret == 0;
606 =item C<conditional_negate ($condstr)>
608 Negate a conditional string.
612 sub conditional_negate ($)
616 $cond =~ s/TRUE$/TRUEO/;
617 $cond =~ s/FALSE$/TRUE/;
618 $cond =~ s/TRUEO$/FALSE/;
627 L<Automake::DisjConditions>.
631 C<AM_CONDITIONAL>s and supporting code were added to Automake 1.1o by
632 Ian Lance Taylor <ian@cygnus.org> in 1997. Since then it has been
633 improved by Tom Tromey <tromey@redhat.com>, Richard Boulton
634 <richard@tartarus.org>, Raja R Harinath <harinath@cs.umn.edu>,
635 Akim Demaille <akim@epita.fr>, and Alexandre Duret-Lutz <adl@gnu.org>.
641 ### Setup "GNU" style for perl-mode and cperl-mode.
643 ## perl-indent-level: 2
644 ## perl-continued-statement-offset: 2
645 ## perl-continued-brace-offset: 0
646 ## perl-brace-offset: 0
647 ## perl-brace-imaginary-offset: 0
648 ## perl-label-offset: -2
649 ## cperl-indent-level: 2
650 ## cperl-brace-offset: 0
651 ## cperl-continued-brace-offset: 0
652 ## cperl-label-offset: -2
653 ## cperl-extra-newline-before-brace: t
654 ## cperl-merge-trailing-else: nil
655 ## cperl-continued-statement-offset: 2