-
-
Notifications
You must be signed in to change notification settings - Fork 25.1k
/
vendor.py
171 lines (134 loc) · 5.93 KB
/
vendor.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
"""Embed vcomp140.dll, vcruntime140.dll and vcruntime140_1.dll.
Note that vcruntime140_1.dll is only required (and available)
for 64-bit architectures.
"""
import os
import os.path as op
import shutil
import sys
import textwrap
TARGET_FOLDER = op.join("sklearn", ".libs")
DISTRIBUTOR_INIT = op.join("sklearn", "_distributor_init.py")
VCOMP140_SRC_PATH = "C:\\Windows\\System32\\vcomp140.dll"
VCRUNTIME140_SRC_PATH = "C:\\Windows\\System32\\vcruntime140.dll"
VCRUNTIME140_1_SRC_PATH = "C:\\Windows\\System32\\vcruntime140_1.dll"
MSVCP140_SRC_PATH = "C:\\Windows\\System32\\msvcp140.dll"
def make_distributor_init_32_bits(
distributor_init, vcomp140_dll_filename, vcruntime140_dll_filename
):
"""Create a _distributor_init.py file for 32-bit architectures.
This file is imported first when importing the sklearn package
so as to pre-load the vendored vcomp140.dll and vcruntime140.dll.
"""
with open(distributor_init, "wt") as f:
f.write(
textwrap.dedent(
"""
'''Helper to preload vcomp140.dll and vcruntime140.dll to
prevent "not found" errors.
Once vcomp140.dll and vcruntime140.dll are preloaded, the
namespace is made available to any subsequent vcomp140.dll
and vcruntime140.dll. This is created as part of the scripts
that build the wheel.
'''
import os
import os.path as op
from ctypes import WinDLL
if os.name == "nt":
# Load vcomp140.dll and vcruntime140.dll
libs_path = op.join(op.dirname(__file__), ".libs")
vcomp140_dll_filename = op.join(libs_path, "{0}")
vcruntime140_dll_filename = op.join(libs_path, "{1}")
WinDLL(op.abspath(vcomp140_dll_filename))
WinDLL(op.abspath(vcruntime140_dll_filename))
""".format(
vcomp140_dll_filename, vcruntime140_dll_filename
)
)
)
def make_distributor_init_64_bits(
distributor_init,
vcomp140_dll_filename,
vcruntime140_dll_filename,
vcruntime140_1_dll_filename,
msvcp140_dll_filename,
):
"""Create a _distributor_init.py file for 64-bit architectures.
This file is imported first when importing the sklearn package
so as to pre-load the vendored vcomp140.dll, vcruntime140.dll,
vcruntime140_1.dll and msvcp140.dll.
"""
with open(distributor_init, "wt") as f:
f.write(
textwrap.dedent(
"""
'''Helper to preload vcomp140.dll, vcruntime140.dll and
vcruntime140_1.dll to prevent "not found" errors.
Once vcomp140.dll, vcruntime140.dll and vcruntime140_1.dll are
preloaded, the namespace is made available to any subsequent
vcomp140.dll, vcruntime140.dll and vcruntime140_1.dll. This is
created as part of the scripts that build the wheel.
'''
import os
import os.path as op
from ctypes import WinDLL
if os.name == "nt":
# Load vcomp140.dll, vcruntime140.dll and vcruntime140_1.dll
libs_path = op.join(op.dirname(__file__), ".libs")
vcomp140_dll_filename = op.join(libs_path, "{0}")
vcruntime140_dll_filename = op.join(libs_path, "{1}")
vcruntime140_1_dll_filename = op.join(libs_path, "{2}")
msvcp140_dll_filename = op.join(libs_path, "{3}")
WinDLL(op.abspath(vcomp140_dll_filename))
WinDLL(op.abspath(vcruntime140_dll_filename))
WinDLL(op.abspath(vcruntime140_1_dll_filename))
WinDLL(op.abspath(msvcp140_dll_filename))
""".format(
vcomp140_dll_filename,
vcruntime140_dll_filename,
vcruntime140_1_dll_filename,
msvcp140_dll_filename,
)
)
)
def main(wheel_dirname):
"""Embed vcomp140.dll, vcruntime140.dll and vcruntime140_1.dll."""
if not op.exists(VCOMP140_SRC_PATH):
raise ValueError(f"Could not find {VCOMP140_SRC_PATH}.")
if not op.exists(VCRUNTIME140_SRC_PATH):
raise ValueError(f"Could not find {VCRUNTIME140_SRC_PATH}.")
if not op.exists(VCRUNTIME140_1_SRC_PATH):
raise ValueError(f"Could not find {VCRUNTIME140_1_SRC_PATH}.")
if not op.exists(MSVCP140_SRC_PATH):
raise ValueError(f"Could not find {MSVCP140_SRC_PATH}.")
if not op.isdir(wheel_dirname):
raise RuntimeError(f"Could not find {wheel_dirname} file.")
vcomp140_dll_filename = op.basename(VCOMP140_SRC_PATH)
vcruntime140_dll_filename = op.basename(VCRUNTIME140_SRC_PATH)
vcruntime140_1_dll_filename = op.basename(VCRUNTIME140_1_SRC_PATH)
msvcp140_dll_filename = op.basename(MSVCP140_SRC_PATH)
target_folder = op.join(wheel_dirname, TARGET_FOLDER)
distributor_init = op.join(wheel_dirname, DISTRIBUTOR_INIT)
# Create the "sklearn/.libs" subfolder
if not op.exists(target_folder):
os.mkdir(target_folder)
print(f"Copying {VCOMP140_SRC_PATH} to {target_folder}.")
shutil.copy2(VCOMP140_SRC_PATH, target_folder)
print(f"Copying {VCRUNTIME140_SRC_PATH} to {target_folder}.")
shutil.copy2(VCRUNTIME140_SRC_PATH, target_folder)
print(f"Copying {VCRUNTIME140_1_SRC_PATH} to {target_folder}.")
shutil.copy2(VCRUNTIME140_1_SRC_PATH, target_folder)
print(f"Copying {MSVCP140_SRC_PATH} to {target_folder}.")
shutil.copy2(MSVCP140_SRC_PATH, target_folder)
# Generate the _distributor_init file in the source tree
print("Generating the '_distributor_init.py' file.")
make_distributor_init_64_bits(
distributor_init,
vcomp140_dll_filename,
vcruntime140_dll_filename,
vcruntime140_1_dll_filename,
msvcp140_dll_filename,
)
if __name__ == "__main__":
_, wheel_file = sys.argv
main(wheel_file)