diff options
author | 2018-06-01 12:42:44 -0700 | |
---|---|---|
committer | 2018-06-01 12:44:35 -0700 | |
commit | 619c6f0edea558d57b3494f5fe28bdf69ce7096a (patch) | |
tree | b10f6e5b5a69c8f481c680c608cbb99749ee7f8e /src/main/java/com/google | |
parent | fc4745471b5240422e2e1b48d55a14c082675620 (diff) |
Migrate remaining java skylark types to skylarkbuildapi
RELNOTES: None.
PiperOrigin-RevId: 198911668
Diffstat (limited to 'src/main/java/com/google')
16 files changed, 946 insertions, 595 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/JavaRules.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/JavaRules.java index 7da470d40a..e670f9f1c4 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/JavaRules.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/JavaRules.java @@ -88,7 +88,7 @@ public class JavaRules implements RuleSet { builder.addSkylarkAccessibleTopLevels("java_common", new JavaSkylarkCommon(BazelJavaSemantics.INSTANCE)); builder.addSkylarkAccessibleTopLevels("JavaInfo", JavaInfo.PROVIDER); - builder.addSkylarkAccessibleTopLevels("java_proto_common", JavaProtoSkylarkCommon.class); + builder.addSkylarkAccessibleTopLevels("java_proto_common", new JavaProtoSkylarkCommon()); try { builder.addWorkspaceFilePrefix( diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaConfiguration.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaConfiguration.java index 294e4054f5..4f2991cba6 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaConfiguration.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaConfiguration.java @@ -28,9 +28,7 @@ import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; import com.google.devtools.build.lib.events.Event; import com.google.devtools.build.lib.events.EventHandler; import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; -import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; -import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; -import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory; +import com.google.devtools.build.lib.skylarkbuildapi.java.JavaConfigurationApi; import com.google.devtools.common.options.TriState; import java.util.List; import java.util.Map; @@ -39,12 +37,7 @@ import javax.annotation.Nullable; /** A java compiler configuration containing the flags required for compilation. */ @AutoCodec @Immutable -@SkylarkModule( - name = "java", - doc = "A java compiler configuration.", - category = SkylarkModuleCategory.CONFIGURATION_FRAGMENT -) -public final class JavaConfiguration extends Fragment { +public final class JavaConfiguration extends Fragment implements JavaConfigurationApi { /** Values for the --java_classpath option */ public enum JavaClasspathMode { /** Use full transitive classpaths, the default behavior. */ @@ -303,19 +296,14 @@ public final class JavaConfiguration extends Fragment { this.useLegacyBazelJavaTest = useLegacyBazelJavaTest; } - @SkylarkCallable(name = "default_javac_flags", structField = true, - doc = "The default flags for the Java compiler.") + @Override // TODO(bazel-team): this is the command-line passed options, we should remove from skylark // probably. public ImmutableList<String> getDefaultJavacFlags() { return commandLineJavacFlags; } - @SkylarkCallable( - name = "strict_java_deps", - structField = true, - doc = "The value of the strict_java_deps flag." - ) + @Override public String getStrictJavaDepsName() { return strictJavaDeps.name().toLowerCase(); } diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaRuntimeClasspathProvider.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaRuntimeClasspathProvider.java index 63fbeedc6d..be9e29226b 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaRuntimeClasspathProvider.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaRuntimeClasspathProvider.java @@ -19,8 +19,7 @@ import com.google.devtools.build.lib.analysis.TransitiveInfoProvider; import com.google.devtools.build.lib.collect.nestedset.NestedSet; import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; -import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; -import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; +import com.google.devtools.build.lib.skylarkbuildapi.java.JavaRuntimeClasspathProviderApi; /** * Provider for the runtime classpath contributions of a Java binary. @@ -28,9 +27,9 @@ import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; * <p>Used to exclude already-available artifacts from related binaries (e.g. plugins). */ @Immutable -@SkylarkModule(name = "JavaRuntimeClasspathProvider", doc = "") @AutoCodec -public final class JavaRuntimeClasspathProvider implements TransitiveInfoProvider { +public final class JavaRuntimeClasspathProvider + implements TransitiveInfoProvider, JavaRuntimeClasspathProviderApi<Artifact> { private final NestedSet<Artifact> runtimeClasspath; @@ -41,7 +40,7 @@ public final class JavaRuntimeClasspathProvider implements TransitiveInfoProvide /** * Returns the artifacts included on the runtime classpath of this binary. */ - @SkylarkCallable(name = "runtime_classpath", documented = false, structField = true) + @Override public NestedSet<Artifact> getRuntimeClasspath() { return runtimeClasspath; } diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaRuntimeInfo.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaRuntimeInfo.java index 0e8c1b6e89..d48db3d595 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaRuntimeInfo.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaRuntimeInfo.java @@ -26,16 +26,14 @@ import com.google.devtools.build.lib.packages.NativeProvider; import com.google.devtools.build.lib.packages.RuleErrorConsumer; import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec.VisibleForSerialization; -import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; -import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; +import com.google.devtools.build.lib.skylarkbuildapi.java.JavaRuntimeInfoApi; import com.google.devtools.build.lib.vfs.PathFragment; import javax.annotation.Nullable; /** Information about the Java runtime used by the <code>java_*</code> rules. */ -@SkylarkModule(name = "JavaRuntimeInfo", doc = "Information about the Java runtime being used.") @Immutable @AutoCodec -public class JavaRuntimeInfo extends NativeInfo { +public class JavaRuntimeInfo extends NativeInfo implements JavaRuntimeInfoApi { public static final String SKYLARK_NAME = "JavaRuntimeInfo"; public static final NativeProvider<JavaRuntimeInfo> PROVIDER = @@ -125,33 +123,18 @@ public class JavaRuntimeInfo extends NativeInfo { } /** The root directory of the Java installation. */ - @SkylarkCallable( - name = "java_home", - doc = "Returns the execpath of the root of the Java installation.", - structField = true - ) + @Override public PathFragment javaHome() { return javaHome; } - @SkylarkCallable( - name = "java_executable_exec_path", - doc = "Returns the execpath of the Java executable.", - structField = true - ) + @Override /** The execpath of the Java binary. */ public PathFragment javaBinaryExecPath() { return javaBinaryExecPath; } - @SkylarkCallable( - name = "java_executable_runfiles_path", - doc = "Returns the path of the Java executable in runfiles trees. This should only be used " - + "when one needs to access the JVM during the execution of a binary or a test built " - + "by Bazel. In particular, when one needs to invoke the JVM during an action, " - + "java_executable_exec_path should be used instead.", - structField = true - ) + @Override /** The runfiles path of the Java binary. */ public PathFragment javaBinaryRunfilesPath() { return javaBinaryRunfilesPath; diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaSkylarkApiProvider.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaSkylarkApiProvider.java index fe3e5d0fb0..b02fcd1295 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaSkylarkApiProvider.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaSkylarkApiProvider.java @@ -24,26 +24,16 @@ import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; import com.google.devtools.build.lib.collect.nestedset.Order; import com.google.devtools.build.lib.packages.SkylarkProviderIdentifier; import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; -import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; -import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; -import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory; +import com.google.devtools.build.lib.skylarkbuildapi.java.JavaSkylarkApiProviderApi; import javax.annotation.Nullable; /** * A class that exposes the Java providers to Skylark. It is intended to provide a simple and stable * interface for Skylark users. */ -@SkylarkModule( - name = "JavaSkylarkApiProvider", - title = "java", - category = SkylarkModuleCategory.PROVIDER, - doc = - "Provides access to information about Java rules. Every Java-related target provides " - + "this struct, accessible as a <code>java</code> field on a " - + "<a href=\"Target.html\">target</a>." -) @AutoCodec -public final class JavaSkylarkApiProvider extends SkylarkApiProvider { +public final class JavaSkylarkApiProvider extends SkylarkApiProvider + implements JavaSkylarkApiProviderApi<Artifact> { /** The name of the field in Skylark used to access this class. */ public static final String NAME = "java"; /** The name of the field in Skylark proto aspects used to access this class. */ @@ -78,11 +68,7 @@ public final class JavaSkylarkApiProvider extends SkylarkApiProvider { return JavaInfo.getProvider(provider, getInfo()); } - @SkylarkCallable( - name = "source_jars", - doc = "Returns the Jars containing Java source files for the target.", - structField = true - ) + @Override public NestedSet<Artifact> getSourceJars() { JavaSourceJarsProvider sourceJarsProvider = getProvider(JavaSourceJarsProvider.class); if (sourceJarsProvider == null) { @@ -91,11 +77,7 @@ public final class JavaSkylarkApiProvider extends SkylarkApiProvider { return NestedSetBuilder.wrap(Order.STABLE_ORDER, sourceJarsProvider.getSourceJars()); } - @SkylarkCallable( - name = "transitive_deps", - doc = "Returns the transitive set of Jars required to build the target.", - structField = true - ) + @Override public NestedSet<Artifact> getTransitiveDeps() { JavaCompilationArgsProvider compilationArgsProvider = getProvider(JavaCompilationArgsProvider.class); @@ -105,11 +87,7 @@ public final class JavaSkylarkApiProvider extends SkylarkApiProvider { return compilationArgsProvider.getTransitiveCompileTimeJars(); } - @SkylarkCallable( - name = "transitive_runtime_deps", - doc = "Returns the transitive set of Jars required on the target's runtime classpath.", - structField = true - ) + @Override public NestedSet<Artifact> getTransitiveRuntimeDeps() { JavaCompilationArgsProvider compilationArgsProvider = getProvider(JavaCompilationArgsProvider.class); @@ -119,13 +97,7 @@ public final class JavaSkylarkApiProvider extends SkylarkApiProvider { return compilationArgsProvider.getRuntimeJars(); } - @SkylarkCallable( - name = "transitive_source_jars", - doc = - "Returns the Jars containing Java source files for the target and all of its transitive " - + "dependencies.", - structField = true - ) + @Override public NestedSet<Artifact> getTransitiveSourceJars() { JavaSourceJarsProvider sourceJarsProvider = getProvider(JavaSourceJarsProvider.class); if (sourceJarsProvider == null) { @@ -134,20 +106,12 @@ public final class JavaSkylarkApiProvider extends SkylarkApiProvider { return sourceJarsProvider.getTransitiveSourceJars(); } - @SkylarkCallable( - name = "outputs", - doc = "Returns information about outputs of this Java target.", - structField = true - ) + @Override public JavaRuleOutputJarsProvider getOutputJars() { return getProvider(JavaRuleOutputJarsProvider.class); } - @SkylarkCallable( - name = "transitive_exports", - structField = true, - doc = "Returns transitive set of labels that are being exported from this rule." - ) + @Override public NestedSet<Label> getTransitiveExports() { JavaExportsProvider exportsProvider = getProvider(JavaExportsProvider.class); if (exportsProvider != null) { @@ -157,22 +121,12 @@ public final class JavaSkylarkApiProvider extends SkylarkApiProvider { } } - @SkylarkCallable( - name = "annotation_processing", - structField = true, - allowReturnNones = true, - doc = "Returns information about annotation processing for this Java target." - ) + @Override public JavaGenJarsProvider getGenJarsProvider() { return getProvider(JavaGenJarsProvider.class); } - @SkylarkCallable( - name = "compilation_info", - structField = true, - allowReturnNones = true, - doc = "Returns compilation information for this Java target." - ) + @Override public JavaCompilationInfoProvider getCompilationInfoProvider() { return getProvider(JavaCompilationInfoProvider.class); } diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaSkylarkCommon.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaSkylarkCommon.java index 310e19733f..585770de10 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaSkylarkCommon.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaSkylarkCommon.java @@ -27,10 +27,7 @@ import com.google.devtools.build.lib.collect.nestedset.NestedSet; import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; import com.google.devtools.build.lib.events.Location; import com.google.devtools.build.lib.packages.Provider; -import com.google.devtools.build.lib.skylarkinterface.Param; -import com.google.devtools.build.lib.skylarkinterface.ParamType; -import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; -import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; +import com.google.devtools.build.lib.skylarkbuildapi.java.JavaCommonApi; import com.google.devtools.build.lib.syntax.Environment; import com.google.devtools.build.lib.syntax.EvalException; import com.google.devtools.build.lib.syntax.Runtime; @@ -40,123 +37,11 @@ import com.google.devtools.build.lib.syntax.Type; import javax.annotation.Nullable; /** A module that contains Skylark utilities for Java support. */ -@SkylarkModule(name = "java_common", doc = "Utilities for Java compilation support in Skylark.") -public class JavaSkylarkCommon { +public class JavaSkylarkCommon implements JavaCommonApi<Artifact, JavaInfo, SkylarkRuleContext, + ConfiguredTarget, SkylarkActionFactory> { private final JavaSemantics javaSemantics; - @SkylarkCallable( - name = "create_provider", - doc = - "Creates a JavaInfo from jars. compile_time/runtime_jars represent the outputs of the " - + "target providing a JavaInfo, while transitive_*_jars represent their dependencies." - + "<p>Note: compile_time_jars and runtime_jars are not automatically merged into the " - + "transitive jars (unless the given transitive_*_jars are empty) - if this is the " - + "desired behaviour the user should merge the jars before creating the provider." - + "<p>This function also creates actions to generate interface jars by default." - + "<p>When use_ijar is True, ijar will be run on the given compile_time_jars and the " - + "resulting interface jars will be stored as compile_jars, " - + "while the initial jars will be stored as full_compile_jars. " - + "<p>When use_ijar=False, the given compile_time_jars will be stored as both " - + "compile_jars and full_compile_jars. No actions are created. " - + "See JavaInfo#compile_jars and JavaInfo#full_compile_jars for more details." - + "<p>Currently only " - + "<a href='https://github.com/bazelbuild/bazel/tree/master/third_party/ijar'>" - + "ijar</a>" - + " is supported for generating interface jars. " - + "Header compilation is not yet supported.", - parameters = { - @Param( - name = "actions", - type = SkylarkActionFactory.class, - noneable = true, - defaultValue = "None", - doc = - "The ctx.actions object, used to register the actions for creating the " - + "interface jars. Only set if use_ijar=True."), - @Param( - name = "compile_time_jars", - positional = false, - named = true, - allowedTypes = { - @ParamType(type = SkylarkList.class), - @ParamType(type = SkylarkNestedSet.class), - }, - generic1 = Artifact.class, - defaultValue = "[]", - doc = "A list or a set of jars that should be used at compilation for a given target."), - @Param( - name = "runtime_jars", - positional = false, - named = true, - allowedTypes = { - @ParamType(type = SkylarkList.class), - @ParamType(type = SkylarkNestedSet.class), - }, - generic1 = Artifact.class, - defaultValue = "[]", - doc = "A list or a set of jars that should be used at runtime for a given target."), - @Param( - name = "use_ijar", - positional = false, - named = true, - type = Boolean.class, - defaultValue = "True", - doc = - "If True it will generate interface jars for every jar in compile_time_jars." - + "The generating interface jars will be stored as compile_jars " - + "and the initial (full) compile_time_jars will be stored as " - + "full_compile_jars. If False the given compile_jars will be " - + "stored as both compile_jars and full_compile_jars."), - @Param( - name = "java_toolchain", - positional = false, - named = true, - type = ConfiguredTarget.class, - noneable = true, - defaultValue = "None", - doc = - "A label pointing to a java_toolchain rule to be used for retrieving the ijar " - + "tool. Only set when use_ijar is True."), - @Param( - name = "transitive_compile_time_jars", - positional = false, - named = true, - allowedTypes = { - @ParamType(type = SkylarkList.class), - @ParamType(type = SkylarkNestedSet.class), - }, - generic1 = Artifact.class, - defaultValue = "[]", - doc = - "A list or set of compile time jars collected from the transitive closure of a " - + "rule."), - @Param( - name = "transitive_runtime_jars", - positional = false, - named = true, - allowedTypes = { - @ParamType(type = SkylarkList.class), - @ParamType(type = SkylarkNestedSet.class), - }, - generic1 = Artifact.class, - defaultValue = "[]", - doc = "A list or set of runtime jars collected from the transitive closure of a rule."), - @Param( - name = "source_jars", - positional = false, - named = true, - allowedTypes = { - @ParamType(type = SkylarkList.class), - @ParamType(type = SkylarkNestedSet.class), - }, - generic1 = Artifact.class, - defaultValue = "[]", - doc = - "A list or set of output source jars that contain the uncompiled source files " - + "including the source files generated by annotation processors if the case.") - }, - useLocation = true, - useEnvironment = true) + @Override public JavaInfo create( @Nullable Object actionsUnchecked, Object compileTimeJars, @@ -193,147 +78,12 @@ public class JavaSkylarkCommon { this.javaSemantics = javaSemantics; } - @SkylarkCallable( - name = "provider", - structField = true, - doc = "Returns the Java declared provider. <br>" - + "The same value is accessible as <code>JavaInfo</code>. <br>" - + "Prefer using <code>JavaInfo</code> in new code." - ) + @Override public Provider getJavaProvider() { return JavaInfo.PROVIDER; } - @SkylarkCallable( - name = "compile", - doc = "Compiles Java source files/jars from the implementation of a Skylark rule and returns a " - + "provider that represents the results of the compilation and can be added to the set of " - + "providers emitted by this rule.", - // There is one mandatory positional: the Skylark rule context. - mandatoryPositionals = 1, - parameters = { - @Param( - name = "source_jars", - positional = false, - named = true, - type = SkylarkList.class, - generic1 = Artifact.class, - defaultValue = "[]", - doc = "A list of the jars to be compiled. At least one of source_jars or source_files" - + " should be specified." - ), - @Param( - name = "source_files", - positional = false, - named = true, - type = SkylarkList.class, - generic1 = Artifact.class, - defaultValue = "[]", - doc = "A list of the Java source files to be compiled. At least one of source_jars or " - + "source_files should be specified." - ), - @Param( - name = "output", - positional = false, - named = true, - type = Artifact.class - ), - @Param( - name = "javac_opts", - positional = false, - named = true, - type = SkylarkList.class, - generic1 = String.class, - defaultValue = "[]", - doc = "A list of the desired javac options. Optional." - ), - @Param( - name = "deps", - positional = false, - named = true, - type = SkylarkList.class, - generic1 = JavaInfo.class, - defaultValue = "[]", - doc = "A list of dependencies. Optional." - ), - @Param( - name = "exports", - positional = false, - named = true, - type = SkylarkList.class, - generic1 = JavaInfo.class, - defaultValue = "[]", - doc = "A list of exports. Optional." - ), - @Param( - name = "plugins", - positional = false, - named = true, - type = SkylarkList.class, - generic1 = JavaInfo.class, - defaultValue = "[]", - doc = "A list of plugins. Optional." - ), - @Param( - name = "exported_plugins", - positional = false, - named = true, - type = SkylarkList.class, - generic1 = JavaInfo.class, - defaultValue = "[]", - doc = "A list of exported plugins. Optional." - ), - @Param( - name = "strict_deps", - defaultValue = "'ERROR'", - positional = false, - named = true, - type = String.class, - doc = "A string that specifies how to handle strict deps. Possible values: 'OFF', 'ERROR'," - + "'WARN' and 'DEFAULT'. For more details see " - + "https://docs.bazel.build/versions/master/bazel-user-manual.html#flag--strict_java_deps" - + ". By default 'ERROR'." - ), - @Param( - name = "java_toolchain", - positional = false, - named = true, - type = ConfiguredTarget.class, - doc = "A label pointing to a java_toolchain rule to be used for this compilation. " - + "Mandatory." - ), - @Param( - name = "host_javabase", - positional = false, - named = true, - type = ConfiguredTarget.class, - doc = "A label pointing to a JDK to be used for this compilation. Mandatory." - ), - @Param( - name = "sourcepath", - positional = false, - named = true, - type = SkylarkList.class, - generic1 = Artifact.class, - defaultValue = "[]" - ), - @Param( - name = "resources", - positional = false, - named = true, - type = SkylarkList.class, - generic1 = Artifact.class, - defaultValue = "[]" - ), - @Param( - name = "neverlink", - positional = false, - named = true, - type = Boolean.class, - defaultValue = "False" - ) - } - ) + @Override public JavaInfo createJavaCompileAction( SkylarkRuleContext skylarkRuleContext, SkylarkList<Artifact> sourceJars, @@ -371,44 +121,7 @@ public class JavaSkylarkCommon { javaSemantics); } - @SkylarkCallable( - name = "run_ijar", - doc = - "Runs ijar on a jar, stripping it of its method bodies. This helps reduce rebuilding " - + "of dependent jars during any recompiles consisting only of simple changes to " - + "method implementations. The return value is typically passed to " - + "<code><a class=\"anchor\" href=\"JavaInfo.html\">" - + "JavaInfo</a>#compile_jar</code>.", - parameters = { - @Param( - name = "actions", - named = true, - type = SkylarkActionFactory.class, - doc = "ctx.actions"), - @Param( - name = "jar", - positional = false, - named = true, - type = Artifact.class, - doc = "The jar to run ijar on."), - @Param( - name = "target_label", - positional = false, - named = true, - type = Label.class, - noneable = true, - defaultValue = "None", - doc = - "A target label to stamp the jar with. Used for <code>add_dep</code> support. " - + "Typically, you would pass <code>ctx.label</code> to stamp the jar " - + "with the current rule's label."), - @Param( - name = "java_toolchain", - positional = false, - named = true, - type = ConfiguredTarget.class, - doc = "A label pointing to a java_toolchain rule to used to find the ijar tool."), - }) + @Override public Artifact runIjar( SkylarkActionFactory actions, Artifact jar, @@ -420,99 +133,14 @@ public class JavaSkylarkCommon { actions, jar, targetLabel != Runtime.NONE ? (Label) targetLabel : null, javaToolchain); } - @SkylarkCallable( - name = "stamp_jar", - doc = - "Stamps a jar with a target label for <code>add_dep</code> support. " - + "The return value is typically passed to " - + "<code><a class=\"anchor\" href=\"JavaInfo.html\">" - + "JavaInfo</a>#compile_jar</code>. " - + "Prefer to use " - + "<code><a class=\"anchor\" href=\"java_common.html#run_ijar\">run_ijar</a></code> " - + "when possible.", - parameters = { - @Param( - name = "actions", - named = true, - type = SkylarkActionFactory.class, - doc = "ctx.actions"), - @Param( - name = "jar", - positional = false, - named = true, - type = Artifact.class, - doc = "The jar to run ijar on."), - @Param( - name = "target_label", - positional = false, - named = true, - type = Label.class, - doc = - "A target label to stamp the jar with. Used for <code>add_dep</code> support. " - + "Typically, you would pass <code>ctx.label</code> to stamp the jar " - + "with the current rule's label."), - @Param( - name = "java_toolchain", - positional = false, - named = true, - type = ConfiguredTarget.class, - doc = "A label pointing to a java_toolchain rule to used to find the ijar tool."), - }) + @Override public Artifact stampJar( SkylarkActionFactory actions, Artifact jar, Label targetLabel, ConfiguredTarget javaToolchain) throws EvalException { return JavaInfoBuildHelper.getInstance().stampJar(actions, jar, targetLabel, javaToolchain); } - @SkylarkCallable( - name = "pack_sources", - doc = - "Packs sources and source jars into a single source jar file. " - + "The return value is typically passed to" - + "<p><code><a class=\"anchor\" href=\"JavaInfo.html\">" - + "JavaInfo</a>#source_jar</code></p>.", - parameters = { - @Param( - name = "actions", - named = true, - type = SkylarkActionFactory.class, - doc = "ctx.actions"), - @Param( - name = "output_jar", - positional = false, - named = true, - type = Artifact.class, - doc = "The output jar of the rule. Used to name the resulting source jar."), - @Param( - name = "sources", - positional = false, - named = true, - type = SkylarkList.class, - generic1 = Artifact.class, - defaultValue = "[]", - doc = "A list of Java source files to be packed into the source jar."), - @Param( - name = "source_jars", - positional = false, - named = true, - type = SkylarkList.class, - generic1 = Artifact.class, - defaultValue = "[]", - doc = "A list of source jars to be packed into the source jar."), - @Param( - name = "java_toolchain", - positional = false, - named = true, - type = ConfiguredTarget.class, - doc = "A label pointing to a java_toolchain rule to used to find the ijar tool."), - @Param( - name = "host_javabase", - positional = false, - named = true, - type = ConfiguredTarget.class, - doc = "A label pointing to a JDK to be used for packing sources."), - }, - allowReturnNones = true) + @Override public Artifact packSources( SkylarkActionFactory actions, Artifact outputJar, @@ -525,18 +153,10 @@ public class JavaSkylarkCommon { .packSourceFiles(actions, outputJar, sourceFiles, sourceJars, javaToolchain, hostJavabase); } - @SkylarkCallable( - name = "default_javac_opts", - // This function is experimental for now. - documented = false, - // There's only one mandatory positional,the Skylark context - mandatoryPositionals = 1, - parameters = { - @Param(name = "java_toolchain_attr", positional = false, named = true, type = String.class) - }) + @Override // TODO(b/78512644): migrate callers to passing explicit javacopts or using custom toolchains, and // delete - public static ImmutableList<String> getDefaultJavacOpts( + public ImmutableList<String> getDefaultJavacOpts( SkylarkRuleContext skylarkRuleContext, String javaToolchainAttr) throws EvalException { RuleContext ruleContext = skylarkRuleContext.getRuleContext(); ConfiguredTarget javaToolchainConfigTarget = @@ -556,26 +176,14 @@ public class JavaSkylarkCommon { javacOptsFromAttr)); } - @SkylarkCallable( - name = "merge", - doc = "Merges the given providers into a single JavaInfo.", - // We have one positional argument: the list of providers to merge. - mandatoryPositionals = 1 - ) - public static JavaInfo mergeJavaProviders(SkylarkList<JavaInfo> providers) { + @Override + public JavaInfo mergeJavaProviders(SkylarkList<JavaInfo> providers) { return JavaInfo.merge(providers); } // TODO(b/65113771): Remove this method because it's incorrect. - @SkylarkCallable( - name = "make_non_strict", - doc = - "Returns a new Java provider whose direct-jars part is the union of both the direct and" - + " indirect jars of the given Java provider.", - // There's only one mandatory positional, the Java provider. - mandatoryPositionals = 1 - ) - public static JavaInfo makeNonStrict(JavaInfo javaInfo) { + @Override + public JavaInfo makeNonStrict(JavaInfo javaInfo) { JavaCompilationArgsProvider directCompilationArgs = makeNonStrict(javaInfo.getProvider(JavaCompilationArgsProvider.class)); @@ -601,14 +209,8 @@ public class JavaSkylarkCommon { provider.getCompileTimeJavaDependencyArtifacts()); } - @SkylarkCallable( - name = JavaRuntimeInfo.SKYLARK_NAME, - doc = - "The key used to retrieve the provider that contains information about the Java " - + "runtime being used.", - structField = true - ) - public static Provider getJavaRuntimeProvider() { + @Override + public Provider getJavaRuntimeProvider() { return JavaRuntimeInfo.PROVIDER; } diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaToolchainSkylarkApiProvider.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaToolchainSkylarkApiProvider.java index 0d9347227b..28937063ca 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaToolchainSkylarkApiProvider.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaToolchainSkylarkApiProvider.java @@ -17,28 +17,22 @@ package com.google.devtools.build.lib.rules.java; import com.google.devtools.build.lib.actions.Artifact; import com.google.devtools.build.lib.analysis.skylark.SkylarkApiProvider; import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; -import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; -import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; +import com.google.devtools.build.lib.skylarkbuildapi.java.JavaToolchainSkylarkApiProviderApi; import java.util.Iterator; /** * A class that exposes the java_toolchain providers to Skylark. It is intended to provide a simple * and stable interface for Skylark users. */ -@SkylarkModule( - name = "JavaToolchainSkylarkApiProvider", - doc = - "Provides access to information about the Java toolchain rule. " - + "Accessible as a 'java_toolchain' field on a Target struct." -) @AutoCodec -public final class JavaToolchainSkylarkApiProvider extends SkylarkApiProvider { +public final class JavaToolchainSkylarkApiProvider extends SkylarkApiProvider + implements JavaToolchainSkylarkApiProviderApi { /** The name of the field in Skylark used to access this class. */ public static final String NAME = "java_toolchain"; /** @return the input Java language level */ // TODO(cushon): remove this API; it bakes a deprecated detail of the javac API into Bazel - @SkylarkCallable(name = "source_version", doc = "The java source version.", structField = true) + @Override public String getSourceVersion() { JavaToolchainProvider javaToolchainProvider = JavaToolchainProvider.from(getInfo()); @@ -53,7 +47,7 @@ public final class JavaToolchainSkylarkApiProvider extends SkylarkApiProvider { /** @return the target Java language level */ // TODO(cushon): remove this API; it bakes a deprecated detail of the javac API into Bazel - @SkylarkCallable(name = "target_version", doc = "The java target version.", structField = true) + @Override public String getTargetVersion() { JavaToolchainProvider javaToolchainProvider = JavaToolchainProvider.from(getInfo()); @@ -67,11 +61,7 @@ public final class JavaToolchainSkylarkApiProvider extends SkylarkApiProvider { } /** @return The {@link Artifact} of the javac jar */ - @SkylarkCallable( - name = "javac_jar", - doc = "The javac jar.", - structField = true - ) + @Override public Artifact getJavacJar() { JavaToolchainProvider javaToolchainProvider = JavaToolchainProvider.from(getInfo()); diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoSkylarkCommon.java b/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoSkylarkCommon.java index 6ec3fcfd0a..cbd866a72f 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoSkylarkCommon.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoSkylarkCommon.java @@ -28,45 +28,16 @@ import com.google.devtools.build.lib.rules.proto.ProtoCompileActionBuilder; import com.google.devtools.build.lib.rules.proto.ProtoLangToolchainProvider; import com.google.devtools.build.lib.rules.proto.ProtoSupportDataProvider; import com.google.devtools.build.lib.rules.proto.SupportData; -import com.google.devtools.build.lib.skylarkinterface.Param; -import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; -import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; +import com.google.devtools.build.lib.skylarkbuildapi.java.JavaProtoCommonApi; import com.google.devtools.build.lib.syntax.EvalException; /** * A class that exposes Java common methods for proto compilation. */ -@SkylarkModule(name = "java_proto_common", doc = "Helper class for Java proto compilation.") -public class JavaProtoSkylarkCommon { - @SkylarkCallable( - name = "create_java_lite_proto_compile_action", - // This function is experimental for now. - documented = false, - // There's 2 mandatory positional arguments, the Skylark context and the ConfiguredTarget. - mandatoryPositionals = 2, - parameters = { - @Param( - name = "src_jar", - positional = false, - named = true, - type = Artifact.class - ), - @Param( - name = "proto_toolchain_attr", - positional = false, - named = true, - type = String.class - ), - @Param( - name = "flavour", - positional = false, - named = true, - type = String.class, - defaultValue = "java" - ) - } - ) - public static void createProtoCompileAction( +public class JavaProtoSkylarkCommon + implements JavaProtoCommonApi<Artifact, SkylarkRuleContext, ConfiguredTarget> { + @Override + public void createProtoCompileAction( SkylarkRuleContext skylarkRuleContext, ConfiguredTarget target, Artifact sourceJar, @@ -91,29 +62,15 @@ public class JavaProtoSkylarkCommon { /* allowServices= */ true); } - @SkylarkCallable( - name = "has_proto_sources", - doc = "Returns whether the given proto_library target contains proto sources. If there are no" - + " sources it means that the proto_library is an alias library, which exports its" - + " dependencies." - ) - public static boolean hasProtoSources(ConfiguredTarget target) { + @Override + public boolean hasProtoSources(ConfiguredTarget target) { SupportData supportData = checkNotNull(target.getProvider(ProtoSupportDataProvider.class).getSupportData()); return supportData.hasProtoSources(); } - @SkylarkCallable( - name = "toolchain_deps", - // This function is experimental for now. - documented = false, - // There's only one mandatory positional,the Skylark context - mandatoryPositionals = 1, - parameters = { - @Param(name = "proto_toolchain_attr", positional = false, named = true, type = String.class) - } - ) - public static JavaInfo getRuntimeToolchainProvider( + @Override + public JavaInfo getRuntimeToolchainProvider( SkylarkRuleContext skylarkRuleContext, String protoToolchainAttr) throws EvalException { TransitiveInfoCollection runtime = getProtoToolchainProvider(skylarkRuleContext, protoToolchainAttr).runtime(); @@ -125,19 +82,10 @@ public class JavaProtoSkylarkCommon { .build(); } - @SkylarkCallable( - name = "javac_opts", - // This function is experimental for now. - documented = false, - // There's only one mandatory positional,the Skylark context - mandatoryPositionals = 1, - parameters = { - @Param(name = "java_toolchain_attr", positional = false, named = true, type = String.class) - } - ) + @Override // TODO(b/78512644): migrate callers to passing explicit proto javacopts or using custom // toolchains, and delete - public static ImmutableList<String> getJavacOpts( + public ImmutableList<String> getJavacOpts( SkylarkRuleContext skylarkRuleContext, String javaToolchainAttr) throws EvalException { ConfiguredTarget javaToolchainConfigTarget = (ConfiguredTarget) checkNotNull(skylarkRuleContext.getAttr().getValue(javaToolchainAttr)); diff --git a/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/java/BUILD b/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/java/BUILD index 59948ecfe6..938435abf6 100644 --- a/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/java/BUILD +++ b/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/java/BUILD @@ -25,6 +25,7 @@ java_library( "//src/main/java/com/google/devtools/build/lib/cmdline", "//src/main/java/com/google/devtools/build/lib/collect/nestedset", "//src/main/java/com/google/devtools/build/lib/skylarkbuildapi", + "//src/main/java/com/google/devtools/build/lib/vfs:pathfragment", "//third_party:guava", "//third_party:jsr305", ], diff --git a/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/java/JavaCommonApi.java b/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/java/JavaCommonApi.java new file mode 100644 index 0000000000..79c9b0a2cd --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/java/JavaCommonApi.java @@ -0,0 +1,512 @@ +// Copyright 2018 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.skylarkbuildapi.java; + +import com.google.common.collect.ImmutableList; +import com.google.devtools.build.lib.cmdline.Label; +import com.google.devtools.build.lib.events.Location; +import com.google.devtools.build.lib.skylarkbuildapi.FileApi; +import com.google.devtools.build.lib.skylarkbuildapi.ProviderApi; +import com.google.devtools.build.lib.skylarkbuildapi.SkylarkActionFactoryApi; +import com.google.devtools.build.lib.skylarkbuildapi.SkylarkRuleContextApi; +import com.google.devtools.build.lib.skylarkbuildapi.TransitiveInfoCollectionApi; +import com.google.devtools.build.lib.skylarkinterface.Param; +import com.google.devtools.build.lib.skylarkinterface.ParamType; +import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; +import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; +import com.google.devtools.build.lib.syntax.Environment; +import com.google.devtools.build.lib.syntax.EvalException; +import com.google.devtools.build.lib.syntax.SkylarkList; +import com.google.devtools.build.lib.syntax.SkylarkNestedSet; +import javax.annotation.Nullable; + +/** + * Utilities for Java compilation support in Skylark. + */ +@SkylarkModule(name = "java_common", doc = "Utilities for Java compilation support in Skylark.") +public interface JavaCommonApi<FileT extends FileApi, JavaInfoT extends JavaInfoApi<FileT>, + SkylarkRuleContextT extends SkylarkRuleContextApi, + TransitiveInfoCollectionT extends TransitiveInfoCollectionApi, + SkylarkActionFactoryT extends SkylarkActionFactoryApi> { + + @SkylarkCallable( + name = "create_provider", + doc = + "Creates a JavaInfo from jars. compile_time/runtime_jars represent the outputs of the " + + "target providing a JavaInfo, while transitive_*_jars represent their dependencies." + + "<p>Note: compile_time_jars and runtime_jars are not automatically merged into the " + + "transitive jars (unless the given transitive_*_jars are empty) - if this is the " + + "desired behaviour the user should merge the jars before creating the provider." + + "<p>This function also creates actions to generate interface jars by default." + + "<p>When use_ijar is True, ijar will be run on the given compile_time_jars and the " + + "resulting interface jars will be stored as compile_jars, " + + "while the initial jars will be stored as full_compile_jars. " + + "<p>When use_ijar=False, the given compile_time_jars will be stored as both " + + "compile_jars and full_compile_jars. No actions are created. " + + "See JavaInfo#compile_jars and JavaInfo#full_compile_jars for more details." + + "<p>Currently only " + + "<a href='https://github.com/bazelbuild/bazel/tree/master/third_party/ijar'>" + + "ijar</a>" + + " is supported for generating interface jars. " + + "Header compilation is not yet supported.", + parameters = { + @Param( + name = "actions", + type = SkylarkActionFactoryApi.class, + noneable = true, + defaultValue = "None", + doc = + "The ctx.actions object, used to register the actions for creating the " + + "interface jars. Only set if use_ijar=True."), + @Param( + name = "compile_time_jars", + positional = false, + named = true, + allowedTypes = { + @ParamType(type = SkylarkList.class), + @ParamType(type = SkylarkNestedSet.class), + }, + generic1 = FileApi.class, + defaultValue = "[]", + doc = "A list or a set of jars that should be used at compilation for a given target."), + @Param( + name = "runtime_jars", + positional = false, + named = true, + allowedTypes = { + @ParamType(type = SkylarkList.class), + @ParamType(type = SkylarkNestedSet.class), + }, + generic1 = FileApi.class, + defaultValue = "[]", + doc = "A list or a set of jars that should be used at runtime for a given target."), + @Param( + name = "use_ijar", + positional = false, + named = true, + type = Boolean.class, + defaultValue = "True", + doc = + "If True it will generate interface jars for every jar in compile_time_jars." + + "The generating interface jars will be stored as compile_jars " + + "and the initial (full) compile_time_jars will be stored as " + + "full_compile_jars. If False the given compile_jars will be " + + "stored as both compile_jars and full_compile_jars."), + @Param( + name = "java_toolchain", + positional = false, + named = true, + type = TransitiveInfoCollectionApi.class, + noneable = true, + defaultValue = "None", + doc = + "A label pointing to a java_toolchain rule to be used for retrieving the ijar " + + "tool. Only set when use_ijar is True."), + @Param( + name = "transitive_compile_time_jars", + positional = false, + named = true, + allowedTypes = { + @ParamType(type = SkylarkList.class), + @ParamType(type = SkylarkNestedSet.class), + }, + generic1 = FileApi.class, + defaultValue = "[]", + doc = + "A list or set of compile time jars collected from the transitive closure of a " + + "rule."), + @Param( + name = "transitive_runtime_jars", + positional = false, + named = true, + allowedTypes = { + @ParamType(type = SkylarkList.class), + @ParamType(type = SkylarkNestedSet.class), + }, + generic1 = FileApi.class, + defaultValue = "[]", + doc = "A list or set of runtime jars collected from the transitive closure of a rule."), + @Param( + name = "source_jars", + positional = false, + named = true, + allowedTypes = { + @ParamType(type = SkylarkList.class), + @ParamType(type = SkylarkNestedSet.class), + }, + generic1 = FileApi.class, + defaultValue = "[]", + doc = + "A list or set of output source jars that contain the uncompiled source files " + + "including the source files generated by annotation processors if the case.") + }, + useLocation = true, + useEnvironment = true) + public JavaInfoT create( + @Nullable Object actionsUnchecked, + Object compileTimeJars, + Object runtimeJars, + Boolean useIjar, + @Nullable Object javaToolchainUnchecked, + Object transitiveCompileTimeJars, + Object transitiveRuntimeJars, + Object sourceJars, + Location location, + Environment environment) + throws EvalException; + + @SkylarkCallable( + name = "provider", + structField = true, + doc = "Returns the Java declared provider. <br>" + + "The same value is accessible as <code>JavaInfo</code>. <br>" + + "Prefer using <code>JavaInfo</code> in new code." + ) + public ProviderApi getJavaProvider(); + + @SkylarkCallable( + name = "compile", + doc = "Compiles Java source files/jars from the implementation of a Skylark rule and returns a " + + "provider that represents the results of the compilation and can be added to the set of " + + "providers emitted by this rule.", + // There is one mandatory positional: the Skylark rule context. + mandatoryPositionals = 1, + parameters = { + @Param( + name = "source_jars", + positional = false, + named = true, + type = SkylarkList.class, + generic1 = FileApi.class, + defaultValue = "[]", + doc = "A list of the jars to be compiled. At least one of source_jars or source_files" + + " should be specified." + ), + @Param( + name = "source_files", + positional = false, + named = true, + type = SkylarkList.class, + generic1 = FileApi.class, + defaultValue = "[]", + doc = "A list of the Java source files to be compiled. At least one of source_jars or " + + "source_files should be specified." + ), + @Param( + name = "output", + positional = false, + named = true, + type = FileApi.class + ), + @Param( + name = "javac_opts", + positional = false, + named = true, + type = SkylarkList.class, + generic1 = String.class, + defaultValue = "[]", + doc = "A list of the desired javac options. Optional." + ), + @Param( + name = "deps", + positional = false, + named = true, + type = SkylarkList.class, + generic1 = JavaInfoApi.class, + defaultValue = "[]", + doc = "A list of dependencies. Optional." + ), + @Param( + name = "exports", + positional = false, + named = true, + type = SkylarkList.class, + generic1 = JavaInfoApi.class, + defaultValue = "[]", + doc = "A list of exports. Optional." + ), + @Param( + name = "plugins", + positional = false, + named = true, + type = SkylarkList.class, + generic1 = JavaInfoApi.class, + defaultValue = "[]", + doc = "A list of plugins. Optional." + ), + @Param( + name = "exported_plugins", + positional = false, + named = true, + type = SkylarkList.class, + generic1 = JavaInfoApi.class, + defaultValue = "[]", + doc = "A list of exported plugins. Optional." + ), + @Param( + name = "strict_deps", + defaultValue = "'ERROR'", + positional = false, + named = true, + type = String.class, + doc = "A string that specifies how to handle strict deps. Possible values: 'OFF', 'ERROR'," + + "'WARN' and 'DEFAULT'. For more details see " + + "https://docs.bazel.build/versions/master/bazel-user-manual.html#flag--strict_java_deps" + + ". By default 'ERROR'." + ), + @Param( + name = "java_toolchain", + positional = false, + named = true, + type = TransitiveInfoCollectionApi.class, + doc = "A label pointing to a java_toolchain rule to be used for this compilation. " + + "Mandatory." + ), + @Param( + name = "host_javabase", + positional = false, + named = true, + type = TransitiveInfoCollectionApi.class, + doc = "A label pointing to a JDK to be used for this compilation. Mandatory." + ), + @Param( + name = "sourcepath", + positional = false, + named = true, + type = SkylarkList.class, + generic1 = FileApi.class, + defaultValue = "[]" + ), + @Param( + name = "resources", + positional = false, + named = true, + type = SkylarkList.class, + generic1 = FileApi.class, + defaultValue = "[]" + ), + @Param( + name = "neverlink", + positional = false, + named = true, + type = Boolean.class, + defaultValue = "False" + ) + } + ) + public JavaInfoT createJavaCompileAction( + SkylarkRuleContextT skylarkRuleContext, + SkylarkList<FileT> sourceJars, + SkylarkList<FileT> sourceFiles, + FileT outputJar, + SkylarkList<String> javacOpts, + SkylarkList<JavaInfoT> deps, + SkylarkList<JavaInfoT> exports, + SkylarkList<JavaInfoT> plugins, + SkylarkList<JavaInfoT> exportedPlugins, + String strictDepsMode, + TransitiveInfoCollectionT javaToolchain, + TransitiveInfoCollectionT hostJavabase, + SkylarkList<FileT> sourcepathEntries, + SkylarkList<FileT> resources, + Boolean neverlink) throws EvalException, InterruptedException; + + @SkylarkCallable( + name = "run_ijar", + doc = + "Runs ijar on a jar, stripping it of its method bodies. This helps reduce rebuilding " + + "of dependent jars during any recompiles consisting only of simple changes to " + + "method implementations. The return value is typically passed to " + + "<code><a class=\"anchor\" href=\"JavaInfo.html\">" + + "JavaInfo</a>#compile_jar</code>.", + parameters = { + @Param( + name = "actions", + named = true, + type = SkylarkActionFactoryApi.class, + doc = "ctx.actions"), + @Param( + name = "jar", + positional = false, + named = true, + type = FileApi.class, + doc = "The jar to run ijar on."), + @Param( + name = "target_label", + positional = false, + named = true, + type = Label.class, + noneable = true, + defaultValue = "None", + doc = + "A target label to stamp the jar with. Used for <code>add_dep</code> support. " + + "Typically, you would pass <code>ctx.label</code> to stamp the jar " + + "with the current rule's label."), + @Param( + name = "java_toolchain", + positional = false, + named = true, + type = TransitiveInfoCollectionApi.class, + doc = "A label pointing to a java_toolchain rule to used to find the ijar tool."), + }) + public FileApi runIjar( + SkylarkActionFactoryT actions, + FileT jar, + Object targetLabel, + TransitiveInfoCollectionT javaToolchain) + throws EvalException; + + @SkylarkCallable( + name = "stamp_jar", + doc = + "Stamps a jar with a target label for <code>add_dep</code> support. " + + "The return value is typically passed to " + + "<code><a class=\"anchor\" href=\"JavaInfo.html\">" + + "JavaInfo</a>#compile_jar</code>. " + + "Prefer to use " + + "<code><a class=\"anchor\" href=\"java_common.html#run_ijar\">run_ijar</a></code> " + + "when possible.", + parameters = { + @Param( + name = "actions", + named = true, + type = SkylarkActionFactoryApi.class, + doc = "ctx.actions"), + @Param( + name = "jar", + positional = false, + named = true, + type = FileApi.class, + doc = "The jar to run ijar on."), + @Param( + name = "target_label", + positional = false, + named = true, + type = Label.class, + doc = + "A target label to stamp the jar with. Used for <code>add_dep</code> support. " + + "Typically, you would pass <code>ctx.label</code> to stamp the jar " + + "with the current rule's label."), + @Param( + name = "java_toolchain", + positional = false, + named = true, + type = TransitiveInfoCollectionApi.class, + doc = "A label pointing to a java_toolchain rule to used to find the ijar tool."), + }) + public FileApi stampJar( + SkylarkActionFactoryT actions, FileT jar, Label targetLabel, + TransitiveInfoCollectionT javaToolchain) + throws EvalException; + + @SkylarkCallable( + name = "pack_sources", + doc = + "Packs sources and source jars into a single source jar file. " + + "The return value is typically passed to" + + "<p><code><a class=\"anchor\" href=\"JavaInfo.html\">" + + "JavaInfo</a>#source_jar</code></p>.", + parameters = { + @Param( + name = "actions", + named = true, + type = SkylarkActionFactoryApi.class, + doc = "ctx.actions"), + @Param( + name = "output_jar", + positional = false, + named = true, + type = FileApi.class, + doc = "The output jar of the rule. Used to name the resulting source jar."), + @Param( + name = "sources", + positional = false, + named = true, + type = SkylarkList.class, + generic1 = FileApi.class, + defaultValue = "[]", + doc = "A list of Java source files to be packed into the source jar."), + @Param( + name = "source_jars", + positional = false, + named = true, + type = SkylarkList.class, + generic1 = FileApi.class, + defaultValue = "[]", + doc = "A list of source jars to be packed into the source jar."), + @Param( + name = "java_toolchain", + positional = false, + named = true, + type = TransitiveInfoCollectionApi.class, + doc = "A label pointing to a java_toolchain rule to used to find the ijar tool."), + @Param( + name = "host_javabase", + positional = false, + named = true, + type = TransitiveInfoCollectionApi.class, + doc = "A label pointing to a JDK to be used for packing sources."), + }, + allowReturnNones = true) + public FileApi packSources( + SkylarkActionFactoryT actions, + FileT outputJar, + SkylarkList<FileT> sourceFiles, + SkylarkList<FileT> sourceJars, + TransitiveInfoCollectionT javaToolchain, + TransitiveInfoCollectionT hostJavabase) + throws EvalException; + + @SkylarkCallable( + name = "default_javac_opts", + // This function is experimental for now. + documented = false, + // There's only one mandatory positional,the Skylark context + mandatoryPositionals = 1, + parameters = { + @Param(name = "java_toolchain_attr", positional = false, named = true, type = String.class) + }) + // TODO(b/78512644): migrate callers to passing explicit javacopts or using custom toolchains, and + // delete + public ImmutableList<String> getDefaultJavacOpts( + SkylarkRuleContextT skylarkRuleContext, String javaToolchainAttr) throws EvalException; + + @SkylarkCallable( + name = "merge", + doc = "Merges the given providers into a single JavaInfo.", + // We have one positional argument: the list of providers to merge. + mandatoryPositionals = 1 + ) + public JavaInfoT mergeJavaProviders(SkylarkList<JavaInfoT> providers); + + @SkylarkCallable( + name = "make_non_strict", + doc = + "Returns a new Java provider whose direct-jars part is the union of both the direct and" + + " indirect jars of the given Java provider.", + // There's only one mandatory positional, the Java provider. + mandatoryPositionals = 1 + ) + public JavaInfoT makeNonStrict(JavaInfoT javaInfo); + + @SkylarkCallable( + name = "JavaRuntimeInfo", + doc = + "The key used to retrieve the provider that contains information about the Java " + + "runtime being used.", + structField = true + ) + public ProviderApi getJavaRuntimeProvider(); +} diff --git a/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/java/JavaConfigurationApi.java b/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/java/JavaConfigurationApi.java new file mode 100644 index 0000000000..eafb90e4bd --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/java/JavaConfigurationApi.java @@ -0,0 +1,44 @@ +// Copyright 2018 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.skylarkbuildapi.java; + +import com.google.common.collect.ImmutableList; +import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; +import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; +import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory; + +/** + * A java compiler configuration. + */ +@SkylarkModule( + name = "java", + doc = "A java compiler configuration.", + category = SkylarkModuleCategory.CONFIGURATION_FRAGMENT +) +public interface JavaConfigurationApi { + + @SkylarkCallable(name = "default_javac_flags", structField = true, + doc = "The default flags for the Java compiler.") + // TODO(bazel-team): this is the command-line passed options, we should remove from skylark + // probably. + public ImmutableList<String> getDefaultJavacFlags(); + + @SkylarkCallable( + name = "strict_java_deps", + structField = true, + doc = "The value of the strict_java_deps flag." + ) + public String getStrictJavaDepsName(); +} diff --git a/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/java/JavaProtoCommonApi.java b/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/java/JavaProtoCommonApi.java new file mode 100644 index 0000000000..3afc99ec0c --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/java/JavaProtoCommonApi.java @@ -0,0 +1,104 @@ +// Copyright 2018 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.skylarkbuildapi.java; + +import com.google.common.collect.ImmutableList; +import com.google.devtools.build.lib.skylarkbuildapi.FileApi; +import com.google.devtools.build.lib.skylarkbuildapi.SkylarkRuleContextApi; +import com.google.devtools.build.lib.skylarkbuildapi.TransitiveInfoCollectionApi; +import com.google.devtools.build.lib.skylarkinterface.Param; +import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; +import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; +import com.google.devtools.build.lib.syntax.EvalException; + +/** + * Helper class for Java proto compilation. + */ +@SkylarkModule(name = "java_proto_common", doc = "Helper class for Java proto compilation.") +public interface JavaProtoCommonApi<FileT extends FileApi, + SkylarkRuleContextT extends SkylarkRuleContextApi, + TransitiveInfoCollectionT extends TransitiveInfoCollectionApi> { + + @SkylarkCallable( + name = "create_java_lite_proto_compile_action", + // This function is experimental for now. + documented = false, + // There's 2 mandatory positional arguments, the Skylark context and the ConfiguredTarget. + mandatoryPositionals = 2, + parameters = { + @Param( + name = "src_jar", + positional = false, + named = true, + type = FileApi.class + ), + @Param( + name = "proto_toolchain_attr", + positional = false, + named = true, + type = String.class + ), + @Param( + name = "flavour", + positional = false, + named = true, + type = String.class, + defaultValue = "java" + ) + } + ) + public void createProtoCompileAction( + SkylarkRuleContextT skylarkRuleContext, + TransitiveInfoCollectionT target, + FileT sourceJar, + String protoToolchainAttr, + String flavour) throws EvalException; + + @SkylarkCallable( + name = "has_proto_sources", + doc = "Returns whether the given proto_library target contains proto sources. If there are no" + + " sources it means that the proto_library is an alias library, which exports its" + + " dependencies." + ) + public boolean hasProtoSources(TransitiveInfoCollectionT target); + + @SkylarkCallable( + name = "toolchain_deps", + // This function is experimental for now. + documented = false, + // There's only one mandatory positional,the Skylark context + mandatoryPositionals = 1, + parameters = { + @Param(name = "proto_toolchain_attr", positional = false, named = true, type = String.class) + } + ) + public JavaInfoApi<FileT> getRuntimeToolchainProvider( + SkylarkRuleContextT skylarkRuleContext, String protoToolchainAttr) throws EvalException; + + @SkylarkCallable( + name = "javac_opts", + // This function is experimental for now. + documented = false, + // There's only one mandatory positional,the Skylark context + mandatoryPositionals = 1, + parameters = { + @Param(name = "java_toolchain_attr", positional = false, named = true, type = String.class) + } + ) + // TODO(b/78512644): migrate callers to passing explicit proto javacopts or using custom + // toolchains, and delete + public ImmutableList<String> getJavacOpts( + SkylarkRuleContextT skylarkRuleContext, String javaToolchainAttr) throws EvalException; +} diff --git a/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/java/JavaRuntimeClasspathProviderApi.java b/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/java/JavaRuntimeClasspathProviderApi.java new file mode 100644 index 0000000000..8d625ed44d --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/java/JavaRuntimeClasspathProviderApi.java @@ -0,0 +1,30 @@ +// Copyright 2018 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.skylarkbuildapi.java; + +import com.google.devtools.build.lib.collect.nestedset.NestedSet; +import com.google.devtools.build.lib.skylarkbuildapi.FileApi; +import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; +import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; + +/** + * Provider for the runtime classpath contributions of a Java binary. + */ +@SkylarkModule(name = "JavaRuntimeClasspathProvider", doc = "", documented = false) +public interface JavaRuntimeClasspathProviderApi<FileT extends FileApi> { + + @SkylarkCallable(name = "runtime_classpath", documented = false, structField = true) + public NestedSet<FileT> getRuntimeClasspath(); +} diff --git a/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/java/JavaRuntimeInfoApi.java b/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/java/JavaRuntimeInfoApi.java new file mode 100644 index 0000000000..bea3b41801 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/java/JavaRuntimeInfoApi.java @@ -0,0 +1,53 @@ +// Copyright 2018 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.skylarkbuildapi.java; + +import com.google.devtools.build.lib.skylarkbuildapi.StructApi; +import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; +import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; +import com.google.devtools.build.lib.vfs.PathFragment; + +/** + * Information about the Java runtime being used. + */ +@SkylarkModule(name = "JavaRuntimeInfo", doc = "Information about the Java runtime being used.") +public interface JavaRuntimeInfoApi extends StructApi { + + @SkylarkCallable( + name = "java_home", + doc = "Returns the execpath of the root of the Java installation.", + structField = true + ) + public PathFragment javaHome(); + + @SkylarkCallable( + name = "java_executable_exec_path", + doc = "Returns the execpath of the Java executable.", + structField = true + ) + /** The execpath of the Java binary. */ + public PathFragment javaBinaryExecPath(); + + @SkylarkCallable( + name = "java_executable_runfiles_path", + doc = "Returns the path of the Java executable in runfiles trees. This should only be used " + + "when one needs to access the JVM during the execution of a binary or a test built " + + "by Bazel. In particular, when one needs to invoke the JVM during an action, " + + "java_executable_exec_path should be used instead.", + structField = true + ) + /** The runfiles path of the Java binary. */ + public PathFragment javaBinaryRunfilesPath(); +} diff --git a/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/java/JavaSkylarkApiProviderApi.java b/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/java/JavaSkylarkApiProviderApi.java new file mode 100644 index 0000000000..6afac7e8aa --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/java/JavaSkylarkApiProviderApi.java @@ -0,0 +1,98 @@ +// Copyright 2018 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.skylarkbuildapi.java; + +import com.google.devtools.build.lib.cmdline.Label; +import com.google.devtools.build.lib.collect.nestedset.NestedSet; +import com.google.devtools.build.lib.skylarkbuildapi.FileApi; +import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; +import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; +import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory; + +/** + * Provides access to information about Java rules. Every Java-related target provides + * this struct, accessible as a java field on a Target. + */ +@SkylarkModule( + name = "JavaSkylarkApiProvider", + title = "java", + category = SkylarkModuleCategory.PROVIDER, + doc = + "Provides access to information about Java rules. Every Java-related target provides " + + "this struct, accessible as a <code>java</code> field on a " + + "<a href=\"Target.html\">target</a>." +) +public interface JavaSkylarkApiProviderApi<FileT extends FileApi> { + + @SkylarkCallable( + name = "source_jars", + doc = "Returns the Jars containing Java source files for the target.", + structField = true + ) + public NestedSet<FileT> getSourceJars(); + + @SkylarkCallable( + name = "transitive_deps", + doc = "Returns the transitive set of Jars required to build the target.", + structField = true + ) + public NestedSet<FileT> getTransitiveDeps(); + + @SkylarkCallable( + name = "transitive_runtime_deps", + doc = "Returns the transitive set of Jars required on the target's runtime classpath.", + structField = true + ) + public NestedSet<FileT> getTransitiveRuntimeDeps(); + + @SkylarkCallable( + name = "transitive_source_jars", + doc = + "Returns the Jars containing Java source files for the target and all of its transitive " + + "dependencies.", + structField = true + ) + public NestedSet<FileT> getTransitiveSourceJars(); + + @SkylarkCallable( + name = "outputs", + doc = "Returns information about outputs of this Java target.", + structField = true + ) + public JavaRuleOutputJarsProviderApi<?> getOutputJars(); + + @SkylarkCallable( + name = "transitive_exports", + structField = true, + doc = "Returns transitive set of labels that are being exported from this rule." + ) + public NestedSet<Label> getTransitiveExports(); + + @SkylarkCallable( + name = "annotation_processing", + structField = true, + allowReturnNones = true, + doc = "Returns information about annotation processing for this Java target." + ) + public JavaAnnotationProcessingApi<?> getGenJarsProvider(); + + @SkylarkCallable( + name = "compilation_info", + structField = true, + allowReturnNones = true, + doc = "Returns compilation information for this Java target." + ) + public JavaCompilationInfoProviderApi<?> getCompilationInfoProvider(); +} diff --git a/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/java/JavaToolchainSkylarkApiProviderApi.java b/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/java/JavaToolchainSkylarkApiProviderApi.java new file mode 100644 index 0000000000..49a8704c28 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/java/JavaToolchainSkylarkApiProviderApi.java @@ -0,0 +1,45 @@ +// Copyright 2018 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.skylarkbuildapi.java; + +import com.google.devtools.build.lib.skylarkbuildapi.FileApi; +import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; +import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; + +/** + * Provides access to information about the Java toolchain rule. + * Accessible as a 'java_toolchain' field on a Target struct. + */ +@SkylarkModule( + name = "JavaToolchainSkylarkApiProvider", + doc = + "Provides access to information about the Java toolchain rule. " + + "Accessible as a 'java_toolchain' field on a Target struct." +) +public interface JavaToolchainSkylarkApiProviderApi { + + @SkylarkCallable(name = "source_version", doc = "The java source version.", structField = true) + public String getSourceVersion(); + + @SkylarkCallable(name = "target_version", doc = "The java target version.", structField = true) + public String getTargetVersion(); + + @SkylarkCallable( + name = "javac_jar", + doc = "The javac jar.", + structField = true + ) + public FileApi getJavacJar(); +} |