Skip to content

Commit 8b35cc2

Browse files
authored
Set a more reasonable LC_ID_DYLIB entry on macOS (#119)
1 parent 303fa40 commit 8b35cc2

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
### Fixed
1616
- Set executable bit on shared library. [#110](https://github.com/PyO3/setuptools-rust/pull/110)
1717
- Don't require optional `wheel` dependency. [#111](https://github.com/PyO3/setuptools-rust/pull/111)
18+
- Set a more reasonable LC_ID_DYLIB entry on macOS. [#119](https://github.com/PyO3/setuptools-rust/pull/119)
1819

1920
## 0.11.6 (2020-12-13)
2021

setuptools_rust/build.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ def run_for_extension(self, ext: RustExtension):
180180
if ext.native:
181181
rustflags += " -C target-cpu=native"
182182

183+
if not executable and sys.platform == "darwin":
184+
ext_basename = os.path.basename(self.get_dylib_ext_path(ext, ext.name))
185+
rustflags += f" -C link-args=-Wl,-install_name,@rpath/{ext_basename}"
186+
183187
if rustflags:
184188
env["RUSTFLAGS"] = (env.get("RUSTFLAGS", "") + " " + rustflags).strip()
185189

@@ -281,16 +285,7 @@ def run_for_extension(self, ext: RustExtension):
281285

282286
ext.install_script(ext_path)
283287
else:
284-
# Technically it's supposed to contain a
285-
# `setuptools.Extension`, but in practice the only attribute it
286-
# checks is `ext.py_limited_api`.
287-
modpath = target_fname.split('.')[-1]
288-
assert modpath not in build_ext.ext_map
289-
build_ext.ext_map[modpath] = ext
290-
try:
291-
ext_path = build_ext.get_ext_fullpath(target_fname)
292-
finally:
293-
del build_ext.ext_map[modpath]
288+
ext_path = self.get_dylib_ext_path(ext, target_fname)
294289

295290
os.makedirs(os.path.dirname(ext_path), exist_ok=True)
296291
shutil.copyfile(dylib_path, ext_path)
@@ -307,11 +302,24 @@ def run_for_extension(self, ext: RustExtension):
307302
args.append(ext_path)
308303
try:
309304
output = subprocess.check_output(args, env=env)
310-
except subprocess.CalledProcessError as e:
305+
except subprocess.CalledProcessError:
311306
pass
312307

313308
# executables, win32(cygwin)-dll's, and shared libraries on
314309
# Unix-like operating systems need X bits
315310
mode = os.stat(ext_path).st_mode
316311
mode |= (mode & 0o444) >> 2 # copy R bits to X
317312
os.chmod(ext_path, mode)
313+
314+
def get_dylib_ext_path(self, ext, target_fname):
315+
build_ext = self.get_finalized_command("build_ext")
316+
# Technically it's supposed to contain a
317+
# `setuptools.Extension`, but in practice the only attribute it
318+
# checks is `ext.py_limited_api`.
319+
modpath = target_fname.split('.')[-1]
320+
assert modpath not in build_ext.ext_map
321+
build_ext.ext_map[modpath] = ext
322+
try:
323+
return build_ext.get_ext_fullpath(target_fname)
324+
finally:
325+
del build_ext.ext_map[modpath]

0 commit comments

Comments
 (0)