rules_patchelf
rules_patchelf
Pitch
Rules to
patchelf
the ELF interpreter and a launcher script. Can be used with downloaded Linux packages.
API
load("@rules_patchelf//patchelf/interpreter:defs.bzl", "patchelf_interpreter")
load("@rules_patchelf//patchelf/interpreter/unpack:defs.bzl", "patchelf_interpreter_unpack")
load("@rules_patchelf//patchelf/interpreter/launcher:defs.bzl", "patchelf_interpreter_launcher")
load("@rules_patchelf//patchelf/interpreter/launcher/library/path:defs.bzl", "patchelf_interpreter_launcher_library_path")
# Downloaded AMD64 Debian packages
# The downloaded packages could come from (a theoretical) `rules_apt`
filegroup(
name = "srcs-amd64",
srcs = ["@amd64-libc//data.tar.xz", "@amd64-abc//:data.tar.xz"]
)
# A CPU selector for the packages
# This would also be in (a theoretical) `rules_apt`
alias(
name = "srcs",
actual = select({
"@plaforms//cpu:x86_64", ":srcs-amd64",
}),
)
# Provides `PatchelfInterpreter`
patchelf_interpreter(
name = "interpreter-amd64",
path = "lib/x86_64-linux-gnu/ld-linux-x86-64.so.2",
)
# Describes the correct interpreter path for the downloaded packages
# We would likely have a built-in `@patchelf//patchelf/interpreter:debian` target
alias(
name = "interpreter",
actual = select({
"@plaforms//cpu:x86_64", ":interpreter-amd64",
}),
)
# Takes some downloaded Debian archives, unpacks them, patches the ELF interpreter and provides a `TreeArtifact` output
# Provides `PatchelfInterpreter` that describes the patched interpreter location
patchelf_interpreter_unpack(
name = "patched",
srcs = [":srcs"],
interpreter = ":interpreter",
)
# `$ORIGIN` based `LD_LIBRARY_PATH`
# Provides `PatchelfInterpreterLauncherLibraryPath`
patchelf_interpreter_launcher_library_path(
name = "path-amd64",
paths = [
"${ORIGIN}/../lib/x86_64-linux-gnu",
"${ORIGIN}/../../lib/x86_64-linux-gnu",
],
)
# Describes the correct library paths for the downloaded packages
# We would likely have a built-in `@patchelf//patchelf/interpreter/launcher/library/path:debian` target
alias(
name = "path",
actual = select({
"@plaforms//cpu:x86_64", ":path-amd64",
}),
)
# A `bazel run` target that sets up the interpreter redirection and `LD_LIBRARY_PATH`
patchelf_interpreter_launcher(
name = "abc",
src = ":patched",
path = ":path",
# executable = "/sbin/foo" # overrides the `/usr/bin/{{name}}` path
)
We have implemented this as part of rules_labgrid
but it is generically useful so makes sense as a separate ruleset.
Usage
Runs the downloaded binary:
$ bazel run -- :abc
Edited by Matthew Clarkson