Skip to content

Commit

Permalink
Switch to use bazel_features
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
illicitonion authored and fmeum committed Dec 28, 2023
1 parent 9751277 commit 4e457d5
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
1 change: 1 addition & 0 deletions go/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ bzl_library(
"//go/platform:apple",
"//go/private:go_toolchain",
"//go/private/rules:transition",
"@bazel_features//:features.bzl",
"@bazel_skylib//lib:paths",
"@bazel_skylib//rules:common_settings",
"@bazel_tools//tools/build_defs/cc:action_names.bzl",
Expand Down
13 changes: 9 additions & 4 deletions go/private/context.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load("@bazel_features//:features.bzl", "bazel_features")
load(
"@bazel_tools//tools/cpp:toolchain_utils.bzl",
"find_cpp_toolchain",
Expand Down Expand Up @@ -643,8 +644,12 @@ def _cgo_context_data_impl(ctx):
# toolchain (to be inputs into actions that need it).
# ctx.files._cc_toolchain won't work when cc toolchain resolution
# is switched on.
cc_toolchain = find_cpp_toolchain(ctx)
if cc_toolchain.compiler in _UNSUPPORTED_C_COMPILERS:
find_cpp_toolchain_kwargs = {}
if bazel_features.cc.find_cpp_toolchain_has_mandatory_param:
find_cpp_toolchain_kwargs["mandatory"] = False

cc_toolchain = find_cpp_toolchain(ctx, **find_cpp_toolchain_kwargs)
if not cc_toolchain or cc_toolchain.compiler in _UNSUPPORTED_C_COMPILERS:
return []

feature_configuration = cc_common.configure_features(
Expand Down Expand Up @@ -833,7 +838,7 @@ def _cgo_context_data_impl(ctx):
cgo_context_data = rule(
implementation = _cgo_context_data_impl,
attrs = {
"_cc_toolchain": attr.label(default = "@bazel_tools//tools/cpp:current_cc_toolchain"),
"_cc_toolchain": attr.label(default = "@bazel_tools//tools/cpp:optional_current_cc_toolchain" if bazel_features.cc.find_cpp_toolchain_has_mandatory_param else "@bazel_tools//tools/cpp:current_cc_toolchain"),
"_xcode_config": attr.label(
default = "@bazel_tools//tools/osx:current_xcode_config",
),
Expand All @@ -843,7 +848,7 @@ cgo_context_data = rule(
# But if we declare a mandatory toolchain dependency here, a cross-compiling C++ toolchain is required at toolchain resolution time.
# So we make this toolchain dependency optional, so that it's only attempted to be looked up if it's actually needed.
# Optional toolchain support was added in bazel 6.0.0.
config_common.toolchain_type("@bazel_tools//tools/cpp:toolchain_type", mandatory = False) if hasattr(config_common, "toolchain_type") else "@bazel_tools//tools/cpp:toolchain_type",
config_common.toolchain_type("@bazel_tools//tools/cpp:toolchain_type", mandatory = False) if bazel_features.cc.find_cpp_toolchain_has_mandatory_param else "@bazel_tools//tools/cpp:toolchain_type",
],
fragments = ["apple", "cpp"],
doc = """Collects information about the C/C++ toolchain. The C/C++ toolchain
Expand Down
35 changes: 35 additions & 0 deletions go/private/fake_bazel_features.bzl
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 fake 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.

_FAKE_BAZEL_FEATURES = """bazel_features = struct(
cc = struct(
find_cpp_toolchain_has_mandatory_param = {find_cpp_toolchain_has_mandatory_param},
)
)
"""

def _fake_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", _FAKE_BAZEL_FEATURES.format(
find_cpp_toolchain_has_mandatory_param = repr(find_cpp_toolchain_has_mandatory_param),
))

fake_bazel_features = repository_rule(
implementation = _fake_bazel_features_impl,
# Force reruns on server restarts to keep native.bazel_version up-to-date.
local = True,
)
6 changes: 6 additions & 0 deletions go/private/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# Once nested repositories work, this file should cease to exist.

load("//go/private:common.bzl", "MINIMUM_BAZEL_VERSION")
load("//go/private:fake_bazel_features.bzl", "fake_bazel_features")
load("//go/private/skylib/lib:versions.bzl", "versions")
load("//go/private:nogo.bzl", "DEFAULT_NOGO", "go_register_nogo")
load("//proto:gogo.bzl", "gogo_special_proto")
Expand Down Expand Up @@ -284,6 +285,11 @@ def go_rules_dependencies(force = False):
nogo = DEFAULT_NOGO,
)

_maybe(
fake_bazel_features,
name = "bazel_features",
)

def _maybe(repo_rule, name, **kwargs):
if name not in native.existing_rules():
repo_rule(name = name, **kwargs)
Expand Down

0 comments on commit 4e457d5

Please sign in to comment.