summaryrefslogtreecommitdiff
path: root/lib/Automake/CondStack.pm
blob: 45cec9b9e118c6a54135468dc10a89239ad73016 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# Copyright (C) 2018  Free Software Foundation, Inc.

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

package Automake::CondStack;

use 5.006;
use strict;

use Exporter;
use Automake::Condition qw (TRUE FALSE);
use Automake::Global;
use Automake::Channels;
use Automake::ChannelDefs;

use vars qw (@ISA @EXPORT);

@ISA = qw (Exporter);
@EXPORT = qw (@cond_stack &cond_stack_if &cond_stack_else &cond_stack_endif);


# This is the conditional stack, updated on if/else/endif, and
# used to build Condition objects.
our @cond_stack;

my %_am_macro_for_cond =
  (
   AMDEP => "one of the compiler tests\n"
   . "    AC_PROG_CC, AC_PROG_CXX, AC_PROG_OBJC, AC_PROG_OBJCXX,\n"
   . "    AM_PROG_AS, AM_PROG_GCJ, AM_PROG_UPC",
   am__fastdepCC => 'AC_PROG_CC',
   am__fastdepCCAS => 'AM_PROG_AS',
   am__fastdepCXX => 'AC_PROG_CXX',
   am__fastdepGCJ => 'AM_PROG_GCJ',
   am__fastdepOBJC => 'AC_PROG_OBJC',
   am__fastdepOBJCXX => 'AC_PROG_OBJCXX',
   am__fastdepUPC => 'AM_PROG_UPC'
  );


# $STRING
# _make_conditional_string ($NEGATE, $COND)
# ----------------------------------------
sub _make_conditional_string
{
  my ($negate, $cond) = @_;
  $cond = "${cond}_TRUE"
    unless $cond =~ /^TRUE|FALSE$/;
  $cond = Automake::Condition::conditional_negate ($cond)
    if $negate;
  return $cond;
}


# $COND
# cond_stack_if ($NEGATE, $COND, $WHERE)
# --------------------------------------
sub cond_stack_if
{
  my ($negate, $cond, $where) = @_;

  if (! $configure_cond{$cond} && $cond !~ /^TRUE|FALSE$/)
    {
      my $text = "$cond does not appear in AM_CONDITIONAL";
      my $scope = US_LOCAL;
      if (exists $_am_macro_for_cond{$cond})
	{
	  my $mac = $_am_macro_for_cond{$cond};
	  $text .= "\n  The usual way to define '$cond' is to add ";
	  $text .= ($mac =~ / /) ? $mac : "'$mac'";
	  $text .= "\n  to '$configure_ac' and run 'aclocal' and 'autoconf' again";
	  # These warnings appear in Automake files (depend2.am),
	  # so there is no need to display them more than once:
	  $scope = US_GLOBAL;
	}
      error $where, $text, uniq_scope => $scope;
    }

  push (@cond_stack, _make_conditional_string ($negate, $cond));

  return new Automake::Condition (@cond_stack);
}


# $COND
# cond_stack_else ($NEGATE, $COND, $WHERE)
# ----------------------------------------
sub cond_stack_else
{
  my ($negate, $cond, $where) = @_;

  if (! @cond_stack)
    {
      error $where, "else without if";
      return FALSE;
    }

  $cond_stack[$#cond_stack] =
    Automake::Condition::conditional_negate ($cond_stack[$#cond_stack]);

  # If $COND is given, check against it.
  if (defined $cond)
    {
      $cond = _make_conditional_string ($negate, $cond);

      error ($where, "else reminder ($negate$cond) incompatible with "
	     . "current conditional: $cond_stack[$#cond_stack]")
	if $cond_stack[$#cond_stack] ne $cond;
    }

  return new Automake::Condition (@cond_stack);
}


# $COND
# cond_stack_endif ($NEGATE, $COND, $WHERE)
# -----------------------------------------
sub cond_stack_endif
{
  my ($negate, $cond, $where) = @_;
  my $old_cond;

  if (! @cond_stack)
    {
      error $where, "endif without if";
      return TRUE;
    }

  # If $COND is given, check against it.
  if (defined $cond)
    {
      $cond = _make_conditional_string ($negate, $cond);

      error ($where, "endif reminder ($negate$cond) incompatible with "
	     . "current conditional: $cond_stack[$#cond_stack]")
	if $cond_stack[$#cond_stack] ne $cond;
    }

  pop @cond_stack;

  return new Automake::Condition (@cond_stack);
}

1;