Skip to content

Commit

Permalink
Remove unused methods in ProtoCommon
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 498547363
Change-Id: I6466feeb7cc74d1131b978f610e25c45170378ab
  • Loading branch information
comius authored and copybara-github committed Dec 30, 2022
1 parent cd6c496 commit d42aedd
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,23 @@

package com.google.devtools.build.lib.rules.proto;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Interner;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.FilesToRunProvider;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.config.CoreOptionConverters.StrictDepsMode;
import com.google.devtools.build.lib.analysis.starlark.Args;
import com.google.devtools.build.lib.cmdline.BazelModuleContext;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.collect.nestedset.Depset;
import com.google.devtools.build.lib.collect.nestedset.Depset.ElementType;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.collect.nestedset.Order;
import com.google.devtools.build.lib.concurrent.BlazeInterners;
import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
import com.google.devtools.build.lib.packages.StarlarkInfo;
import com.google.devtools.build.lib.vfs.PathFragment;
import javax.annotation.Nullable;
import net.starlark.java.eval.Dict;
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.Module;
import net.starlark.java.eval.Sequence;
import net.starlark.java.eval.Starlark;
import net.starlark.java.eval.StarlarkCallable;
import net.starlark.java.eval.StarlarkFunction;
import net.starlark.java.eval.StarlarkList;
import net.starlark.java.eval.StarlarkThread;
Expand All @@ -52,12 +42,6 @@ private ProtoCommon() {
throw new UnsupportedOperationException();
}

// Keep in sync with the migration label in
// https://github.com/bazelbuild/rules_proto/blob/master/proto/defs.bzl.
@VisibleForTesting
public static final String PROTO_RULES_MIGRATION_LABEL =
"__PROTO_RULES_MIGRATION_DO_NOT_USE_WILL_BREAK__";

private static final Interner<PathFragment> PROTO_SOURCE_ROOT_INTERNER =
BlazeInterners.newWeakInterner();

Expand All @@ -74,20 +58,6 @@ static PathFragment memoryEfficientProtoSourceRoot(PathFragment protoSourceRoot)
return PROTO_SOURCE_ROOT_INTERNER.intern(protoSourceRoot);
}

// =================================================================
// Protocol compiler invocation stuff.

/**
* Decides whether this proto_library should check for strict proto deps.
*
* <p>Only takes into account the command-line flag --strict_proto_deps.
*/
@VisibleForTesting
public static boolean areDepsStrict(RuleContext ruleContext) {
StrictDepsMode getBool = ruleContext.getFragment(ProtoConfiguration.class).strictProtoDeps();
return getBool != StrictDepsMode.OFF && getBool != StrictDepsMode.DEFAULT;
}

public static void checkPrivateStarlarkificationAllowlist(StarlarkThread thread)
throws EvalException {
Label label =
Expand Down Expand Up @@ -121,85 +91,6 @@ public static ImmutableList<Artifact> declareGeneratedFiles(
}
}

private static final StarlarkCallable pythonMapper =
new StarlarkCallable() {
@Override
public Object call(StarlarkThread thread, Tuple args, Dict<String, Object> kwargs) {
return args.get(0).toString().replace('-', '_').replace('.', '/');
}

@Override
public String getName() {
return "python_mapper";
}
};

/**
* Each language-specific initialization method will call this to construct Artifacts representing
* its protocol compiler outputs. The cals replaces hyphens in the file name with underscores, and
* dots in the file name with forward slashes, as required for Python modules.
*
* @param extension Remove ".proto" and replace it with this to produce the output file name, e.g.
* ".pb.cc".
*/
public static ImmutableList<Artifact> declareGeneratedFilesPython(
RuleContext ruleContext, ConfiguredTarget protoTarget, String extension)
throws RuleErrorException, InterruptedException {
StarlarkFunction declareGeneratedFiles =
(StarlarkFunction)
ruleContext.getStarlarkDefinedBuiltin("proto_common_declare_generated_files");
ruleContext.initStarlarkRuleContext();
Sequence<?> outputs =
(Sequence<?>)
ruleContext.callStarlarkOrThrowRuleError(
declareGeneratedFiles,
ImmutableList.of(
/* actions */ ruleContext.getStarlarkRuleContext().actions(),
/* proto_info */ protoTarget.get(ProtoInfo.PROVIDER),
/* extension */ extension,
/* experimental_python_names */ pythonMapper),
ImmutableMap.of());
try {
return Sequence.cast(outputs, Artifact.class, "declare_generated_files").getImmutableList();
} catch (EvalException e) {
throw new RuleErrorException(e.getMessageWithStack());
}
}

