Skip to content

Commit

Permalink
Move more android only fields & methods from JavaSemantics to `Andr…
Browse files Browse the repository at this point in the history
…oidSemantics`

PiperOrigin-RevId: 585076985
Change-Id: I5f400c6ab5ff8166258e0d623f2694c64344b13b
  • Loading branch information
hvadehra authored and copybara-github committed Nov 24, 2023
1 parent 36614cf commit 7d10999
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.devtools.build.lib.rules.android.AndroidDataContext;
import com.google.devtools.build.lib.rules.android.AndroidSemantics;
import com.google.devtools.build.lib.rules.android.ProguardHelper.ProguardOutput;
import com.google.devtools.build.lib.rules.java.JavaCommon;
import com.google.devtools.build.lib.rules.java.JavaTargetAttributes;

/**
Expand All @@ -45,6 +46,8 @@ public class BazelAndroidSemantics implements AndroidSemantics {
"src/tools/android/java/com/google/devtools/build/android/incrementaldeployment"))
.add(PackageIdentifier.createUnchecked("bazel_tools", "tools/android"))
.build();
private static final String BAZEL_TEST_RUNNER_MAIN_CLASS =
"com.google.testing.junit.runner.BazelTestRunner";

private BazelAndroidSemantics() {}

Expand Down Expand Up @@ -182,4 +185,12 @@ public Artifact getObfuscatedConstantStringMap(RuleContext ruleContext)
throws InterruptedException {
return null;
}

@Override
public void checkRule(RuleContext ruleContext, JavaCommon javaCommon) {}

@Override
public String getTestRunnerMainClass() {
return BAZEL_TEST_RUNNER_MAIN_CLASS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ public class BazelJavaSemantics implements JavaSemantics {
Template.forResource(BazelJavaSemantics.class, "java_stub_template.txt");
private static final String CLASSPATH_PLACEHOLDER = "%classpath%";

private static final String BAZEL_TEST_RUNNER_MAIN_CLASS =
"com.google.testing.junit.runner.BazelTestRunner";

private BazelJavaSemantics() {}

private static final String JAVA_TOOLCHAIN_TYPE =
Expand All @@ -91,19 +88,10 @@ public Label getJavaRuntimeToolchainType() {
return JAVA_RUNITME_TOOLCHAIN_TYPE;
}

@Override
public void checkRule(RuleContext ruleContext, JavaCommon javaCommon) {
}

@Override
public void checkForProtoLibraryAndJavaProtoLibraryOnSameProto(
RuleContext ruleContext, JavaCommon javaCommon) {}

@Override
public String getTestRunnerMainClass() {
return BAZEL_TEST_RUNNER_MAIN_CLASS;
}

@Override
public ImmutableList<Artifact> collectResources(RuleContext ruleContext) {
if (!ruleContext.getRule().isAttrDefined("resources", BuildType.LABEL_LIST)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public ConfiguredTarget create(RuleContext ruleContext)
/* compileDeps = */ targets,
/* runtimeDeps = */ targets,
/* bothDeps = */ targets);
javaSemantics.checkRule(ruleContext, common);
androidSemantics.checkRule(ruleContext, common);

JavaConfiguration javaConfig = ruleContext.getFragment(JavaConfiguration.class);
JavaCompilationArtifacts.Builder javaCompilationArtifactsBuilder =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ private static RuleConfiguredTargetBuilder init(
resourceApk.asDataBindingContext(),
/* isLibrary */ false,
shouldCompileJavaSrcs);
javaSemantics.checkRule(ruleContext, javaCommon);
androidSemantics.checkRule(ruleContext, javaCommon);
javaSemantics.checkForProtoLibraryAndJavaProtoLibraryOnSameProto(ruleContext, javaCommon);

AndroidCommon androidCommon = new AndroidCommon(javaCommon, /* asNeverLink= */ true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public ConfiguredTarget create(RuleContext ruleContext)
resourceApk.asDataBindingContext(),
/* isLibrary */ true,
/* shouldCompileJavaSrcs */ true);
javaSemantics.checkRule(ruleContext, javaCommon);
androidSemantics.checkRule(ruleContext, javaCommon);
AndroidCommon androidCommon = new AndroidCommon(javaCommon);

