diff --git a/BUILD.bazel b/BUILD.bazel index b1e3b7fb..e407ae29 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -56,6 +56,12 @@ config_setting( constraint_values = ["@platforms//os:openbsd"], ) +# NOTE: Fuchsia is not an officially supported platform. +config_setting( + name = "fuchsia", + constraint_values = ["@platforms//os:fuchsia"], +) + config_setting( name = "msvc_compiler", flag_values = { @@ -147,6 +153,17 @@ cc_library( "@com_googlesource_code_re2//:re2", ], "//conditions:default": [], + }) + select({ + # `gtest-death-test.cc` has `EXPECT_DEATH` that spawns a process, + # expects it to crash and inspects its logs with the given matcher, + # so that's why these libraries are needed. + # Otherwise, builds targeting Fuchsia would fail to compile. + ":fuchsia": [ + "@fuchsia_sdk//pkg/fdio", + "@fuchsia_sdk//pkg/syslog", + "@fuchsia_sdk//pkg/zx", + ], + "//conditions:default": [], }), ) diff --git a/MODULE.bazel b/MODULE.bazel index d4ad2b3a..b81ebf04 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -53,5 +53,9 @@ bazel_dep(name = "re2", bazel_dep(name = "rules_python", version = "0.29.0") + +fake_fuchsia_sdk = use_repo_rule("//:fake_fuchsia_sdk.bzl", "fake_fuchsia_sdk") +fake_fuchsia_sdk(name = "fuchsia_sdk") + # https://github.com/bazelbuild/rules_python/blob/main/BZLMOD_SUPPORT.md#default-toolchain-is-not-the-local-system-python register_toolchains("@bazel_tools//tools/python:autodetecting_toolchain") diff --git a/fake_fuchsia_sdk.bzl b/fake_fuchsia_sdk.bzl new file mode 100644 index 00000000..2024dc6c --- /dev/null +++ b/fake_fuchsia_sdk.bzl @@ -0,0 +1,33 @@ +"""Provides a fake @fuchsia_sdk implementation that's used when the real one isn't available. + +This is needed since bazel queries on targets that depend on //:gtest (eg: +`bazel query "deps(set(//googletest/test:gtest_all_test))"`) will fail if @fuchsia_sdk is not +defined when bazel is evaluating the transitive closure of the query target. + +See https://github.com/google/googletest/issues/4472. +""" + +def _fake_fuchsia_sdk_impl(repo_ctx): + for stub_target in repo_ctx.attr._stub_build_targets: + stub_package = stub_target + stub_target_name = stub_target.split("/")[-1] + repo_ctx.file("%s/BUILD.bazel" % stub_package, """ +filegroup( + name = "%s", +) +""" % stub_target_name) + +fake_fuchsia_sdk = repository_rule( + doc = "Used to create a fake @fuchsia_sdk repository with stub build targets.", + implementation = _fake_fuchsia_sdk_impl, + attrs = { + "_stub_build_targets": attr.string_list( + doc = "The stub build targets to initialize.", + default = [ + "pkg/fdio", + "pkg/syslog", + "pkg/zx", + ], + ), + }, +) diff --git a/googletest_deps.bzl b/googletest_deps.bzl index 8958890c..1b7d2c8b 100644 --- a/googletest_deps.bzl +++ b/googletest_deps.bzl @@ -1,6 +1,7 @@ """Load dependencies needed to use the googletest library as a 3rd-party consumer.""" load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") +load("//:fake_fuchsia_sdk.bzl", "fake_fuchsia_sdk") def googletest_deps(): """Loads common dependencies needed to use the googletest library.""" @@ -20,3 +21,8 @@ def googletest_deps(): strip_prefix = "abseil-cpp-20240116.0", urls = ["https://github.com/abseil/abseil-cpp/releases/download/20240116.0/abseil-cpp-20240116.0.tar.gz"], ) + + if not native.existing_rule("fuchsia_sdk"): + fake_fuchsia_sdk( + name = "fuchsia_sdk", + )