Repository navigation
Expand file tree
/
Copy pathsetup.py
More file actions
146 lines (124 loc) · 5.18 KB
/
Copy pathsetup.py
File metadata and controls
146 lines (124 loc) · 5.18 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
import os
import re
import shutil
import sys
from setuptools import setup
from setuptools.command.build_ext import build_ext as _build_ext
def parse_r_h_symbols():
r_h_path = os.path.join("rchitect", "_cffi", "R.h")
with open(r_h_path, "r") as f:
content = f.read()
cdef_m = re.search(r"// begin cdef(.*?)// end cdef", content, re.S)
if sys.platform.startswith("win"):
plat_m = re.search(r"// begin win cdef(.*?)// end win cdef", content, re.S)
else:
plat_m = re.search(r"// begin unix cdef(.*?)// end unix cdef", content, re.S)
sections = (cdef_m.group(1) if cdef_m else "") + "\n" + (plat_m.group(1) if plat_m else "")
r_funcs = []
r_data = []
rgraphapp_funcs = []
for line in sections.splitlines():
line = line.strip()
if not line or line.startswith("//"):
continue
if line.startswith("RAPI_FUNC "):
m = re.match(r"^RAPI_FUNC\s+.*?\b([A-Za-z_][A-Za-z0-9_]*)\s*\(", line)
if m:
r_funcs.append(m.group(1))
elif line.startswith("RGRAPHAPP_FUNC "):
m = re.match(r"^RGRAPHAPP_FUNC\s+.*?\b([A-Za-z_][A-Za-z0-9_]*)\s*\(", line)
if m:
rgraphapp_funcs.append(m.group(1))
elif line.startswith("RAPI_EXTERN "):
m = re.search(r"\(\s*\*\s*([A-Za-z_][A-Za-z0-9_]*)\s*\)", line)
if not m:
m = re.search(r"\b([A-Za-z_][A-Za-z0-9_]*)\s*;$", line)
if m:
r_data.append(m.group(1))
return r_funcs, r_data, rgraphapp_funcs
class build_ext(_build_ext):
def run(self):
self._extra_outputs = []
if sys.platform.startswith("win"):
self.build_win_import_libs()
super().run()
if sys.platform.startswith("win"):
self.build_host()
def get_outputs(self):
outputs = super().get_outputs()
return outputs + getattr(self, "_extra_outputs", [])
def _get_initialized_compiler(self):
from setuptools._distutils.ccompiler import new_compiler
from setuptools._distutils.sysconfig import customize_compiler
compiler = getattr(self, "compiler", None)
if compiler is None or isinstance(compiler, str):
compiler = new_compiler(compiler=compiler)
customize_compiler(compiler)
if hasattr(compiler, "initialize") and not getattr(compiler, "initialized", False):
compiler.initialize()
return compiler
def _add_temp_library_dir(self, lib_dir):
abs_dir = os.path.abspath(lib_dir)
if self.library_dirs is None:
self.library_dirs = []
if abs_dir not in self.library_dirs:
self.library_dirs.append(abs_dir)
for ext in getattr(self, "extensions", []):
if abs_dir not in ext.library_dirs:
ext.library_dirs.append(abs_dir)
def build_win_import_libs(self):
os.makedirs(self.build_temp, exist_ok=True)
r_funcs, r_data, rgraphapp_funcs = parse_r_h_symbols()
compiler = self._get_initialized_compiler()
r_def = os.path.join(self.build_temp, "R.def")
with open(r_def, "w") as f:
f.write("LIBRARY R\nEXPORTS\n")
for sym in r_funcs:
f.write(" {}\n".format(sym))
for sym in r_data:
f.write(" {} DATA\n".format(sym))
rga_def = os.path.join(self.build_temp, "Rgraphapp.def")
with open(rga_def, "w") as f:
f.write("LIBRARY Rgraphapp\nEXPORTS\n")
for sym in rgraphapp_funcs:
f.write(" {}\n".format(sym))
import sysconfig
plat = (getattr(self, "plat_name", None) or sysconfig.get_platform()).lower()
if "arm64" in plat:
machine = "/MACHINE:ARM64"
else:
machine = "/MACHINE:X64"
r_lib = os.path.join(self.build_temp, "R.lib")
rga_lib = os.path.join(self.build_temp, "Rgraphapp.lib")
lib_exe = getattr(compiler, "lib", "lib.exe")
compiler.spawn([lib_exe, "/nologo", machine, "/def:" + r_def, "/out:" + r_lib])
compiler.spawn([lib_exe, "/nologo", machine, "/def:" + rga_def, "/out:" + rga_lib])
self._add_temp_library_dir(self.build_temp)
def build_host(self):
compiler = self._get_initialized_compiler()
sources = [
os.path.join("rchitect", "_cffi", "host.c"),
os.path.join("rchitect", "_cffi", "host.rc"),
]
objs = compiler.compile(
sources,
output_dir=self.build_temp,
include_dirs=[os.path.join("rchitect", "_cffi")],
)
out_dir = os.path.join(self.build_lib, "rchitect")
os.makedirs(out_dir, exist_ok=True)
compiler.link_executable(
objs,
"host",
output_dir=out_dir,
libraries=["advapi32"],
extra_postargs=["/MANIFEST:NO", "/STACK:0x4000000"],
)
built_exe = os.path.join(out_dir, "host.exe")
self._extra_outputs.append(built_exe)
if self.inplace:
shutil.copy2(built_exe, os.path.join("rchitect", "host.exe"))
setup(
cffi_modules=["rchitect/build.py:ffibuilder"],
cmdclass={"build_ext": build_ext},
)