summaryrefslogtreecommitdiff
path: root/SCons/Tool/MSCommon/MSVC/Util.py
blob: 4b487da0e094c8930a23516a3a7a60add4af568c (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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# 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.

"""
Helper functions for Microsoft Visual C/C++.
"""

import os
import re

from collections import (
    namedtuple,
)

from . import Config

# path utilities

def listdir_dirs(p):
    """
    Return a list of tuples for each subdirectory of the given directory path.
    Each tuple is comprised of the subdirectory name and the qualified subdirectory path.

    Args:
        p: str
            directory path

    Returns:
        list[tuple[str,str]]: a list of tuples

    """
    dirs = []
    if p and os.path.exists(p) and os.path.isdir(p):
        for dir_name in os.listdir(p):
            dir_path = os.path.join(p, dir_name)
            if os.path.isdir(dir_path):
                dirs.append((dir_name, dir_path))
    return dirs

def process_path(p):
    """
    Normalize a system path

    Args:
        p: str
            system path

    Returns:
        str: normalized system path

    """
    if p:
        p = os.path.normpath(p)
        p = os.path.realpath(p)
        p = os.path.normcase(p)
    return p

# msvc version and msvc toolset version regexes

re_version_prefix = re.compile('^(?P<version>[0-9]+(?:[.][0-9]+)*)(?![.]).*$')

re_msvc_version_prefix = re.compile(r'^(?P<version>[1-9][0-9]?[.][0-9]).*$')

re_msvc_version = re.compile(r'^(?P<msvc_version>[1-9][0-9]?[.][0-9])(?P<suffix>[A-Z]+)*$', re.IGNORECASE)

re_extended_version = re.compile(r'''^
    (?P<version>(?:
        ([1-9][0-9]?[.][0-9]{1,2})|                     # XX.Y       - XX.YY
        ([1-9][0-9][.][0-9]{2}[.][0-9]{1,5})|           # XX.YY.Z    - XX.YY.ZZZZZ
        ([1-9][0-9][.][0-9]{2}[.][0-9]{2}[.][0-9]{1,2}) # XX.YY.AA.B - XX.YY.AA.BB
    ))
    (?P<suffix>[A-Z]+)*
$''', re.IGNORECASE | re.VERBOSE)

re_toolset_full = re.compile(r'''^(?:
    (?:[1-9][0-9][.][0-9]{1,2})|           # XX.Y    - XX.YY
    (?:[1-9][0-9][.][0-9]{2}[.][0-9]{1,5}) # XX.YY.Z - XX.YY.ZZZZZ
)$''', re.VERBOSE)

re_toolset_140 = re.compile(r'''^(?:
    (?:14[.]0{1,2})|       # 14.0    - 14.00
    (?:14[.]0{2}[.]0{1,5}) # 14.00.0 - 14.00.00000
)$''', re.VERBOSE)

re_toolset_sxs = re.compile(
    r'^[1-9][0-9][.][0-9]{2}[.][0-9]{2}[.][0-9]{1,2}$' # MM.mm.VV.vv format
)

# msvc sdk version regexes

re_msvc_sdk_version = re.compile(r'''^
    (?P<version>(?:
        ([1-9][0-9]?[.][0-9])|                           # XX.Y
        ([1-9][0-9][.][0-9]{1}[.][0-9]{5}[.][0-9]{1,2})  # XX.Y.ZZZZZ.A - XX.Y.ZZZZZ.AA
    ))
$''', re.IGNORECASE | re.VERBOSE)

# version prefix utilities

def get_version_prefix(version):
    """
    Get the version number prefix from a string.

    Args:
        version: str
            version specification

    Returns:
        str: the version number prefix

    """
    rval = ''
    if version:
        m = re_version_prefix.match(version)
        if m:
            rval = m.group('version')
    return rval

def get_msvc_version_prefix(version):
    """
    Get the msvc version number prefix from a string.

    Args:
        version: str
            version specification

    Returns:
        str: the msvc version number prefix

    """
    rval = ''
    if version:
        m = re_msvc_version_prefix.match(version)
        if m:
            rval = m.group('version')
    return rval

# toolset version query utilities

def is_toolset_full(toolset_version):
    rval = False
    if toolset_version:
        if re_toolset_full.match(toolset_version):
            rval = True
    return rval

def is_toolset_140(toolset_version):
    rval = False
    if toolset_version:
        if re_toolset_140.match(toolset_version):
            rval = True
    return rval

def is_toolset_sxs(toolset_version):
    rval = False
    if toolset_version:
        if re_toolset_sxs.match(toolset_version):
            rval = True
    return rval

# msvc version and msvc toolset version decomposition utilties

_MSVC_VERSION_COMPONENTS_DEFINITION = namedtuple('MSVCVersionComponentsDefinition', [
    'msvc_version', # msvc version (e.g., '14.1Exp')
    'msvc_verstr',  # msvc version numeric string (e.g., '14.1')
    'msvc_suffix',  # msvc version component type (e.g., 'Exp')
    'msvc_vernum',  # msvc version floating point number (e.g, 14.1)
    'msvc_major',   # msvc major version integer number (e.g., 14)
    'msvc_minor',   # msvc minor version integer number (e.g., 1)
    'msvc_comps',   # msvc version components tuple (e.g., ('14', '1'))
])

def msvc_version_components(vcver):
    """
    Decompose an msvc version into components.

    Tuple fields:
        msvc_version: msvc version (e.g., '14.1Exp')
        msvc_verstr:  msvc version numeric string (e.g., '14.1')
        msvc_suffix:  msvc version component type (e.g., 'Exp')
        msvc_vernum:  msvc version floating point number (e.g., 14.1)
        msvc_major:   msvc major version integer number (e.g., 14)
        msvc_minor:   msvc minor version integer number (e.g., 1)
        msvc_comps:   msvc version components tuple (e.g., ('14', '1'))

    Args:
        vcver: str
            msvc version specification

    Returns:
        None or MSVCVersionComponents namedtuple:
    """

    if not vcver:
        return None

    m = re_msvc_version.match(vcver)
    if not m:
        return None

    vs_def = Config.MSVC_VERSION_SUFFIX.get(vcver)
    if not vs_def:
        return None

    msvc_version = vcver
    msvc_verstr = m.group('msvc_version')
    msvc_suffix = m.group('suffix') if m.group('suffix') else ''
    msvc_vernum = float(msvc_verstr)

    msvc_comps = tuple(msvc_verstr.split('.'))
    msvc_major, msvc_minor = [int(x) for x in msvc_comps]

    msvc_version_components_def = _MSVC_VERSION_COMPONENTS_DEFINITION(
        msvc_version = msvc_version,
        msvc_verstr = msvc_verstr,
        msvc_suffix = msvc_suffix,
        msvc_vernum = msvc_vernum,
        msvc_major = msvc_major,
        msvc_minor = msvc_minor,
        msvc_comps = msvc_comps,
    )

    return msvc_version_components_def

_MSVC_EXTENDED_VERSION_COMPONENTS_DEFINITION = namedtuple('MSVCExtendedVersionComponentsDefinition', [
    'msvc_version', # msvc version (e.g., '14.1Exp')
    'msvc_verstr',  # msvc version numeric string (e.g., '14.1')
    'msvc_suffix',  # msvc version component type (e.g., 'Exp')
    'msvc_vernum',  # msvc version floating point number (e.g, 14.1)
    'msvc_major',   # msvc major version integer number (e.g., 14)
    'msvc_minor',   # msvc minor version integer number (e.g., 1)
    'msvc_comps',   # msvc version components tuple (e.g., ('14', '1'))
    'msvc_toolset_version',  # msvc toolset version
    'msvc_toolset_comps',    # msvc toolset version components
    'version',               # msvc version or msvc toolset version
])

def msvc_extended_version_components(version):
    """
    Decompose an msvc version or msvc toolset version into components.

    Args:
        version: str
            version specification

    Returns:
        None or MSVCExtendedVersionComponents namedtuple:
    """

    if not version:
        return None

    m = re_extended_version.match(version)
    if not m:
        return None

    msvc_toolset_version = m.group('version')
    msvc_toolset_comps = tuple(msvc_toolset_version.split('.'))

    msvc_verstr = get_msvc_version_prefix(msvc_toolset_version)
    if not msvc_verstr:
        return None

    msvc_suffix = m.group('suffix') if m.group('suffix') else ''
    msvc_version = msvc_verstr + msvc_suffix

    vs_def = Config.MSVC_VERSION_SUFFIX.get(msvc_version)
    if not vs_def:
        return None

    msvc_vernum = float(msvc_verstr)

    msvc_comps = tuple(msvc_verstr.split('.'))
    msvc_major, msvc_minor = [int(x) for x in msvc_comps]

    msvc_extended_version_components_def = _MSVC_EXTENDED_VERSION_COMPONENTS_DEFINITION(
        msvc_version = msvc_version,
        msvc_verstr = msvc_verstr,
        msvc_suffix = msvc_suffix,
        msvc_vernum = msvc_vernum,
        msvc_major = msvc_major,
        msvc_minor = msvc_minor,
        msvc_comps = msvc_comps,
        msvc_toolset_version = msvc_toolset_version,
        msvc_toolset_comps = msvc_toolset_comps,
        version = version,
    )

    return msvc_extended_version_components_def

# msvc sdk version decomposition utilties

_MSVC_SDK_VERSION_COMPONENTS_DEFINITION = namedtuple('MSVCSDKVersionComponentsDefinition', [
    'sdk_version', # sdk version (e.g., '10.0.20348.0')
    'sdk_verstr',  # sdk version numeric string (e.g., '10.0')
    'sdk_vernum',  # sdk version floating point number (e.g, 10.0)
    'sdk_major',   # sdk major version integer number (e.g., 10)
    'sdk_minor',   # sdk minor version integer number (e.g., 0)
    'sdk_comps',   # sdk version components tuple (e.g., ('10', '0', '20348', '0'))
])

def msvc_sdk_version_components(version):
    """
    Decompose an msvc sdk version into components.

    Tuple fields:
        sdk_version: sdk version (e.g., '10.0.20348.0')
        sdk_verstr:  sdk version numeric string (e.g., '10.0')
        sdk_vernum:  sdk version floating point number (e.g., 10.0)
        sdk_major:   sdk major version integer number (e.g., 10)
        sdk_minor:   sdk minor version integer number (e.g., 0)
        sdk_comps:   sdk version components tuple (e.g., ('10', '0', '20348', '0'))

    Args:
        version: str
            sdk version specification

    Returns:
        None or MSVCSDKVersionComponents namedtuple:
    """

    if not version:
        return None

    m = re_msvc_sdk_version.match(version)
    if not m:
        return None

    sdk_version = version
    sdk_comps = tuple(sdk_version.split('.'))
    sdk_verstr = '.'.join(sdk_comps[:2])
    sdk_vernum = float(sdk_verstr)

    sdk_major, sdk_minor = [int(x) for x in sdk_comps[:2]]

    msvc_sdk_version_components_def = _MSVC_SDK_VERSION_COMPONENTS_DEFINITION(
        sdk_version = sdk_version,
        sdk_verstr = sdk_verstr,
        sdk_vernum = sdk_vernum,
        sdk_major = sdk_major,
        sdk_minor = sdk_minor,
        sdk_comps = sdk_comps,
    )

    return msvc_sdk_version_components_def