Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to run tests on Linux #26

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .bcr/presubmit.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
bcr_test_module:
module_path: "bcr_test"
matrix:
platform: ["centos7", "debian10", "macos", "macos_arm64", "ubuntu2004"]
platform: ["debian10", "macos", "macos_arm64", "ubuntu2004"]
bazel: [6.x, 7.x]
tasks:
run_tests:
Expand Down
16 changes: 8 additions & 8 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -474,20 +474,20 @@ jobs:
set -euo pipefail


file "$BINARY_LINUX_X86_64" | grep ''statically linked'' || (echo "Binary
$BINARY_LINUX_X86_64 is not statically linked: $(file "$BINARY_LINUX_X86_64")"
file -L "$BINARY_LINUX_X86_64" | grep ''statically linked'' || (echo "Binary
$BINARY_LINUX_X86_64 is not statically linked: $(file -L "$BINARY_LINUX_X86_64")"
&& exit 1)

file "$BINARY_LINUX_X86_64" | grep ''x86-64'' || (echo "Binary $BINARY_LINUX_X86_64
is not x86-64: $(file "$BINARY_LINUX_X86_64")" && exit 1)
file -L "$BINARY_LINUX_X86_64" | grep ''x86-64'' || (echo "Binary $BINARY_LINUX_X86_64
is not x86-64: $(file -L "$BINARY_LINUX_X86_64")" && exit 1)


file "$BINARY_LINUX_AARCH64" | grep ''statically linked'' || (echo "Binary
$BINARY_LINUX_AARCH64 is not statically linked: $(file "$BINARY_LINUX_AARCH64")"
file -L "$BINARY_LINUX_AARCH64" | grep ''statically linked'' || (echo "Binary
$BINARY_LINUX_AARCH64 is not statically linked: $(file -L "$BINARY_LINUX_AARCH64")"
&& exit 1)

file "$BINARY_LINUX_AARCH64" | grep ''aarch64'' || (echo "Binary $BINARY_LINUX_AARCH64
is not aarch64: $(file "$BINARY_LINUX_AARCH64")" && exit 1)
file -L "$BINARY_LINUX_AARCH64" | grep ''aarch64'' || (echo "Binary $BINARY_LINUX_AARCH64
is not aarch64: $(file -L "$BINARY_LINUX_AARCH64")" && exit 1)


echo "All tests passed"
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@

This toolchain allows cross-compiling binaries for Linux from various platforms. It can be used to produce binaries which don't dynamically link `libc`, by statically linking `musl`'s `libc` implementation.

## Setup

Setup instructions are available with [each release](https://github.com/bazel-contrib/musl-toolchain/releases).

## Usage

The toolchain automatically enables the `fully_static_link` feature to produce statically linked binaries that run anywhere.
`cc_binary` targets with dynamic library dependencies (e.g. `dynamic_deps` or `cc_import`s) as well as `cc_test` targets with `--dynamic_mode` at its default value will be linked dynamically. Such binaries require the `musl` dynamic linker to be present at `/lib/ld-musl-<arch>.so.1` on the target system (i.e. the host when running `bazel test` on Linux).
Since the path to the dynamic linker is hardcoded into the binary as an absolute path, there is no way to supply it hermetically.

## Comparison with other `cc_toolchain` implementations

### [aspect-build/gcc-toolchain](https://github.com/aspect-build/gcc-toolchain)
Expand Down
8 changes: 4 additions & 4 deletions generate-actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,11 +564,11 @@ def load_musl_toolchains(extra_exec_compatible_with=[], extra_target_compatible_

set -euo pipefail

file "$BINARY_LINUX_X86_64" | grep 'statically linked' || (echo "Binary $BINARY_LINUX_X86_64 is not statically linked: $(file "$BINARY_LINUX_X86_64")" && exit 1)
file "$BINARY_LINUX_X86_64" | grep 'x86-64' || (echo "Binary $BINARY_LINUX_X86_64 is not x86-64: $(file "$BINARY_LINUX_X86_64")" && exit 1)
file -L "$BINARY_LINUX_X86_64" | grep 'statically linked' || (echo "Binary $BINARY_LINUX_X86_64 is not statically linked: $(file -L "$BINARY_LINUX_X86_64")" && exit 1)
file -L "$BINARY_LINUX_X86_64" | grep 'x86-64' || (echo "Binary $BINARY_LINUX_X86_64 is not x86-64: $(file -L "$BINARY_LINUX_X86_64")" && exit 1)

file "$BINARY_LINUX_AARCH64" | grep 'statically linked' || (echo "Binary $BINARY_LINUX_AARCH64 is not statically linked: $(file "$BINARY_LINUX_AARCH64")" && exit 1)
file "$BINARY_LINUX_AARCH64" | grep 'aarch64' || (echo "Binary $BINARY_LINUX_AARCH64 is not aarch64: $(file "$BINARY_LINUX_AARCH64")" && exit 1)
file -L "$BINARY_LINUX_AARCH64" | grep 'statically linked' || (echo "Binary $BINARY_LINUX_AARCH64 is not statically linked: $(file -L "$BINARY_LINUX_AARCH64")" && exit 1)
file -L "$BINARY_LINUX_AARCH64" | grep 'aarch64' || (echo "Binary $BINARY_LINUX_AARCH64 is not aarch64: $(file -L "$BINARY_LINUX_AARCH64")" && exit 1)

echo "All tests passed"
EOF
Expand Down
8 changes: 8 additions & 0 deletions musl-toolchain.BUILD.bazel.template
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ filegroup(

filegroup(name = "empty")

filegroup(
name = "dynamic_runtime_lib",
srcs = glob(["x86_64-linux-musl/lib/*.so*"]),
)

musl_cc_toolchain_config(name = "k8_musl_toolchain_config", target_arch = "{{target_arch}}")

cc_toolchain(
Expand All @@ -40,4 +45,7 @@ cc_toolchain(
supports_param_files = True,
toolchain_config = ":k8_musl_toolchain_config",
toolchain_identifier = "k8-musl-toolchain",
dynamic_runtime_lib = ":dynamic_runtime_lib",
# Linked in automatically by the toolchain.
static_runtime_lib = ":empty",
)
34 changes: 34 additions & 0 deletions musl_cc_toolchain_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,11 @@ def _impl(ctx):

dbg_feature = feature(name = "dbg")

static_link_cpp_runtimes_feature = feature(
name = "static_link_cpp_runtimes",
enabled = True,
)

fully_static_link_feature = feature(
name = "fully_static_link",
enabled = True,
Expand Down Expand Up @@ -345,6 +350,33 @@ def _impl(ctx):
],
)

# The default implementation of this feature uses the Google-internal
# $EXEC_ORIGIN, which is not available in the open-source dynamic linker.
runtime_library_search_directories_feature = feature(
name = "runtime_library_search_directories",
flag_sets = [
flag_set(
actions = all_link_actions,
flag_groups = [
flag_group(
iterate_over = "runtime_library_search_directories",
flag_groups = [
flag_group(
flags = [
"-Xlinker",
"-rpath",
"-Xlinker",
"$ORIGIN/%{runtime_library_search_directories}",
],
),
],
expand_if_available = "runtime_library_search_directories",
),
],
),
],
)

coverage_feature = feature(
name = "coverage",
provides = ["profile"],
Expand Down Expand Up @@ -408,6 +440,7 @@ def _impl(ctx):
features = [
random_seed_feature,
generate_linkmap_feature,
runtime_library_search_directories_feature,
coverage_feature,
default_compile_flags_feature,
default_link_flags_feature,
Expand All @@ -416,6 +449,7 @@ def _impl(ctx):
objcopy_embed_flags_feature,
opt_feature,
dbg_feature,
static_link_cpp_runtimes_feature,
fully_static_link_feature,
user_compile_flags_feature,
sysroot_feature,
Expand Down
Loading