Skip to content

Commit 9d55180

Browse files
alexeaglefmeum
andauthored
chore: ensure we don't try to compile any C++ code (#16)
* chore: ensure we don't try to compile any C++ code We don't have any C++ code in this repo, and want to make sure that our examples don't transitively depend on building protoc from source. * Update MODULE.bazel Co-authored-by: Fabian Meumertzheim <[email protected]> * Disable CGo (#17) * chore: buildifier --------- Co-authored-by: Fabian Meumertzheim <[email protected]>
1 parent c72c433 commit 9d55180

File tree

5 files changed

+69
-1
lines changed

5 files changed

+69
-1
lines changed

.bazelrc

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ common --action_env=RULES_PYTHON_ENABLE_PYSTAR=0
1414
# https://bazelbuild.slack.com/archives/C014RARENH0/p1691158021917459?thread_ts=1691156601.420349&cid=C014RARENH0
1515
common --check_direct_dependencies=off
1616

17+
# Force rules_go to disable CGO even though we have a (fake) C++ toolchain registered.
18+
common --host_platform=//:no_cgo_host_platform
19+
1720
# Load any settings specific to the current user.
1821
# .bazelrc.user should appear in .gitignore so that settings are not shared with team members
1922
# This needs to be last statement in this

BUILD.bazel

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
load("@platforms//host:constraints.bzl", "HOST_CONSTRAINTS")
2+
3+
platform(
4+
name = "no_cgo_host_platform",
5+
constraint_values = HOST_CONSTRAINTS + [
6+
"@rules_go//go/toolchain:cgo_off",
7+
],
8+
)

MODULE.bazel

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module(
99
bazel_dep(name = "bazel_features", version = "1.9.0")
1010
bazel_dep(name = "bazel_skylib", version = "1.4.1")
1111
bazel_dep(name = "rules_proto", version = "6.0.0")
12-
bazel_dep(name = "platforms", version = "0.0.8")
12+
bazel_dep(name = "platforms", version = "0.0.10")
1313

1414
protoc = use_extension("//protoc:extensions.bzl", "protoc")
1515
protoc.toolchain(
@@ -20,6 +20,12 @@ use_repo(protoc, "com_google_protobuf", "toolchains_protoc_hub")
2020

2121
register_toolchains("@toolchains_protoc_hub//:all")
2222

23+
# Assert no CC compilation occurs
24+
register_toolchains(
25+
"//tools/toolchains:all",
26+
dev_dependency = True,
27+
)
28+
2329
bazel_dep(name = "aspect_bazel_lib", version = "1.32.1", dev_dependency = True)
2430
bazel_dep(name = "buildifier_prebuilt", version = "6.1.2", dev_dependency = True)
2531
bazel_dep(name = "aspect_rules_py", version = "0.7.1", dev_dependency = True)

tools/toolchains/BUILD.bazel

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""Define a non-functional cc toolchain.
2+
3+
To fail-fast in cases where we are forced to compile third-party C++ code,
4+
define a cc toolchain that doesn't work, by using 'false' as the compiler.
5+
See https://bazel.build/tutorials/ccp-toolchain-config
6+
"""
7+
8+
load("defs.bzl", "cc_toolchain_config")
9+
10+
filegroup(name = "empty")
11+
12+
cc_toolchain_config(name = "noop_toolchain_config")
13+
14+
cc_toolchain(
15+
name = "noop_toolchain",
16+
all_files = ":empty",
17+
compiler_files = ":empty",
18+
dwp_files = ":empty",
19+
linker_files = ":empty",
20+
objcopy_files = ":empty",
21+
strip_files = ":empty",
22+
supports_param_files = 0,
23+
toolchain_config = ":noop_toolchain_config",
24+
toolchain_identifier = "noop-toolchain",
25+
)
26+
27+
toolchain(
28+
name = "cc_toolchain",
29+
toolchain = ":noop_toolchain",
30+
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
31+
)

tools/toolchains/defs.bzl

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"Configure a cc toolchain to call 'false' if used."
2+
3+
def _impl(ctx):
4+
return cc_common.create_cc_toolchain_config_info(
5+
ctx = ctx,
6+
toolchain_identifier = "noop-toolchain",
7+
host_system_name = "local",
8+
target_system_name = "local",
9+
target_cpu = "k8",
10+
target_libc = "unknown",
11+
compiler = "false",
12+
abi_version = "unknown",
13+
abi_libc_version = "unknown",
14+
)
15+
16+
cc_toolchain_config = rule(
17+
implementation = _impl,
18+
attrs = {},
19+
provides = [CcToolchainConfigInfo],
20+
)

0 commit comments

Comments
 (0)