-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.py
617 lines (518 loc) · 19.9 KB
/
setup.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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
#!/usr/bin/env python
import os
import re
import sys
import platform
import subprocess
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
from setuptools import Command
from setuptools.command.install_egg_info import install_egg_info
__thisdir__ = os.path.dirname(__file__)
# ---------------------------------------------------------------------------- #
# Setup the compilers
#
if platform.system() == "Darwin":
# force Clang on macOS
os.environ["CC"] = "/usr/bin/clang"
os.environ["CXX"] = "/usr/bin/clang++"
elif platform.system() == "Linux":
# choose GCC if not set
if os.environ.get("CC") is None and os.path.exists("/usr/bin/gcc"):
os.environ["CC"] = "/usr/bin/gcc"
if os.environ.get("CXX") is None and os.path.exists("/usr/bin/g++"):
os.environ["CXX"] = "/usr/bin/g++"
# make sure we don't use clang
if os.environ.get("CC") is not None:
if re.search(r"clang", os.environ.get("CC")) is not None:
os.environ["CC"] = "/usr/bin/gcc"
# make sure we don't use clang++
if os.environ.get("CXX") is not None:
if re.search(r"clang", os.environ.get("CXX")) is not None:
os.environ["CC"] = "/usr/bin/g++"
# ---------------------------------------------------------------------------- #
# work around to avoid using disttools.LooseVersion
#
def get_integer_version(version_string):
version_array = version_string.split(".")
# if more than 3 version numbers
while len(version_array) > 3:
version_array.pop()
# if less than 3 version numbers
while len(version_array) < 3:
version_array.append("0")
integer_version = 0
factors = [100000, 1000, 1]
for i in range(0, len(factors)):
integer_version += factors[i] * int(version_array[i])
return integer_version
# ---------------------------------------------------------------------------- #
# work around to avoid using disttools.LooseVersion
#
def get_string_version(version_integer):
major_ver = "{}".format(int(version_integer / 100000))
minor_ver = "{}".format(int((version_integer % 100000) / 1000))
patch_ver = "{}".format(int(version_integer % 1000))
return "{}.{}.{}".format(major_ver, minor_ver, patch_ver)
# ---------------------------------------------------------------------------- #
#
def get_project_version():
# open "VERSION"
with open(os.path.join(__thisdir__, "VERSION"), "r") as f:
data = f.read().replace("\n", "")
# make sure is string
if isinstance(data, list) or isinstance(data, tuple):
return data[0]
else:
return data
# ---------------------------------------------------------------------------- #
#
class CMakeExtension(Extension):
def __init__(self, name, sourcedir=""):
Extension.__init__(self, name, sources=[])
self.sourcedir = os.path.abspath(sourcedir)
# ---------------------------------------------------------------------------- #
#
class CMakeConfigure(build_ext, Command):
cmake_version = get_integer_version("2.7.12")
cmake_min_version = get_integer_version("2.8.12")
build_type = "Release"
cxx_standard = 11
cmake_prefix_path = ""
cmake_include_path = ""
cmake_library_path = ""
pybind11_install = "OFF"
devel_install = "ON"
# --------------------------------------------------------------------------#
# init
def __init__(self, *args, **kwargs):
build_ext.__init__(self, *args, **kwargs)
Command.__init__(self, *args, **kwargs)
# --------------------------------------------------------------------------#
def check_env(self, var, key):
ret = var
try:
ret = os.environ[key]
except KeyError:
pass
return ret
# --------------------------------------------------------------------------#
def cmake_version_error(self):
"""
Raise exception about CMake
"""
ma = "{}. {}.".format(
"Error finding/putting cmake in path",
"Either no CMake found or no cmake module",
)
mb = 'This error can commonly be resolved with "{}"'.format(
"pip install -U pip cmake"
)
mc = "CMake version found: {}".format(
get_string_version(CMakeConfigure.cmake_version)
)
md = (
"CMake must be installed to build the following extensions: "
+ ", ".join(e.name for e in self.extensions)
)
mt = "\n\n\t{}\n\t{}\n\t{}\n\t{}\n\n".format(ma, mb, mc, md)
raise RuntimeError(mt)
# --------------------------------------------------------------------------#
def init_cmake(self):
"""
Ensure cmake is in PATH
"""
try:
out = subprocess.check_output(["cmake", "--version"])
CMakeConfigure.cmake_version = get_integer_version(
re.search(r"version\s*([\d.]+)", out.decode()).group(1)
)
except OSError:
# if fail, try the module
try:
import cmake
if cmake.CMAKE_BIN_DIR not in sys.path:
sys.path.append(cmake.CMAKE_BIN_DIR)
if platform.system() != "Windows":
curr_path = os.environ["PATH"]
if cmake.CMAKE_BIN_DIR not in curr_path:
os.environ["PATH"] = "{}:{}".format(
curr_path, cmake.CMAKE_BIN_DIR
)
CMakeConfigure.cmake_version = cmake.sys.version.split(" ")[0]
except ImportError:
self.cmake_version_error()
# --------------------------------------------------------------------------#
# run
def run(self):
self.init_cmake()
print(
"Using CMake version {}...".format(
get_string_version(CMakeBuild.cmake_version)
)
)
if CMakeConfigure.cmake_version < CMakeConfigure.cmake_min_version:
raise RuntimeError(
"CMake >= {} is required. Found CMake version {}".format(
get_string_version(CMakeConfigure.cmake_min_version),
get_string_version(CMakeConfigure.cmake_version),
)
)
for ext in self.extensions:
self.build_extension(ext)
# --------------------------------------------------------------------------#
# build extension
def build_extension(self, ext):
self.init_cmake()
# check function for setup.cfg
def valid_string(_str):
if len(_str) > 0 and _str != '""' and _str != "''":
return True
return False
# allow environment to over-ride setup.cfg
# options are prefixed with PYCTEST_ if not already
def compose(str):
return "PYCTEST_{}".format(str.upper())
extdir = os.path.abspath(
os.path.dirname(self.get_ext_fullpath(ext.name))
)
# Always the same
cmake_args = [
"-DPYTHON_EXECUTABLE=" + sys.executable,
"-DPYCTEST_SETUP_PY=ON",
]
# ----------------------------------------------------------------------#
#
# Process options
#
# ----------------------------------------------------------------------#
self.build_type = self.check_env(self.build_type, compose("build_type"))
self.cxx_standard = self.check_env(
self.cxx_standard, compose("cxx_standard")
)
self.cmake_prefix_path = self.check_env(
self.cmake_prefix_path, compose("cmake_prefix_path")
)
self.cmake_include_path = self.check_env(
self.cmake_include_path, compose("cmake_include_path")
)
self.cmake_library_path = self.check_env(
self.cmake_library_path, compose("cmake_library_path")
)
self.pybind11_install = self.check_env(
self.pybind11_install, compose("pybind11_install")
)
self.devel_install = self.check_env(
self.devel_install, compose("devel_install")
)
_valid_type = False
for _type in ["Release", "Debug", "RelWithDebInfo", "MinSizeRel"]:
if _type == self.build_type:
_valid_type = True
break
if not _valid_type:
self.build_type = "Release"
cmake_args += ["-DCMAKE_BUILD_TYPE={}".format(self.build_type)]
cmake_args += ["-DBUILD_SHARED_LIBS=OFF"]
_cxxstd = int(self.cxx_standard)
if _cxxstd < 14 and platform.system() == "Windows":
# unique_ptr support
_cxxstd = 14
self.cxx_standard = "{}".format(_cxxstd)
if _cxxstd == 11 or _cxxstd == 14 or _cxxstd == 17:
cmake_args += ["-DCMAKE_CXX_STANDARD={}".format(self.cxx_standard)]
if valid_string(self.cmake_prefix_path):
cmake_args += [
"-DCMAKE_PREFIX_PATH={}".format(self.cmake_prefix_path)
]
if valid_string(self.cmake_library_path):
cmake_args += [
"-DCMAKE_LIBRARY_PATH={}".format(self.cmake_library_path)
]
if valid_string(self.cmake_include_path):
cmake_args += [
"-DCMAKE_INCLUDE_PATH={}".format(self.cmake_include_path)
]
cmake_args += [
"-DCMAKE_INSTALL_PREFIX={}".format(os.path.join(extdir, "pyctest"))
]
cmake_args += [
"-DPYBIND11_INSTALL={}".format(str.upper(self.pybind11_install))
]
env_arch = os.environ.get("PYTHON_ARCH")
if platform.system() == "Windows":
if platform.architecture()[0] == "64bit":
cmake_args += ["-A", "x64"]
elif env_arch is None:
if sys.maxsize > 2**32:
cmake_args += ["-A", "x64"]
elif env_arch is not None and env_arch == "64":
cmake_args += ["-A", "x64"]
cmake_args += ["-DCMake_MSVC_PARALLEL=ON"]
_generator = os.environ.get("CMAKE_GENERATOR", None)
if _generator is not None:
cmake_args += ["-G", _generator]
env = os.environ.copy()
env["CXXFLAGS"] = "{}".format(env.get("CXXFLAGS", ""))
# make directory if not exist
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
# set to absolute path
self.build_temp = os.path.abspath(self.build_temp)
# print the CMake args
print("CMake args: {}".format(cmake_args))
# configure the project
subprocess.check_call(
["cmake"] + cmake_args + [ext.sourcedir],
cwd=self.build_temp,
env=env,
)
print() # Add an empty line for cleaner output
# ---------------------------------------------------------------------------- #
#
class CMakeBuild(CMakeConfigure):
# --------------------------------------------------------------------------#
# init
def __init__(self, *args, **kwargs):
CMakeConfigure.__init__(self, *args, **kwargs)
# --------------------------------------------------------------------------#
# run
def run(self):
self.init_cmake()
print(
"Using CMake version {}...".format(
get_string_version(CMakeBuild.cmake_version)
)
)
if CMakeBuild.cmake_version < CMakeBuild.cmake_min_version:
raise RuntimeError(
"CMake >= {} is required. Found CMake version {}".format(
get_string_version(CMakeBuild.cmake_min_version),
get_string_version(CMakeBuild.cmake_version),
)
)
for ext in self.extensions:
self.build_extension(ext)
# --------------------------------------------------------------------------#
# build extension
def build_extension(self, ext):
self.init_cmake()
# allow environment to over-ride setup.cfg
# options are prefixed with PYCTEST_ if not already
def compose(str):
return "PYCTEST_{}".format(str.upper())
extdir = os.path.abspath(
os.path.dirname(self.get_ext_fullpath(ext.name))
)
cache_file = os.path.join(extdir, "CMakeCache.txt")
if not os.path.exists(extdir) or not os.path.exists(cache_file):
CMakeConfigure.build_extension(self, ext)
# ----------------------------------------------------------------------#
#
# Process options
#
# ----------------------------------------------------------------------#
self.build_type = self.check_env(self.build_type, compose("build_type"))
_valid_type = False
for _type in ["Release", "Debug", "RelWithDebInfo", "MinSizeRel"]:
if _type == self.build_type:
_valid_type = True
break
if not _valid_type:
self.build_type = "Release"
build_args = ["--config", self.build_type]
install_args = [
"-DBUILD_TYPE={}".format(self.build_type),
"-P",
"cmake_install.cmake",
]
if platform.system() == "Windows":
build_args += ["--target", "ALL_BUILD", "--", "/m"]
else:
nproc = "-j4"
try:
import multiprocessing as mp
ncpu = mp.cpu_count()
if ncpu > 8:
ncpu = 8
nproc = "-j{}".format(ncpu + 1)
except ImportError:
pass
build_args += ["--", nproc]
env = os.environ.copy()
env["CXXFLAGS"] = "{}".format(env.get("CXXFLAGS", ""))
# make directory if not exist
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
# set to absolute path
self.build_temp = os.path.abspath(self.build_temp)
# print the build_args
print("Build args: {}".format(build_args))
# print the install args
print("Install args: {}".format(install_args))
# build the project
try:
subprocess.check_call(
["cmake", "--build", self.build_temp] + build_args,
cwd=self.build_temp,
env=env,
)
except Exception as e:
# for building docs, sometimes we run out of memory causing internal
# compiler error
print(
"Exception occurred: {}.\nTrying build with one process".format(
e
)
)
_new_build_args = []
for arg in build_args:
if re.search(r"^-j", arg) is not None:
_new_build_args.append("-j1")
else:
_new_build_args.append(arg)
build_args = _new_build_args
subprocess.check_call(
["cmake", "--build", self.build_temp] + build_args,
cwd=self.build_temp,
env=env,
)
# build the project (second time)
subprocess.check_call(
["cmake", "--build", self.build_temp] + build_args,
cwd=self.build_temp,
env=env,
)
# install the CMake build
subprocess.check_call(
["cmake", "-DCOMPONENT=python"] + install_args,
cwd=self.build_temp,
env=env,
)
# install the development
subprocess.check_call(
["cmake", "-DCOMPONENT=development"] + install_args,
cwd=self.build_temp,
env=env,
)
CMakeInstallEggInfo.dirs[self.build_temp] = extdir
pyctestdir = os.path.join(extdir, "pyctest")
initpath = os.path.join(pyctestdir, "__init__.py")
if not os.path.exists(initpath):
f = open(initpath, "w")
if os.path.exists(".license.py"):
lic = open(".license.py")
contents = lic.read()
f.write("#!{}\n".format(sys.executable))
f.write(contents)
f.write("\n")
else:
f.write("#!{}\n".format(sys.executable))
f.close()
print() # Add an empty line for cleaner output
# ---------------------------------------------------------------------------- #
#
class CMakeInstallEggInfo(install_egg_info):
dirs = {}
files = []
# --------------------------------------------------------------------------#
def run(self):
install_egg_info.run(self)
for f in CMakeInstallEggInfo.files:
print('Adding "{}"...'.format(f))
self.outputs.append(f)
# read the install manifest from CMake
for tmpdir, libdir in CMakeInstallEggInfo.dirs.items():
for manifest in [
"install_manifest.txt",
"install_manifest_development.txt",
"install_manifest_python.txt",
]:
fname = os.path.join(tmpdir, manifest)
if not os.path.exists(fname):
continue
f = open(fname, "r")
if libdir[len(libdir) - 1] != "/":
libdir += "/"
for itr in f.read().splitlines():
b = itr.replace(libdir, "")
f = os.path.join(self.install_dir, b)
# print ('Adding "{}"...'.format(f))
self.outputs.append(f)
# ---------------------------------------------------------------------------- #
#
def get_long_description():
long_descript = ""
try:
long_descript = open("README.md").read()
except IOError:
long_descript = ""
return long_descript
# ---------------------------------------------------------------------------- #
#
def get_short_description():
part_a = "Python wrappers for generating CTest and submitting to CDash"
part_b = "without a CMake build system"
return "{} {}".format(part_a, part_b)
# ---------------------------------------------------------------------------- #
#
def get_keywords():
return ["cmake", "ctest", "pybind11"]
# ---------------------------------------------------------------------------- #
#
def get_classifiers():
return [
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"Topic :: Utilities",
"Natural Language :: English",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: Implementation :: CPython",
]
# ---------------------------------------------------------------------------- #
#
def get_name():
return "Jonathan R. Madsen"
# ---------------------------------------------------------------------------- #
#
def get_email():
return "[email protected]"
# ---------------------------------------------------------------------------- #
# calls the setup and declare package
#
setup(
name="pyctest",
version=get_project_version(),
author=get_name(),
author_email=get_email(),
maintainer=get_name(),
maintainer_email=get_email(),
contact=get_name(),
contact_email=get_email(),
description=get_short_description(),
long_description=get_long_description(),
long_description_content_type="text/markdown",
url="https://github.com/jrmadsen/pyctest.git",
license="MIT",
# add extension module
ext_modules=[CMakeExtension("pyctest")],
# add custom build_ext command
cmdclass=dict(
configure=CMakeConfigure,
build_ext=CMakeBuild,
install_egg_info=CMakeInstallEggInfo,
),
zip_safe=False,
# extra
install_requires=[],
setup_requires=[],
# packages=[ 'pyctest' ],
keywords=get_keywords(),
classifiers=get_classifiers(),
python_requires=">=2.6",
)