Linking for a pre-compiled binary through bazel

Hi! I’m having trouble using GitHub - bazelbuild/rules_rust: Rust rules for Bazel because it downloads the compiled rustc, which has dependencies on shared libraries it can’t find:

❯ patchelf --print-needed ./rustc
librustc_driver-a07dcbb4ed0bdde8.so
libstd-c147cd9c030850ef.so
libpthread.so.0
libdl.so.2
librt.so.1
libc.so.6
ld-linux-x86-64.so.2
❯ bazel --version
bazel 3.3.1- (@non-git)

What can I do to make this work? (Complete example shown below)

❯ cat -p WORKSPACE
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

rules_rust_version="7004ca34cb3a4b4a0ab98bbc0174ef69eeeaf121"rules_rust_sha256="01fc6e235eaecb4c4e3d53200bd5a1a52ed9ccde88b2c16a9daa0fcfdb4f0e96"http_archive(
    name = "io_bazel_rules_rust",
    sha256 = rules_rust_sha256,
    strip_prefix = "rules_rust-" + rules_rust_version,    url = "https://github.com/bazelbuild/rules_rust/archive/" + rules_rust_version + ".tar.gz",
)
bazel_skylib_version="07922b040c3d8b8b0317e3dc687db625dd3e3cd4"bazel_skylib_sha256="1e7f5116e792b77e2ac4c84f65daf1d72461a3a7daf513aee34b4725f6547bd0"
http_archive(
    name = "bazel_skylib",
    sha256 = bazel_skylib_sha256,
    strip_prefix = "bazel-skylib-" + bazel_skylib_version,
    url = "https://github.com/bazelbuild/bazel-skylib/archive/" + bazel_skylib_version + ".tar.gz",
)
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
bazel_skylib_workspace()

load("@io_bazel_rules_rust//rust:repositories.bzl", "rust_repositories")
rust_repositories()

load("@io_bazel_rules_rust//:workspace.bzl", "bazel_version")
bazel_version(name = "bazel_version")

❯ cat -p BUILD
load("@io_bazel_rules_rust//rust:rust.bzl", "rust_binary")

rust_binary(
  name = "main",
  srcs = ["main.rs"],
)

❯ cat -p main.rs
fn main() {
    println!("Hello, world!");
}

You need patchelf to patch the downloaded rustcs interpreter.

I had a script on my computer that automatically found all executables in the bazel cache and patched them to the interpreter of the zsh in PATH, but I do not have access to that computer currently.

Here’s a one-liner I’m now using for future readers (unfortunate dependency on nix-index)

❯ fd -L . bazel-*/external | egrep "bin/[^/]+" | xargs -I {} patchelf --set-interpreter $(nix-locate ld-linux | head -n 1 | tr -s ' ' | cut -d' ' -f 4) {} 2>/dev/null
1 Like