summaryrefslogtreecommitdiffstats
path: root/SCons/Tool/MSCommon/MSVC/Policy.py
blob: 95643b25e9cf5b8209194db87ef572171d6d8010 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# MIT License
#
# Copyright The SCons Foundation
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

"""
Microsoft Visual C/C++ policy handlers.

Notes:
    * The default msvc not found policy is that a warning is issued. This can be
      changed globally via the function set_msvc_notfound_policy and/or through
      the environment via the MSVC_NOTFOUND_POLICY construction variable.
    * The default msvc script error policy is to suppress all msvc batch file
      error messages. This can be changed globally via the function
      set_msvc_scripterror_policy and/or through the environment via the
      MSVC_SCRIPTERROR_POLICY construction variable.
"""

from collections import (
    namedtuple,
)

import SCons.Warnings

from ..common import (
    debug,
)

from .Exceptions import (
    MSVCVersionNotFound,
    MSVCScriptExecutionError,
)

from .Warnings import (
    MSVCScriptExecutionWarning,
)

from . import Dispatcher
Dispatcher.register_modulename(__name__)


# MSVC_NOTFOUND_POLICY definition:
#     error:   raise exception
#     warning: issue warning and continue
#     ignore:  continue

MSVC_NOTFOUND_POLICY_DEFINITION = namedtuple('MSVCNotFoundPolicyDefinition', [
    'value',
    'symbol',
])

MSVC_NOTFOUND_DEFINITION_LIST = []

MSVC_NOTFOUND_POLICY_INTERNAL = {}
MSVC_NOTFOUND_POLICY_EXTERNAL = {}

for policy_value, policy_symbol_list in [
    (True,  ['Error',   'Exception']),
    (False, ['Warning', 'Warn']),
    (None,  ['Ignore',  'Suppress']),
]:

    policy_symbol = policy_symbol_list[0].lower()
    policy_def = MSVC_NOTFOUND_POLICY_DEFINITION(policy_value, policy_symbol)

    MSVC_NOTFOUND_DEFINITION_LIST.append(policy_def)

    MSVC_NOTFOUND_POLICY_INTERNAL[policy_symbol] = policy_def

    for policy_symbol in policy_symbol_list:
        MSVC_NOTFOUND_POLICY_EXTERNAL[policy_symbol.lower()] = policy_def
        MSVC_NOTFOUND_POLICY_EXTERNAL[policy_symbol] = policy_def
        MSVC_NOTFOUND_POLICY_EXTERNAL[policy_symbol.upper()] = policy_def

# default definition
_MSVC_NOTFOUND_POLICY_DEF = MSVC_NOTFOUND_POLICY_INTERNAL['warning']


# MSVC_SCRIPTERROR_POLICY definition:
#     error:   raise exception
#     warning: issue warning and continue
#     ignore:  continue

MSVC_SCRIPTERROR_POLICY_DEFINITION = namedtuple('MSVCBatchErrorPolicyDefinition', [
    'value',
    'symbol',
])

MSVC_SCRIPTERROR_DEFINITION_LIST = []

MSVC_SCRIPTERROR_POLICY_INTERNAL = {}
MSVC_SCRIPTERROR_POLICY_EXTERNAL = {}

for policy_value, policy_symbol_list in [
    (True,  ['Error',   'Exception']),
    (False, ['Warning', 'Warn']),
    (None,  ['Ignore',  'Suppress']),
]:

    policy_symbol = policy_symbol_list[0].lower()
    policy_def = MSVC_SCRIPTERROR_POLICY_DEFINITION(policy_value, policy_symbol)

    MSVC_SCRIPTERROR_DEFINITION_LIST.append(policy_def)

    MSVC_SCRIPTERROR_POLICY_INTERNAL[policy_symbol] = policy_def

    for policy_symbol in policy_symbol_list:
        MSVC_SCRIPTERROR_POLICY_EXTERNAL[policy_symbol.lower()] = policy_def
        MSVC_SCRIPTERROR_POLICY_EXTERNAL[policy_symbol] = policy_def
        MSVC_SCRIPTERROR_POLICY_EXTERNAL[policy_symbol.upper()] = policy_def

# default definition
_MSVC_SCRIPTERROR_POLICY_DEF = MSVC_SCRIPTERROR_POLICY_INTERNAL['ignore']


def _msvc_notfound_policy_lookup(symbol):

    try:
        notfound_policy_def = MSVC_NOTFOUND_POLICY_EXTERNAL[symbol]
    except KeyError:
        err_msg = "Value specified for MSVC_NOTFOUND_POLICY is not supported: {}.\n" \
                  "  Valid values are: {}".format(
                     repr(symbol),
                     ', '.join([repr(s) for s in MSVC_NOTFOUND_POLICY_EXTERNAL.keys()])
                  )
        raise ValueError(err_msg)

    return notfound_policy_def

