Skip to content

Commit

Permalink
Hard code preferred Starlark rule kinds in --compile_one_dependency
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 526079318
Change-Id: Ie425490b1ac524eb9a0c3303f285cb28760662a5
  • Loading branch information
mai93 authored and copybara-github committed Apr 21, 2023
1 parent 4235f14 commit 015707c
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@
import com.google.devtools.build.lib.packages.Type;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.SerializationConstant;
import com.google.devtools.build.lib.starlarkbuildapi.StarlarkRuleFunctionsApi;
import com.google.devtools.build.lib.util.FileType;
import com.google.devtools.build.lib.util.FileTypeSet;
import com.google.devtools.build.lib.util.Pair;
import com.google.errorprone.annotations.FormatMethod;
Expand Down Expand Up @@ -295,7 +294,6 @@ public StarlarkRuleFunction rule(
Object buildSetting,
Object cfg,
Object execGroups,
Object compileOneFiletype,
Object name,
StarlarkThread thread)
throws EvalException {
Expand All @@ -316,7 +314,6 @@ public StarlarkRuleFunction rule(
buildSetting,
cfg,
execGroups,
compileOneFiletype,
name,
thread);
}
Expand All @@ -338,7 +335,6 @@ public static StarlarkRuleFunction createRule(
Object buildSetting,
Object cfg,
Object execGroups,
Object compileOneFiletype,
Object name,
StarlarkThread thread)
throws EvalException {
Expand Down Expand Up @@ -491,17 +487,6 @@ public static StarlarkRuleFunction createRule(
builder.addExecutionPlatformConstraints(parseExecCompatibleWith(execCompatibleWith, thread));
}

if (compileOneFiletype instanceof Sequence) {
if (!bzlModule.label().getRepository().getNameWithAt().equals("@_builtins")) {
throw Starlark.errorf(
"Rule in '%s' cannot use private API", bzlModule.label().getPackageName());
}
ImmutableList<String> filesTypes =
Sequence.cast(compileOneFiletype, String.class, "compile_one_filetype")
.getImmutableList();
builder.setPreferredDependencyPredicate(FileType.of(filesTypes));
}

StarlarkRuleFunction starlarkRuleFunction =
new StarlarkRuleFunction(
builder,
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/google/devtools/build/lib/pkgcache/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/skyframe:detailed_exceptions",
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec",
"//src/main/java/com/google/devtools/build/lib/util:detailed_exit_code",
"//src/main/java/com/google/devtools/build/lib/util:filetype",
"//src/main/java/com/google/devtools/build/lib/util:resource_converter",
"//src/main/java/com/google/devtools/build/lib/vfs",
"//src/main/java/com/google/devtools/build/lib/vfs:pathfragment",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
// limitations under the License.
package com.google.devtools.build.lib.pkgcache;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.devtools.build.lib.cmdline.Label;
Expand All @@ -29,6 +32,7 @@
import com.google.devtools.build.lib.packages.RuleClass;
import com.google.devtools.build.lib.packages.Target;
import com.google.devtools.build.lib.server.FailureDetails.TargetPatterns;
import com.google.devtools.build.lib.util.FileType;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
Expand All @@ -41,6 +45,14 @@
*/
public final class CompileOneDependencyTransformer {
private final TargetProvider targetProvider;
private static final ImmutableMap<String, Predicate<String>> preferredRules =
ImmutableMap.of(
"cc_library",
FileType.of(".cc", ".h", ".c"),
"java_library",
FileType.of(".java"),
"py_library",
FileType.of(".py"));

public CompileOneDependencyTransformer(TargetProvider targetProvider) {
this.targetProvider = targetProvider;
Expand Down Expand Up @@ -76,7 +88,9 @@ private Target transformCompileOneDependency(ExtendedEventHandler eventHandler,
for (Rule rule : orderedRuleList) {
Set<Label> labels = getInputLabels(rule);
if (listContainsFile(eventHandler, labels, target.getLabel(), Sets.<Label>newHashSet())) {
if (rule.getRuleClassObject().isPreferredDependency(target.getName())) {
if (preferredRules
.getOrDefault(rule.getRuleClass(), Predicates.alwaysFalse())
.apply(target.getName())) {
result = rule;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ public void analysisTest(
/* buildSetting= */ Starlark.NONE,
/* cfg= */ Starlark.NONE,
/* execGroups= */ Starlark.NONE,
/* compileOneFiletype= */ Starlark.NONE,
/* name= */ Starlark.NONE,
thread);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,18 +441,6 @@ Object provider(Object doc, Object fields, Object init, StarlarkThread thread)
+ " allows rules to run actions on multiple execution platforms within a"
+ " single target. See <a href='${link exec-groups}'>execution groups"
+ " documentation</a> for more info."),
@Param(
name = "compile_one_filetype",
defaultValue = "None",
allowedTypes = {
@ParamType(type = Sequence.class, generic1 = String.class),
@ParamType(type = NoneType.class),
},
named = true,
positional = false,
doc =
"Used by --compile_one_dependency: if multiple rules consume the specified file, "
+ "should we choose this rule over others."),
@Param(
name = "name",
named = true,
Expand Down Expand Up @@ -495,7 +483,6 @@ StarlarkCallable rule(
Object buildSetting,
Object cfg,
Object execGroups,
Object compileOneFiletype,
Object name,
StarlarkThread thread)
throws EvalException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ public StarlarkCallable rule(
Object buildSetting,
Object cfg,
Object execGroups,
Object compileOneFiletype,
Object name,
StarlarkThread thread)
throws EvalException {
Expand Down
1 change: 0 additions & 1 deletion src/main/starlark/builtins_bzl/common/cc/cc_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -615,5 +615,4 @@ cc_library = rule(
exec_groups = {
"cpp_link": exec_group(toolchains = cc_helper.use_cpp_toolchain()),
},
compile_one_filetype = [".cc", ".h", ".c"],
)
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,5 @@ java_library = rule(
"sourcejar": "lib%{name}-src.jar",
},
fragments = ["java", "cpp"],
compile_one_filetype = [".java"],
toolchains = [semantics.JAVA_TOOLCHAIN],
)
3 changes: 0 additions & 3 deletions src/main/starlark/builtins_bzl/common/python/py_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,5 @@ def create_py_library_rule(*, attrs = {}, **kwargs):
# TODO(b/253818097): fragments=py is only necessary so that
# RequiredConfigFragmentsTest passes
fragments = ["py"],
# Necessary for --compile_one_dependency to give preference to
# py_library if a .py file is associated with multiple targets.
compile_one_filetype = [".py"],
**kwargs
)

0 comments on commit 015707c

Please sign in to comment.