File Coverage

File:/usr/local/share/automake-1.11/Automake/Condition.pm
Coverage:35.8%

linestmtbrancondsubpodtimecode
1# Copyright (C) 1997, 2001, 2002, 2003, 2006, 2008 Free Software
2# Foundation, Inc.
3
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.
8
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.
13
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/>.
16
17package Automake::Condition;
18
1
1
1
4
1
5
use strict;
19
1
1
1
16
1
6
use Carp;
20
21require Exporter;
22
1
1
1
4
1
5
use vars '@ISA', '@EXPORT_OK';
23@ISA = qw/Exporter/;
24@EXPORT_OK = qw/TRUE FALSE reduce_and reduce_or/;
25
26 - 164
=head1 NAME

Automake::Condition - record a conjunction of conditionals

=head1 SYNOPSIS

  use Automake::Condition;

  # Create a condition to represent "COND1 and not COND2".
  my $cond = new Automake::Condition "COND1_TRUE", "COND2_FALSE";
  # Create a condition to represent "not COND3".
  my $other = new Automake::Condition "COND3_FALSE";

  # Create a condition to represent
  #   "COND1 and not COND2 and not COND3".
  my $both = $cond->merge ($other);

  # Likewise, but using a list of conditional strings
  my $both2 = $cond->merge_conds ("COND3_FALSE");

  # Strip from $both any subconditions which are in $other.
  # This is the opposite of merge.
  $cond = $both->strip ($other);

  # Return the list of conditions ("COND1_TRUE", "COND2_FALSE"):
  my @conds = $cond->conds;

  # Is $cond always true?  (Not in this example)
  if ($cond->true) { ... }

  # Is $cond always false? (Not in this example)
  if ($cond->false) { ... }

  # Return the list of conditionals as a string:
  #  "COND1_TRUE COND2_FALSE"
  my $str = $cond->string;

  # Return the list of conditionals as a human readable string:
  #  "COND1 and !COND2"
  my $str = $cond->human;

  # Return the list of conditionals as a AC_SUBST-style string:
  #  "@COND1_TRUE@@COND2_FALSE@"
  my $subst = $cond->subst_string;

  # Is $cond true when $both is true?  (Yes in this example)
  if ($cond->true_when ($both)) { ... }

  # Is $cond redundant w.r.t. {$other, $both}?
  # (Yes in this example)
  if ($cond->redundant_wrt ($other, $both)) { ... }

  # Does $cond imply any of {$other, $both}?
  # (Not in this example)
  if ($cond->implies_any ($other, $both)) { ... }

  # Remove superfluous conditionals assuming they will eventually
  # be multiplied together.
  # (Returns @conds = ($both) in this example, because
  # $other and $cond are implied by $both.)
  @conds = Automake::Condition::reduce_and ($other, $both, $cond);

  # Remove superfluous conditionals assuming they will eventually
  # be summed together.
  # (Returns @conds = ($cond, $other) in this example, because
  # $both is a subset condition of $cond: $cond is true whenever $both
  # is true.)
  @conds = Automake::Condition::reduce_or ($other, $both, $cond);

  # Invert a Condition.  This returns a list of Conditions.
  @conds = $both->not;

=head1 DESCRIPTION

