forked from bazelbuild/bazel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Symbolic macro attribute inheritance
Allows the following syntax: ``` my_cc_library = macro( # inherit all attributes from a given rule or macro symbol (or "common" for # the set of common Starlark rule attributes) inherit_attrs = native.cc_library, attrs = { # remove cxxopts from inherited attrs list "cxxopts": None, # override the copts attribute inherited from native.cc_library "copts": attr.string_list(default = ["-D_FOO"]), }, ... ) ``` Fixes bazelbuild#24066 Tricky parts: * Some common rule attributes ("testonly", "size", etc.) have computed defaults; native rule "license" and "distribs" attrs have defaults which fail type check if lifted. The only sane way of handling such attrs, if the attribute is unset in the macro function call, to pass the value as None to the implementation function so that the implementation function will pass None to the rule function, which will thus set the default appropriately. * We need RuleFunctionApi and MacroFunctionApi interfaces (yet more interfaces, alas, or we get a circular dependency) to express the type of inherit_attrs param * Iterating over all native rules (for testing inheritance from them) is tricky: we need to look at builtins (since the ConfiguredRuleClassProvider may have stubs for java rules overridden by builtins). This is already correctly handled by `bazel info build-language`, so let's move the logic into a utility class. RELNOTES: Add inherit_attrs param to macro() to allow symbolic macros to inherit attributes from rules or other symbolic macros. PiperOrigin-RevId: 694154352 Change-Id: I849a7f16b4da8eb2829cdbc6a131d85a28bc4740
- Loading branch information
1 parent
90b051b
commit 417fd24
Showing
15 changed files
with
808 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/main/java/com/google/devtools/build/lib/packages/RuleClassUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright 2024 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.devtools.build.lib.packages; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.devtools.build.lib.packages.semantics.BuildLanguageOptions; | ||
import com.google.devtools.build.lib.skyframe.StarlarkBuiltinsValue; | ||
import com.google.devtools.build.lib.starlarkbuildapi.MacroFunctionApi; | ||
import java.util.ArrayList; | ||
import java.util.Comparator; | ||
import java.util.Map; | ||
import net.starlark.java.eval.StarlarkFunction; | ||
|
||
/** Rule class utilities. */ | ||
public final class RuleClassUtils { | ||
|
||
/** | ||
* Returns the sorted list of all builtin rule classes. | ||
* | ||
* <p>Unlike {@link RuleClassProvider#getRuleClassMap}, this method returns real Starlark builtins | ||
* instead of stub overridden native rules. | ||
* | ||
* @param includeMacroWrappedRules if true, include rule classes for rules wrapped in macros. | ||
*/ | ||
public static ImmutableList<RuleClass> getBuiltinRuleClasses( | ||
StarlarkBuiltinsValue builtins, | ||
RuleClassProvider ruleClassProvider, | ||
boolean includeMacroWrappedRules) { | ||
ImmutableMap<String, RuleClass> nativeRuleClasses = ruleClassProvider.getRuleClassMap(); | ||
// The conditional for selecting whether or not to load symbols from @_builtins is the same as | ||
// in PackageFunction.compileBuildFile | ||
if (builtins | ||
.starlarkSemantics | ||
.get(BuildLanguageOptions.EXPERIMENTAL_BUILTINS_BZL_PATH) | ||
.isEmpty()) { | ||
return ImmutableList.sortedCopyOf( | ||
Comparator.comparing(RuleClass::getName), nativeRuleClasses.values()); | ||
} else { | ||
ArrayList<RuleClass> ruleClasses = new ArrayList<>(builtins.predeclaredForBuild.size()); | ||
for (Map.Entry<String, Object> entry : builtins.predeclaredForBuild.entrySet()) { | ||
if (entry.getValue() instanceof RuleFunction) { | ||
ruleClasses.add(((RuleFunction) entry.getValue()).getRuleClass()); | ||
} else if ((entry.getValue() instanceof StarlarkFunction | ||
|| entry.getValue() instanceof MacroFunctionApi) | ||
&& includeMacroWrappedRules) { | ||
// entry.getValue() is a macro in @_builtins which overrides a native rule and wraps a | ||
// instantiation of a rule target. We cannot get at that main target's rule class | ||
// directly, so we attempt heuristics. | ||
// Note that we do not rely on the StarlarkFunction or MacroFunction object's name because | ||
// the name under which the macro was defined may not match the name under which | ||
// @_builtins re-exported it. | ||
if (builtins.exportedToJava.containsKey(entry.getKey() + "_rule_function")) { | ||
ruleClasses.add( | ||
((RuleFunction) builtins.exportedToJava.get(entry.getKey() + "_rule_function")) | ||
.getRuleClass()); | ||
} else if (nativeRuleClasses.containsKey(entry.getKey())) { | ||
ruleClasses.add(nativeRuleClasses.get(entry.getKey())); | ||
} | ||
} | ||
} | ||
return ImmutableList.sortedCopyOf(Comparator.comparing(RuleClass::getName), ruleClasses); | ||
} | ||
} | ||
|
||
private RuleClassUtils() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.