forked from scikit-build/scikit-build
/
cmaker.py
508 lines (414 loc) · 18.4 KB
/
cmaker.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
"""
This module provides an interface for invoking CMake executable.
"""
import argparse
import distutils.sysconfig as du_sysconfig
import glob
import io
import itertools
import os
import os.path
import platform
import re
import subprocess
import shlex
import sys
import sysconfig
from .constants import (CMAKE_BUILD_DIR,
CMAKE_DEFAULT_EXECUTABLE,
CMAKE_INSTALL_DIR,
SETUPTOOLS_INSTALL_DIR)
from .platform_specifics import get_platform
from .exceptions import SKBuildError
RE_FILE_INSTALL = re.compile(
r"""[ \t]*file\(INSTALL DESTINATION "([^"]+)".*"([^"]+)"\).*""")
def pop_arg(arg, args, default=None):
"""Pops an argument ``arg`` from an argument list ``args`` and returns the
new list and the value of the argument if present and a default otherwise.
"""
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument(arg)
namespace, args = parser.parse_known_args(args)
namespace = tuple(vars(namespace).items())
if namespace and namespace[0][1] is not None:
val = namespace[0][1]
else:
val = default
return args, val
def _remove_cwd_prefix(path):
cwd = os.getcwd()
result = path.replace("/", os.sep)
if result.startswith(cwd):
result = os.path.relpath(result, cwd)
if platform.system() == "Windows":
result = result.replace("\\\\", os.sep)
result = result.replace("\n", "")
return result
def has_cmake_cache_arg(cmake_args, arg_name, arg_value=None):
"""Return True if ``-D<arg_name>:TYPE=<arg_value>`` is found
in ``cmake_args``. If ``arg_value`` is None, return True only if
``-D<arg_name>:`` is found in the list."""
for arg in reverse(cmake_args):
if arg.startswith("-D%s:" % arg_name):
if arg_value is None:
return True
elif "=" in arg:
return arg.split("=")[1] == arg_value
return False
def get_cmake_version(cmake_executable=CMAKE_DEFAULT_EXECUTABLE):
"""Runs CMake and extracts associated version information.
Raises :class:`skbuild.exceptions.SKBuildError` if it failed to execute CMake.
"""
try:
version_string = subprocess.check_output([cmake_executable, '--version'])
except (OSError, subprocess.CalledProcessError):
raise SKBuildError(
"Problem with the CMake installation, aborting build. CMake executable is %s" % cmake_executable)
if sys.version_info > (3, 0):
version_string = version_string.decode()
return version_string.splitlines()[0].split(' ')[-1]
class CMaker(object):
"""Interface to CMake executable."""
def __init__(self, cmake_executable=CMAKE_DEFAULT_EXECUTABLE):
self.cmake_executable = cmake_executable
self.cmake_version = get_cmake_version(self.cmake_executable)
self.platform = get_platform()
def get_cached_generator_name(self):
"""Reads and returns the cached generator from the :const:`skbuild.constants.CMAKE_BUILD_DIR`:.
Returns None if not found.
"""
try:
cmake_generator = 'CMAKE_GENERATOR:INTERNAL='
with open(os.path.join(CMAKE_BUILD_DIR, 'CMakeCache.txt')) as fp:
for line in fp:
if line.startswith(cmake_generator):
return line[len(cmake_generator):].strip()
except (OSError, IOError):
pass
return None
def get_cached_generator_env(self):
"""If any, return a mapping of environment associated with the cached generator.
"""
generator_name = self.get_cached_generator_name()
if generator_name is not None:
return self.platform.get_generator(generator_name).env
return None
def configure(self, clargs=(), generator_name=None,
cmake_source_dir='.', cmake_install_dir='',
languages=('C', 'CXX'), cleanup=True):
"""Calls cmake to generate the Makefile/VS Solution/XCode project.
clargs: tuple
List of command line arguments to pass to cmake executable.
generator_name: string
The string representing the CMake generator to use.
If None, uses defaults for your platform.
cmake_source_dir: string
Path to source tree containing a ``CMakeLists.txt``
cmake_install_dir: string
Relative directory to append
to :const:`skbuild.constants.CMAKE_INSTALL_DIR`.
languages: tuple
List of languages required to configure the project and expected to
be supported by the compiler. The language identifier that can be specified
in the list corresponds to the one recognized by CMake.
cleanup: bool
If True, cleans up temporary folder used to test
generators. Set to False for debugging to see CMake's
output files.
Return a mapping of the environment associated with the
selected :class:`skbuild.platform_specifics.abstract.CMakeGenerator`.
Mapping of the environment can also be later retrieved using :meth:`.get_cached_generator_env`.
"""
# if no provided default generator_name, check environment
if generator_name is None:
generator_name = os.environ.get("CMAKE_GENERATOR")
# if generator_name is provided on command line, use it
clargs, cli_generator_name = pop_arg('-G', clargs)
if cli_generator_name is not None:
generator_name = cli_generator_name
# use the generator returned from the platform, with the current
# generator_name as a suggestion
generator = self.platform.get_best_generator(
generator_name, cmake_executable=self.cmake_executable, cmake_args=clargs,
languages=languages, cleanup=cleanup)
if not os.path.exists(CMAKE_BUILD_DIR):
os.makedirs(CMAKE_BUILD_DIR)
if not os.path.exists(CMAKE_INSTALL_DIR):
os.makedirs(CMAKE_INSTALL_DIR)
if not os.path.exists(SETUPTOOLS_INSTALL_DIR):
os.makedirs(SETUPTOOLS_INSTALL_DIR)
python_version = CMaker.get_python_version()
python_include_dir = CMaker.get_python_include_dir(python_version)
python_library = CMaker.get_python_library(python_version)
cmake_source_dir = os.path.abspath(cmake_source_dir)
cmd = [
self.cmake_executable, cmake_source_dir, '-G', generator.name,
("-DCMAKE_INSTALL_PREFIX:PATH=" +
os.path.abspath(
os.path.join(CMAKE_INSTALL_DIR, cmake_install_dir))),
("-DPYTHON_EXECUTABLE:FILEPATH=" +
sys.executable),
("-DPYTHON_VERSION_STRING:STRING=" +
sys.version.split(' ')[0]),
("-DPYTHON_INCLUDE_DIR:PATH=" +
python_include_dir),
("-DPYTHON_LIBRARY:FILEPATH=" +
python_library),
("-DSKBUILD:BOOL=" +
"TRUE"),
("-DCMAKE_MODULE_PATH:PATH=" +
os.path.join(os.path.dirname(__file__), "resources", "cmake"))
]
cmd.extend(clargs)
cmd.extend(
filter(bool,
shlex.split(os.environ.get("SKBUILD_CONFIGURE_OPTIONS", "")))
)
# changes dir to cmake_build and calls cmake's configure step
# to generate makefile
rtn = subprocess.call(cmd, cwd=CMAKE_BUILD_DIR, env=generator.env)
if rtn != 0:
raise SKBuildError(
"An error occurred while configuring with CMake.\n"
" Command:\n"
" {}\n"
" Source directory:\n"
" {}\n"
" Working directory:\n"
" {}\n"
"Please see CMake's output for more information.".format(
self._formatArgsForDisplay(cmd),
os.path.abspath(cmake_source_dir),
os.path.abspath(CMAKE_BUILD_DIR)))
CMaker.check_for_bad_installs()
return generator.env
@staticmethod
def get_python_version():
"""Get version associated with the current python interpreter."""
python_version = sysconfig.get_config_var('VERSION')
if not python_version:
python_version = sysconfig.get_config_var('py_version_short')
if not python_version:
python_version = ".".join(map(str, sys.version_info[:2]))
return python_version
# NOTE(opadron): The try-excepts raise the cyclomatic complexity, but we
# need them for this function.
@staticmethod # noqa: C901
def get_python_include_dir(python_version):
"""Get include directory associated with the current python
interpreter."""
# determine python include dir
python_include_dir = sysconfig.get_config_var('INCLUDEPY')
# if Python.h not found (or python_include_dir is None), try to find a
# suitable include dir
found_python_h = (
python_include_dir is not None or
os.path.exists(os.path.join(python_include_dir, 'Python.h'))
)
if not found_python_h:
# NOTE(opadron): these possible prefixes must be guarded against
# AttributeErrors and KeyErrors because they each can throw on
# different platforms or even different builds on the same platform.
include_py = sysconfig.get_config_var('INCLUDEPY')
include_dir = sysconfig.get_config_var('INCLUDEDIR')
include = None
plat_include = None
python_inc = None
python_inc2 = None
try:
include = sysconfig.get_path('include')
except (AttributeError, KeyError):
pass
try:
plat_include = sysconfig.get_path('platinclude')
except (AttributeError, KeyError):
pass
try:
python_inc = sysconfig.get_python_inc()
except AttributeError:
pass
if include_py is not None:
include_py = os.path.dirname(include_py)
if include is not None:
include = os.path.dirname(include)
if plat_include is not None:
plat_include = os.path.dirname(plat_include)
if python_inc is not None:
python_inc2 = os.path.join(
python_inc, ".".join(map(str, sys.version_info[:2])))
candidate_prefixes = list(filter(bool, (
include_py,
include_dir,
include,
plat_include,
python_inc,
python_inc2,
)))
candidate_versions = (python_version,)
if python_version:
candidate_versions += ('',)
candidates = (
os.path.join(prefix, ''.join(('python', ver)))
for (prefix, ver) in itertools.product(
candidate_prefixes,
candidate_versions
)
)
for candidate in candidates:
if os.path.exists(os.path.join(candidate, 'Python.h')):
# we found an include directory
python_include_dir = candidate
break
# TODO(opadron): what happens if we don't find an include directory?
# Throw SKBuildError?
return python_include_dir
@staticmethod
def get_python_library(python_version):
"""Get path to the python library associated with the current python
interpreter."""
# determine direct path to libpython
python_library = sysconfig.get_config_var('LIBRARY')
# if static (or nonexistent), try to find a suitable dynamic libpython
if (python_library is None or
os.path.splitext(python_library)[1][-2:] == '.a'):
candidate_lib_prefixes = ['', 'lib']
candidate_extensions = ['.lib', '.so', '.a']
if sysconfig.get_config_var('WITH_DYLD'):
candidate_extensions.insert(0, '.dylib')
candidate_versions = [python_version]
if python_version:
candidate_versions.append('')
candidate_versions.insert(
0, "".join(python_version.split(".")[:2]))
abiflags = getattr(sys, 'abiflags', '')
candidate_abiflags = [abiflags]
if abiflags:
candidate_abiflags.append('')
# Ensure the value injected by virtualenv is
# returned on windows.
# Because calling `sysconfig.get_config_var('multiarchsubdir')`
# returns an empty string on Linux, `du_sysconfig` is only used to
# get the value of `LIBDIR`.
libdir = du_sysconfig.get_config_var('LIBDIR')
if sysconfig.get_config_var('MULTIARCH'):
masd = sysconfig.get_config_var('multiarchsubdir')
if masd:
if masd.startswith(os.sep):
masd = masd[len(os.sep):]
libdir = os.path.join(libdir, masd)
if libdir is None:
libdir = os.path.abspath(os.path.join(
sysconfig.get_config_var('LIBDEST'), "..", "libs"))
candidates = (
os.path.join(
libdir,
''.join((pre, 'python', ver, abi, ext))
)
for (pre, ext, ver, abi) in itertools.product(
candidate_lib_prefixes,
candidate_extensions,
candidate_versions,
candidate_abiflags
)
)
for candidate in candidates:
if os.path.exists(candidate):
# we found a (likely alternate) libpython
python_library = candidate
break
# TODO(opadron): what happens if we don't find a libpython?
return python_library
@staticmethod
def check_for_bad_installs():
"""This function tries to catch files that are meant to be installed
outside the project root before they are actually installed.
Indeed, we can not wait for the manifest, so we try to extract the
information (install destination) from the CMake build files
``*.cmake`` found in :const:`skbuild.constants.CMAKE_BUILD_DIR`.
It raises :class:`skbuild.exceptions.SKBuildError` if it found install destination outside of
:const:`skbuild.constants.CMAKE_INSTALL_DIR`.
"""
bad_installs = []
install_dir = os.path.join(os.getcwd(), CMAKE_INSTALL_DIR)
for root, _, file_list in os.walk(CMAKE_BUILD_DIR):
for filename in file_list:
if os.path.splitext(filename)[1] != ".cmake":
continue
for line in io.open(os.path.join(root, filename), encoding="utf-8"):
match = RE_FILE_INSTALL.match(line)
if match is None:
continue
destination = os.path.normpath(
match.group(1).replace("${CMAKE_INSTALL_PREFIX}",
install_dir))
if not destination.startswith(install_dir):
bad_installs.append(
os.path.join(
destination,
os.path.basename(match.group(2))
)
)
if bad_installs:
raise SKBuildError("\n".join((
" CMake-installed files must be within the project root.",
" Project Root:",
" " + install_dir,
" Violating Files:",
"\n".join(
(" " + _install) for _install in bad_installs)
)))
def make(self, clargs=(), config="Release", source_dir=".", env=None):
"""Calls the system-specific make program to compile code.
"""
clargs, config = pop_arg('--config', clargs, config)
if not os.path.exists(CMAKE_BUILD_DIR):
raise SKBuildError(("CMake build folder ({}) does not exist. "
"Did you forget to run configure before "
"make?").format(CMAKE_BUILD_DIR))
cmd = [self.cmake_executable, "--build", source_dir,
"--target", "install", "--config", config, "--"]
cmd.extend(clargs)
cmd.extend(
filter(bool,
shlex.split(os.environ.get("SKBUILD_BUILD_OPTIONS", "")))
)
rtn = subprocess.call(cmd, cwd=CMAKE_BUILD_DIR, env=env)
if rtn != 0:
raise SKBuildError(
"An error occurred while building with CMake.\n"
" Command:\n"
" {}\n"
" Source directory:\n"
" {}\n"
" Working directory:\n"
" {}\n"
"Please see CMake's output for more information.".format(
self._formatArgsForDisplay(cmd),
os.path.abspath(source_dir),
os.path.abspath(CMAKE_BUILD_DIR)))
def install(self):
"""Returns a list of file paths to install via setuptools that is
compatible with the data_files keyword argument.
"""
return self._parse_manifests()
def _parse_manifests(self):
paths = \
glob.glob(os.path.join(CMAKE_BUILD_DIR, "install_manifest*.txt"))
try:
return [self._parse_manifest(path) for path in paths][0]
except IndexError:
return []
@staticmethod
def _parse_manifest(install_manifest_path):
with open(install_manifest_path, "r") as manifest:
return [_remove_cwd_prefix(path) for path in manifest]
return []
@staticmethod
def _formatArgsForDisplay(args):
"""Format a list of arguments appropriately for display. When formatting
a command and its arguments, the user should be able to execute the
command by copying and pasting the output directly into a shell.
Currently, the only formatting is naively surrounding each argument with
quotation marks.
"""
return ' '.join("\"{}\"".format(arg) for arg in args)