diff --git a/src/main/starlark/builtins_bzl/bazel/java/bazel_java_binary_deploy_jar.bzl b/src/main/starlark/builtins_bzl/bazel/java/bazel_java_binary_deploy_jar.bzl
index bed4a2de7a38db..204de369a57a93 100644
--- a/src/main/starlark/builtins_bzl/bazel/java/bazel_java_binary_deploy_jar.bzl
+++ b/src/main/starlark/builtins_bzl/bazel/java/bazel_java_binary_deploy_jar.bzl
@@ -20,7 +20,7 @@ the generating actions, so that the runfiles symlink tree is staged for the depl
load(":common/java/java_binary_deploy_jar.bzl", "create_deploy_archives", "make_deploy_jars_rule")
load(":common/java/java_binary.bzl", "InternalDeployJarInfo")
-load(":common/java/java_common.bzl", "get_build_info")
+load(":common/java/java_common_internal_for_builtins.bzl", "get_build_info")
def _stamping_enabled(ctx, stamp):
if ctx.configuration.is_tool_configuration():
diff --git a/src/main/starlark/builtins_bzl/common/java/basic_java_library.bzl b/src/main/starlark/builtins_bzl/common/java/basic_java_library.bzl
index d1c6c781a80a45..bb6d9c4c2d8056 100644
--- a/src/main/starlark/builtins_bzl/common/java/basic_java_library.bzl
+++ b/src/main/starlark/builtins_bzl/common/java/basic_java_library.bzl
@@ -24,7 +24,8 @@ load(":common/java/proguard_validation.bzl", "validate_proguard_specs")
load(":common/cc/cc_info.bzl", "CcInfo")
load(":common/java/java_info.bzl", "JavaInfo")
load(":common/java/java_plugin_info.bzl", "JavaPluginInfo")
-load(":common/java/java_common.bzl", "java_common", "target_kind")
+load(":common/java/java_common.bzl", "java_common")
+load(":common/java/java_common_internal_for_builtins.bzl", "target_kind")
coverage_common = _builtins.toplevel.coverage_common
diff --git a/src/main/starlark/builtins_bzl/common/java/compile_action.bzl b/src/main/starlark/builtins_bzl/common/java/compile_action.bzl
index 0e4e9a36d533b7..fdc29b5f6aa5d8 100644
--- a/src/main/starlark/builtins_bzl/common/java/compile_action.bzl
+++ b/src/main/starlark/builtins_bzl/common/java/compile_action.bzl
@@ -17,7 +17,7 @@ Java compile action
"""
load(":common/java/java_semantics.bzl", "semantics")
-load(":common/java/java_common.bzl", "java_common")
+load(":common/java/java_common_internal_for_builtins.bzl", _compile_private_for_builtins = "compile")
def _filter_strict_deps(mode):
return "error" if mode in ["strict", "default"] else mode
@@ -130,8 +130,10 @@ def compile_action(
or resources present, whereas runfiles in this case are empty.
"""
- java_info = java_common.compile(
+ java_info = _compile_private_for_builtins(
ctx,
+ output = output_class_jar,
+ java_toolchain = semantics.find_java_toolchain(ctx),
source_files = source_files,
source_jars = source_jars,
resources = resources,
@@ -145,8 +147,6 @@ def compile_action(
exported_plugins = exported_plugins,
javac_opts = [ctx.expand_location(opt) for opt in javacopts],
neverlink = neverlink,
- java_toolchain = semantics.find_java_toolchain(ctx),
- output = output_class_jar,
output_source_jar = output_source_jar,
strict_deps = _filter_strict_deps(strict_deps),
enable_compile_jar_action = enable_compile_jar_action,
diff --git a/src/main/starlark/builtins_bzl/common/java/java_binary.bzl b/src/main/starlark/builtins_bzl/common/java/java_binary.bzl
index 8e3e924b293dc0..1939d91f1fcc1b 100644
--- a/src/main/starlark/builtins_bzl/common/java/java_binary.bzl
+++ b/src/main/starlark/builtins_bzl/common/java/java_binary.bzl
@@ -24,11 +24,11 @@ load(":common/cc/cc_info.bzl", "CcInfo")
load(":common/paths.bzl", "paths")
load(":common/java/java_info.bzl", "JavaInfo")
load(":common/java/java_plugin_info.bzl", "JavaPluginInfo")
+load(":common/java/java_common.bzl", "java_common")
load(
- ":common/java/java_common.bzl",
+ ":common/java/java_common_internal_for_builtins.bzl",
"collect_native_deps_dirs",
"get_runtime_classpath_for_archive",
- "java_common",
"to_java_binary_info",
)
diff --git a/src/main/starlark/builtins_bzl/common/java/java_common.bzl b/src/main/starlark/builtins_bzl/common/java/java_common.bzl
index 6c8ae716aca504..21b9ecd03eb06e 100644
--- a/src/main/starlark/builtins_bzl/common/java/java_common.bzl
+++ b/src/main/starlark/builtins_bzl/common/java/java_common.bzl
@@ -14,71 +14,238 @@
""" Utilities for Java compilation support in Starlark. """
+load(":common/java/java_info.bzl", "JavaInfo")
+load(":common/java/java_common_internal_for_builtins.bzl", "compile", "merge", "run_ijar")
+load(":common/java/java_plugin_info.bzl", "JavaPluginInfo")
+load(":common/java/java_semantics.bzl", "semantics")
+
_java_common_internal = _builtins.internal.java_common_internal_do_not_use
-def target_kind(target, dereference_aliases = False):
- """ Get the rule class string for a target
+def _compile(
+ ctx,
+ output,
+ java_toolchain,
+ source_jars = [],
+ source_files = [],
+ output_source_jar = None,
+ javac_opts = [],
+ deps = [],
+ runtime_deps = [],
+ exports = [],
+ plugins = [],
+ exported_plugins = [],
+ native_libraries = [],
+ annotation_processor_additional_inputs = [],
+ annotation_processor_additional_outputs = [],
+ strict_deps = "ERROR",
+ bootclasspath = None,
+ sourcepath = [],
+ resources = [],
+ neverlink = False,
+ enable_annotation_processing = True,
+ add_exports = [],
+ add_opens = []):
+ return compile(
+ ctx,
+ output,
+ java_toolchain,
+ source_jars = source_jars,
+ source_files = source_files,
+ output_source_jar = output_source_jar,
+ javac_opts = javac_opts,
+ deps = deps,
+ runtime_deps = runtime_deps,
+ exports = exports,
+ plugins = plugins,
+ exported_plugins = exported_plugins,
+ native_libraries = native_libraries,
+ annotation_processor_additional_inputs = annotation_processor_additional_inputs,
+ annotation_processor_additional_outputs = annotation_processor_additional_outputs,
+ strict_deps = strict_deps,
+ bootclasspath = bootclasspath,
+ sourcepath = sourcepath,
+ resources = resources,
+ neverlink = neverlink,
+ enable_annotation_processing = enable_annotation_processing,
+ add_exports = add_exports,
+ add_opens = add_opens,
+ )
+
+def _run_ijar(actions, jar, java_toolchain, target_label = None):
+ return run_ijar(
+ actions = actions,
+ jar = jar,
+ java_toolchain = java_toolchain,
+ target_label = target_label,
+ )
+
+def _stamp_jar(actions, jar, java_toolchain, target_label):
+ """Stamps a jar with a target label for add_dep
support.
+
+ The return value is typically passed to `JavaInfo.compile_jar`. Prefer to use `run_ijar` when
+ possible.
Args:
- target: (Target)
- dereference_aliases: (bool) resolve the actual target rule class if an
- alias
+ actions: (actions) ctx.actions
+ jar: (File) The jar to run stamp_jar on.
+ java_toolchain: (JavaToolchainInfo) The toolchain to used to find the stamp_jar tool.
+ target_label: (Label) A target label to stamp the jar with. Used for `add_dep` support.
+ Typically, you would pass `ctx.label` to stamp the jar with the current rule's label.
Returns:
- (str) The rule class string of the target
+ (File) The output artifact
+
"""
- return _java_common_internal.target_kind(
- target,
- dereference_aliases = dereference_aliases,
+ return _java_common_internal.stamp_jar(
+ actions = actions,
+ jar = jar,
+ java_toolchain = java_toolchain,
+ target_label = target_label,
)
-def to_java_binary_info(java_info):
- """ Get a copy of the given JavaInfo with minimal info returned by a java_binary
+def _pack_sources(
+ actions,
+ java_toolchain,
+ output_source_jar = None,
+ sources = [],
+ source_jars = []):
+ """Packs sources and source jars into a single source jar file.
+
+ The return value is typically passed to `JavaInfo.source_jar`. At least one of parameters
+ output_jar or output_source_jar is required.
Args:
- java_info: (JavaInfo) A JavaInfo provider instance
+ actions: (actions) ctx.actions
+ java_toolchain: (JavaToolchainInfo) The toolchain used to find the ijar tool.
+ output_source_jar: (File) The output source jar.
+ sources: ([File]) A list of Java source files to be packed into the source jar.
+ source_jars: ([File]) A list of source jars to be packed into the source jar.
Returns:
- (JavaInfo) A JavaInfo instance representing a java_binary target
+ (File) The output artifact
"""
- return _java_common_internal.to_java_binary_info(java_info)
+ return _java_common_internal.pack_sources(
+ actions = actions,
+ java_toolchain = java_toolchain,
+ sources = sources,
+ source_jars = source_jars,
+ output_source_jar = output_source_jar,
+ )
-def get_build_info(ctx, is_stamping_enabled):
- """ Get the artifacts representing the workspace status for this build
+def _default_javac_opts(java_toolchain):
+ """Experimental! Get default javacopts from a java toolchain
Args:
- ctx: (RuleContext) The rule context
- is_stamping_enabled: (bool) If stamping is enabled
+ java_toolchain: (JavaToolchainInfo) the toolchain from which to get the javac options.
- Returns
- ([File]) The build info artifacts
+ Returns:
+ ([str]) A list of javac options
"""
- return _java_common_internal.get_build_info(ctx, is_stamping_enabled)
+ return _java_common_internal.default_javac_opts(java_toolchain = java_toolchain)
-def collect_native_deps_dirs(deps):
- """ Collect the set of root-relative paths containing native libraries
+def _merge(providers):
+ return merge(providers)
+
+def _make_non_strict(java_info):
+ """Returns a new JavaInfo instance whose direct-jars part is the union of both the direct and indirect jars of the given Java provider.
Args:
- deps: [Target] list of targets
+ java_info: (JavaInfo) The java info to make non-strict.
Returns:
- ([String]) A set of root-relative paths as a list
+ (JavaInfo)
"""
- return _java_common_internal.collect_native_deps_dirs(deps)
+ return _java_common_internal.make_non_strict(java_info)
-def get_runtime_classpath_for_archive(jars, excluded_jars):
- """ Filters a classpath to remove certain entries
+def _get_message_bundle_info():
+ return None if semantics.IS_BAZEL else _java_common_internal.MessageBundleInfo
- Args
- jars: (depset[File]) The classpath to filter
- excluded_jars: (depset[File]) The files to remove
+def _add_constraints(java_info, constraints = []):
+ """Returns a copy of the given JavaInfo with the given constraints added.
+
+ Args:
+ java_info: (JavaInfo) The JavaInfo to enhance
+ constraints: ([str]) Constraints to add
Returns:
- (depset[File]) The filtered classpath
+ (JavaInfo)
"""
- return _java_common_internal.get_runtime_classpath_for_archive(
- jars,
- excluded_jars,
+ return _java_common_internal.add_constraints(java_info, constraints = constraints)
+
+def _get_constraints(java_info):
+ """Returns a set of constraints added.
+
+ Args:
+ java_info: (JavaInfo) The JavaInfo to get constraints from.
+
+ Returns:
+ ([str]) The constraints set on the supplied JavaInfo
+ """
+ return _java_common_internal.get_constraints(java_info)
+
+def _set_annotation_processing(
+ java_info,
+ enabled = False,
+ processor_classnames = [],
+ processor_classpath = None,
+ class_jar = None,
+ source_jar = None):
+ """Returns a copy of the given JavaInfo with the given annotation_processing info.
+
+ Args:
+ java_info: (JavaInfo) The JavaInfo to enhance.
+ enabled: (bool) Whether the rule uses annotation processing.
+ processor_classnames: ([str]) Class names of annotation processors applied.
+ processor_classpath: (depset[File]) Class names of annotation processors applied.
+ class_jar: (File) Optional. Jar that is the result of annotation processing.
+ source_jar: (File) Optional. Source archive resulting from annotation processing.
+
+ Returns:
+ (JavaInfo)
+ """
+ return _java_common_internal.set_annotation_processing(
+ java_info,
+ enabled = enabled,
+ processor_classnames = processor_classnames,
+ processor_classpath = processor_classpath,
+ class_jar = class_jar,
+ source_jar = source_jar,
)
-java_common = _java_common_internal
+def _java_toolchain_label(java_toolchain):
+ """Returns the toolchain's label.
+
+ Args:
+ java_toolchain: (JavaToolchainInfo) The toolchain.
+ Returns:
+ (Label)
+ """
+ return _java_common_internal.java_toolchain_label(java_toolchain)
+
+def _make_java_common():
+ methods = {
+ "provider": JavaInfo,
+ "compile": _compile,
+ "run_ijar": _run_ijar,
+ "stamp_jar": _stamp_jar,
+ "pack_sources": _pack_sources,
+ "default_javac_opts": _default_javac_opts,
+ "merge": _merge,
+ "make_non_strict": _make_non_strict,
+ "JavaPluginInfo": JavaPluginInfo,
+ "JavaToolchainInfo": _java_common_internal.JavaToolchainInfo,
+ "JavaRuntimeInfo": _java_common_internal.JavaRuntimeInfo,
+ "BootClassPathInfo": _java_common_internal.BootClassPathInfo,
+ "experimental_java_proto_library_default_has_services": _java_common_internal.experimental_java_proto_library_default_has_services,
+ }
+ if _java_common_internal._google_legacy_api_enabled():
+ methods.update(
+ MessageBundleInfo = _get_message_bundle_info(), # struct field that is None in bazel
+ add_constraints = _add_constraints,
+ get_constraints = _get_constraints,
+ set_annotation_processing = _set_annotation_processing,
+ java_toolchain_label = _java_toolchain_label,
+ )
+ return struct(**methods)
+
+java_common = _make_java_common()
diff --git a/src/main/starlark/builtins_bzl/common/java/java_common_internal_for_builtins.bzl b/src/main/starlark/builtins_bzl/common/java/java_common_internal_for_builtins.bzl
new file mode 100644
index 00000000000000..63484016de6307
--- /dev/null
+++ b/src/main/starlark/builtins_bzl/common/java/java_common_internal_for_builtins.bzl
@@ -0,0 +1,265 @@
+# Copyright 2023 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.
+
+""" Private utilities for Java compilation support in Starlark. """
+
+_java_common_internal = _builtins.internal.java_common_internal_do_not_use
+
+def compile(
+ ctx,
+ output,
+ java_toolchain,
+ source_jars = [],
+ source_files = [],
+ output_source_jar = None,
+ javac_opts = [],
+ deps = [],
+ runtime_deps = [],
+ exports = [],
+ plugins = [],
+ exported_plugins = [],
+ native_libraries = [],
+ annotation_processor_additional_inputs = [],
+ annotation_processor_additional_outputs = [],
+ strict_deps = "ERROR",
+ bootclasspath = None,
+ sourcepath = [],
+ resources = [],
+ add_exports = [],
+ add_opens = [],
+ neverlink = False,
+ enable_annotation_processing = True,
+ # private to @_builtins:
+ enable_compile_jar_action = True,
+ enable_jspecify = True,
+ include_compilation_info = True,
+ classpath_resources = [],
+ resource_jars = [],
+ injecting_rule_kind = None):
+ """Compiles Java source files/jars from the implementation of a Starlark rule
+
+ The result is a provider that represents the results of the compilation and can be added to the
+ set of providers emitted by this rule.
+
+ Args:
+ ctx: (RuleContext) The rule context
+ output: (File) The output of compilation
+ java_toolchain: (JavaToolchainInfo) Toolchain to be used for this compilation. Mandatory.
+ source_jars: ([File]) A list of the jars to be compiled. At least one of source_jars or
+ source_files should be specified.
+ source_files: ([File]) A list of the Java source files to be compiled. At least one of
+ source_jars or source_files should be specified.
+ output_source_jar: (File) The output source jar. Optional. Defaults to
+ `{output_jar}-src.jar` if unset.
+ javac_opts: ([str]) A list of the desired javac options. Optional.
+ deps: ([JavaInfo]) A list of dependencies. Optional.
+ runtime_deps: ([JavaInfo]) A list of runtime dependencies. Optional.
+ exports: ([JavaInfo]) A list of exports. Optional.
+ plugins: ([JavaPluginInfo|JavaInfo]) A list of plugins. Optional.
+ exported_plugins: ([JavaPluginInfo|JavaInfo]) A list of exported plugins. Optional.
+ native_libraries: ([CcInfo]) CC library dependencies that are needed for this library.
+ annotation_processor_additional_inputs: ([File]) A list of inputs that the Java compilation
+ action will take in addition to the Java sources for annotation processing.
+ annotation_processor_additional_outputs: ([File]) A list of outputs that the Java
+ compilation action will output in addition to the class jar from annotation processing.
+ strict_deps: (str) A string that specifies how to handle strict deps. Possible values:
+ 'OFF', 'ERROR', 'WARN' and 'DEFAULT'.
+ bootclasspath: (BootClassPathInfo) If present, overrides the bootclasspath associated with
+ the provided java_toolchain. Optional.
+ sourcepath: ([File])
+ resources: ([File])
+ resource_jars: ([File])
+ classpath_resources: ([File])
+ neverlink: (bool)
+ enable_annotation_processing: (bool) Disables annotation processing in this compilation,
+ causing any annotation processors provided in plugins or in exported_plugins of deps to
+ be ignored.
+ enable_compile_jar_action: (bool) Enables header compilation or ijar creation. If set to
+ False, it forces use of the full class jar in the compilation classpaths of any
+ dependants. Doing so is intended for use by non-library targets such as binaries that
+ do not have dependants.
+ enable_jspecify: (bool)
+ include_compilation_info: (bool)
+ injecting_rule_kind: (str|None)
+ add_exports: ([str]) Allow this library to access the given /. Optional.
+ add_opens: ([str]) Allow this library to reflectively access the given /.
+ Optional.
+
+ Returns:
+ (JavaInfo)
+ """
+ return _java_common_internal.compile(
+ ctx,
+ output = output,
+ java_toolchain = java_toolchain,
+ source_jars = source_jars,
+ source_files = source_files,
+ output_source_jar = output_source_jar,
+ javac_opts = javac_opts,
+ deps = deps,
+ runtime_deps = runtime_deps,
+ exports = exports,
+ plugins = plugins,
+ exported_plugins = exported_plugins,
+ native_libraries = native_libraries,
+ annotation_processor_additional_inputs = annotation_processor_additional_inputs,
+ annotation_processor_additional_outputs = annotation_processor_additional_outputs,
+ strict_deps = strict_deps,
+ bootclasspath = bootclasspath,
+ sourcepath = sourcepath,
+ resources = resources,
+ neverlink = neverlink,
+ enable_annotation_processing = enable_annotation_processing,
+ add_exports = add_exports,
+ add_opens = add_opens,
+ enable_compile_jar_action = enable_compile_jar_action,
+ enable_jspecify = enable_jspecify,
+ include_compilation_info = include_compilation_info,
+ classpath_resources = classpath_resources,
+ resource_jars = resource_jars,
+ injecting_rule_kind = injecting_rule_kind,
+ )
+
+def run_ijar(
+ actions,
+ jar,
+ java_toolchain,
+ target_label = None,
+ # private to @_builtins:
+ output = None):
+ """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 JavaInfo.compile_jar
+
+ Args:
+ actions: (actions) ctx.actions
+ jar: (File) The jar to run ijar on.
+ java_toolchain: (JavaToolchainInfo) The toolchain to used to find the ijar tool.
+ target_label: (Label|None) A target label to stamp the jar with. Used for `add_dep` support.
+ Typically, you would pass `ctx.label` to stamp the jar with the current rule's label.
+ output: (File) Optional.
+
+ Returns:
+ (File) The output artifact
+ """
+ return _java_common_internal.run_ijar(
+ actions = actions,
+ jar = jar,
+ java_toolchain = java_toolchain,
+ target_label = target_label,
+ output = output,
+ )
+
+def merge(
+ providers,
+ # private to @_builtins:
+ merge_java_outputs = True,
+ merge_source_jars = True):
+ """Merges the given providers into a single JavaInfo.
+
+ Args:
+ providers: ([JavaInfo]) The list of providers to merge.
+ merge_java_outputs: (bool)
+ merge_source_jars: (bool)
+
+ Returns:
+ (JavaInfo) The merged JavaInfo
+ """
+ return _java_common_internal.merge(
+ providers,
+ merge_java_outputs = merge_java_outputs,
+ merge_source_jars = merge_source_jars,
+ )
+
+def target_kind(target, dereference_aliases = False):
+ """Get the rule class string for a target
+
+ Args:
+ target: (Target)
+ dereference_aliases: (bool) resolve the actual target rule class if an
+ alias
+
+ Returns:
+ (str) The rule class string of the target
+ """
+ return _java_common_internal.target_kind(
+ target,
+ dereference_aliases = dereference_aliases,
+ )
+
+def to_java_binary_info(java_info):
+ """Get a copy of the given JavaInfo with minimal info returned by a java_binary
+
+ Args:
+ java_info: (JavaInfo) A JavaInfo provider instance
+
+ Returns:
+ (JavaInfo) A JavaInfo instance representing a java_binary target
+ """
+ return _java_common_internal.to_java_binary_info(java_info)
+
+def get_build_info(ctx, is_stamping_enabled):
+ """Get the artifacts representing the workspace status for this build
+
+ Args:
+ ctx: (RuleContext) The rule context
+ is_stamping_enabled: (bool) If stamping is enabled
+
+ Returns
+ ([File]) The build info artifacts
+ """
+ return _java_common_internal.get_build_info(ctx, is_stamping_enabled)
+
+def collect_native_deps_dirs(deps):
+ """Collect the set of root-relative paths containing native libraries
+
+ Args:
+ deps: [Target] list of targets
+
+ Returns:
+ ([String]) A set of root-relative paths as a list
+ """
+ return _java_common_internal.collect_native_deps_dirs(deps)
+
+def get_runtime_classpath_for_archive(jars, excluded_jars):
+ """Filters a classpath to remove certain entries
+
+ Args
+ jars: (depset[File]) The classpath to filter
+ excluded_jars: (depset[File]) The files to remove
+
+ Returns:
+ (depset[File]) The filtered classpath
+ """
+ return _java_common_internal.get_runtime_classpath_for_archive(
+ jars,
+ excluded_jars,
+ )
+
+def filter_protos_for_generated_extension_registry(runtime_jars, deploy_env):
+ """Get proto artifacts from runtime_jars excluding those in deploy_env
+
+ Args:
+ runtime_jars: (depset[File]) the artifacts to scan
+ deploy_env: (depset[File]) the artifacts to exclude
+
+ Returns
+ (depset[File], bool) A tuple of the filtered protos and whether all protos are 'lite'
+ flavored
+ """
+ return _java_common_internal.filter_protos_for_generated_extension_registry(
+ runtime_jars,
+ deploy_env,
+ )
diff --git a/src/main/starlark/builtins_bzl/common/java/java_import.bzl b/src/main/starlark/builtins_bzl/common/java/java_import.bzl
index 6aa2e850504fd3..6dce021f97dc39 100644
--- a/src/main/starlark/builtins_bzl/common/java/java_import.bzl
+++ b/src/main/starlark/builtins_bzl/common/java/java_import.bzl
@@ -23,6 +23,7 @@ load(":common/java/import_deps_check.bzl", "import_deps_check")
load(":common/cc/cc_info.bzl", "CcInfo")
load(":common/java/java_info.bzl", "JavaInfo")
load(":common/java/java_common.bzl", "java_common")
+load(":common/java/java_common_internal_for_builtins.bzl", _run_ijar_private_for_builtins = "run_ijar")
def _filter_provider(provider, *attrs):
return [dep[provider] for attr in attrs for dep in attr if provider in dep]
@@ -49,7 +50,7 @@ def _process_with_ijars_if_needed(jars, ctx):
interface_jar_directory = "_ijar/" + ctx.label.name + "/" + ijar_basename
interface_jar = ctx.actions.declare_file(interface_jar_directory)
- java_common.run_ijar(
+ _run_ijar_private_for_builtins(
ctx.actions,
target_label = ctx.label,
jar = jar,
diff --git a/src/main/starlark/builtins_bzl/common/java/java_plugin_info.bzl b/src/main/starlark/builtins_bzl/common/java/java_plugin_info.bzl
index 7c0f79fb0cd6be..8743af38e8edda 100644
--- a/src/main/starlark/builtins_bzl/common/java/java_plugin_info.bzl
+++ b/src/main/starlark/builtins_bzl/common/java/java_plugin_info.bzl
@@ -16,7 +16,7 @@
Definition of JavaPluginInfo provider.
"""
-load(":common/java/java_common.bzl", "java_common")
+load(":common/java/java_common_internal_for_builtins.bzl", _merge_private_for_builtins = "merge")
_JavaPluginDataInfo = provider(
doc = "Provider encapsulating information about a Java compatible plugin.",
@@ -58,7 +58,9 @@ def _javaplugininfo_init(
Returns:
(JavaPluginInfo)
"""
- java_infos = java_common.merge(runtime_deps)
+
+ # we don't need the private API but java_common needs JavaPluginInfo which would be a cycle
+ java_infos = _merge_private_for_builtins(runtime_deps)
processor_data = data if type(data) == "depset" else depset(data)
plugins = _JavaPluginDataInfo(
processor_classes = depset([processor_class]) if processor_class else depset(),
diff --git a/src/main/starlark/builtins_bzl/common/java/java_semantics.bzl b/src/main/starlark/builtins_bzl/common/java/java_semantics.bzl
index a13b1404e6ff10..07686bedb09e06 100644
--- a/src/main/starlark/builtins_bzl/common/java/java_semantics.bzl
+++ b/src/main/starlark/builtins_bzl/common/java/java_semantics.bzl
@@ -57,4 +57,5 @@ semantics = struct(
JAVA_STUB_TEMPLATE_LABEL = "@bazel_tools//tools/jdk:java_stub_template.txt",
BUILD_INFO_TRANSLATOR_LABEL = None,
JAVA_TEST_RUNNER_LABEL = "@bazel_tools//tools/jdk:TestRunner",
+ IS_BAZEL = True,
)
diff --git a/src/main/starlark/builtins_bzl/common/java/proto/java_proto_library.bzl b/src/main/starlark/builtins_bzl/common/java/proto/java_proto_library.bzl
index f3b557dd6528de..c4ed89c0e187d1 100644
--- a/src/main/starlark/builtins_bzl/common/java/proto/java_proto_library.bzl
+++ b/src/main/starlark/builtins_bzl/common/java/proto/java_proto_library.bzl
@@ -18,7 +18,11 @@ load(":common/java/java_semantics.bzl", "semantics")
load(":common/proto/proto_common.bzl", "ProtoLangToolchainInfo", proto_common = "proto_common_do_not_use")
load(":common/proto/proto_info.bzl", "ProtoInfo")
load(":common/java/java_info.bzl", "JavaInfo")
-load(":common/java/java_common.bzl", "java_common")
+load(
+ ":common/java/java_common_internal_for_builtins.bzl",
+ _compile_private_for_builtins = "compile",
+ _merge_private_for_builtins = "merge",
+)
# The provider is used to collect source and runtime jars in the `proto_library` dependency graph.
JavaProtoAspectInfo = provider("JavaProtoAspectInfo", fields = ["jars"])
@@ -106,23 +110,23 @@ def java_compile_for_protos(ctx, output_jar_suffix, source_jar = None, deps = []
path, sep, filename = ctx.label.name.rpartition("/")
output_jar = ctx.actions.declare_file(path + sep + "lib" + filename + output_jar_suffix)
java_toolchain = semantics.find_java_toolchain(ctx)
- java_info = java_common.compile(
+ java_info = _compile_private_for_builtins(
ctx,
+ output = output_jar,
+ java_toolchain = java_toolchain,
source_jars = [source_jar],
deps = deps,
exports = exports,
- output = output_jar,
output_source_jar = source_jar,
injecting_rule_kind = injecting_rule_kind,
javac_opts = java_toolchain.compatible_javacopts("proto"),
enable_jspecify = False,
- java_toolchain = java_toolchain,
include_compilation_info = False,
)
jars = [source_jar, output_jar]
else:
# If there are no proto sources just pass along the compilation dependencies.
- java_info = java_common.merge(deps + exports, merge_java_outputs = False, merge_source_jars = False)
+ java_info = _merge_private_for_builtins(deps + exports, merge_java_outputs = False, merge_source_jars = False)
jars = []
return java_info, jars
@@ -149,7 +153,7 @@ def bazel_java_proto_library_rule(ctx):
([JavaInfo, DefaultInfo, OutputGroupInfo])
"""
- java_info = java_common.merge([dep[JavaInfo] for dep in ctx.attr.deps], merge_java_outputs = False)
+ java_info = _merge_private_for_builtins([dep[JavaInfo] for dep in ctx.attr.deps], merge_java_outputs = False)
transitive_src_and_runtime_jars = depset(transitive = [dep[JavaProtoAspectInfo].jars for dep in ctx.attr.deps])
transitive_runtime_jars = depset(transitive = [java_info.transitive_runtime_jars])
diff --git a/src/test/java/com/google/devtools/build/lib/rules/java/JavaStarlarkApiTest.java b/src/test/java/com/google/devtools/build/lib/rules/java/JavaStarlarkApiTest.java
index a9544944a49a43..579eaf8c695c03 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/java/JavaStarlarkApiTest.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/java/JavaStarlarkApiTest.java
@@ -29,7 +29,6 @@
import com.google.devtools.build.lib.actions.CommandLineExpansionException;
import com.google.devtools.build.lib.actions.util.ActionsTestUtil;
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
-import com.google.devtools.build.lib.analysis.FileProvider;
import com.google.devtools.build.lib.analysis.actions.SpawnAction;
import com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction;
import com.google.devtools.build.lib.analysis.test.InstrumentedFilesInfo;
@@ -3191,7 +3190,7 @@ public void testCompileWithDisablingCompileJarIsPrivateApi() throws Exception {
getConfiguredTarget("//foo:custom");
- assertContainsEvent("Rule in 'foo' cannot use private API");
+ assertContainsEvent("got unexpected keyword argument: enable_compile_jar_action");
}
@Test
@@ -3226,7 +3225,7 @@ public void testCompileWithClasspathResourcesIsPrivateApi() throws Exception {
getConfiguredTarget("//foo:custom");
- assertContainsEvent("Rule in 'foo' cannot use private API");
+ assertContainsEvent("got unexpected keyword argument: classpath_resources");
}
@Test
@@ -3246,7 +3245,7 @@ public void testGetBuildInfoArtifactsIsPrivateApi() throws Exception {
getConfiguredTarget("//foo:custom");
- assertContainsEvent("Rule in 'foo' cannot use private API");
+ assertContainsEvent("no field or method 'get_build_info'");
}
@Test
@@ -3266,7 +3265,7 @@ public void testIsGoogleLegacyApiEnabledIsPrivateAPI() throws Exception {
getConfiguredTarget("//foo:custom");
- assertContainsEvent("Rule in 'foo' cannot use private API");
+ assertContainsEvent("no field or method '_google_legacy_api_enabled'");
}
@Test
@@ -3286,7 +3285,7 @@ public void testInstanceOfProviderIsPrivateApi() throws Exception {
getConfiguredTarget("//foo:custom");
- assertContainsEvent("Rule in 'foo' cannot use private API");
+ assertContainsEvent("no field or method 'check_provider_instances'");
}
@Test
@@ -3319,7 +3318,7 @@ public void testInjectingRuleKindIsPrivateApi() throws Exception {
getConfiguredTarget("//foo:custom");
- assertContainsEvent("Rule in 'foo' cannot use private API");
+ assertContainsEvent("got unexpected keyword argument: injecting_rule_kind");
}
@Test
@@ -3352,7 +3351,7 @@ public void testEnableJSpecifyIsPrivateApi() throws Exception {
getConfiguredTarget("//foo:custom");
- assertContainsEvent("Rule in 'foo' cannot use private API");
+ assertContainsEvent("got unexpected keyword argument: enable_jspecify");
}
@Test
@@ -3378,7 +3377,7 @@ public void testMergeJavaOutputsIsPrivateApi() throws Exception {
getConfiguredTarget("//foo:custom");
- assertContainsEvent("Rule in 'foo' cannot use private API");
+ assertContainsEvent("got unexpected keyword argument: merge_java_outputs");
}
@Test
@@ -3404,7 +3403,7 @@ public void testMergeSourceJarsIsPrivateApi() throws Exception {
getConfiguredTarget("//foo:custom");
- assertContainsEvent("Rule in 'foo' cannot use private API");
+ assertContainsEvent("got unexpected keyword argument: merge_source_jars");
}
@Test
@@ -3437,7 +3436,7 @@ public void testCompileIncludeCompilationInfoIsPrivateApi() throws Exception {
getConfiguredTarget("//foo:custom");
- assertContainsEvent("Rule in 'foo' cannot use private API");
+ assertContainsEvent("got unexpected keyword argument: include_compilation_info");
}
@Test
@@ -3495,7 +3494,7 @@ public void disallowJavaImportEmptyJars_fails() throws Exception {
}
@Test
- public void testRunIjarIsPrivateApi() throws Exception {
+ public void testRunIjarWithOutputParameterIsPrivateApi() throws Exception {
JavaToolchainTestUtil.writeBuildFileForJavaToolchain(scratch);
scratch.file(
"foo/custom_rule.bzl",
@@ -3524,31 +3523,7 @@ public void testRunIjarIsPrivateApi() throws Exception {
getConfiguredTarget("//foo:custom");
- assertContainsEvent("Rule in 'foo' cannot use private API");
- }
-
- @Test
- public void testGetBuildInfoArtifacts() throws Exception {
- scratch.file(
- "bazel_internal/test/custom_rule.bzl",
- "def _impl(ctx):",
- " artifacts = java_common.get_build_info(ctx, False)",
- " return [DefaultInfo(files = depset(artifacts))]",
- "custom_rule = rule(",
- " implementation = _impl,",
- " attrs = {},",
- ")");
- scratch.file(
- "bazel_internal/test/BUILD",
- "load(':custom_rule.bzl', 'custom_rule')",
- "custom_rule(name = 'custom')");
-
- NestedSet artifacts =
- getConfiguredTarget("//bazel_internal/test:custom")
- .getProvider(FileProvider.class)
- .getFilesToBuild();
-
- assertThat(prettyArtifactNames(artifacts)).containsExactly("build-info-redacted.properties");
+ assertContainsEvent("got unexpected keyword argument: output");
}
@Test
@@ -3643,7 +3618,7 @@ public void testCollectNativeLibsDirsIsPrivateApi() throws Exception {
getConfiguredTarget("//foo:custom");
- assertContainsEvent("Rule in 'foo' cannot use private API");
+ assertContainsEvent("no field or method 'collect_native_deps_dirs'");
}
@Test
@@ -3664,7 +3639,7 @@ public void testGetRuntimeClasspathForArchiveIsPrivateApi() throws Exception {
getConfiguredTarget("//foo:custom");
- assertContainsEvent("Rule in 'foo' cannot use private API");
+ assertContainsEvent("no field or method 'get_runtime_classpath_for_archive'");
}
@Test