-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsetup.py
More file actions
executable file
·125 lines (109 loc) · 4.79 KB
/
setup.py
File metadata and controls
executable file
·125 lines (109 loc) · 4.79 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
# File: setup.py
# Date: 17-Dec-2017
#
# Update: 17-Jan-2018 jdw - resolve python virtual env issues with Tox.
# 8-Aug-2018 jdw - add py3.7
# 14-May-2021 jdw - make requirements*.txt authoritative
# 18-Oct-2025 ep - move metadata to pyproject.toml
#
import glob
import os
import platform
import re
import subprocess
import sys
from distutils.version import LooseVersion # pylint: disable=no-name-in-module,import-error
from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext
class CMakeExtension(Extension):
def __init__(self, name, sourcedir="", sources=None):
sources = sources if sources else []
Extension.__init__(self, name, sources=sources)
self.sourcedir = os.path.abspath(sourcedir)
class CMakeBuild(build_ext):
def run(self):
try:
out = subprocess.check_output(["cmake", "--version"])
except OSError:
raise RuntimeError("CMake >= 3.4 must be installed to build the following extensions: " + ", ".join(e.name for e in self.extensions))
if platform.system() == "Windows":
cmakeVersion = LooseVersion(re.search(r"version\s*([\d.]+)", out.decode()).group(1))
if cmakeVersion < "3.4.0":
raise RuntimeError("CMake >= 3.4.0 is required on Windows")
for ext in self.extensions:
self.build_extension(ext)
def build_extension(self, ext):
debug = True
extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
cmakeArgs = ["-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=" + extdir, "-DPYTHON_EXECUTABLE=" + sys.executable]
# We need to help cmake find the correct python for this virtual env -
# ---
libPath = None
lsp = os.path.join(sys.exec_prefix, "lib", "libpython") + "*"
lpL = glob.glob(lsp)
if lpL:
libPath = lpL[0]
elif hasattr(sys, "base_exec_prefix"):
lsp = os.path.join(sys.base_exec_prefix, "lib", "libpython") + "*" # pylint: disable=no-member
lpL = glob.glob(lsp)
if lpL:
libPath = lpL[0]
if libPath:
cmakeArgs += ["-DPYTHON_LIBRARY=" + libPath]
else:
print("------ WARNING could not locate python library")
# ---
inclPath = None
isp = os.path.join(sys.exec_prefix, "include", "python") + "%s.%s" % (sys.version_info.major, sys.version_info.minor) + "*"
ipL = glob.glob(isp)
if ipL:
inclPath = ipL[0]
elif hasattr(sys, "base_exec_prefix"):
isp = os.path.join(sys.base_exec_prefix, "include", "python") + "%s.%s" % (sys.version_info.major, sys.version_info.minor) + "*" # pylint: disable=no-member
ipL = glob.glob(isp)
if ipL:
inclPath = ipL[0]
if inclPath:
cmakeArgs += ["-DPYTHON_INCLUDE_DIR=" + inclPath]
else:
print("------ WARNING could not locate python include files")
# ---
cfg = "Debug" if self.debug else "Release"
buildArgs = ["--config", cfg]
if platform.system() == "Windows":
cmakeArgs += ["-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}".format(cfg.upper(), extdir)]
if sys.maxsize > 2 ** 32:
cmakeArgs += ["-A", "x64"]
buildArgs += ["--", "/m"]
else:
cmakeArgs += ["-DCMAKE_BUILD_TYPE=" + cfg]
buildArgs += ["--", "-j2"]
if sys.platform.startswith("darwin"):
# Cross-compile support for macOS - respect ARCHFLAGS if set
archs = re.findall(r"-arch (\S+)", os.environ.get("ARCHFLAGS", ""))
if archs:
cmakeArgs += ["-DCMAKE_OSX_ARCHITECTURES={}".format(";".join(archs))]
env = os.environ.copy()
env["CXXFLAGS"] = '{} -DVERSION_INFO=\\"{}\\"'.format(env.get("CXXFLAGS", ""), self.distribution.get_version())
env["RUN_FROM_DISUTILS"] = "yes"
#
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
#
if debug:
print("------------- setup.py -----------------")
print("Extension source path ", ext.sourcedir)
print("CMAKE_ARGS ", cmakeArgs)
print("self.build_temp ", self.build_temp)
print("extdir", extdir)
print("ext.name", ext.name)
print("sys.executable", sys.executable)
print("sys.exec_prefix", sys.exec_prefix)
print("CXXFLAGS ", env["CXXFLAGS"])
#
subprocess.check_call(["cmake", ext.sourcedir] + cmakeArgs, cwd=self.build_temp, env=env)
subprocess.check_call(["cmake", "--build", "."] + buildArgs, cwd=self.build_temp)
setup(
ext_modules=[CMakeExtension("mmcif.core.mmciflib")],
cmdclass=dict(build_ext=CMakeBuild),
)