Skip to content

Commit 48d30c5

Browse files
committed
fix: Fix up search path of bootstrapped Python toolchain dylib
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. Since this needs macOS to be the host platform, restrict this change to macOS host systems only by checking the host OS name. Qualifies the mentioned test as executable on Mac, now only Windows linker errors are left to fix.
1 parent 014ad58 commit 48d30c5

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
@@ -31,6 +31,10 @@ A brief description of the categories of changes:
3131
It would work well in cases to reduce merge conflicts.
3232

3333
### Fixed
34+
* (rules) Fixes build targets linking against `@rules_python//python/cc:current_py_cc_libs`
35+
in host platform builds on macOS, by editing the `LC_ID_DYLIB` field of the hermetic interpreter's
36+
`libpython3.x.dylib` using `install_name_tool`, setting it to its absolute path under Bazel's
37+
execroot.
3438
* (rules) Signals are properly received when using {obj}`--bootstrap_impl=script`
3539
(for non-zip builds).
3640
([#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)