A C<Condition> is a conjunction of conditionals (i.e., atomic conditions
defined in F<configure.ac> by C<AM_CONDITIONAL>.  In Automake they
are used to represent the conditions into which F<Makefile> variables and
F<Makefile> rules are defined.

If the variable C<VAR> is defined as

  if COND1
    if COND2
      VAR = value
    endif
  endif

then it will be associated a C<Condition> created with
the following statement.

  new Automake::Condition "COND1_TRUE", "COND2_TRUE";

Remember that a C<Condition> is a I<conjunction> of conditionals, so
the above C<Condition> means C<VAR> is defined when C<COND1>
B<and> C<COND2> are true. There is no way to express disjunctions
(i.e., I<or>s) with this class (but see L<DisjConditions>).

Another point worth to mention is that each C<Condition> object is
unique with respect to its conditionals.  Two C<Condition> objects
created for the same set of conditionals will have the same address.
This makes it easy to compare C<Condition>s: just compare the
references.

  my $c1 = new Automake::Condition "COND1_TRUE", "COND2_TRUE";
  my $c2 = new Automake::Condition "COND1_TRUE", "COND2_TRUE";
  $c1 == $c2;  # True!

=head2 Methods

=over 4

=item C<$cond = new Automake::Condition [@conds]>

Return a C<Condition> objects for the conjunctions of conditionals
listed in C<@conds> as strings.

An item in C<@conds> should be either C<"FALSE">, C<"TRUE">, or have
the form C<"NAME_FALSE"> or C<"NAME_TRUE"> where C<NAME> can be
anything (in practice C<NAME> should be the name of a conditional
declared in F<configure.ac> with C<AM_CONDITIONAL>, but it's not
C<Automake::Condition>'s responsibility to ensure this).

An empty C<@conds> means C<"TRUE">.

As explained previously, the reference (object) returned is unique
with respect to C<@conds>.  For this purpose, duplicate elements are
ignored, and C<@conds> is rewritten as C<("FALSE")> if it contains
C<"FALSE"> or two contradictory conditionals (such as C<"NAME_FALSE">
and C<"NAME_TRUE">.)

Therefore the following two statements create the same object (they
both create the C<"FALSE"> condition).

  my $c3 = new Automake::Condition "COND1_TRUE", "COND1_FALSE";
  my $c4 = new Automake::Condition "COND2_TRUE", "FALSE";
  $c3 == $c4;   # True!
  $c3 == FALSE; # True!

=cut
165
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
1
1
1
6
1
4
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 = ();
174
175sub new ($;@)
176{
177
2
1
5
  my ($class, @conds) = @_;
178
2
5
  my $self = {
179    hash => {},
180  };
181
2
12
  bless $self, $class;
182
183  # Accept strings like "FOO BAR" as shorthand for ("FOO", "BAR").
184
2
2
4
7
  @conds = map { split (' ', $_) } @conds;
185
186
2
4
  for my $cond (@conds)
187    {
188
2
11
      next if $cond eq 'TRUE';
189
190      # Catch some common programming errors:
191      # - A Condition passed to new
192
1
3
      confess "`$cond' is a reference, expected a string" if ref $cond;
193      # - A Condition passed as a string to new
194
1
6
      confess "`$cond' does not look like a condition" if $cond =~ /::/;
195
196      # Detect cases when @conds can be simplified to FALSE.
197
1
23
      if (($cond eq 'FALSE' && $#conds > 0)
198          || ($cond =~ /^(.*)_TRUE$/ && exists $self->{'hash'}{"${1}_FALSE"})
199          || ($cond =~ /^(.*)_FALSE$/ && exists $self->{'hash'}{"${1}_TRUE"}))
200        {
201
0
0
          return &FALSE;
202        }
203
204
1
5
      $self->{'hash'}{$cond} = 1;
205    }
206
207
2
5
  my $key = $self->string;
208
2
4
  if (exists $_condition_singletons{$key})
209    {
210
0
0
      return $_condition_singletons{$key};
211    }
212
2
8
  $_condition_singletons{$key} = $self;
213
2
10
  return $self;
214}
215
216 - 221
=item C<$newcond = $cond-E<gt>merge (@otherconds)>

Return a new condition which is the conjunction of
C<$cond> and C<@otherconds>.

=cut
222
223sub merge ($@)
224{
225
0
1
0
  my ($self, @otherconds) = @_;
226
0
0
0
0
  new Automake::Condition (map { $_->conds } ($self, @otherconds));
227}
228
229 - 235
=item C<$newcond = $cond-E<gt>merge_conds (@conds)>

Return a new condition which is the conjunction of C<$cond> and
C<@conds>, where C<@conds> is a list of conditional strings, as
passed to C<new>.

=cut
236
237sub merge_conds ($@)
238{
239
0
1
0
  my ($self, @conds) = @_;
240
0
0
  new Automake::Condition $self->conds, @conds;
241}
242
243 - 248
=item C<$newcond = $cond-E<gt>strip ($minuscond)>

Return a new condition which has all the conditionals of C<$cond>
except those of C<$minuscond>.  This is the opposite of C<merge>.

=cut
249
250sub strip ($$)
251{
252
0
1
0
  my ($self, $minus) = @_;
253
0
0
0
0
  my @res = grep { not $minus->has ($_) } $self->conds;
254
0
0
  return new Automake::Condition @res;
255}
256
257 - 266
=item C<@list = $cond-E<gt>conds>

Return the set of conditionals defining C<$cond>, as strings.  Note that
this might not be exactly the list passed to C<new> (or a
concatenation of such lists if C<merge> was used), because of the
cleanup mentioned in C<new>'s description.

For instance C<$c3-E<gt>conds> will simply return C<("FALSE")>.

=cut
267
268sub conds ($ )
269{
270
1
1
2
  my ($self) = @_;
271
1
1
1
3
  my @conds = keys %{$self->{'hash'}};
272
1
5
  return ("TRUE") unless @conds;
273
0
0
  return sort @conds;
274}
275
276# Undocumented, shouldn't be needed outside of this class.
277sub has ($$)
278{
279
2
0
4
  my ($self, $cond) = @_;
280
2
8
  return exists $self->{'hash'}{$cond};
281}
282
283 - 287
=item C<$cond-E<gt>false>

Return 1 iff this condition is always false.

=cut
288
289sub false ($ )
290{
291
2
1
5
  my ($self) = @_;
292
2
4
  return $self->has ('FALSE');
293}
294
295 - 299
=item C<$cond-E<gt>true>

Return 1 iff this condition is always true.

=cut
300
301sub true ($ )
302{
303
0
1
0
  my ($self) = @_;
304
0
0
0
0
  return 0 == keys %{$self->{'hash'}};
305}
306
307 - 314
=item C<$cond-E<gt>string>

Build a string which denotes the condition.

For instance using the C<$cond> definition from L<SYNOPSYS>,
C<$cond-E<gt>string> will return C<"COND1_TRUE COND2_FALSE">.

=cut
315
316sub string ($ )
317{
318
2
1
2
  my ($self) = @_;
319
320
2
7
  return $self->{'string'} if defined $self->{'string'};
321
322
2
2
  my $res = '';
323
2
7
  if ($self->false)
324    {
325
1
1
      $res = 'FALSE';
326    }
327  else
328    {
329
1
2
      $res = join (' ', $self->conds);
330    }
331
2
4
  $self->{'string'} = $res;
332
2
4
  return $res;
333}
334
335 - 342
=item C<$cond-E<gt>human>

Build a human readable string which denotes the condition.

For instance using the C<$cond> definition from L<SYNOPSYS>,
C<$cond-E<gt>string> will return C<"COND1 and !COND2">.

=cut
343
344sub _to_human ($ )
345{
346
0
  my ($s) = @_;
347
0
  if ($s =~ /^(.*)_(TRUE|FALSE)$/)
348    {
349
0
      return (($2 eq 'FALSE') ? '!' : '') . $1;
350    }
351  else
352    {
353
0
      return $s;
354    }
355}
356
357sub human ($ )
358{
359
0
1
  my ($self) = @_;
360
361
0
  return $self->{'human'} if defined $self->{'human'};
362
363
0
  my $res = '';
364
0
  if ($self->false)
365    {
366
0
      $res = 'FALSE';
367    }
368  else
369    {
370
0
0
      $res = join (' and ', map { _to_human $_ } $self->conds);
371    }
372
0
  $self->{'human'} = $res;
373
0
  return $res;
374}
375
376 - 383
=item C<$cond-E<gt>subst_string>

Build a C<AC_SUBST>-style string for output in F<Makefile.in>.

For instance using the C<$cond> definition from L<SYNOPSYS>,
C<$cond-E<gt>subst_string> will return C<"@COND1_TRUE@@COND2_FALSE@">.

=cut
384
385sub subst_string ($ )
386{
387
0
1
  my ($self) = @_;
388
389
0
  return $self->{'subst_string'} if defined $self->{'subst_string'};
390
391
0
  my $res = '';
392
0
  if ($self->false)
393    {
394
0
      $res = '#';
395    }
396  elsif (! $self->true)
397    {
398
0
      $res = '@' . join ('@@', sort $self->conds) . '@';
399    }
400
0
  $self->{'subst_string'} = $res;
401
0
  return $res;
402}
403
404 - 412
=item C<$cond-E<gt>true_when ($when)>

Return 1 iff C<$cond> is true when C<$when> is true.
Return 0 otherwise.

Using the definitions from L<SYNOPSYS>, C<$cond> is true
when C<$both> is true, but the converse is wrong.

=cut
413
414sub true_when ($$)
415{
416
0
1
  my ($self, $when) = @_;
417
418  # Nothing is true when FALSE (not even FALSE itself, but it
419  # shouldn't hurt if you decide to change that).
420
0
  return 0 if $self->false || $when->false;
421
422  # If we are true, we stay true when $when is true :)
423
0
  return 1 if $self->true;
424
425  # $SELF is true under $WHEN if each conditional component of $SELF
426  # exists in $WHEN.
427
0
  foreach my $cond ($self->conds)
428    {
429
0
      return 0 unless $when->has ($cond);
430    }
431
0
  return 1;
432}
433
434 - 440
=item C<$cond-E<gt>redundant_wrt (@conds)>

Return 1 iff C<$cond> is true for any condition in C<@conds>.
If @conds is empty, return 1 iff C<$cond> is C<FALSE>.
Return 0 otherwise.

=cut
441
442sub redundant_wrt ($@)
443{
444
0
1
  my ($self, @conds) = @_;
445
446
0
  foreach my $cond (@conds)
447    {
448
0
      return 1 if $self->true_when ($cond);
449    }
450
0
  return $self->false;
451}
452
453 - 458
=item C<$cond-E<gt>implies_any (@conds)>

Return 1 iff C<$cond> implies any of the conditions in C<@conds>.
Return 0 otherwise.

=cut
459
460sub implies_any ($@)
461{
462
0
1
  my ($self, @conds) = @_;
463
464
0
  foreach my $cond (@conds)
465    {
466
0
      return 1 if $cond->true_when ($self);
467    }
468
0
  return 0;
469}
470
471 - 478
=item C<$cond-E<gt>not>

Return a negation of C<$cond> as a list of C<Condition>s.
This list should be used to construct a C<DisjConditions>
(we cannot return a C<DisjConditions> from C<Automake::Condition>,
because that would make these two packages interdependent).

=cut
479
480sub not ($ )
481{
482
0
1
  my ($self) = @_;
483
0
0
  return @{$self->{'not'}} if defined $self->{'not'};
484
0
  my @res =
485
0
    map { new Automake::Condition &conditional_negate ($_) } $self->conds;
486
0
  $self->{'not'} = [@res];
487
0
  return @res;
488}
489
490 - 498
=item C<$cond-E<gt>multiply (@conds)>

Assumption: C<@conds> represent a disjunction of conditions.

Return the result of multiplying C<$cond> with that disjunction.
The result will be a list of conditions suitable to construct a
C<DisjConditions>.

=cut
499
500sub multiply ($@)
501{
502
0
1
  my ($self, @set) = @_;
503
0
  my %res = ();
504
0
  for my $cond (@set)
505    {
506
0
      my $ans = $self->merge ($cond);
507
0
      $res{$ans} = $ans;
508    }
509
510  # FALSE can always be removed from a disjunction.
511
0
  delete $res{FALSE};
512
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
0
  return ($self, ())
516    if exists $res{$self};
517
518
0
  return (values %res);
519}
520
521 - 533
=head2 Other helper functions

=over 4

=item C<TRUE>

The C<"TRUE"> conditional.

=item C<FALSE>

The C<"FALSE"> conditional.

=cut
534
535
1
1
1
5
1
4
use constant TRUE => new Automake::Condition "TRUE";
536
1
1
1
4
1
2
use constant FALSE => new Automake::Condition "FALSE";
537
538 - 545
=item C<reduce_and (@conds)>

Return a subset of @conds with the property that the conjunction of
the subset is the same as the conjunction of @conds.  For example, if
both C<COND1_TRUE COND2_TRUE> and C<COND1_TRUE> are in the list,
discard the latter.  If the input list is empty, return C<(TRUE)>.

=cut
546
547sub reduce_and (@)
548{
549
0
1
  my (@conds) = @_;
550
0
  my @ret = ();
551
0
  my $cond;
552
0
  while (@conds > 0)
553    {
554
0
      $cond = shift @conds;
555
556      # FALSE is absorbent.
557
0
      return FALSE
558        if $cond == FALSE;
559
560
0
      if (! $cond->redundant_wrt (@ret, @conds))
561        {
562
0
          push (@ret, $cond);
563        }
564    }
565
566
0
  return TRUE if @ret == 0;
567
0
  return @ret;
568}
569
570 - 577
=item C<reduce_or (@conds)>

Return a subset of @conds with the property that the disjunction of
the subset is equivalent to the disjunction of @conds.  For example,
if both C<COND1_TRUE COND2_TRUE> and C<COND1_TRUE> are in the list,
discard the former.  If the input list is empty, return C<(FALSE)>.

=cut
578
579sub reduce_or (@)
580{
581
0
1
  my (@conds) = @_;
582
0
  my @ret = ();
583
0
  my $cond;
584
0
  while (@conds > 0)
585    {
586
0
      $cond = shift @conds;
587
588      next
589
0
       if $cond == FALSE;
590
0
      return TRUE
591       if $cond == TRUE;
592
593
0
      push (@ret, $cond)
594       unless $cond->implies_any (@ret, @conds);
595    }
596
597
0
  return FALSE if @ret == 0;
598
0
  return @ret;
599}
600
601 - 605
=item C<conditional_negate ($condstr)>

Negate a conditional string.

=cut
606
607sub conditional_negate ($)
608{
609
0
1
  my ($cond) = @_;
610
611
0
  $cond =~ s/TRUE$/TRUEO/;
612
0
  $cond =~ s/FALSE$/TRUE/;
613
0
  $cond =~ s/TRUEO$/FALSE/;
614
615
0
  return $cond;
616}
617
618 - 630
=head1 SEE ALSO

L<Automake::DisjConditions>.

=head1 HISTORY

C<AM_CONDITIONAL>s and supporting code were added to Automake 1.1o by
Ian Lance Taylor <ian@cygnus.org> in 1997.  Since then it has been
improved by Tom Tromey <tromey@redhat.com>, Richard Boulton
<richard@tartarus.org>, Raja R Harinath <harinath@cs.umn.edu>,
Akim Demaille <akim@epita.fr>, and  Alexandre Duret-Lutz <adl@gnu.org>.

=cut
631
6321;
633
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: