Skip to content

Commit

Permalink
Update CC rule helpers to use an optional CC toolchain.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
katre authored and copybara-github committed Jul 12, 2022
1 parent c7792e2 commit a2ba0ac
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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 = []
Expand Down

0 comments on commit a2ba0ac

Please sign in to comment.