-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy path__init__.py
More file actions
302 lines (246 loc) · 9.32 KB
/
Copy path__init__.py
File metadata and controls
302 lines (246 loc) · 9.32 KB
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
# Copyright 2016-2018 Dirk Thomas
# Licensed under the Apache License, Version 2.0
import os
import re
import shutil
import subprocess
import sys
from colcon_core.environment_variable import EnvironmentVariable
from colcon_core.subprocess import check_output
from pkg_resources import parse_version
"""Environment variable to override the CMake executable"""
CMAKE_COMMAND_ENVIRONMENT_VARIABLE = EnvironmentVariable(
'CMAKE_COMMAND', 'The full path to the CMake executable')
"""Environment variable to override the CTest executable"""
CTEST_COMMAND_ENVIRONMENT_VARIABLE = EnvironmentVariable(
'CTEST_COMMAND', 'The full path to the CTest executable')
def which_executable(environment_variable, executable_name):
"""
Determine the path of an executable.
An environment variable can be used to override the location instead of
relying on searching the PATH.
:param str environment_variable: The name of the environment variable
:param str executable_name: The name of the executable
:rtype: str
"""
value = os.getenv(environment_variable)
if value:
return value
return shutil.which(executable_name)
CMAKE_EXECUTABLE = which_executable(
CMAKE_COMMAND_ENVIRONMENT_VARIABLE.name, 'cmake')
CTEST_EXECUTABLE = which_executable(
CTEST_COMMAND_ENVIRONMENT_VARIABLE.name, 'ctest')
MSBUILD_EXECUTABLE = shutil.which('msbuild')
async def has_target(path, target):
"""
Check if the CMake generated build system has a specific target.
:param str path: The path of the directory contain the generated build
system
:param str target: The name of the target
:rtype: bool
"""
generator = get_generator(path)
if 'Unix Makefiles' in generator:
return target in await get_makefile_targets(path)
if 'Ninja' in generator:
return target in get_ninja_targets(path)
if 'Visual Studio' in generator:
assert target == 'install'
install_project_file = get_project_file(path, 'INSTALL')
return install_project_file is not None
assert False, \
"'has_target' not implemented for CMake generator '{generator}'" \
.format_map(locals())
async def get_makefile_targets(path):
"""
Get all targets from a `Makefile`.
:param str path: The path of the directory contain the Makefile
:returns: The target names
:rtype: list
"""
output = await check_output([
CMAKE_EXECUTABLE, '--build', path, '--target', 'help'], cwd=path)
lines = output.decode().splitlines()
prefix = '... '
return [line[len(prefix):] for line in lines if line.startswith(prefix)]
def get_ninja_targets(path):
"""
Get all targets from a `build.ninja` file.
:param str path: The path of the directory contain the Makefile
:returns: The target names
:rtype: list
"""
output = subprocess.check_output([
CMAKE_EXECUTABLE, '--build', path, '--target', 'help'], cwd=path)
lines = output.decode().splitlines()
suffix = ':'
return [
line.split(' ')[0][:-len(suffix)]
for line in lines
if len(line.split(' ')) == 2 and line.split(' ')[0].endswith(suffix)]
def get_buildfile(cmake_cache):
"""
Get the buildfile of the used CMake generator.
:param Path cmake_cache: The path of the directory contain the build system
:returns: The buildfile
:rtype: Path
"""
generator = get_variable_from_cmake_cache(
str(cmake_cache.parent), 'CMAKE_GENERATOR')
if generator == 'Ninja':
return cmake_cache.parent / 'build.ninja'
return cmake_cache.parent / 'Makefile'
def get_generator(path, cmake_args=None):
"""
Get CMake generator name.
Either the CMake generator is specified in the command line arguments or it
is being read from the `CMakeCache.txt` file.
:param str path: The path of the directory contain the CMake cache file
:param list cmake_args: The CMake command line arguments
:rtype: str
"""
# check for generator in the command line arguments first
generator = None
for i, cmake_arg in enumerate(cmake_args or []):
if cmake_arg == '-G' and i < len(cmake_args) - 1:
generator = cmake_args[i + 1]
if cmake_arg.startswith('-G') and len(cmake_arg) > 2:
generator = cmake_arg[2:]
if generator is None:
# get the generator from the CMake cache
generator = get_variable_from_cmake_cache(
path, 'CMAKE_GENERATOR')
return generator
def is_jobs_base_generator(path, cmake_args=None):
"""
Check if the used CMake generator supports jobs base arguments (-jN).
:param str path: The path of the directory contain the CMake cache file
:param list cmake_args: The CMake command line arguments
:rtype: bool
"""
known_jobs_base_multi_configuration_generators = (
'Ninja Multi-Config',
)
if not is_multi_configuration_generator(path, cmake_args):
# Historically we assume any non-multi-config generator is jobs based.
return True
generator = get_generator(path, cmake_args)
for multi in known_jobs_base_multi_configuration_generators:
if multi in generator:
return True
return False
def is_multi_configuration_generator(path, cmake_args=None):
"""
Check if the used CMake generator is a multi configuration generator.
:param str path: The path of the directory contain the CMake cache file
:param list cmake_args: The CMake command line arguments
:rtype: bool
"""
known_multi_configuration_generators = (
'Visual Studio',
'Xcode',
)
generator = get_generator(path, cmake_args)
for multi in known_multi_configuration_generators:
if multi in generator:
return True
return False
def get_variable_from_cmake_cache(path, var, *, default=None):
"""
Get a variable value from the CMake cache.
:param str path: The path of the directory contain the CMake cache file
:param str var: The name of the variable
:param default: The default value returned if the variable is not defined
in the cache
:rtype: str
"""
lines = _get_cmake_cache_lines(path)
if lines is None:
return default
line_prefix = '{var}:'.format_map(locals())
for line in lines:
if line.startswith(line_prefix):
try:
index = line.index('=')
except ValueError:
continue
return line[index + 1:]
return default
def _get_cmake_cache_lines(path):
cmake_cache = os.path.join(path, 'CMakeCache.txt')
if not os.path.exists(cmake_cache):
return None
with open(cmake_cache, 'r') as h:
content = h.read()
return content.splitlines()
def get_project_file(path, target):
"""
Get a Visual Studio project file for a specific target.
:param str path: The path of the directory project files
:param str target: The name of the target
:returns: The path of the project file if it exists, otherwise None
:rtype: str
"""
project_file = os.path.join(path, target + '.vcxproj')
if not os.path.isfile(project_file):
return None
return project_file
def get_visual_studio_version():
"""
Get the Visual Studio version.
:rtype: str
"""
return os.environ.get('VisualStudioVersion', None)
"""
Global variable for the cached CMake version number.
When valid, this will be a pkg_resources.extern.packaging.version.Version.
It may also be None when the CMake version could not be determined to avoid
trying to determine it again.
"""
_cached_cmake_version = False
def get_cmake_version():
"""
Get the CMake version.
The function caches the result on the first invocation and reuses that on
subsequent invocations.
:returns: The version as reported by `CMAKE_EXECUTABLE --version`, or None
when the version number could not be determined
:rtype pkg_resources.extern.packaging.version.Version
"""
global _cached_cmake_version
if _cached_cmake_version is False:
_cached_cmake_version = _parse_cmake_version()
return _cached_cmake_version
def _parse_cmake_version():
"""
Parse the CMake version printed by `CMAKE_EXECUTABLE --version`.
:returns: The version parsed by pkg_resources.parse_version, or None
:rtype pkg_resources.extern.packaging.version.Version
"""
try:
output = subprocess.check_output(
[CMAKE_EXECUTABLE, '--version'], stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
print('Failed to determine CMake version: ' + e.output.decode(),
file=sys.stderr)
else:
lines = output.decode().splitlines()
if lines:
# Parse just the version part of the string.
return _parse_cmake_version_string(lines[0])
return None
def _parse_cmake_version_string(version_string):
"""
Parse the given CMake version string.
Expects strings of the form 'cmake version 3.15.1'.
:param str version_string: The version string to parse.
:returns: The parsed version string or None on failure to parse.
:rtype pkg_resources.extern.packaging.version.Version
"""
# Extract just the version part of the string.
ver_re_str = r'^(?:.*\s)?(\d+\.\d+\.\d+).*'
ver_match = re.match(ver_re_str, version_string)
if ver_match:
return parse_version(ver_match.group(1))
return None