diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml index 9908185c4a..708baab8fe 100644 --- a/.bazelci/presubmit.yml +++ b/.bazelci/presubmit.yml @@ -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 }} diff --git a/.bazelrc b/.bazelrc index d8b82d91dd..03b8a6c348 100644 --- a/.bazelrc +++ b/.bazelrc @@ -1,9 +1,11 @@ common --enable_platform_specific_config +test --test_output=errors # Go requires a C toolchain that accepts options and emits errors like # 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 @@ -19,7 +21,6 @@ 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 @@ -30,5 +31,5 @@ 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' diff --git a/WORKSPACE b/WORKSPACE index 3e894e07ec..2e5b09296d 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -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"], +) + +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", +) diff --git a/tests/core/cgo/cc_libs_linux_test.go b/tests/core/cgo/cc_libs_linux_test.go index 204a8fcfb9..1ed22f864c 100644 --- a/tests/core/cgo/cc_libs_linux_test.go +++ b/tests/core/cgo/cc_libs_linux_test.go @@ -16,6 +16,7 @@ package cc_libs_test import ( "debug/elf" + "os" "path" "strings" "testing" @@ -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) { @@ -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 { diff --git a/tests/core/go_binary/static_test.go b/tests/core/go_binary/static_test.go index 7c23cdae28..e66cfb084e 100644 --- a/tests/core/go_binary/static_test.go +++ b/tests/core/go_binary/static_test.go @@ -18,6 +18,7 @@ package static_cgo_test import ( "debug/elf" + "os" "testing" "github.com/bazelbuild/rules_go/go/tools/bazel" @@ -25,6 +26,11 @@ import ( 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 {