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 bazelbuild#14727.

PiperOrigin-RevId: 460405864
Change-Id: I0e9a0861037238b1283528c5d05b3f79fda28f5e
  • Loading branch information
katre authored and aranguyen committed Jul 20, 2022
1 parent f858612 commit a0548f0
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions tools/cpp/toolchain_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,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 '%s' in the toolchains argument to your rule." % CPP_TOOLCHAIN_TYPE)
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 @@ -50,8 +53,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 @@ -69,4 +71,4 @@ 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)]

0 comments on commit a0548f0

Please sign in to comment.