Skip to content

Commit 4f97f1a

Browse files
authored
fix: Fix up search path of bootstrapped Python toolchain dylib (#2089)
Previously, `//tests/cc/current_py_cc_libs::python_libs_linking_test` failed on macOS because the bootstrapped toolchain's dylib had an incorrect LC_ID_DYLIB field set, pointing to a local directory on the Python standalone build host machine. To fix, add a small conditional to the Python repository rule patching the LC_ID_DYLIB field of the bootstrapped Python's dylib with its fully qualified file system path. Patching is carried out with macOS's own `install_name_tool`, which is part of the standard macOS dynamic linking toolchain. Qualifies the mentioned test as executable on Mac, now only Windows linker errors are left to fix.
1 parent 5d27829 commit 4f97f1a

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ A brief description of the categories of changes:
3535
* `3.12 -> 3.12.4`
3636

3737
### Fixed
38+
* (rules) Fixes build targets linking against `@rules_python//python/cc:current_py_cc_libs`
39+
in host platform builds on macOS, by editing the `LC_ID_DYLIB` field of the hermetic interpreter's
40+
`libpython3.x.dylib` using `install_name_tool`, setting it to its absolute path under Bazel's
41+
execroot.
3842
* (rules) Signals are properly received when using {obj}`--bootstrap_impl=script`
3943
(for non-zip builds).
4044
([#2043](https://github.com/bazelbuild/rules_python/issues/2043))

python/private/python_repositories.bzl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,22 @@ def _python_repository_impl(rctx):
191191
elif rctx.attr.distutils_content:
192192
rctx.file(distutils_path, rctx.attr.distutils_content)
193193

194+
if "darwin" in platform and "osx" == repo_utils.get_platforms_os_name(rctx):
195+
# Fix up the Python distribution's LC_ID_DYLIB field.
196+
# It points to a build directory local to the GitHub Actions
197+
# host machine used in the Python standalone build, which causes
198+
# dyld lookup errors. To fix, set the full path to the dylib as
199+
# it appears in the Bazel workspace as its LC_ID_DYLIB using
200+
# the `install_name_tool` bundled with macOS.
201+
dylib = "lib/libpython{}.dylib".format(python_short_version)
202+
full_dylib_path = rctx.path(dylib)
203+
repo_utils.execute_checked(
204+
rctx,
205+
op = "python_repository.FixUpDyldIdPath",
206+
arguments = [repo_utils.which_checked(rctx, "install_name_tool"), "-id", full_dylib_path, dylib],
207+
logger = logger,
208+
)
209+
194210
# Make the Python installation read-only. This is to prevent issues due to
195211
# pycs being generated at runtime:
196212
# * The pycs are not deterministic (they contain timestamps)

tests/cc/current_py_cc_libs/BUILD.bazel

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ current_py_cc_libs_test_suite(name = "current_py_cc_libs_tests")
2020
cc_test(
2121
name = "python_libs_linking_test",
2222
srcs = ["python_libs_linking_test.cc"],
23-
# Mac and Windows fail with linking errors, but its not clear why; someone
24-
# with more C + Mac/Windows experience will have to figure it out.
23+
# Windows fails with linking errors, but its not clear why; someone
24+
# with more C + Windows experience will have to figure it out.
2525
# - rickeylev@
26-
target_compatible_with = [
27-
"@platforms//os:linux",
28-
],
26+
target_compatible_with = select({
27+
"@platforms//os:linux": [],
28+
"@platforms//os:osx": [],
29+
"//conditions:default": ["@platforms//:incompatible"],
30+
}),
2931
deps = [
3032
"@rules_python//python/cc:current_py_cc_headers",
3133
"@rules_python//python/cc:current_py_cc_libs",

0 commit comments

Comments
 (0)