public static void compile(
RuleContext ruleContext,
ConfiguredTarget protoTarget,
StarlarkInfo protoLangToolchainInfo,
Iterable<Artifact> generatedFiles,
@Nullable Object pluginOutput,
@Nullable Args additionalArgs,
Iterable<FilesToRunProvider> additionalTools,
Iterable<Artifact> additionalInputs,
@Nullable StarlarkCallable resourceSet,
String progressMessage)
throws RuleErrorException, InterruptedException {
StarlarkFunction compile =
(StarlarkFunction) ruleContext.getStarlarkDefinedBuiltin("proto_common_compile");
ruleContext.initStarlarkRuleContext();
ruleContext.callStarlarkOrThrowRuleError(
compile,
ImmutableList.of(
/* actions */ ruleContext.getStarlarkRuleContext().actions(),
/* proto_info */ protoTarget.get(ProtoInfo.PROVIDER),
/* proto_lang_toolchain_info */ protoLangToolchainInfo,
/* generated_files */ StarlarkList.immutableCopyOf(generatedFiles),
/* plugin_output */ pluginOutput == null ? Starlark.NONE : pluginOutput,
/* additional_args */ additionalArgs == null ? Starlark.NONE : additionalArgs,
/* additional_tools */ StarlarkList.immutableCopyOf(additionalTools),
/* additional_inputs */ additionalInputs == null
? Depset.of(ElementType.EMPTY, NestedSetBuilder.emptySet(Order.STABLE_ORDER))
: Depset.of(
Artifact.TYPE, NestedSetBuilder.wrap(Order.STABLE_ORDER, additionalInputs)),
/* resource_set */ resourceSet == null ? Starlark.NONE : resourceSet,
/* experimental_progress_message */ progressMessage),
ImmutableMap.of());
}

public static void compile(
RuleContext ruleContext,
ConfiguredTarget protoTarget,
Expand Down Expand Up @@ -227,27 +118,6 @@ public static void compile(
execGroup));
}

public static boolean shouldGenerateCode(
RuleContext ruleContext,
ConfiguredTarget protoTarget,
StarlarkInfo protoLangToolchainInfo,
String ruleName)
throws RuleErrorException, InterruptedException {
StarlarkFunction shouldGenerateCode =
(StarlarkFunction)
ruleContext.getStarlarkDefinedBuiltin("proto_common_experimental_should_generate_code");
ruleContext.initStarlarkRuleContext();
return (Boolean)
ruleContext.callStarlarkOrThrowRuleError(
shouldGenerateCode,
ImmutableList.of(
/* proto_info */ protoTarget.get(ProtoInfo.PROVIDER),
/* proto_lang_toolchain_info */ protoLangToolchainInfo,
/* rule_name */ ruleName,
/* target_label */ protoTarget.getLabel()),
ImmutableMap.of());
}

public static Sequence<Artifact> filterSources(
RuleContext ruleContext, ConfiguredTarget protoTarget, StarlarkInfo protoLangToolchainInfo)
throws RuleErrorException, InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

package com.google.devtools.build.lib.packages.util;

import com.google.devtools.build.lib.rules.proto.ProtoCommon;
import com.google.devtools.build.lib.testutil.Scratch;
import com.google.devtools.build.lib.testutil.TestConstants;
import java.io.IOException;
Expand All @@ -27,10 +26,6 @@ private MockProtoSupport() {
throw new UnsupportedOperationException();
}

/** This is workaround for tests that do not use {@code Scratch} (e.g. aquery-tests). */
public static final String MIGRATION_TAG =
String.format("tags = ['%s'],", ProtoCommon.PROTO_RULES_MIGRATION_LABEL);

/**
* Setup the support for building proto_library. You additionally need to setup support for each
* of the languages used in the specific test.
Expand Down

0 comments on commit d42aedd

Please sign in to comment.