Add libutil to sysroot configs for Rust toolchain compatibility#222
Add libutil to sysroot configs for Rust toolchain compatibility#222yliu120 wants to merge 1 commit into
Conversation
Rust's standard library (`std`) links against `-lutil`, which requires `libutil.so` to be available during linking. The hermetic sysroot configs for glibc < 2.34 contain `libutil` but do not declare it as a `cc_toolchain_import`, so the library files are not included in the Bazel sandbox during link actions. This causes `rust-lld: error: unable to find library -lutil` when using `rules_rust` with the hermetic CC toolchain. Add `cc_toolchain_import` for `libutil` and include it in `:syslibs` for all glibc < 2.34 sysroot configs (ubuntu18/ubuntu20, x86_64/aarch64). Not needed for ubuntu22 (glibc 2.35) where libutil was merged into libc. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
|
@yuriivcs Please help review this PR. I think the fundamental issue is that the rules_ml_toolchain defines the sysroot and the cc_import_toolchain. Then when using rules_ml_toolchain, the user side has no way to add additional toolchains without changing rules_ml_toolchain. I think if we add a new starlark, it can perhaps do that. Otherwise we need to upstream changes like this. |
|
@yliu120 Please add libutil to |
| shared_library = "usr/lib/aarch64-linux-gnu/libutil.so", | ||
| static_library = "usr/lib/aarch64-linux-gnu/libutil.a", | ||
| ) | ||
|
|
There was a problem hiding this comment.
Refactor the cc_toolchain_import invocation as follows:
cc_toolchain_import(
name = "util",
additional_libs = [
"lib/aarch64-linux-gnu/libutil-{glibc_version}.so".format(glibc_version = GLIBC_VERSION),
"lib/aarch64-linux-gnu/libutil.so.1",
"usr/lib/aarch64-linux-gnu/libutil.so",
"usr/lib/aarch64-linux-gnu/libutil.a",
],
)
Why?
Defining these as shared_library or static_library forces the toolchain to link them into every binary. Instead, we should only provide the library paths via the -L flag and populate the Bazel cache. This ensures that when -lutil is invoked, the linker can resolve the dependency without error.
| shared_library = "usr/lib/aarch64-linux-gnu/libutil.so", | ||
| static_library = "usr/lib/aarch64-linux-gnu/libutil.a", | ||
| ) | ||
|
|
| ], | ||
| shared_library = "usr/lib/x86_64-linux-gnu/libutil.so", | ||
| static_library = "usr/lib/x86_64-linux-gnu/libutil.a", | ||
| ) |
| shared_library = "usr/lib/x86_64-linux-gnu/libutil.so", | ||
| static_library = "usr/lib/x86_64-linux-gnu/libutil.a", | ||
| ) | ||
|
|
Hi @yliu120 |
Summary
cc_toolchain_importforlibutilin all 4 sysroot BUILD configs (x86_64 and aarch64, ubuntu18/gcc8.4 and ubuntu20/gcc10):utilto:syslibsdeps so it's available to downstream toolchainsstdlibrary links against-lutil(forforkpty/openptyin glibc), which fails in Bazel's hermetic sandbox without this declarationContext
When using
rules_rustwith the hermetic CC toolchain fromrules_ml_toolchain, Rust compilation fails with:The sysroot archives already contain
libutil.soandlibutil.a, but they are not declared ascc_toolchain_importtargets, so Bazel's sandbox doesn't make them visible to the linker.This only affects glibc < 2.34 (ubuntu18 and ubuntu20 configs). In glibc >= 2.34,
libutilwas merged intolibcitself, so ubuntu22+ configs don't need this change.Test plan
rules_rust0.61.0 against the patched sysroot configs🤖 Generated with Claude Code