RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / toolchains / hndtools-arm-linux-2.6.36-uclibc-4.5.3 / share / automake-1.11 / Automake / Condition.pm
blob2d649f6863118edf8b99350a89a2d9a701256d95
1 # Copyright (C) 1997, 2001, 2002, 2003, 2006, 2008 Free Software
2 # Foundation, Inc.
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)
7 # any later version.
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;
18 use strict;
19 use Carp;
21 require Exporter;
22 use vars '@ISA', '@EXPORT_OK';
23 @ISA = qw/Exporter/;
24 @EXPORT_OK = qw/TRUE FALSE reduce_and reduce_or/;
26 =head1 NAME
28 Automake::Condition - record a conjunction of conditionals
30 =head1 SYNOPSIS
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:
64 # "COND1 and !COND2"
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
89 # be summed together.
90 # (Returns @conds = ($cond, $other) in this example, because
91 # $both is a subset condition of $cond: $cond is true whenever $both
92 # is true.)
93 @conds = Automake::Condition::reduce_or ($other, $both, $cond);
95 # Invert a Condition. This returns a list of Conditions.
96 @conds = $both->not;
98 =head1 DESCRIPTION
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
107 if COND1
108 if COND2
109 VAR = value
110 endif
111 endif
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
127 references.
129 my $c1 = new Automake::Condition "COND1_TRUE", "COND2_TRUE";
130 my $c2 = new Automake::Condition "COND1_TRUE", "COND2_TRUE";
131 $c1 == $c2; # True!
133 =head2 Methods
135 =over 4
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">
154 and C<"NAME_TRUE">.)
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";
161 $c3 == $c4; # True!
162 $c3 == FALSE; # True!
164 =cut
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 = ();
175 sub new ($;@)
177 my ($class, @conds) = @_;
178 my $self = {
179 hash => {},
181 bless $self, $class;
183 # Accept strings like "FOO BAR" as shorthand for ("FOO", "BAR").
184 @conds = map { split (' ', $_) } @conds;
186 for my $cond (@conds)
188 next if $cond eq 'TRUE';
190 # Catch some common programming errors:
191 # - A Condition passed to new
192 confess "`$cond' is a reference, expected a string" if ref $cond;
193 # - A Condition passed as a string to new
194 confess "`$cond' does not look like a condition" if $cond =~ /::/;
196 # Detect cases when @conds can be simplified to FALSE.
197 if (($cond eq 'FALSE' && $#conds > 0)
198 || ($cond =~ /^(.*)_TRUE$/ && exists $self->{'hash'}{"${1}_FALSE"})
199 || ($cond =~ /^(.*)_FALSE$/ && exists $self->{'hash'}{"${1}_TRUE"}))
201 return &FALSE;
204 $self->{'hash'}{$cond} = 1;
207 my $key = $self->string;
208 if (exists $_condition_singletons{$key})
210 return $_condition_singletons{$key};
212 $_condition_singletons{$key} = $self;
213 return $self;
216 =item C<$newcond = $cond-E<gt>merge (@otherconds)>
218 Return a new condition which is the conjunction of
219 C<$cond> and C<@otherconds>.
221 =cut
223 sub merge ($@)
225 my ($self, @otherconds) = @_;
226 new Automake::Condition (map { $_->conds } ($self, @otherconds));
229 =item C<$newcond = $cond-E<gt>merge_conds (@conds)>
231 Return a new condition which is the conjunction of C<$cond> and
232 C<@conds>, where C<@conds> is a list of conditional strings, as
233 passed to C<new>.
235 =cut
237 sub merge_conds ($@)
239 my ($self, @conds) = @_;
240 new Automake::Condition $self->conds, @conds;
243 =item C<$newcond = $cond-E<gt>strip ($minuscond)>
245 Return a new condition which has all the conditionals of C<$cond>
246 except those of C<$minuscond>. This is the opposite of C<merge>.
248 =cut
250 sub strip ($$)
252 my ($self, $minus) = @_;
253 my @res = grep { not $minus->has ($_) } $self->conds;
254 return new Automake::Condition @res;
257 =item C<@list = $cond-E<gt>conds>
259 Return the set of conditionals defining C<$cond>, as strings. Note that
260 this might not be exactly the list passed to C<new> (or a
261 concatenation of such lists if C<merge> was used), because of the
262 cleanup mentioned in C<new>'s description.
264 For instance C<$c3-E<gt>conds> will simply return C<("FALSE")>.
266 =cut
268 sub conds ($ )
270 my ($self) = @_;
271 my @conds = keys %{$self->{'hash'}};
272 return ("TRUE") unless @conds;
273 return sort @conds;
276 # Undocumented, shouldn't be needed outside of this class.
277 sub has ($$)
279 my ($self, $cond) = @_;
280 return exists $self->{'hash'}{$cond};
283 =item C<$cond-E<gt>false>
285 Return 1 iff this condition is always false.
287 =cut
289 sub false ($ )
291 my ($self) = @_;
292 return $self->has ('FALSE');
295 =item C<$cond-E<gt>true>
297 Return 1 iff this condition is always true.
299 =cut
301 sub true ($ )
303 my ($self) = @_;
304 return 0 == keys %{$self->{'hash'}};
307 =item C<$cond-E<gt>string>
309 Build a string which denotes the condition.
311 For instance using the C<$cond> definition from L<SYNOPSYS>,
312 C<$cond-E<gt>string> will return C<"COND1_TRUE COND2_FALSE">.
314 =cut
316 sub string ($ )
318 my ($self) = @_;
320 return $self->{'string'} if defined $self->{'string'};
322 my $res = '';
323 if ($self->false)
325 $res = 'FALSE';
327 else
329 $res = join (' ', $self->conds);
331 $self->{'string'} = $res;
332 return $res;
335 =item C<$cond-E<gt>human>
337 Build a human readable string which denotes the condition.
339 For instance using the C<$cond> definition from L<SYNOPSYS>,
340 C<$cond-E<gt>string> will return C<"COND1 and !COND2">.
342 =cut
344 sub _to_human ($ )
346 my ($s) = @_;
347 if ($s =~ /^(.*)_(TRUE|FALSE)$/)
349 return (($2 eq 'FALSE') ? '!' : '') . $1;
351 else
353 return $s;
357 sub human ($ )
359 my ($self) = @_;
361 return $self->{'human'} if defined $self->{'human'};
363 my $res = '';
364 if ($self->false)
366 $res = 'FALSE';
368 else
370 $res = join (' and ', map { _to_human $_ } $self->conds);
372 $self->{'human'} = $res;
373 return $res;
376 =item C<$cond-E<gt>subst_string>
378 Build a C<AC_SUBST>-style string for output in F<Makefile.in>.
380 For instance using the C<$cond> definition from L<SYNOPSYS>,
381 C<$cond-E<gt>subst_string> will return C<"@COND1_TRUE@@COND2_FALSE@">.
383 =cut
385 sub subst_string ($ )
387 my ($self) = @_;
389 return $self->{'subst_string'} if defined $self->{'subst_string'};
391 my $res = '';
392 if ($self->false)
394 $res = '#';
396 elsif (! $self->true)
398 $res = '@' . join ('@@', sort $self->conds) . '@';
400 $self->{'subst_string'} = $res;
401 return $res;
404 =item C<$cond-E<gt>true_when ($when)>
406 Return 1 iff C<$cond> is true when C<$when> is true.
407 Return 0 otherwise.
409 Using the definitions from L<SYNOPSYS>, C<$cond> is true
410 when C<$both> is true, but the converse is wrong.
412 =cut
414 sub true_when ($$)
416 my ($self, $when) = @_;
418 # Nothing is true when FALSE (not even FALSE itself, but it
419 # shouldn't hurt if you decide to change that).
420 return 0 if $self->false || $when->false;
422 # If we are true, we stay true when $when is true :)
423 return 1 if $self->true;
425 # $SELF is true under $WHEN if each conditional component of $SELF
426 # exists in $WHEN.
427 foreach my $cond ($self->conds)
429 return 0 unless $when->has ($cond);
431 return 1;
434 =item C<$cond-E<gt>redundant_wrt (@conds)>
436 Return 1 iff C<$cond> is true for any condition in C<@conds>.
437 If @conds is empty, return 1 iff C<$cond> is C<FALSE>.
438 Return 0 otherwise.
440 =cut
442 sub redundant_wrt ($@)
444 my ($self, @conds) = @_;
446 foreach my $cond (@conds)
448 return 1 if $self->true_when ($cond);
450 return $self->false;
453 =item C<$cond-E<gt>implies_any (@conds)>
455 Return 1 iff C<$cond> implies any of the conditions in C<@conds>.
456 Return 0 otherwise.
458 =cut
460 sub implies_any ($@)
462 my ($self, @conds) = @_;
464 foreach my $cond (@conds)
466 return 1 if $cond->true_when ($self);
468 return 0;
471 =item C<$cond-E<gt>not>
473 Return a negation of C<$cond> as a list of C<Condition>s.
474 This list should be used to construct a C<DisjConditions>
475 (we cannot return a C<DisjConditions> from C<Automake::Condition>,
476 because that would make these two packages interdependent).
478 =cut
480 sub not ($ )
482 my ($self) = @_;
483 return @{$self->{'not'}} if defined $self->{'not'};
484 my @res =
485 map { new Automake::Condition &conditional_negate ($_) } $self->conds;
486 $self->{'not'} = [@res];
487 return @res;
490 =item C<$cond-E<gt>multiply (@conds)>
492 Assumption: C<@conds> represent a disjunction of conditions.
494 Return the result of multiplying C<$cond> with that disjunction.
495 The result will be a list of conditions suitable to construct a
496 C<DisjConditions>.
498 =cut
500 sub multiply ($@)
502 my ($self, @set) = @_;
503 my %res = ();
504 for my $cond (@set)
506 my $ans = $self->merge ($cond);
507 $res{$ans} = $ans;
510 # FALSE can always be removed from a disjunction.
511 delete $res{FALSE};
513 # Now, $self is a common factor of the remaining conditions.
514 # If one of the conditions is $self, we can discard the rest.
515 return ($self, ())
516 if exists $res{$self};
518 return (values %res);
521 =head2 Other helper functions
523 =over 4
525 =item C<TRUE>
527 The C<"TRUE"> conditional.
529 =item C<FALSE>
531 The C<"FALSE"> conditional.
533 =cut
535 use constant TRUE => new Automake::Condition "TRUE";
536 use constant FALSE => new Automake::Condition "FALSE";
538 =item C<reduce_and (@conds)>
540 Return a subset of @conds with the property that the conjunction of
541 the subset is the same as the conjunction of @conds. For example, if
542 both C<COND1_TRUE COND2_TRUE> and C<COND1_TRUE> are in the list,
543 discard the latter. If the input list is empty, return C<(TRUE)>.
545 =cut
547 sub reduce_and (@)
549 my (@conds) = @_;
550 my @ret = ();
551 my $cond;
552 while (@conds > 0)
554 $cond = shift @conds;
556 # FALSE is absorbent.
557 return FALSE
558 if $cond == FALSE;
560 if (! $cond->redundant_wrt (@ret, @conds))
562 push (@ret, $cond);
566 return TRUE if @ret == 0;
567 return @ret;
570 =item C<reduce_or (@conds)>
572 Return a subset of @conds with the property that the disjunction of
573 the subset is equivalent to the disjunction of @conds. For example,
574 if both C<COND1_TRUE COND2_TRUE> and C<COND1_TRUE> are in the list,
575 discard the former. If the input list is empty, return C<(FALSE)>.
577 =cut
579 sub reduce_or (@)
581 my (@conds) = @_;
582 my @ret = ();
583 my $cond;
584 while (@conds > 0)
586 $cond = shift @conds;
588 next
589 if $cond == FALSE;
590 return TRUE
591 if $cond == TRUE;
593 push (@ret, $cond)
594 unless $cond->implies_any (@ret, @conds);
597 return FALSE if @ret == 0;
598 return @ret;
601 =item C<conditional_negate ($condstr)>
603 Negate a conditional string.
605 =cut
607 sub conditional_negate ($)
609 my ($cond) = @_;
611 $cond =~ s/TRUE$/TRUEO/;
612 $cond =~ s/FALSE$/TRUE/;
613 $cond =~ s/TRUEO$/FALSE/;
615 return $cond;
618 =head1 SEE ALSO
620 L<Automake::DisjConditions>.
622 =head1 HISTORY
624 C<AM_CONDITIONAL>s and supporting code were added to Automake 1.1o by
625 Ian Lance Taylor <ian@cygnus.org> in 1997. Since then it has been
626 improved by Tom Tromey <tromey@redhat.com>, Richard Boulton
627 <richard@tartarus.org>, Raja R Harinath <harinath@cs.umn.edu>,
628 Akim Demaille <akim@epita.fr>, and Alexandre Duret-Lutz <adl@gnu.org>.
630 =cut
634 ### Setup "GNU" style for perl-mode and cperl-mode.
635 ## Local Variables:
636 ## perl-indent-level: 2
637 ## perl-continued-statement-offset: 2
638 ## perl-continued-brace-offset: 0
639 ## perl-brace-offset: 0
640 ## perl-brace-imaginary-offset: 0
641 ## perl-label-offset: -2
642 ## cperl-indent-level: 2
643 ## cperl-brace-offset: 0
644 ## cperl-continued-brace-offset: 0
645 ## cperl-label-offset: -2
646 ## cperl-extra-newline-before-brace: t
647 ## cperl-merge-trailing-else: nil
648 ## cperl-continued-statement-offset: 2
649 ## End: