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

Adding Uber's hermetic cc toolchain for tests #3609

Merged
merged 10 commits into from
Jul 2, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
15 changes: 15 additions & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ tasks:
- "//..."
test_targets:
- "//..."
debian11_zig_cc:
platform: debian11
shell_commands:
- tests/core/cgo/generate_imported_dylib.sh
build_flags:
- "--config=incompatible"
- "--extra_toolchains=@zig_sdk//toolchain:linux_amd64_gnu.2.31"
test_flags:
- "--config=incompatible"
- "--extra_toolchains=@zig_sdk//toolchain:linux_amd64_gnu.2.31"
- "--test_env=ZIG_CC=1"
build_targets:
- "//..."
test_targets:
- "//..."
bcr_tests:
name: BCR test module
platform: ${{ platform }}
Expand Down
14 changes: 12 additions & 2 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
common --enable_platform_specific_config
test --test_output=errors

# Go requires a C toolchain that accepts options and emits errors like

linzhp marked this conversation as resolved.
Show resolved Hide resolved
# gcc or clang. The Go SDK does not support MSVC.

build:windows --cpu=x64_windows
build:windows --compiler=mingw-gcc

# NOTE(bazelbuild/bazel#10529): bazel doesn't register the mingw toolchain automatically.

# We also need the host and target platforms to have the mingw constraint value.

build:windows --extra_toolchains=@local_config_cc//:cc-toolchain-x64_windows_mingw
build:windows --host_platform=@io_bazel_rules_go//go/toolchain:windows_amd64_cgo
build:windows --platforms=@io_bazel_rules_go//go/toolchain:windows_amd64_cgo
Expand All @@ -19,16 +25,20 @@ build:ci --sandbox_debug
build:ci --spawn_strategy=standalone
build:ci --genrule_strategy=standalone
test:ci --test_strategy=standalone
test:ci --test_output=errors

# Incompatible flags to test in a dedicated CI pipeline.

build:incompatible --incompatible_load_proto_rules_from_bzl
build:incompatible --incompatible_enable_cc_toolchain_resolution
build:incompatible --incompatible_config_setting_private_default_visibility
build:incompatible --incompatible_enforce_config_setting_visibility
build:incompatible --incompatible_disallow_empty_glob
build:incompatible --incompatible_disable_starlark_host_transitions

# Also enable all incompatible flags in go_bazel_test by default.

# TODO: Add --incompatible_disallow_empty_glob once
# https://github.com/bazelbuild/bazel-gazelle/pull/1405 has been released.

# https://github.com/bazelbuild/bazel-gazelle/pull/1405 has been released.

test:incompatible --test_env=GO_BAZEL_TEST_BAZELFLAGS='--incompatible_load_proto_rules_from_bzl --incompatible_enable_cc_toolchain_resolution --incompatible_config_setting_private_default_visibility --incompatible_enforce_config_setting_visibility --incompatible_disable_starlark_host_transitions'
21 changes: 21 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,24 @@ load(
)

apple_support_dependencies()

# For testing the compatibility with a hermetic cc toolchain. Users should not have to enable it.

http_archive(
name = "hermetic_cc_toolchain",
sha256 = "bd2234acd0837251361be3270d7d3ce599b418be123d902d84762302e31a3014",
strip_prefix = "hermetic_cc_toolchain-13c904dce0cb9b6d07f0d557e6ce3cf7013a562e",
urls = ["https://github.com/uber/hermetic_cc_toolchain/archive/13c904dce0cb9b6d07f0d557e6ce3cf7013a562e.zip"],
linzhp marked this conversation as resolved.
Show resolved Hide resolved
)

load("@hermetic_cc_toolchain//toolchain:defs.bzl", zig_toolchains = "toolchains")

