36
36
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37
37
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38
38
# SOFTWARE.
39
-
40
39
from importlib import invalidate_caches
41
40
42
41
import gc
43
42
import os
43
+ import shutil
44
44
import sys
45
45
import unittest
46
46
from copy import deepcopy
@@ -109,18 +109,18 @@ def ccompile(self, name, check_duplicate_name=True):
109
109
m .update (block )
110
110
cur_checksum = m .hexdigest ()
111
111
112
- install_dir = DIR / 'build' / name
112
+ build_dir = DIR / 'build' / name
113
113
114
114
# see if there is already a checksum file
115
- checksum_file = install_dir / f'{ name } { EXT_SUFFIX } .sha256'
115
+ checksum_file = build_dir / f'{ name } { EXT_SUFFIX } .sha256'
116
116
available_checksum = ""
117
117
if checksum_file .exists ():
118
118
# read checksum file
119
119
with open (checksum_file , "r" ) as f :
120
120
available_checksum = f .readline ()
121
121
122
122
# note, the suffix is already a string like '.so'
123
- lib_file = install_dir / f'{ name } { EXT_SUFFIX } '
123
+ lib_file = build_dir / f'{ name } { EXT_SUFFIX } '
124
124
125
125
if check_duplicate_name and available_checksum != cur_checksum and name in compiled_registry :
126
126
raise RuntimeError (f"\n \n Module with name '{ name } ' was already compiled, but with different source code. "
@@ -134,20 +134,28 @@ def ccompile(self, name, check_duplicate_name=True):
134
134
# Note: It could be that the C source file's checksum didn't change but someone
135
135
# manually deleted the shared library file.
136
136
if available_checksum != cur_checksum or not lib_file .exists ():
137
- module = Extension (name , sources = [str (source_file )])
138
- args = [
139
- '--verbose' if sys .flags .verbose else '--quiet' ,
140
- 'build' , f'--build-base={ install_dir } ' ,
141
- 'install_lib' , '-f' , f'--install-dir={ install_dir } ' ,
142
- ]
143
- setup (
144
- script_name = 'setup' ,
145
- script_args = args ,
146
- name = name ,
147
- version = '1.0' ,
148
- description = '' ,
149
- ext_modules = [module ]
150
- )
137
+ os .makedirs (build_dir , exist_ok = True )
138
+ # MSVC linker doesn't like absolute paths in some parameters, so just run from the build dir
139
+ old_cwd = os .getcwd ()
140
+ os .chdir (build_dir )
141
+ try :
142
+ shutil .copy (source_file , '.' )
143
+ module = Extension (name , sources = [source_file .name ])
144
+ args = [
145
+ '--verbose' if sys .flags .verbose else '--quiet' ,
146
+ 'build' ,
147
+ 'install_lib' , '-f' , '--install-dir=.' ,
148
+ ]
149
+ setup (
150
+ script_name = 'setup' ,
151
+ script_args = args ,
152
+ name = name ,
153
+ version = '1.0' ,
154
+ description = '' ,
155
+ ext_modules = [module ]
156
+ )
157
+ finally :
158
+ os .chdir (old_cwd )
151
159
152
160
# write new checksum
153
161
with open (checksum_file , "w" ) as f :
@@ -163,7 +171,7 @@ def ccompile(self, name, check_duplicate_name=True):
163
171
if GRAALPYTHON :
164
172
file_not_empty (lib_file )
165
173
166
- return str (install_dir )
174
+ return str (build_dir )
167
175
168
176
169
177
def file_not_empty (path ):
0 commit comments