Skip to content

Commit 4400cf1

Browse files
authored
Merge pull request #182 from davidhewitt/fix-windows-exec
exec: fix for windows
2 parents cbc59ed + bf2ad8b commit 4400cf1

File tree

3 files changed

+34
-41
lines changed

3 files changed

+34
-41
lines changed

examples/hello-world/setup.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
from setuptools import setup
2+
23
from setuptools_rust import Binding, RustExtension
34

45
setup(
56
name="hello-world",
67
version="1.0",
78
rust_extensions=[
8-
RustExtension("hello_world.hello_world", binding=Binding.Exec, script=True)
9+
RustExtension(
10+
{"hello-world": "hello_world.hello_world"},
11+
binding=Binding.Exec,
12+
script=True,
13+
)
914
],
1015
# rust extensions are not zip safe, just like C-extensions.
1116
zip_safe=False,

setuptools_rust/build.py

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
)
1515
from distutils.sysconfig import get_config_var
1616
from subprocess import check_output
17-
from typing import NamedTuple, Optional
17+
from typing import List, NamedTuple, Optional, Tuple
1818

1919
from setuptools.command.build_ext import get_abi3_suffix
2020

@@ -324,31 +324,18 @@ def build_extension(self, ext: RustExtension, target_triple=None):
324324

325325
if executable:
326326
for name, dest in ext.target.items():
327-
if name:
328-
path = os.path.join(artifactsdir, name)
329-
if os.access(path, os.X_OK):
330-
dylib_paths.append((dest, path))
331-
continue
332-
else:
333-
raise DistutilsExecError(
334-
"Rust build failed; "
335-
f"unable to find executable '{name}' in '{target_dir}'"
336-
)
337-
else:
338-
# search executable
339-
for name in os.listdir(artifactsdir):
340-
path = os.path.join(artifactsdir, name)
341-
if name.startswith(".") or not os.path.isfile(path):
342-
continue
327+
if not name:
328+
name = dest.split(".")[-1]
329+
name += sysconfig.get_config_var("EXE")
343330

344-
if os.access(path, os.X_OK):
345-
dylib_paths.append((ext.name, path))
346-
break
347-
348-
if not dylib_paths:
349-
raise DistutilsExecError(
350-
f"Rust build failed; unable to find executable in {target_dir}"
351-
)
331+
path = os.path.join(artifactsdir, name)
332+
if os.access(path, os.X_OK):
333+
dylib_paths.append((dest, path))
334+
else:
335+
raise DistutilsExecError(
336+
"Rust build failed; "
337+
f"unable to find executable '{name}' in '{artifactsdir}'"
338+
)
352339
else:
353340
if sys.platform == "win32" or sys.platform == "cygwin":
354341
dylib_ext = "dll"
@@ -372,7 +359,7 @@ def build_extension(self, ext: RustExtension, target_triple=None):
372359
)
373360
return dylib_paths
374361

375-
def install_extension(self, ext: RustExtension, dylib_paths):
362+
def install_extension(self, ext: RustExtension, dylib_paths: List[Tuple[str, str]]):
376363
executable = ext.binding == Binding.Exec
377364
debug_build = ext.debug if ext.debug is not None else self.inplace
378365
debug_build = self.debug if self.debug is not None else debug_build
@@ -383,23 +370,26 @@ def install_extension(self, ext: RustExtension, dylib_paths):
383370
build_ext = self.get_finalized_command("build_ext")
384371
build_ext.inplace = self.inplace
385372

386-
for target_fname, dylib_path in dylib_paths:
387-
if not target_fname:
388-
target_fname = os.path.basename(
373+
for module_name, dylib_path in dylib_paths:
374+
if not module_name:
375+
module_name = os.path.basename(
389376
os.path.splitext(os.path.basename(dylib_path)[3:])[0]
390377
)
391378

392379
if executable:
393-
ext_path = build_ext.get_ext_fullpath(target_fname)
380+
ext_path = build_ext.get_ext_fullpath(module_name)
394381
# remove .so extension
395382
ext_path, _ = os.path.splitext(ext_path)
396383
# remove python3 extension (i.e. cpython-36m)
397384
ext_path, _ = os.path.splitext(ext_path)
398385

386+
# Add expected extension
387+
ext_path += sysconfig.get_config_var("EXE")
388+
399389
os.makedirs(os.path.dirname(ext_path), exist_ok=True)
400-
ext.install_script(ext_path)
390+
ext.install_script(module_name.split(".")[-1], ext_path)
401391
else:
402-
ext_path = self.get_dylib_ext_path(ext, target_fname)
392+
ext_path = self.get_dylib_ext_path(ext, module_name)
403393
os.makedirs(os.path.dirname(ext_path), exist_ok=True)
404394

405395
shutil.copyfile(dylib_path, ext_path)

setuptools_rust/extension.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,25 +180,23 @@ def entry_points(self):
180180

181181
return entry_points
182182

183-
def install_script(self, ext_path):
183+
def install_script(self, module_name: str, exe_path: str):
184184
if self.script and self.binding == Binding.Exec:
185-
dirname, name = os.path.split(ext_path)
186-
file = os.path.join(dirname, "_gen_%s.py" % name)
185+
dirname, executable = os.path.split(exe_path)
186+
file = os.path.join(dirname, "_gen_%s.py" % module_name)
187187
with open(file, "w") as f:
188-
f.write(TMPL.format(name=name))
188+
f.write(TMPL.format(executable=repr(executable)))
189189

190190

191191
TMPL = """
192192
import os
193193
import sys
194194
195-
196195
def run():
197196
path = os.path.split(__file__)[0]
198-
name = os.path.split(sys.argv[0])[1]
199-
file = os.path.join(path, name)
197+
file = os.path.join(path, {executable})
200198
if os.path.isfile(file):
201199
os.execv(file, sys.argv)
202200
else:
203-
print("can't execute '{name}'")
201+
raise RuntimeError("can't find " + file)
204202
"""

0 commit comments

Comments
 (0)