Skip to content

Commit

Permalink
Add private compilation APIs for use in Starlark java_common.compile()
Browse files Browse the repository at this point in the history
Since `JavaCompilationHelper` is also used in the android rules code, the plan is to use these high-level APIs to begin with, and look into moving more code into Starlark in a later follow up.

The methods currently replicate the corresponding behavior in `JavaLibraryHelper.build()`.

PiperOrigin-RevId: 550564868
Change-Id: I25fe9bed329a1997dfc8e2c2789edc06954cc26f
  • Loading branch information
hvadehra authored and copybara-github committed Jul 24, 2023
1 parent 2dc7375 commit fb2df4d
Show file tree
Hide file tree
Showing 5 changed files with 247 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ private Artifact turbineOutput(Artifact classJar, String newExtension) {
* @param headerJar the jar output of this java compilation
* @param headerDeps the .jdeps output of this java compilation
*/
private void createHeaderCompilationAction(Artifact headerJar, Artifact headerDeps) {
public void createHeaderCompilationAction(Artifact headerJar, Artifact headerDeps) {

JavaTargetAttributes attributes = getAttributes();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,12 @@ private static List<String> tokenize(List<String> input) throws EvalException {
return output;
}

private static StrictDepsMode getStrictDepsMode(String strictDepsMode) {
/**
* Converts the supplied string to the appropriate {@link StrictDepsMode} enum instance.
*
* @param strictDepsMode the value to convert
*/
static StrictDepsMode getStrictDepsMode(String strictDepsMode) {
switch (strictDepsMode) {
case "OFF":
return StrictDepsMode.OFF;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
package com.google.devtools.build.lib.rules.java;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.devtools.build.lib.rules.java.JavaInfoBuildHelper.getStrictDepsMode;

import com.google.common.base.Ascii;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
Expand Down Expand Up @@ -196,6 +198,137 @@ public JavaInfo createJavaCompileAction(
thread);
}

@Override
public void createHeaderCompilationAction(
StarlarkRuleContext ctx,
JavaToolchainProvider toolchain,
Artifact headerJar,
Artifact headerDepsProto,
Info pluginInfo,
Depset sourceFiles,
Sequence<?> sourceJars,
Depset compileTimeClasspath,
Depset directJars,
Object bootClassPath,
Depset compileTimeJavaDeps,
Sequence<?> javacOpts,
String strictDepsMode,
Label targetLabel,
Object injectingRuleKind,
boolean enableDirectClasspath,
Sequence<?> additionalInputs)
throws EvalException, TypeException, RuleErrorException, LabelSyntaxException {
checkJavaToolchainIsDeclaredOnRule(ctx.getRuleContext());
JavaTargetAttributes.Builder attributesBuilder =
new JavaTargetAttributes.Builder(javaSemantics)
.addSourceJars(Sequence.cast(sourceJars, Artifact.class, "source_jars"))
.addSourceFiles(sourceFiles.toList(Artifact.class))
.addDirectJars(directJars.getSet(Artifact.class))
.addCompileTimeClassPathEntries(compileTimeClasspath.getSet(Artifact.class))
.setStrictJavaDeps(getStrictDepsMode(Ascii.toUpperCase(strictDepsMode)))
.setTargetLabel(targetLabel)
.setInjectingRuleKind(
injectingRuleKind == Starlark.NONE ? null : (String) injectingRuleKind)
.addPlugin(JavaPluginInfo.PROVIDER.wrap(pluginInfo))
.addCompileTimeDependencyArtifacts(compileTimeJavaDeps.getSet(Artifact.class));
if (bootClassPath instanceof BootClassPathInfo
&& !((BootClassPathInfo) bootClassPath).isEmpty()) {
attributesBuilder.setBootClassPath((BootClassPathInfo) bootClassPath);
}
JavaCompilationHelper compilationHelper =
new JavaCompilationHelper(
ctx.getRuleContext(),
javaSemantics,
Sequence.cast(javacOpts, String.class, "javac_opts").getImmutableList(),
attributesBuilder,
toolchain,
Sequence.cast(additionalInputs, Artifact.class, "additional_inputs")
.getImmutableList());
compilationHelper.enableDirectClasspath(enableDirectClasspath);
compilationHelper.createHeaderCompilationAction(headerJar, headerDepsProto);
}

@Override
public void createCompilationAction(
StarlarkRuleContext ctx,
JavaToolchainProvider javaToolchain,
Artifact output,
Object depsProto,
Object genClass,
Object genSource,
Artifact manifestProto,
Artifact nativeHeader,
Info pluginInfo,
Depset sourceFiles,
Sequence<?> sourceJars,
Sequence<?> resources,
Depset resourceJars,
Depset compileTimeClasspath,
Sequence<?> classpathResources,
Sequence<?> sourcepath,
Depset directJars,
Object bootClassPath,
Depset compileTimeJavaDeps,
Sequence<?> javacOpts,
String strictDepsMode,
Label targetLabel,
Object injectingRuleKind,
boolean enableJSpecify,
boolean enableDirectClasspath,
Sequence<?> additionalInputs,
Sequence<?> additionalOutputs)
throws EvalException, TypeException, RuleErrorException, LabelSyntaxException {
checkJavaToolchainIsDeclaredOnRule(ctx.getRuleContext());
JavaCompileOutputs<Artifact> outputs =
JavaCompileOutputs.builder()
.output(output)
.depsProto(depsProto == Starlark.NONE ? null : (Artifact) depsProto)
.genClass(genClass == Starlark.NONE ? null : (Artifact) genClass)
.genSource(genSource == Starlark.NONE ? null : (Artifact) genSource)
.manifestProto(manifestProto)
.nativeHeader(nativeHeader)
.build();
JavaTargetAttributes.Builder attributesBuilder =
new JavaTargetAttributes.Builder(javaSemantics)
.addSourceJars(Sequence.cast(sourceJars, Artifact.class, "source_jars"))
.addSourceFiles(sourceFiles.toList(Artifact.class))
.addDirectJars(directJars.getSet(Artifact.class))
.addCompileTimeClassPathEntries(compileTimeClasspath.getSet(Artifact.class))
.addClassPathResources(
Sequence.cast(classpathResources, Artifact.class, "classpath_resources"))
.setStrictJavaDeps(getStrictDepsMode(Ascii.toUpperCase(strictDepsMode)))
.setTargetLabel(targetLabel)
.setInjectingRuleKind(
injectingRuleKind == Starlark.NONE ? null : (String) injectingRuleKind)
.setSourcePath(
Sequence.cast(sourcepath, Artifact.class, "source_path").getImmutableList())
.addPlugin(JavaPluginInfo.PROVIDER.wrap(pluginInfo))
.addAdditionalOutputs(
Sequence.cast(additionalOutputs, Artifact.class, "additional_outputs"));
if (bootClassPath instanceof BootClassPathInfo
&& !((BootClassPathInfo) bootClassPath).isEmpty()) {
attributesBuilder.setBootClassPath((BootClassPathInfo) bootClassPath);
}
for (Artifact resource : Sequence.cast(resources, Artifact.class, "resources")) {
attributesBuilder.addResource(
JavaHelper.getJavaResourcePath(javaSemantics, ctx.getRuleContext(), resource), resource);
}
attributesBuilder.addResourceJars(resourceJars.getSet(Artifact.class));
attributesBuilder.addCompileTimeDependencyArtifacts(compileTimeJavaDeps.getSet(Artifact.class));
JavaCompilationHelper compilationHelper =
new JavaCompilationHelper(
ctx.getRuleContext(),
javaSemantics,
Sequence.cast(javacOpts, String.class, "javac_opts").getImmutableList(),
attributesBuilder,
javaToolchain,
Sequence.cast(additionalInputs, Artifact.class, "additional_inputs")
.getImmutableList());
compilationHelper.enableJspecify(enableJSpecify);
compilationHelper.enableDirectClasspath(enableDirectClasspath);
compilationHelper.createCompileAction(outputs);
}

@Override
// TODO(b/78512644): migrate callers to passing explicit javacopts or using custom toolchains, and
// delete
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import com.google.common.collect.ImmutableList;
import com.google.devtools.build.docgen.annot.DocCategory;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.cmdline.LabelSyntaxException;
import com.google.devtools.build.lib.collect.nestedset.Depset;
import com.google.devtools.build.lib.collect.nestedset.Depset.TypeException;
Expand Down Expand Up @@ -316,6 +317,110 @@ JavaInfoT createJavaCompileAction(
StarlarkThread thread)
throws EvalException, InterruptedException, RuleErrorException, LabelSyntaxException;

@StarlarkMethod(
name = "create_header_compilation_action",
documented = false,
parameters = {
@Param(name = "ctx"),
@Param(name = "java_toolchain"),
@Param(name = "compile_jar"),
@Param(name = "compile_deps_proto"),
@Param(name = "plugin_info"),
@Param(name = "source_files"),
@Param(name = "source_jars"),
@Param(name = "compilation_classpath"),
@Param(name = "direct_jars"),
@Param(name = "bootclasspath"),
@Param(name = "compile_time_java_deps"),
@Param(name = "javac_opts"),
@Param(name = "strict_deps_mode"),
@Param(name = "target_label"),
@Param(name = "injecting_rule_kind"),
@Param(name = "enable_direct_classpath"),
@Param(name = "additional_inputs"),
})
void createHeaderCompilationAction(
StarlarkRuleContextT ctx,
JavaToolchainT javaToolchain,
FileT compileJar,
FileT compileDepsProto,
Info pluginInfo,
Depset sourceFiles,
Sequence<?> sourceJars,
Depset compileTimeClasspath,
Depset directJars,
Object bootClassPath,
Depset compileTimeDeps,
Sequence<?> javacOpts,
String strictDepsMode,
Label targetLabel,
Object injectingRuleKind,
boolean enableDirectClasspath,
Sequence<?> additionalInputs)
throws EvalException, TypeException, RuleErrorException, LabelSyntaxException;

@StarlarkMethod(
name = "create_compilation_action",
documented = false,
parameters = {
@Param(name = "ctx"),
@Param(name = "java_toolchain"),
@Param(name = "output"),
@Param(name = "deps_proto"),
@Param(name = "gen_class"),
@Param(name = "gen_source"),
@Param(name = "manifest_proto"),
@Param(name = "native_header_jar"),
@Param(name = "plugin_info"),
@Param(name = "sources"),
@Param(name = "source_jars"),
@Param(name = "resources"),
@Param(name = "resource_jars"),
@Param(name = "compilation_classpath"),
@Param(name = "classpath_resources"),
@Param(name = "sourcepath"),
@Param(name = "direct_jars"),
@Param(name = "bootclasspath"),
@Param(name = "compile_time_java_deps"),
@Param(name = "javac_opts"),
@Param(name = "strict_deps_mode"),
@Param(name = "target_label"),
@Param(name = "injecting_rule_kind"),
@Param(name = "enable_jspecify"),
@Param(name = "enable_direct_classpath"),
@Param(name = "additional_inputs"),
@Param(name = "additional_outputs"),
})
void createCompilationAction(
StarlarkRuleContextT ctx,
JavaToolchainT javaToolchain,
FileT output,
Object depsProto,
Object genClass,
Object genSource,
FileT manifestProto,
FileT nativeHeader,
Info pluginInfo,
Depset sourceFiles,
Sequence<?> sourceJars,
Sequence<?> resources,
Depset resourceJars,
Depset compileTimeClasspath,
Sequence<?> classpathResources,
Sequence<?> sourcepath,
Depset directJars,
Object bootClassPath,
Depset compileTimeJavaDeps,
Sequence<?> javacOpts,
String strictDepsMode,
Label targetLabel,
Object injectingRuleKind,
boolean enableJSpecify,
boolean enableDirectClasspath,
Sequence<?> additionalInputs,
Sequence<?> additionalOutputs)
throws EvalException, TypeException, RuleErrorException, LabelSyntaxException;

@StarlarkMethod(
name = "default_javac_opts",
// This function is experimental for now.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3545,6 +3545,8 @@ public void testNoArgsPrivateAPIsAreIndeedPrivate(String module, String api) thr

@Test
@TestParameters({
"{api: create_header_compilation_action}",
"{api: create_compilation_action}",
"{api: target_kind}",
"{api: get_build_info}",
"{api: collect_native_deps_dirs}",
Expand Down

0 comments on commit fb2df4d

Please sign in to comment.