-
-
Notifications
You must be signed in to change notification settings - Fork 668
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Make C++ toolchain optional Currently, if you transition a go_binary, you must have a C++ toolchain for it even if it's building in pure mode without cgo. By making this toolchain optional, we push the error from a toolchain-resolution-time error to an analysis-time error only if a toolchain is both _needed_ and not found. * Add tests * Add target that actually uses cgo * Switch to use bazel_features Also switch from @bazel_tools//tools/cpp:current_cc_toolchain to @bazel_tools//tools/cpp:optional_current_cc_toolchain because otherwise we still get a hard error if the toolchain can't be found. * Review comments
- Loading branch information
1 parent
0a6311c
commit b5d0db5
Showing
9 changed files
with
174 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# bazel_features when used from a WORKSPACE rather than bzlmod context requires a two-step set-up (loading bazel_features, then calling a function from inside it). | ||
# rules_go only has one-step set-up, and it would be a breaking change to require a second step. | ||
# Accordingly, we supply a polyfill implementation of bazel_features which is only used when using rules_go from a WORKSPACE file, | ||
# to avoid complicating code in rules_go itself. | ||
# We just implement the checks we've seen we actually need, and hope to delete this completely when we are in a pure-bzlmod world. | ||
|
||
_POLYFILL_BAZEL_FEATURES = """bazel_features = struct( | ||
cc = struct( | ||
find_cpp_toolchain_has_mandatory_param = {find_cpp_toolchain_has_mandatory_param}, | ||
) | ||
) | ||
""" | ||
|
||
def _polyfill_bazel_features_impl(rctx): | ||
# An empty string is treated as a "dev version", which is greater than anything. | ||
bazel_version = native.bazel_version or "999999.999999.999999" | ||
version_parts = bazel_version.split(".") | ||
if len(version_parts) != 3: | ||
fail("invalid Bazel version '{}': got {} dot-separated segments, want 3".format(bazel_version, len(version_parts))) | ||
major_version_int = int(version_parts[0]) | ||
minor_version_int = int(version_parts[1]) | ||
|
||
find_cpp_toolchain_has_mandatory_param = major_version_int > 6 or (major_version_int == 6 and minor_version_int >= 1) | ||
|
||
rctx.file("BUILD.bazel", """exports_files(["features.bzl"]) | ||
""") | ||
rctx.file("features.bzl", _POLYFILL_BAZEL_FEATURES.format( | ||
find_cpp_toolchain_has_mandatory_param = repr(find_cpp_toolchain_has_mandatory_param), | ||
)) | ||
|
||
polyfill_bazel_features = repository_rule( | ||
implementation = _polyfill_bazel_features_impl, | ||
# Force reruns on server restarts to keep native.bazel_version up-to-date. | ||
local = True, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
load("@io_bazel_rules_go//go/private:common.bzl", "GO_TOOLCHAIN") | ||
load(":cgo_test.bzl", "cgo_test_suite") | ||
|
||
constraint_value( | ||
name = "os_does_not_exist", | ||
constraint_setting = "@platforms//os:os", | ||
) | ||
|
||
# Make a platform we know won't have a C++ toolchain registered for it. | ||
platform( | ||
name = "platform_has_no_cc_toolchain", | ||
constraint_values = [":os_does_not_exist"], | ||
) | ||
|
||
# Make a fake Go toolchain for this platform | ||
toolchain( | ||
name = "fake_go_toolchain", | ||
target_compatible_with = [ | ||
":os_does_not_exist", | ||
], | ||
toolchain = "@go_sdk//:go_linux_amd64-impl", | ||
toolchain_type = GO_TOOLCHAIN, | ||
) | ||
|
||
cgo_test_suite() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") | ||
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_cross_binary") | ||
|
||
def _missing_cc_toolchain_explicit_pure_off_test(ctx): | ||
env = analysistest.begin(ctx) | ||
|
||
asserts.expect_failure(env, "has pure explicitly set to off, but no C++ toolchain could be found for its platform") | ||
|
||
return analysistest.end(env) | ||
|
||
missing_cc_toolchain_explicit_pure_off_test = analysistest.make( | ||
_missing_cc_toolchain_explicit_pure_off_test, | ||
expect_failure = True, | ||
config_settings = { | ||
"//command_line_option:extra_toolchains": str(Label("//tests/core/starlark/cgo:fake_go_toolchain")), | ||
}, | ||
) | ||
|
||
def cgo_test_suite(): | ||
go_binary( | ||
name = "cross_impure", | ||
srcs = ["main.go"], | ||
pure = "off", | ||
tags = ["manual"], | ||
) | ||
|
||
go_cross_binary( | ||
name = "go_cross_impure_cgo", | ||
platform = ":platform_has_no_cc_toolchain", | ||
target = ":cross_impure", | ||
tags = ["manual"], | ||
) | ||
|
||
missing_cc_toolchain_explicit_pure_off_test( | ||
name = "missing_cc_toolchain_explicit_pure_off_test", | ||
target_under_test = ":go_cross_impure_cgo", | ||
) | ||
|
||
"""Creates the test targets and test suite for cgo.bzl tests.""" | ||
native.test_suite( | ||
name = "cgo_tests", | ||
tests = [":missing_cc_toolchain_explicit_pure_off_test"], | ||
) |