-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtoolchain.py
343 lines (293 loc) · 13.1 KB
/
toolchain.py
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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0.
import os
import re
from builder.core.data import COMPILERS
from builder.core.host import current_os, current_arch, normalize_target, normalize_arch
from builder.core import util
# helpful list of XCode clang output: https://gist.github.com/yamaya/2924292
def _compiler_version(cc):
if current_os() != 'windows':
result = util.run_command(cc, '--version', quiet=True)
lines = result.output.split('\n')
for text in lines:
# Apple clang
m = re.match(r'Apple (LLVM|clang) version (\d+)', text)
if m:
return 'appleclang', m.group(2)
# LLVM clang
m = re.match(r'.*(LLVM|clang) version (\d+)', text)
if m:
return 'clang', m.group(2)
# GCC 4.x
m = re.match(r'gcc .+ (4\.\d+)', text)
if m:
return 'gcc', m.group(1)
# GCC 5+
m = re.match(r'gcc .+ (\d+)\.', text)
if m:
return 'gcc', m.group(1)
return None, None
def _find_compiler_tool(name, versions):
# look for the default tool, and see if the version is in the search set
path = util.where(name, resolve_symlinks=False)
if path:
version = _compiler_version(path)[1]
if version in versions:
return path, version
for version in versions:
for pattern in ('{name}-{version}', '{name}-{version}.0'):
exe = pattern.format(name=name, version=version)
print("version: {}".format(exe))
path = util.where(exe, resolve_symlinks=False)
if path:
return path, version
return None, None
def _clang_versions(version=None):
versions = []
all_versions = [v for v in COMPILERS['clang']
['versions'].keys() if v != 'default']
if version:
if version in all_versions:
versions = [version]
if 'releases' in COMPILERS['clang']['versions'][str(version)]:
versions += COMPILERS['clang']['versions'][str(version)]['releases']
else:
versions += all_versions
versions.sort()
versions.reverse()
return versions
def _appleclang_versions():
versions = [v for v in COMPILERS['appleclang']
['versions'].keys() if v != 'default']
versions.sort()
versions.reverse()
return versions
def _gcc_versions():
versions = [v for v in COMPILERS['gcc']
['versions'].keys() if v != 'default']
versions.sort()
versions.reverse()
return versions
def _msvc_versions():
versions = [v for v in COMPILERS['msvc']
['versions'].keys() if v != 'default']
# sorted high to low by int value
versions.sort(key=lambda x: int(x), reverse=True)
return versions
def _is_cross_compile(target_os, target_arch):
# Mac compiling for anything that isn't iOS or itself
if current_os() == 'macos' and target_os in ["macos", "ios"]:
return False
# Windows is never a cross compile, just toolset swap
if current_os() == 'windows' and target_os == 'windows':
return False
if target_os != current_os() or normalize_arch(target_arch) != current_arch():
return True
return False
class Toolchain(object):
""" Represents a compiler toolchain """
def __init__(self, **kwargs):
if 'default' in kwargs or len(kwargs) == 0:
for slot in ('host', 'target', 'arch', 'compiler', 'compiler_version'):
setattr(self, slot, 'default')
if 'spec' in kwargs:
spec = kwargs['spec']
self.host = spec.host
self.compiler = spec.compiler
self.compiler_version = spec.compiler_version
self.target = spec.target
self.arch = normalize_arch(spec.arch)
# Pull out individual fields. Note this is not in an else to support overriding at construction time
for slot in ('host', 'target', 'arch', 'compiler', 'compiler_version'):
if slot in kwargs:
setattr(self, slot, kwargs[slot])
if self.target == 'default':
self.target = current_os()
if self.arch == 'default':
self.arch = current_arch()
# detect cross-compile
self.cross_compile = _is_cross_compile(self.target, self.arch)
self.platform = normalize_target(
'{}-{}'.format(self.target, self.arch))
self.shell_env = []
if self.cross_compile:
print('Setting compiler to gcc for cross compile')
self.compiler = 'gcc'
# it's really 4.9, but don't need a separate entry for that
self.compiler_version = '4.8'
else:
# resolve default compiler and/or version
if self.compiler == 'default':
c, v = Toolchain.default_compiler()
if c and v:
self.compiler, self.compiler_version = c, v
elif self.compiler_version == 'default':
self.compiler_version = _compiler_version(
self.compiler_path())[1]
if not self.compiler_version:
self.compiler_version = 'default'
self.name = '-'.join([self.host, self.compiler,
self.compiler_version, self.target, self.arch])
def compiler_path(self):
assert not self.cross_compile
if self.compiler == 'default':
return Toolchain.default_compiler()[0]
return Toolchain.find_compiler(self.compiler, self.compiler_version if self.compiler_version != 'default' else None)[0]
def cxx_compiler_path(self):
assert not self.cross_compile
compiler = self.compiler
if self.compiler == 'default':
compiler = Toolchain.default_compiler()[0]
if compiler == 'clang' or compiler == 'appleclang':
return Toolchain.find_compiler_tool(compiler, 'clang++', self.compiler_version)[0]
elif compiler == 'gcc':
return Toolchain.find_compiler_tool(compiler, 'g++', self.compiler_version)[0]
# msvc can compile with cl.exe regardless of language
return self.compiler_path()
def __str__(self):
return self.name
def __repr__(self):
return self.name
@staticmethod
def find_gcc_tool(name, version=None):
""" Finds gcc, gcc-ld, gcc-ranlib, etc at a specific version, or the latest one available """
versions = [version] if version else _gcc_versions()
return _find_compiler_tool(name, versions)
@staticmethod
def find_llvm_tool(name, version=None):
""" Finds clang, clang-tidy, lld, etc at a specific version, or the
latest one available """
versions = _clang_versions(version)
return _find_compiler_tool(name, versions)
@staticmethod
def find_apple_llvm_compiler(name, version=None):
""" Finds apple versions of clang compilers at a specific version, or the latest one available.
Note: apple does not ship tools like clang-tidy, so if installed
those will follow regular llvm versioning """
versions = [version] if version else _appleclang_versions()
return _find_compiler_tool(name, versions)
@staticmethod
def find_msvc(version=None):
""" Finds MSVC at a specific version, or the latest one available """
def _find_msvc(version, install_vswhere=True):
vswhere = util.where('vswhere')
# if that fails, install vswhere and try again
if not vswhere and install_vswhere:
result = util.run_command(
'choco', 'install', '--no-progress', 'vswhere')
if result.returncode == 0:
return _find_msvc(version, False)
return None, None
# A Visual Studio installation might have toolsets available for compiling
# earlier versions. vswhere doesn't know about these toolsets, so
# we'll just assume that any installation >= version can do the job.
#
# vswhere's -version flag expects a range, and if you just pass
# a single int you'll get told about anything >= version.
# Perfect, exactly what we want.
result = util.run_command('vswhere', '-legacy', '-version', version,
'-property', 'installationPath', '-sort', quiet=True)
installations = result.output.splitlines()
if installations:
return installations[0], version
return None, None
versions = [version] if version else _msvc_versions()
for version in versions:
path, version = _find_msvc(version)
if path:
return path, version
return None, None
@staticmethod
def find_compiler(compiler, version=None):
""" Returns path, found_version for the requested compiler if it is installed """
if compiler == 'clang':
if current_os() == "macos":
return Toolchain.find_apple_llvm_compiler(compiler, version)
else:
return Toolchain.find_llvm_tool(compiler, version)
elif compiler == 'appleclang':
return Toolchain.find_apple_llvm_compiler('clang', version)
elif compiler == 'gcc':
return Toolchain.find_gcc_tool(compiler, version)
elif compiler == 'msvc':
return Toolchain.find_msvc(version)
return None, None
@staticmethod
def find_compiler_tool(compiler, tool, version=None):
""" Returns path, found_version for the requested tool if it is installed """
if compiler == 'clang':
return Toolchain.find_llvm_tool(tool, version)
elif compiler == 'appleclang':
return Toolchain.find_apple_llvm_compiler(tool, version)
elif compiler == 'gcc':
return Toolchain.find_gcc_tool(tool, version)
return None, None
@staticmethod
def all_compilers():
""" Returns a list of tuples of all available (compiler, version) """
compilers = []
for version in _gcc_versions():
path, _version = Toolchain.find_gcc_tool('gcc', version)
if path:
compilers.append(('gcc', version))
is_mac = current_os() == "macos"
versions = _appleclang_versions() if is_mac else _clang_versions()
for version in versions:
path, _version = Toolchain.find_llvm_tool('clang', version)
if path:
compilers.append(('appleclang' if is_mac else 'clang', version))
if current_os() == 'windows':
for version in _msvc_versions():
path, _version = Toolchain.find_msvc(version)
if path:
compilers.append(('msvc', version))
return compilers
_default_compiler = None
_default_version = None
@staticmethod
def default_compiler(target=None, arch=None):
""" Finds the system default compiler and returns (compiler, version) """
if Toolchain._default_compiler and Toolchain._default_version:
return Toolchain._default_compiler, Toolchain._default_version
if target and arch and _is_cross_compile(target, arch):
return 'gcc', '4.8'
def _find_compiler():
compiler = None
version = None
platform = current_os()
if platform == 'windows':
compiler = 'msvc'
version = Toolchain.find_msvc()[1]
else:
# resolve CC and /usr/bin/cc
for env_cc in (util.where(os.environ.get('CC', None)), util.where('cc')):
if env_cc:
cc, ccver = _compiler_version(env_cc)
if cc and ccver:
return cc, ccver
# Try to find clang or gcc
clang_path, clang_version = Toolchain.find_llvm_tool('clang')
print(clang_path)
gcc_path, gcc_version = Toolchain.find_gcc_tool('gcc')
if clang_path:
compiler = 'clang'
version = clang_version
elif gcc_path:
compiler = 'gcc'
version = gcc_version
else:
print(
'Neither GCC or Clang could be found on this system, perhaps not installed yet?')
if not compiler or not version:
print('WARNING: Default compiler could not be found')
print('Default Compiler: {} {}'.format(compiler, version))
return compiler, version
Toolchain._default_compiler, Toolchain._default_version = _find_compiler()
return Toolchain._default_compiler, Toolchain._default_version
@staticmethod
def is_compiler_installed(compiler, version):
""" Returns True if the specified compiler is already installed, False otherwise """
compiler_path, found_version = Toolchain.find_compiler(
compiler, version)
return compiler_path != None