Skip to content

Commit

Permalink
Update native CC rules 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, CppHelper will fail with a more useful error message.

Part of bazelbuild#14727.

PiperOrigin-RevId: 459785208
Change-Id: If191c0a5dc7cf5a1759fa0af63bd53096e9deda8
  • Loading branch information
katre authored and aranguyen committed Jul 20, 2022
1 parent 9c47ab6 commit b6a21ed
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,11 @@ public static Label getToolchainTypeFromRuleClass(RuleContext ruleContext) {
private static CcToolchainProvider getToolchainFromPlatformConstraints(
RuleContext ruleContext, Label toolchainType) throws RuleErrorException {
ToolchainInfo toolchainInfo = ruleContext.getToolchainContext().forToolchainType(toolchainType);
if (toolchainInfo == null) {
throw ruleContext.throwWithRuleError(
"Unable to find a CC toolchain using toolchain resolution. Did you properly set"
+ " --platforms?");
}
try {
return (CcToolchainProvider) toolchainInfo.getValue("cc");
} catch (EvalException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ public static Label ccToolchainTypeAttribute(RuleDefinitionEnvironment env) {
}

public static ToolchainTypeRequirement ccToolchainTypeRequirement(Label ccToolchainType) {
// TODO(https://github.com/bazelbuild/bazel/issues/14727): Evaluate whether this can be
// optional.
return ToolchainTypeRequirement.builder(ccToolchainType).mandatory(true).build();
// This is an optional dependency: if a toolchain cannot be found, CppHelper will give an
// appropriate error.
return ToolchainTypeRequirement.builder(ccToolchainType).mandatory(false).build();
}

public static ToolchainTypeRequirement ccToolchainTypeRequirement(RuleDefinitionEnvironment env) {
Expand Down

0 comments on commit b6a21ed

Please sign in to comment.