From a2ba0ac870237932365572c5d0c14d35f822a79d Mon Sep 17 00:00:00 2001 From: Googler Date: Tue, 12 Jul 2022 01:55:46 -0700 Subject: [PATCH] Update CC rule helpers to use an optional CC toolchain. If the toolchain is needed and can't be found, _find_cpp_toolchain will fail with a more useful error message. Part of #14727. PiperOrigin-RevId: 460404686 Change-Id: Idb3eeffb4a96ff0158616e7a3d0201f5603cd050 --- src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl index 0935debe6524ee..719ceb30482cc4 100644 --- a/src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl +++ b/src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl @@ -20,6 +20,7 @@ CcInfo = _builtins.toplevel.CcInfo cc_common = _builtins.toplevel.cc_common cc_internal = _builtins.internal.cc_internal CcNativeLibraryInfo = _builtins.internal.CcNativeLibraryInfo +config_common = _builtins.toplevel.config_common platform_common = _builtins.toplevel.platform_common artifact_category = struct( @@ -189,6 +190,9 @@ def _find_cpp_toolchain(ctx): if not _CPP_TOOLCHAIN_TYPE in ctx.toolchains: fail("In order to use find_cpp_toolchain, you must include the '//tools/cpp:toolchain_type' in the toolchains argument to your rule.") toolchain_info = ctx.toolchains[_CPP_TOOLCHAIN_TYPE] + if toolchain_info == None: + # No cpp toolchain was found, so report an error. + fail("Unable to find a CC toolchain using toolchain resolution. Did you properly set --platforms?") if hasattr(toolchain_info, "cc_provider_in_toolchain") and hasattr(toolchain_info, "cc"): return toolchain_info.cc return toolchain_info @@ -200,8 +204,7 @@ def _find_cpp_toolchain(ctx): # We didn't find anything. fail("In order to use find_cpp_toolchain, you must define the '_cc_toolchain' attribute on your rule or aspect.") -# buildifier: disable=unused-variable -def _use_cpp_toolchain(mandatory = True): +def _use_cpp_toolchain(mandatory = False): """ Helper to depend on the c++ toolchain. @@ -219,7 +222,7 @@ def _use_cpp_toolchain(mandatory = True): Returns: A list that can be used as the value for `rule.toolchains`. """ - return [_CPP_TOOLCHAIN_TYPE] + return [config_common.toolchain_type(_CPP_TOOLCHAIN_TYPE, mandatory = mandatory)] def _collect_compilation_prerequisites(ctx, compilation_context): direct = []