Skip to content

Commit e54519b

Browse files
Abseil Teamcopybara-github
authored andcommitted
Put the fake Fuchsia SDK in a module extension
This allows users to override the fake SDK with a real one using https://bazel.build/rules/lib/globals/module#override_repo. Without this change, it is impossible for a project that depends on googletest as a `bazel_dep` to build tests using the "real" Fuchsia SDK, because any references to `@fuchsia_sdk` within googletest `BUILD.bazel` files unconditionally resolve to the "fake" Fuchsia SDK. With this change, if you have the real Fuchsia SDK declared in your `MODULE.bazel`, you can add the following lines to coerce googletest to use the real Fuchsia SDK as well: fake_fuchsia_sdk_extension = use_extension("@com_google_googletest//:fake_fuchsia_sdk.bzl", "fuchsia_sdk") override_repo(fake_fuchsia_sdk_extension, "fuchsia_sdk") PiperOrigin-RevId: 709139784 Change-Id: I4d10d441c76b7a2481f15723a24f11525dba3878
1 parent f3c355f commit e54519b

File tree

2 files changed

+59
-22
lines changed

2 files changed

+59
-22
lines changed

MODULE.bazel

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,38 @@ module(
3939
# Only direct dependencies need to be listed below.
4040
# Please keep the versions in sync with the versions in the WORKSPACE file.
4141

42-
bazel_dep(name = "abseil-cpp",
43-
version = "20240116.2")
44-
45-
bazel_dep(name = "platforms",
46-
version = "0.0.10")
47-
48-
bazel_dep(name = "re2",
49-
version = "2024-07-02")
42+
bazel_dep(
43+
name = "abseil-cpp",
44+
version = "20240116.2",
45+
)
46+
bazel_dep(
47+
name = "platforms",
48+
version = "0.0.10",
49+
)
50+
bazel_dep(
51+
name = "re2",
52+
version = "2024-07-02",
53+
)
5054

51-
bazel_dep(name = "rules_python",
52-
version = "0.34.0",
53-
dev_dependency = True)
55+
bazel_dep(
56+
name = "rules_python",
57+
version = "0.34.0",
58+
dev_dependency = True,
59+
)
5460

5561
# https://rules-python.readthedocs.io/en/stable/toolchains.html#library-modules-with-dev-only-python-usage
5662
python = use_extension(
5763
"@rules_python//python/extensions:python.bzl",
5864
"python",
59-
dev_dependency = True
65+
dev_dependency = True,
66+
)
67+
python.toolchain(
68+
ignore_root_user_error = True,
69+
is_default = True,
70+
python_version = "3.12",
6071
)
6172

62-
python.toolchain(python_version = "3.12",
63-
is_default = True,
64-
ignore_root_user_error = True)
65-
66-
fake_fuchsia_sdk = use_repo_rule("//:fake_fuchsia_sdk.bzl", "fake_fuchsia_sdk")
67-
fake_fuchsia_sdk(name = "fuchsia_sdk")
73+
# See fake_fuchsia_sdk.bzl for instructions on how to override this with a real SDK, if needed.
74+
fuchsia_sdk = use_extension("//:fake_fuchsia_sdk.bzl", "fuchsia_sdk")
75+
fuchsia_sdk.create_fake()
76+
use_repo(fuchsia_sdk, "fuchsia_sdk")

fake_fuchsia_sdk.bzl

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
"""Provides a fake @fuchsia_sdk implementation that's used when the real one isn't available.
22
3-
This is needed since bazel queries on targets that depend on //:gtest (eg:
4-
`bazel query "deps(set(//googletest/test:gtest_all_test))"`) will fail if @fuchsia_sdk is not
5-
defined when bazel is evaluating the transitive closure of the query target.
3+
GoogleTest can be used with the [Fuchsia](https://fuchsia.dev/) SDK. However,
4+
because the Fuchsia SDK does not yet support bzlmod, GoogleTest's `MODULE.bazel`
5+
file by default provides a "fake" Fuchsia SDK.
66
7-
See https://github.com/google/googletest/issues/4472.
7+
To override this and use the real Fuchsia SDK, you can add the following to your
8+
project's `MODULE.bazel` file:
9+
10+
fake_fuchsia_sdk_extension =
11+
use_extension("@com_google_googletest//:fake_fuchsia_sdk.bzl", "fuchsia_sdk")
12+
override_repo(fake_fuchsia_sdk_extension, "fuchsia_sdk")
13+
14+
NOTE: The `override_repo` built-in is only available in Bazel 8.0 and higher.
15+
16+
See https://github.com/google/googletest/issues/4472 for more details of why the
17+
fake Fuchsia SDK is needed.
818
"""
919

1020
def _fake_fuchsia_sdk_impl(repo_ctx):
@@ -31,3 +41,21 @@ fake_fuchsia_sdk = repository_rule(
3141
),
3242
},
3343
)
44+
45+
_create_fake = tag_class()
46+
47+
def _fuchsia_sdk_impl(module_ctx):
48+
create_fake_sdk = False
49+
for mod in module_ctx.modules:
50+
for _ in mod.tags.create_fake:
51+
create_fake_sdk = True
52+
53+
if create_fake_sdk:
54+
fake_fuchsia_sdk(name = "fuchsia_sdk")
55+
56+
return module_ctx.extension_metadata(reproducible = True)
57+
58+
fuchsia_sdk = module_extension(
59+
implementation = _fuchsia_sdk_impl,
60+
tag_classes = {"create_fake": _create_fake},
61+
)

0 commit comments

Comments
 (0)