// As android_library makes use of the Java rule compilation pipeline, we collect all
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public ConfiguredTarget create(RuleContext ruleContext)
resourceApk.asDataBindingContext(),
/* isLibrary */ false,
/* shouldCompileJavaSrcs */ true);
javaSemantics.checkRule(ruleContext, javaCommon);
androidSemantics.checkRule(ruleContext, javaCommon);

// Use the regular Java javacopts, plus any extra needed for databinding. Enforcing
// android-compatible Java (-source 7 -target 7 and no TWR) is unnecessary for robolectric tests
Expand Down Expand Up @@ -223,7 +223,7 @@ public ConfiguredTarget create(RuleContext ruleContext)
executable = ruleContext.createOutputArtifact();
}

String mainClass = javaSemantics.getTestRunnerMainClass();
String mainClass = androidSemantics.getTestRunnerMainClass();
String originalMainClass = mainClass;
if (ruleContext.getConfiguration().isCodeCoverageEnabled()) {
mainClass =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ is exceeded. Assumes multidex classes are loaded through application code (i.e.
.add(
attr(":bytecode_optimizer", LABEL)
.cfg(ExecutionTransitionFactory.createFactory())
.value(JavaSemantics.BYTECODE_OPTIMIZER))
.value(AndroidSemantics.BYTECODE_OPTIMIZER))
// We need the C++ toolchain for every sub-configuration to get the correct linker.
.add(
attr("$cc_toolchain_split", LABEL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment envi
.add(
attr(":proguard", LABEL)
.cfg(ExecutionTransitionFactory.createFactory())
.value(JavaSemantics.PROGUARD)
.value(AndroidSemantics.PROGUARD)
.exec())
// This is the Proguard in the BUILD file that contains the android_sdk rule. Used when
// --proguard_top is not specified.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.
package com.google.devtools.build.lib.rules.android;

import static com.google.devtools.build.lib.packages.BuildType.LABEL_LIST;
import static com.google.devtools.build.lib.packages.ImplicitOutputsFunction.fromTemplates;

import com.google.common.collect.ImmutableList;
Expand All @@ -23,12 +24,16 @@
import com.google.devtools.build.lib.analysis.actions.SpawnAction;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.packages.Attribute.LabelLateBoundDefault;
import com.google.devtools.build.lib.packages.ImplicitOutputsFunction.SafeImplicitOutputsFunction;
import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
import com.google.devtools.build.lib.packages.Type;
import com.google.devtools.build.lib.rules.android.ProguardHelper.ProguardOutput;
import com.google.devtools.build.lib.rules.java.BootClassPathInfo;
import com.google.devtools.build.lib.rules.java.JavaCommon;
import com.google.devtools.build.lib.rules.java.JavaConfiguration;
import com.google.devtools.build.lib.rules.java.JavaTargetAttributes;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.SerializationConstant;
import java.util.Map;
import java.util.Optional;

Expand All @@ -53,6 +58,32 @@ public interface AndroidSemantics {
SafeImplicitOutputsFunction ANDROID_BINARY_PROGUARD_CONFIG =
fromTemplates("%{name}_proguard.config");

/** Implementation for the :proguard attribute. */
@SerializationConstant
LabelLateBoundDefault<JavaConfiguration> PROGUARD =
LabelLateBoundDefault.fromTargetConfiguration(
JavaConfiguration.class,
null,
(rule, attributes, javaConfig) -> javaConfig.getProguardBinary());

@SerializationConstant
LabelLateBoundDefault<JavaConfiguration> BYTECODE_OPTIMIZER =
LabelLateBoundDefault.fromTargetConfiguration(
JavaConfiguration.class,
null,
(rule, attributes, javaConfig) -> {
// Use a modicum of smarts to avoid implicit dependencies where we don't need them.
boolean hasProguardSpecs =
attributes.has("proguard_specs")
&& !attributes.get("proguard_specs", LABEL_LIST).isEmpty();
JavaConfiguration.NamedLabel optimizer = javaConfig.getBytecodeOptimizer();
if ((!hasProguardSpecs && !javaConfig.runLocalJavaOptimizations())
|| !optimizer.label().isPresent()) {
return null;
}
return optimizer.label().get();
});

default AndroidManifest renameManifest(
AndroidDataContext dataContext, AndroidManifest rawManifest) throws InterruptedException {
return rawManifest.renameManifestIfNeeded(dataContext);
Expand Down Expand Up @@ -222,4 +253,13 @@ default BootClassPathInfo getBootClassPathInfo(RuleContext ruleContext)
Artifact getProtoMapping(RuleContext ruleContext) throws InterruptedException;

Artifact getObfuscatedConstantStringMap(RuleContext ruleContext) throws InterruptedException;

/**
* Verifies if the rule contains any errors.
*
* <p>Errors should be signaled through {@link RuleContext}.
*/
void checkRule(RuleContext ruleContext, JavaCommon javaCommon) throws RuleErrorException;

String getTestRunnerMainClass();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

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

import static com.google.devtools.build.lib.packages.BuildType.LABEL_LIST;
import static com.google.devtools.build.lib.packages.ImplicitOutputsFunction.fromTemplates;

import com.google.common.collect.ImmutableList;
Expand All @@ -28,7 +27,6 @@
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.packages.Attribute.LabelLateBoundDefault;
import com.google.devtools.build.lib.packages.Attribute.LabelListLateBoundDefault;
import com.google.devtools.build.lib.packages.ImplicitOutputsFunction.SafeImplicitOutputsFunction;
import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
Expand Down Expand Up @@ -81,43 +79,10 @@ static Label javaToolchainAttribute(RuleDefinitionEnvironment environment) {
JavaConfiguration.class,
(rule, attributes, javaConfig) -> ImmutableList.copyOf(javaConfig.getPlugins()));

/** Implementation for the :proguard attribute. */
@SerializationConstant
LabelLateBoundDefault<JavaConfiguration> PROGUARD =
LabelLateBoundDefault.fromTargetConfiguration(
JavaConfiguration.class,
null,
(rule, attributes, javaConfig) -> javaConfig.getProguardBinary());

@SerializationConstant
LabelLateBoundDefault<JavaConfiguration> BYTECODE_OPTIMIZER =
LabelLateBoundDefault.fromTargetConfiguration(
JavaConfiguration.class,
null,
(rule, attributes, javaConfig) -> {
// Use a modicum of smarts to avoid implicit dependencies where we don't need them.
boolean hasProguardSpecs =
attributes.has("proguard_specs")
&& !attributes.get("proguard_specs", LABEL_LIST).isEmpty();
JavaConfiguration.NamedLabel optimizer = javaConfig.getBytecodeOptimizer();
if ((!hasProguardSpecs && !javaConfig.runLocalJavaOptimizations())
|| !optimizer.label().isPresent()) {
return null;
}
return optimizer.label().get();
});

String JACOCO_METADATA_PLACEHOLDER = "%set_jacoco_metadata%";
String JACOCO_MAIN_CLASS_PLACEHOLDER = "%set_jacoco_main_class%";
String JACOCO_JAVA_RUNFILES_ROOT_PLACEHOLDER = "%set_jacoco_java_runfiles_root%";

/**
* Verifies if the rule contains any errors.
*
* <p>Errors should be signaled through {@link RuleContext}.
*/
void checkRule(RuleContext ruleContext, JavaCommon javaCommon) throws RuleErrorException;

/**
* Verifies there are no conflicts in protos.
*
Expand All @@ -132,8 +97,6 @@ void checkForProtoLibraryAndJavaProtoLibraryOnSameProto(
*/
ImmutableList<Artifact> collectResources(RuleContext ruleContext) throws RuleErrorException;

String getTestRunnerMainClass();

/**
* Constructs the command line to call SingleJar to join all artifacts from {@code classpath}
* (java code) and {@code resources} into {@code output}.
Expand Down

0 comments on commit 7d10999

Please sign in to comment.