def set_msvc_notfound_policy(MSVC_NOTFOUND_POLICY=None):
    """ Set the default policy when MSVC is not found.

    Args:
        MSVC_NOTFOUND_POLICY:
           string representing the policy behavior
           when MSVC is not found or None

    Returns:
        The previous policy is returned when the MSVC_NOTFOUND_POLICY argument
        is not None. The active policy is returned when the MSVC_NOTFOUND_POLICY
        argument is None.

    """
    global _MSVC_NOTFOUND_POLICY_DEF

    prev_policy = _MSVC_NOTFOUND_POLICY_DEF.symbol

    policy = MSVC_NOTFOUND_POLICY
    if policy is not None:
        _MSVC_NOTFOUND_POLICY_DEF = _msvc_notfound_policy_lookup(policy)

    debug(
        'prev_policy=%s, set_policy=%s, policy.symbol=%s, policy.value=%s',
        repr(prev_policy), repr(policy),
        repr(_MSVC_NOTFOUND_POLICY_DEF.symbol), repr(_MSVC_NOTFOUND_POLICY_DEF.value)
    )

    return prev_policy

def get_msvc_notfound_policy():
    """Return the active policy when MSVC is not found."""
    debug(
        'policy.symbol=%s, policy.value=%s',
        repr(_MSVC_NOTFOUND_POLICY_DEF.symbol), repr(_MSVC_NOTFOUND_POLICY_DEF.value)
    )
    return _MSVC_NOTFOUND_POLICY_DEF.symbol

def msvc_notfound_handler(env, msg):

    if env and 'MSVC_NOTFOUND_POLICY' in env:
        # environment setting
        notfound_policy_src = 'environment'
        policy = env['MSVC_NOTFOUND_POLICY']
        if policy is not None:
            # user policy request
            notfound_policy_def = _msvc_notfound_policy_lookup(policy)
        else:
            # active global setting
            notfound_policy_def = _MSVC_NOTFOUND_POLICY_DEF
    else:
        # active global setting
        notfound_policy_src = 'default'
        policy = None
        notfound_policy_def = _MSVC_NOTFOUND_POLICY_DEF

    debug(
        'source=%s, set_policy=%s, policy.symbol=%s, policy.value=%s',
        notfound_policy_src, repr(policy), repr(notfound_policy_def.symbol), repr(notfound_policy_def.value)
    )

    if notfound_policy_def.value is None:
        # ignore
        pass
    elif notfound_policy_def.value:
        raise MSVCVersionNotFound(msg)
    else:
        SCons.Warnings.warn(SCons.Warnings.VisualCMissingWarning, msg)


def _msvc_scripterror_policy_lookup(symbol):

    try:
        scripterror_policy_def = MSVC_SCRIPTERROR_POLICY_EXTERNAL[symbol]
    except KeyError:
        err_msg = "Value specified for MSVC_SCRIPTERROR_POLICY is not supported: {}.\n" \
                  "  Valid values are: {}".format(
                     repr(symbol),
                     ', '.join([repr(s) for s in MSVC_SCRIPTERROR_POLICY_EXTERNAL.keys()])
                  )
        raise ValueError(err_msg)

    return scripterror_policy_def

def set_msvc_scripterror_policy(MSVC_SCRIPTERROR_POLICY=None):
    """ Set the default policy when msvc batch file execution errors are detected.

    Args:
        MSVC_SCRIPTERROR_POLICY:
           string representing the policy behavior
           when msvc batch file execution errors are detected or None

    Returns:
        The previous policy is returned when the MSVC_SCRIPTERROR_POLICY argument
        is not None. The active policy is returned when the MSVC_SCRIPTERROR_POLICY
        argument is None.

    """
    global _MSVC_SCRIPTERROR_POLICY_DEF

    prev_policy = _MSVC_SCRIPTERROR_POLICY_DEF.symbol

    policy = MSVC_SCRIPTERROR_POLICY
    if policy is not None:
        _MSVC_SCRIPTERROR_POLICY_DEF = _msvc_scripterror_policy_lookup(policy)

    debug(
        'prev_policy=%s, set_policy=%s, policy.symbol=%s, policy.value=%s',
        repr(prev_policy), repr(policy),
        repr(_MSVC_SCRIPTERROR_POLICY_DEF.symbol), repr(_MSVC_SCRIPTERROR_POLICY_DEF.value)
    )

    return prev_policy

def get_msvc_scripterror_policy():
    """Return the active policy when msvc batch file execution errors are detected."""
    debug(
        'policy.symbol=%s, policy.value=%s',
        repr(_MSVC_SCRIPTERROR_POLICY_DEF.symbol), repr(_MSVC_SCRIPTERROR_POLICY_DEF.value)
    )
    return _MSVC_SCRIPTERROR_POLICY_DEF.symbol

def msvc_scripterror_handler(env, msg):

    if env and 'MSVC_SCRIPTERROR_POLICY' in env:
        # environment setting
        scripterror_policy_src = 'environment'
        policy = env['MSVC_SCRIPTERROR_POLICY']
        if policy is not None:
            # user policy request
            scripterror_policy_def = _msvc_scripterror_policy_lookup(policy)
        else:
            # active global setting
            scripterror_policy_def = _MSVC_SCRIPTERROR_POLICY_DEF
    else:
        # active global setting
        scripterror_policy_src = 'default'
        policy = None
        scripterror_policy_def = _MSVC_SCRIPTERROR_POLICY_DEF

    debug(
        'source=%s, set_policy=%s, policy.symbol=%s, policy.value=%s',
        scripterror_policy_src, repr(policy), repr(scripterror_policy_def.symbol), repr(scripterror_policy_def.value)
    )

    if scripterror_policy_def.value is None:
        # ignore
        pass
    elif scripterror_policy_def.value:
        raise MSVCScriptExecutionError(msg)
    else:
        SCons.Warnings.warn(MSVCScriptExecutionWarning, msg)