zig_toolchains(
host_platform_sha256 = {
"linux-aarch64": "12be476ed53c219507e77737dbb7f2a77b280760b8acbc6ba2eaaeb42b7d145e",
"linux-x86_64": "1b1c115c4ccbdc215cc3b07833c7957336d9f5fff816f97e5cafee556a9d8be8",
"macos-aarch64": "3943612c560dd066fba5698968317a146a0f585f6cdaa1e7c1df86685c7c4eaf",
"macos-x86_64": "0c89e5d934ecbf9f4d2dea6e3b8dfcc548a3d4184a856178b3db74e361031a2b",
},
version = "0.11.0-dev.3886+0c1bfe271",
)
57 changes: 53 additions & 4 deletions tests/core/cgo/cc_libs_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package cc_libs_test

import (
"debug/elf"
"os"
"path"
"strings"
"testing"
Expand All @@ -30,16 +31,16 @@ func TestBinaries(t *testing.T) {
}{
{
shortPath: "tests/core/cgo/pure_bin",
wantLibs: map[string]bool{"libc": false, "libstdc++": false},
wantLibs: map[string]bool{"libc": false},
}, {
shortPath: "tests/core/cgo/c_srcs_bin",
wantLibs: map[string]bool{"libc": true, "libstdc++": false},
wantLibs: map[string]bool{"libc": true},
}, {
shortPath: "tests/core/cgo/cc_srcs_bin",
wantLibs: map[string]bool{"libc": true, "libstdc++": true},
wantLibs: map[string]bool{"libc": true},
}, {
shortPath: "tests/core/cgo/cc_deps_bin",
wantLibs: map[string]bool{"libc": true, "libstdc++": true},
wantLibs: map[string]bool{"libc": true},
},
} {
t.Run(path.Base(test.shortPath), func(t *testing.T) {
Expand Down Expand Up @@ -67,6 +68,54 @@ func TestBinaries(t *testing.T) {
}
}

// zig cc statically links libstdc++: https://github.com/ziglang/zig/issues/12147
// Consider merging this into TestBinaries once the above issue is resolved.
func TestBinaries_libstdcpp(t *testing.T) {
if os.Getenv("ZIG_CC") == "1" {
t.Skip()
}

for _, test := range []struct {
shortPath string
wantLibs map[string]bool
}{
{
shortPath: "tests/core/cgo/pure_bin",
wantLibs: map[string]bool{"libstdc++": false},
}, {
shortPath: "tests/core/cgo/c_srcs_bin",
wantLibs: map[string]bool{"libstdc++": false},
}, {
shortPath: "tests/core/cgo/cc_srcs_bin",
wantLibs: map[string]bool{"libstdc++": true},
}, {
shortPath: "tests/core/cgo/cc_deps_bin",
wantLibs: map[string]bool{"libstdc++": true},
},
} {
t.Run(path.Base(test.shortPath), func(t *testing.T) {
libs, err := listLibs(test.shortPath)
if err != nil {
t.Fatal(err)
}
haveLibs := make(map[string]bool)
for _, lib := range libs {
haveLibs[lib] = true
}
for haveLib := range haveLibs {
if wantLib, ok := test.wantLibs[haveLib]; ok && !wantLib {
t.Errorf("unexpected dependency on library %q", haveLib)
}
}
for wantLib, want := range test.wantLibs {
if want && !haveLibs[wantLib] {
t.Errorf("wanted dependency on library %q", wantLib)
}
}
})
}
}

func listLibs(shortPath string) ([]string, error) {
binPath, err := bazel.Runfile(shortPath)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions tests/core/go_binary/static_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@ package static_cgo_test

import (
"debug/elf"
"os"
"testing"

"github.com/bazelbuild/rules_go/go/tools/bazel"
)

func TestStatic(t *testing.T) {
for _, name := range []string{"static_bin", "static_cgo_bin", "static_pure_bin"} {
if name != "static_pure_bin" && os.Getenv("ZIG_CC") == "1" {
// zig does not statically link glibc, by design or accident.
t.Skip()
}

t.Run(name, func(t *testing.T) {
path, ok := bazel.FindBinary("tests/core/go_binary", name)
if !ok {
Expand Down