From 942dddaaad9bf4fbb20b66535aad4fc464ad69ba Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Wed, 25 Jan 2023 02:11:50 -0800 Subject: [PATCH] Prepare backwards compatible usage of optional C++ toolchain By adding an alias and a no-op keyword argument now, rulesets can start using the new optional form of `find_cpp_toolchain` now and benefit from the toolchain actually being optional once that has been implemented in a future version of Bazel. Work towards #16966 Closes #17308. PiperOrigin-RevId: 504501621 Change-Id: I549fea6290195aadad4ccbd045c0aa3c180946d2 --- tools/cpp/BUILD.tools | 8 ++++++++ tools/cpp/toolchain_utils.bzl | 5 ++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/tools/cpp/BUILD.tools b/tools/cpp/BUILD.tools index 1c584df44ee6e5..1e292c3b130404 100644 --- a/tools/cpp/BUILD.tools +++ b/tools/cpp/BUILD.tools @@ -57,6 +57,14 @@ constraint_value( cc_toolchain_alias(name = "current_cc_toolchain") +# In future versions of Bazel, this target will not fail if no C++ toolchain is +# available. Instead, it will not advertise the cc_common.CcToolchainInfo +# provider. +alias( + name = "optional_current_cc_toolchain", + actual = ":current_cc_toolchain", +) + cc_host_toolchain_alias(name = "current_cc_host_toolchain") cc_libc_top_alias(name = "current_libc_top") diff --git a/tools/cpp/toolchain_utils.bzl b/tools/cpp/toolchain_utils.bzl index 23d461d4dd8e57..1583aedcc1873f 100644 --- a/tools/cpp/toolchain_utils.bzl +++ b/tools/cpp/toolchain_utils.bzl @@ -19,7 +19,7 @@ Utilities to help work with c++ toolchains. CPP_TOOLCHAIN_TYPE = "@bazel_tools//tools/cpp:toolchain_type" -def find_cpp_toolchain(ctx): +def find_cpp_toolchain(ctx, *, mandatory = True): """ Finds the c++ toolchain. @@ -29,6 +29,9 @@ def find_cpp_toolchain(ctx): Args: ctx: The rule context for which to find a toolchain. + mandatory: This is currently a no-op. In future releases of Bazel, if this + is set to False, this function will return None rather than fail if no + toolchain is found. Returns: A CcToolchainProvider.