Skip to content

Add libutil to sysroot configs for Rust toolchain compatibility#222

Open
yliu120 wants to merge 1 commit into
google-ml-infra:mainfrom
yliu120:add-libutil-to-sysroot
Open

Add libutil to sysroot configs for Rust toolchain compatibility#222
yliu120 wants to merge 1 commit into
google-ml-infra:mainfrom
yliu120:add-libutil-to-sysroot

Conversation

@yliu120
Copy link
Copy Markdown
Contributor

@yliu120 yliu120 commented Mar 20, 2026

Summary

  • Add cc_toolchain_import for libutil in all 4 sysroot BUILD configs (x86_64 and aarch64, ubuntu18/gcc8.4 and ubuntu20/gcc10)
  • Add :util to :syslibs deps so it's available to downstream toolchains
  • This is needed because Rust's std library links against -lutil (for forkpty/openpty in glibc), which fails in Bazel's hermetic sandbox without this declaration

Context

When using rules_rust with the hermetic CC toolchain from rules_ml_toolchain, Rust compilation fails with:

rust-lld: error: unable to find library -lutil

The sysroot archives already contain libutil.so and libutil.a, but they are not declared as cc_toolchain_import targets, 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, libutil was merged into libc itself, so ubuntu22+ configs don't need this change.

Test plan

  • Verified that all 4 modified sysroot BUILD files follow the same pattern as existing library declarations
  • Confirmed library paths match the actual sysroot archive contents for each platform/glibc combination
  • Tested Rust compilation with rules_rust 0.61.0 against the patched sysroot configs

🤖 Generated with Claude Code

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>
@google-cla
Copy link
Copy Markdown

google-cla Bot commented Mar 20, 2026

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.

@yliu120
Copy link
Copy Markdown
Contributor Author

yliu120 commented Mar 20, 2026

@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.

@yuriivcs
Copy link
Copy Markdown
Collaborator

@yliu120 Please add libutil to x86_64_ubuntu22_gcc12 sysroot

shared_library = "usr/lib/aarch64-linux-gnu/libutil.so",
static_library = "usr/lib/aarch64-linux-gnu/libutil.a",
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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",
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

],
shared_library = "usr/lib/x86_64-linux-gnu/libutil.so",
static_library = "usr/lib/x86_64-linux-gnu/libutil.a",
)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

shared_library = "usr/lib/x86_64-linux-gnu/libutil.so",
static_library = "usr/lib/x86_64-linux-gnu/libutil.a",
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

@yuriivcs
Copy link
Copy Markdown
Collaborator

yuriivcs commented Mar 24, 2026

@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.

Hi @yliu120
This shouldn't be an issue, as projects can leverage register_toolchain to select only the specific toolchains they require from rules_ml_toolchain. Additionally, we aren't aiming for a comprehensive set of system libraries immediately; instead, we are taking an incremental approach, adding libraries as specific needs arise.

@yuriivcs yuriivcs self-requested a review March 24, 2026 03:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants