bin: Rely only on the shebang line
[automake.git] / lib / Automake / Condition.pm
blobc028c24feb6e4d6149b28bf3ec7889425d7ab8ac
1 # Copyright (C) 1997-2018 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)
6 # any later version.
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 <https://www.gnu.org/licenses/>.
16 package Automake::Condition;
18 use 5.006;
19 use strict;
20 use Carp;
22 require Exporter;
23 use vars '@ISA', '@EXPORT_OK';
24 @ISA = qw/Exporter/;
25 @EXPORT_OK = qw/TRUE FALSE reduce_and reduce_or/;
27 =head1 NAME
29 Automake::Condition - record a conjunction of conditionals
31 =head1 SYNOPSIS
33 use Automake::Condition;
35 # Create a condition to represent "COND1 and not COND2".
36 my $cond = new Automake::Condition "COND1_TRUE", "COND2_FALSE";
37 # Create a condition to represent "not COND3".
38 my $other = new Automake::Condition "COND3_FALSE";
40 # Create a condition to represent
41 # "COND1 and not COND2 and not COND3".
42 my $both = $cond->merge ($other);
44 # Likewise, but using a list of conditional strings
45 my $both2 = $cond->merge_conds ("COND3_FALSE");
47 # Strip from $both any subconditions which are in $other.
48 # This is the opposite of merge.
49 $cond = $both->strip ($other);
51 # Return the list of conditions ("COND1_TRUE", "COND2_FALSE"):
52 my @conds = $cond->conds;
54 # Is $cond always true? (Not in this example)
55 if ($cond->true) { ... }
57 # Is $cond always false? (Not in this example)
58 if ($cond->false) { ... }
60 # Return the list of conditionals as a string:
61 # "COND1_TRUE COND2_FALSE"
62 my $str = $cond->string;
64 # Return the list of conditionals as a human readable string:
65 # "COND1 and !COND2"
66 my $str = $cond->human;
68 # Return the list of conditionals as a AC_SUBST-style string:
69 # "@COND1_TRUE@@COND2_FALSE@"
70 my $subst = $cond->subst_string;
72 # Is $cond true when $both is true? (Yes in this example)
73 if ($cond->true_when ($both)) { ... }
75 # Is $cond redundant w.r.t. {$other, $both}?
76 # (Yes in this example)
77 if ($cond->redundant_wrt ($other, $both)) { ... }
79 # Does $cond imply any of {$other, $both}?
80 # (Not in this example)
81 if ($cond->implies_any ($other, $both)) { ... }
83 # Remove superfluous conditionals assuming they will eventually
84 # be multiplied together.
85 # (Returns @conds = ($both) in this example, because
86 # $other and $cond are implied by $both.)
87 @conds = Automake::Condition::reduce_and ($other, $both, $cond);
89 # Remove superfluous conditionals assuming they will eventually
90 # be summed together.
91 # (Returns @conds = ($cond, $other) in this example, because
92 # $both is a subset condition of $cond: $cond is true whenever $both
93 # is true.)
94 @conds = Automake::Condition::reduce_or ($other, $both, $cond);
96 # Invert a Condition. This returns a list of Conditions.
97 @conds = $both->not;
99 =head1 DESCRIPTION
101 A C<Condition> is a conjunction of conditionals (i.e., atomic conditions
102 defined in F<configure.ac> by C<AM_CONDITIONAL>. In Automake they
103 are used to represent the conditions into which F<Makefile> variables and
104 F<Makefile> rules are defined.
106 If the variable C<VAR> is defined as
108 if COND1
109 if COND2
110 VAR = value
111 endif
112 endif
114 then it will be associated a C<Condition> created with
115 the following statement.
117 new Automake::Condition "COND1_TRUE", "COND2_TRUE";
119 Remember that a C<Condition> is a I<conjunction> of conditionals, so
120 the above C<Condition> means C<VAR> is defined when C<COND1>
121 B<and> C<COND2> are true. There is no way to express disjunctions
122 (i.e., I<or>s) with this class (but see L<DisjConditions>).
124 Another point worth to mention is that each C<Condition> object is
125 unique with respect to its conditionals. Two C<Condition> objects
126 created for the same set of conditionals will have the same address.
127 This makes it easy to compare C<Condition>s: just compare the
128 references.
130 my $c1 = new Automake::Condition "COND1_TRUE", "COND2_TRUE";
131 my $c2 = new Automake::Condition "COND1_TRUE", "COND2_TRUE";
132 $c1 == $c2; # True!
134 =head2 Methods
136 =over 4
138 =item C<$cond = new Automake::Condition [@conds]>
140 Return a C<Condition> objects for the conjunctions of conditionals
141 listed in C<@conds> as strings.
143 An item in C<@conds> should be either C<"FALSE">, C<"TRUE">, or have
144 the form C<"NAME_FALSE"> or C<"NAME_TRUE"> where C<NAME> can be
145 anything (in practice C<NAME> should be the name of a conditional
146 declared in F<configure.ac> with C<AM_CONDITIONAL>, but it's not
147 C<Automake::Condition>'s responsibility to ensure this).
149 An empty C<@conds> means C<"TRUE">.
151 As explained previously, the reference (object) returned is unique
152 with respect to C<@conds>. For this purpose, duplicate elements are
153 ignored, and C<@conds> is rewritten as C<("FALSE")> if it contains
154 C<"FALSE"> or two contradictory conditionals (such as C<"NAME_FALSE">
155 and C<"NAME_TRUE">.)
157 Therefore the following two statements create the same object (they
158 both create the C<"FALSE"> condition).
160 my $c3 = new Automake::Condition "COND1_TRUE", "COND1_FALSE";
161 my $c4 = new Automake::Condition "COND2_TRUE", "FALSE";
162 $c3 == $c4; # True!
163 $c3 == FALSE; # True!
165 =cut
167 # Keys in this hash are conditional strings. Values are the
168 # associated object conditions. This is used by 'new' to reuse
169 # Condition objects with identical conditionals.
170 use vars '%_condition_singletons';
171 # Do NOT reset this hash here. It's already empty by default,
172 # and any setting would otherwise occur AFTER the 'TRUE' and 'FALSE'
173 # constants definitions.
174 # %_condition_singletons = ();
176 sub new ($;@)
178 my ($class, @conds) = @_;
179 my $self = {
180 hash => {},
182 bless $self, $class;
184 for my $cond (@conds)
186 # Catch some common programming errors:
187 # - A Condition passed to new
188 confess "'$cond' is a reference, expected a string" if ref $cond;
189 # - A Condition passed as a string to new
190 confess "'$cond' does not look like a condition" if $cond =~ /::/;
193 # Accept strings like "FOO BAR" as shorthand for ("FOO", "BAR").
194 @conds = map { split (' ', $_) } @conds;
196 for my $cond (@conds)
198 next if $cond eq 'TRUE';
200 # Detect cases when @conds can be simplified to FALSE.
201 if (($cond eq 'FALSE' && $#conds > 0)
202 || ($cond =~ /^(.*)_TRUE$/ && exists $self->{'hash'}{"${1}_FALSE"})
203 || ($cond =~ /^(.*)_FALSE$/ && exists $self->{'hash'}{"${1}_TRUE"}))
205 return &FALSE;
208 $self->{'hash'}{$cond} = 1;
211 my $key = $self->string;
212 if (exists $_condition_singletons{$key})
214 return $_condition_singletons{$key};
216 $_condition_singletons{$key} = $self;
217 return $self;
220 =item C<$newcond = $cond-E<gt>merge (@otherconds)>
222 Return a new condition which is the conjunction of
223 C<$cond> and C<@otherconds>.
225 =cut
227 sub merge ($@)
229 my ($self, @otherconds) = @_;
230 new Automake::Condition (map { $_->conds } ($self, @otherconds));
233 =item C<$newcond = $cond-E<gt>merge_conds (@conds)>
235 Return a new condition which is the conjunction of C<$cond> and
236 C<@conds>, where C<@conds> is a list of conditional strings, as
237 passed to C<new>.
239 =cut
241 sub merge_conds ($@)
243 my ($self, @conds) = @_;
244 new Automake::Condition $self->conds, @conds;
247 =item C<$newcond = $cond-E<gt>strip ($minuscond)>
249 Return a new condition which has all the conditionals of C<$cond>
250 except those of C<$minuscond>. This is the opposite of C<merge>.
252 =cut
254 sub strip ($$)
256 my ($self, $minus) = @_;
257 my @res = grep { not $minus->_has ($_) } $self->conds;
258 return new Automake::Condition @res;
261 =item C<@list = $cond-E<gt>conds>
263 Return the set of conditionals defining C<$cond>, as strings. Note that
264 this might not be exactly the list passed to C<new> (or a
265 concatenation of such lists if C<merge> was used), because of the
266 cleanup mentioned in C<new>'s description.
268 For instance C<$c3-E<gt>conds> will simply return C<("FALSE")>.
270 =cut
272 sub conds ($ )
274 my ($self) = @_;
275 my @conds = keys %{$self->{'hash'}};
276 return ("TRUE") unless @conds;
277 return sort @conds;
280 # Undocumented, shouldn't be needed outside of this class.
281 sub _has ($$)
283 my ($self, $cond) = @_;
284 return exists $self->{'hash'}{$cond};
287 =item C<$cond-E<gt>false>
289 Return 1 iff this condition is always false.
291 =cut
293 sub false ($ )
295 my ($self) = @_;
296 return $self->_has ('FALSE');
299 =item C<$cond-E<gt>true>
301 Return 1 iff this condition is always true.
303 =cut
305 sub true ($ )
307 my ($self) = @_;
308 return 0 == keys %{$self->{'hash'}};
311 =item C<$cond-E<gt>string>
313 Build a string which denotes the condition.
315 For instance using the C<$cond> definition from L<SYNOPSYS>,
316 C<$cond-E<gt>string> will return C<"COND1_TRUE COND2_FALSE">.
318 =cut
320 sub string ($ )
322 my ($self) = @_;
324 return $self->{'string'} if defined $self->{'string'};
326 my $res = '';
327 if ($self->false)
329 $res = 'FALSE';
331 else
333 $res = join (' ', $self->conds);
335 $self->{'string'} = $res;
336 return $res;
339 =item C<$cond-E<gt>human>
341 Build a human readable string which denotes the condition.
343 For instance using the C<$cond> definition from L<SYNOPSYS>,
344 C<$cond-E<gt>string> will return C<"COND1 and !COND2">.
346 =cut
348 sub _to_human ($ )
350 my ($s) = @_;
351 if ($s =~ /^(.*)_(TRUE|FALSE)$/)
353 return (($2 eq 'FALSE') ? '!' : '') . $1;
355 else
357 return $s;
361 sub human ($ )
363 my ($self) = @_;
365 return $self->{'human'} if defined $self->{'human'};
367 my $res = '';
368 if ($self->false)
370 $res = 'FALSE';
372 else
374 $res = join (' and ', map { _to_human $_ } $self->conds);
376 $self->{'human'} = $res;
377 return $res;
380 =item C<$cond-E<gt>subst_string>
382 Build a C<AC_SUBST>-style string for output in F<Makefile.in>.
384 For instance using the C<$cond> definition from L<SYNOPSYS>,
385 C<$cond-E<gt>subst_string> will return C<"@COND1_TRUE@@COND2_FALSE@">.
387 =cut
389 sub subst_string ($ )
391 my ($self) = @_;
393 return $self->{'subst_string'} if defined $self->{'subst_string'};
395 my $res = '';
396 if ($self->false)
398 $res = '#';
400 elsif (! $self->true)
402 $res = '@' . join ('@@', sort $self->conds) . '@';
404 $self->{'subst_string'} = $res;
405 return $res;
408 =item C<$cond-E<gt>true_when ($when)>
410 Return 1 iff C<$cond> is true when C<$when> is true.
411 Return 0 otherwise.
413 Using the definitions from L<SYNOPSYS>, C<$cond> is true
414 when C<$both> is true, but the converse is wrong.
416 =cut
418 sub true_when ($$)
420 my ($self, $when) = @_;
422 # Nothing is true when FALSE (not even FALSE itself, but it
423 # shouldn't hurt if you decide to change that).
424 return 0 if $self->false || $when->false;
426 # If we are true, we stay true when $when is true :)
427 return 1 if $self->true;
429 # $SELF is true under $WHEN if each conditional component of $SELF
430 # exists in $WHEN.
431 foreach my $cond ($self->conds)
433 return 0 unless $when->_has ($cond);
435 return 1;
438 =item C<$cond-E<gt>redundant_wrt (@conds)>
440 Return 1 iff C<$cond> is true for any condition in C<@conds>.
441 If @conds is empty, return 1 iff C<$cond> is C<FALSE>.
442 Return 0 otherwise.
444 =cut
446 sub redundant_wrt ($@)
448 my ($self, @conds) = @_;
450 foreach my $cond (@conds)
452 return 1 if $self->true_when ($cond);
454 return $self->false;
457 =item C<$cond-E<gt>implies_any (@conds)>
459 Return 1 iff C<$cond> implies any of the conditions in C<@conds>.
460 Return 0 otherwise.
462 =cut
464 sub implies_any ($@)
466 my ($self, @conds) = @_;
468 foreach my $cond (@conds)
470 return 1 if $cond->true_when ($self);
472 return 0;
475 =item C<$cond-E<gt>not>
477 Return a negation of C<$cond> as a list of C<Condition>s.
478 This list should be used to construct a C<DisjConditions>
479 (we cannot return a C<DisjConditions> from C<Automake::Condition>,
480 because that would make these two packages interdependent).
482 =cut
484 sub not ($ )
486 my ($self) = @_;
487 return @{$self->{'not'}} if defined $self->{'not'};
488 my @res =
489 map { new Automake::Condition &conditional_negate ($_) } $self->conds;
490 $self->{'not'} = [@res];
491 return @res;
494 =item C<$cond-E<gt>multiply (@conds)>
496 Assumption: C<@conds> represent a disjunction of conditions.
498 Return the result of multiplying C<$cond> with that disjunction.
499 The result will be a list of conditions suitable to construct a
500 C<DisjConditions>.
502 =cut
504 sub multiply ($@)
506 my ($self, @set) = @_;
507 my %res = ();
508 for my $cond (@set)
510 my $ans = $self->merge ($cond);
511 $res{$ans} = $ans;
514 # FALSE can always be removed from a disjunction.
515 delete $res{FALSE};
517 # Now, $self is a common factor of the remaining conditions.
518 # If one of the conditions is $self, we can discard the rest.
519 return ($self, ())
520 if exists $res{$self};
522 return (values %res);
525 =back
527 =head2 Other helper functions
529 =over 4
531 =item C<TRUE>
533 The C<"TRUE"> conditional.
535 =item C<FALSE>
537 The C<"FALSE"> conditional.
539 =cut
541 use constant TRUE => new Automake::Condition "TRUE";
542 use constant FALSE => new Automake::Condition "FALSE";
544 =item C<reduce_and (@conds)>
546 Return a subset of @conds with the property that the conjunction of
547 the subset is the same as the conjunction of @conds. For example, if
548 both C<COND1_TRUE COND2_TRUE> and C<COND1_TRUE> are in the list,
549 discard the latter. If the input list is empty, return C<(TRUE)>.
551 =cut
553 sub reduce_and (@)
555 my (@conds) = @_;
556 my @ret = ();
557 my $cond;
558 while (@conds > 0)
560 $cond = shift @conds;
562 # FALSE is absorbent.
563 return FALSE
564 if $cond == FALSE;
566 if (! $cond->redundant_wrt (@ret, @conds))
568 push (@ret, $cond);
572 return TRUE if @ret == 0;
573 return @ret;
576 =item C<reduce_or (@conds)>
578 Return a subset of @conds with the property that the disjunction of
579 the subset is equivalent to the disjunction of @conds. For example,
580 if both C<COND1_TRUE COND2_TRUE> and C<COND1_TRUE> are in the list,
581 discard the former. If the input list is empty, return C<(FALSE)>.
583 =cut
585 sub reduce_or (@)
587 my (@conds) = @_;
588 my @ret = ();
589 my $cond;
590 while (@conds > 0)
592 $cond = shift @conds;
594 next
595 if $cond == FALSE;
596 return TRUE
597 if $cond == TRUE;
599 push (@ret, $cond)
600 unless $cond->implies_any (@ret, @conds);
603 return FALSE if @ret == 0;
604 return @ret;
607 =item C<conditional_negate ($condstr)>
609 Negate a conditional string.
611 =cut
613 sub conditional_negate ($)
615 my ($cond) = @_;
617 $cond =~ s/TRUE$/TRUEO/;
618 $cond =~ s/FALSE$/TRUE/;
619 $cond =~ s/TRUEO$/FALSE/;
621 return $cond;
624 =back
626 =head1 SEE ALSO
628 L<Automake::DisjConditions>.
630 =head1 HISTORY
632 C<AM_CONDITIONAL>s and supporting code were added to Automake 1.1o by
633 Ian Lance Taylor <ian@cygnus.org> in 1997. Since then it has been
634 improved by Tom Tromey <tromey@redhat.com>, Richard Boulton
635 <richard@tartarus.org>, Raja R Harinath <harinath@cs.umn.edu>,
636 Akim Demaille <akim@epita.fr>, and Alexandre Duret-Lutz <adl@gnu.org>.
638 =cut