Delete unused java rule code from `@_builtins`
PiperOrigin-RevId: 685617340
Change-Id: I78937da0298740aa1cf8f09a21c847818a94bea0
diff --git a/src/main/starlark/builtins_bzl/bazel/exports.bzl b/src/main/starlark/builtins_bzl/bazel/exports.bzl
index 5c4e095..4c9dd92 100644
--- a/src/main/starlark/builtins_bzl/bazel/exports.bzl
+++ b/src/main/starlark/builtins_bzl/bazel/exports.bzl
@@ -15,7 +15,6 @@
"""Exported builtins symbols that are specific to OSS Bazel."""
load("@_builtins//:common/cc/cc_proto_library.bzl", "cc_proto_aspect", "cc_proto_library")
-load("@_builtins//:common/java/java_library.bzl", "JAVA_LIBRARY_ATTRS", "bazel_java_library_rule")
load("@_builtins//:common/java/proto/java_proto_library.bzl", "java_proto_library")
load("@_builtins//:common/proto/proto_library.bzl", "proto_library")
load("@_builtins//:common/python/py_internal.bzl", "py_internal")
@@ -25,13 +24,6 @@
load(":common/java/java_toolchain.bzl", "java_toolchain")
exported_toplevels = {
- # This is an experimental export in Bazel. The interface will change in a way
- # that breaks users. In the future, Build API team will provide an interface
- # that is conceptually similar to this one and stable.
- "experimental_java_library_export_do_not_use": struct(
- bazel_java_library_rule = bazel_java_library_rule,
- JAVA_LIBRARY_ATTRS = JAVA_LIBRARY_ATTRS,
- ),
"cc_proto_aspect": cc_proto_aspect,
"py_internal": py_internal,
}
diff --git a/src/main/starlark/builtins_bzl/bazel/java/bazel_java_binary.bzl b/src/main/starlark/builtins_bzl/bazel/java/bazel_java_binary.bzl
deleted file mode 100644
index 999135d..0000000
--- a/src/main/starlark/builtins_bzl/bazel/java/bazel_java_binary.bzl
+++ /dev/null
@@ -1,570 +0,0 @@
-# Copyright 2022 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.
-
-load(":common/cc/cc_helper.bzl", "cc_helper")
-load(":common/java/android_lint.bzl", "android_lint_subrule")
-load(":common/java/java_binary.bzl", "BASE_TEST_ATTRIBUTES", "BASIC_JAVA_BINARY_ATTRIBUTES", "basic_java_binary")
-load(":common/java/java_binary_deploy_jar.bzl", "create_deploy_archives")
-load(":common/java/java_helper.bzl", "helper")
-load(":common/java/java_info.bzl", "JavaInfo")
-load(":common/java/java_semantics.bzl", "semantics")
-load(":common/paths.bzl", "paths")
-load(":common/rule_util.bzl", "merge_attrs")
-
-def _bazel_java_binary_impl(ctx):
- return _bazel_base_binary_impl(ctx, is_test_rule_class = False) + helper.executable_providers(ctx)
-
-def _bazel_java_test_impl(ctx):
- return _bazel_base_binary_impl(ctx, is_test_rule_class = True) + helper.test_providers(ctx)
-
-def _bazel_base_binary_impl(ctx, is_test_rule_class):
- deps = _collect_all_targets_as_deps(ctx, classpath_type = "compile_only")
- runtime_deps = _collect_all_targets_as_deps(ctx)
-
- main_class = _check_and_get_main_class(ctx)
- coverage_main_class = main_class
- coverage_config = helper.get_coverage_config(ctx, _get_coverage_runner(ctx))
- if coverage_config:
- main_class = coverage_config.main_class
-
- launcher_info = _get_launcher_info(ctx)
-
- executable = _get_executable(ctx)
-
- feature_config = helper.get_feature_config(ctx)
- if feature_config:
- strip_as_default = helper.should_strip_as_default(ctx, feature_config)
- else:
- # No C++ toolchain available.
- strip_as_default = False
-
- providers, default_info, jvm_flags = basic_java_binary(
- ctx,
- deps,
- runtime_deps,
- ctx.files.resources,
- main_class,
- coverage_main_class,
- coverage_config,
- launcher_info,
- executable,
- strip_as_default,
- is_test_rule_class = is_test_rule_class,
- )
-
- if ctx.attr.use_testrunner:
- if semantics.find_java_runtime_toolchain(ctx).version >= 17:
- jvm_flags.append("-Djava.security.manager=allow")
- test_class = ctx.attr.test_class if hasattr(ctx.attr, "test_class") else ""
- if test_class == "":
- test_class = helper.primary_class(ctx)
- if test_class == None:
- fail("cannot determine test class. You might want to rename the " +
- " rule or add a 'test_class' attribute.")
- jvm_flags.extend([
- "-ea",
- "-Dbazel.test_suite=" + helper.shell_escape(test_class),
- ])
-
- java_attrs = providers["InternalDeployJarInfo"].java_attrs
-
- if executable:
- _create_stub(ctx, java_attrs, launcher_info.launcher, executable, jvm_flags, main_class, coverage_main_class)
-
- runfiles = default_info.runfiles
-
- if executable:
- runtime_toolchain = semantics.find_java_runtime_toolchain(ctx)
- runfiles = runfiles.merge(ctx.runfiles(transitive_files = runtime_toolchain.files))
-
- test_support = helper.get_test_support(ctx)
- if test_support:
- runfiles = runfiles.merge(test_support[DefaultInfo].default_runfiles)
-
- providers["DefaultInfo"] = DefaultInfo(
- files = default_info.files,
- runfiles = runfiles,
- executable = default_info.executable,
- )
-
- info = providers.pop("InternalDeployJarInfo")
- create_deploy_archives(
- ctx,
- info.java_attrs,
- launcher_info,
- main_class,
- coverage_main_class,
- info.strip_as_default,
- add_exports = info.add_exports,
- add_opens = info.add_opens,
- )
-
- return providers.values()
-
-def _get_coverage_runner(ctx):
- if ctx.configuration.coverage_enabled and ctx.attr.create_executable:
- toolchain = semantics.find_java_toolchain(ctx)
- runner = toolchain.jacocorunner
- if not runner:
- fail("jacocorunner not set in java_toolchain: %s" % toolchain.label)
- runner_jar = runner.executable
-
- # wrap the jar in JavaInfo so we can add it to deps for java_common.compile()
- return JavaInfo(output_jar = runner_jar, compile_jar = runner_jar)
-
- return None
-
-def _collect_all_targets_as_deps(ctx, classpath_type = "all"):
- deps = helper.collect_all_targets_as_deps(ctx, classpath_type = classpath_type)
-
- if classpath_type == "compile_only" and ctx.fragments.java.enforce_explicit_java_test_deps():
- return deps
-
- test_support = helper.get_test_support(ctx)
- if test_support:
- deps.append(test_support)
- return deps
-
-def _check_and_get_main_class(ctx):
- create_executable = ctx.attr.create_executable
- main_class = _get_main_class(ctx)
-
- if not create_executable and main_class:
- fail("main class must not be specified when executable is not created")
- if create_executable and not main_class:
- if not ctx.attr.srcs:
- fail("need at least one of 'main_class' or Java source files")
- main_class = helper.primary_class(ctx)
- if main_class == None:
- fail("main_class was not provided and cannot be inferred: " +
- "source path doesn't include a known root (java, javatests, src, testsrc)")
-
- return _get_main_class(ctx)
-
-def _get_main_class(ctx):
- if not ctx.attr.create_executable:
- return None
-
- main_class = _get_main_class_from_rule(ctx)
-
- if main_class == "":
- main_class = helper.primary_class(ctx)
- return main_class
-
-def _get_main_class_from_rule(ctx):
- main_class = ctx.attr.main_class
- if main_class:
- return main_class
- if ctx.attr.use_testrunner:
- return "com.google.testing.junit.runner.BazelTestRunner"
- return main_class
-
-def _get_launcher_info(ctx):
- launcher = helper.launcher_artifact_for_target(ctx)
- return struct(
- launcher = launcher,
- unstripped_launcher = launcher,
- runfiles = [],
- runtime_jars = [],
- jvm_flags = [],
- classpath_resources = [],
- )
-
-def _get_executable(ctx):
- if not ctx.attr.create_executable:
- return None
- executable_name = ctx.label.name
- if helper.is_target_platform_windows(ctx):
- executable_name = executable_name + ".exe"
-
- return ctx.actions.declare_file(executable_name)
-
-def _create_stub(ctx, java_attrs, launcher, executable, jvm_flags, main_class, coverage_main_class):
- java_runtime_toolchain = semantics.find_java_runtime_toolchain(ctx)
- java_executable = helper.get_java_executable(ctx, java_runtime_toolchain, launcher)
- workspace_name = ctx.workspace_name
- workspace_prefix = workspace_name + ("/" if workspace_name else "")
- runfiles_enabled = helper.runfiles_enabled(ctx)
- coverage_enabled = ctx.configuration.coverage_enabled
-
- test_support = helper.get_test_support(ctx)
- test_support_jars = test_support[JavaInfo].transitive_runtime_jars if test_support else depset()
- classpath = depset(
- transitive = [
- java_attrs.runtime_classpath,
- test_support_jars if ctx.fragments.java.enforce_explicit_java_test_deps() else depset(),
- ],
- )
-
- if helper.is_target_platform_windows(ctx):
- jvm_flags_for_launcher = []
- for flag in jvm_flags:
- jvm_flags_for_launcher.extend(ctx.tokenize(flag))
- return _create_windows_exe_launcher(ctx, java_executable, classpath, main_class, jvm_flags_for_launcher, runfiles_enabled, executable)
-
- if runfiles_enabled:
- prefix = "" if helper.is_absolute_target_platform_path(ctx, java_executable) else "${JAVA_RUNFILES}/"
- java_bin = "JAVABIN=${JAVABIN:-" + prefix + java_executable + "}"
- else:
- java_bin = "JAVABIN=${JAVABIN:-$(rlocation " + java_executable + ")}"
-
- td = ctx.actions.template_dict()
- td.add_joined(
- "%classpath%",
- classpath,
- map_each = lambda file: _format_classpath_entry(runfiles_enabled, workspace_prefix, file),
- join_with = ctx.configuration.host_path_separator,
- format_joined = "\"%s\"",
- allow_closure = True,
- )
-
- ctx.actions.expand_template(
- template = ctx.file._stub_template,
- output = executable,
- substitutions = {
- "%runfiles_manifest_only%": "" if runfiles_enabled else "1",
- "%workspace_prefix%": workspace_prefix,
- "%javabin%": java_bin,
- "%needs_runfiles%": "0" if helper.is_absolute_target_platform_path(ctx, java_runtime_toolchain.java_executable_exec_path) else "1",
- "%set_jacoco_metadata%": "",
- "%set_jacoco_main_class%": "export JACOCO_MAIN_CLASS=" + coverage_main_class if coverage_enabled else "",
- "%set_jacoco_java_runfiles_root%": "export JACOCO_JAVA_RUNFILES_ROOT=${JAVA_RUNFILES}/" + workspace_prefix if coverage_enabled else "",
- "%java_start_class%": helper.shell_escape(main_class),
- "%jvm_flags%": " ".join(jvm_flags),
- },
- computed_substitutions = td,
- is_executable = True,
- )
- return executable
-
-def _format_classpath_entry(runfiles_enabled, workspace_prefix, file):
- if runfiles_enabled:
- return "${RUNPATH}" + file.short_path
-
- return "$(rlocation " + paths.normalize(workspace_prefix + file.short_path) + ")"
-
-def _create_windows_exe_launcher(ctx, java_executable, classpath, main_class, jvm_flags_for_launcher, runfiles_enabled, executable):
- launch_info = ctx.actions.args().use_param_file("%s", use_always = True).set_param_file_format("multiline")
- launch_info.add("binary_type=Java")
- launch_info.add(ctx.workspace_name, format = "workspace_name=%s")
- launch_info.add("1" if runfiles_enabled else "0", format = "symlink_runfiles_enabled=%s")
- launch_info.add(java_executable, format = "java_bin_path=%s")
- launch_info.add(main_class, format = "java_start_class=%s")
- launch_info.add_joined(classpath, map_each = _short_path, join_with = ";", format_joined = "classpath=%s", omit_if_empty = False)
- launch_info.add_joined(jvm_flags_for_launcher, join_with = "\t", format_joined = "jvm_flags=%s", omit_if_empty = False)
- launch_info.add(semantics.find_java_runtime_toolchain(ctx).java_home_runfiles_path, format = "jar_bin_path=%s/bin/jar.exe")
-
- # TODO(b/295221112): Change to use the "launcher" attribute (only windows use a fixed _launcher attribute)
- launcher_artifact = ctx.executable._launcher
- ctx.actions.run(
- executable = ctx.executable._windows_launcher_maker,
- inputs = [launcher_artifact],
- outputs = [executable],
- arguments = [launcher_artifact.path, launch_info, executable.path],
- use_default_shell_env = True,
- )
- return executable
-
-def _short_path(file):
- return file.short_path
-
-def _compute_test_support(use_testrunner):
- return Label(semantics.JAVA_TEST_RUNNER_LABEL) if use_testrunner else None
-
-def _make_binary_rule(implementation, *, doc, attrs, executable = False, test = False, initializer = None):
- return rule(
- implementation = implementation,
- initializer = initializer,
- doc = doc,
- attrs = attrs,
- executable = executable,
- test = test,
- fragments = ["cpp", "java"],
- provides = [JavaInfo],
- toolchains = [semantics.JAVA_TOOLCHAIN] + cc_helper.use_cpp_toolchain() + (
- [semantics.JAVA_RUNTIME_TOOLCHAIN] if executable or test else []
- ),
- # TODO(hvd): replace with filegroups?
- outputs = {
- "classjar": "%{name}.jar",
- "sourcejar": "%{name}-src.jar",
- "deploysrcjar": "%{name}_deploy-src.jar",
- "deployjar": "%{name}_deploy.jar",
- "unstrippeddeployjar": "%{name}_deploy.jar.unstripped",
- },
- exec_groups = {
- "cpp_link": exec_group(toolchains = cc_helper.use_cpp_toolchain()),
- },
- subrules = [android_lint_subrule],
- )
-
-_BASE_BINARY_ATTRS = merge_attrs(
- BASIC_JAVA_BINARY_ATTRIBUTES,
- {
- "resource_strip_prefix": attr.string(
- doc = """
-The path prefix to strip from Java resources.
-<p>
-If specified, this path prefix is stripped from every file in the <code>resources</code>
-attribute. It is an error for a resource file not to be under this directory. If not
-specified (the default), the path of resource file is determined according to the same
-logic as the Java package of source files. For example, a source file at
-<code>stuff/java/foo/bar/a.txt</code> will be located at <code>foo/bar/a.txt</code>.
-</p>
- """,
- ),
- "_test_support": attr.label(default = _compute_test_support),
- "_launcher": attr.label(
- cfg = "exec",
- executable = True,
- default = "@bazel_tools//tools/launcher:launcher",
- ),
- "_windows_launcher_maker": attr.label(
- default = "@bazel_tools//tools/launcher:launcher_maker",
- cfg = "exec",
- executable = True,
- ),
- },
-)
-
-def make_java_binary(executable):
- return _make_binary_rule(
- _bazel_java_binary_impl,
- doc = """
-<p>
- Builds a Java archive ("jar file"), plus a wrapper shell script with the same name as the rule.
- The wrapper shell script uses a classpath that includes, among other things, a jar file for each
- library on which the binary depends. When running the wrapper shell script, any nonempty
- <code>JAVABIN</code> environment variable will take precedence over the version specified via
- Bazel's <code>--java_runtime_version</code> flag.
-</p>
-<p>
- The wrapper script accepts several unique flags. Refer to
- <code>//src/main/java/com/google/devtools/build/lib/bazel/rules/java/java_stub_template.txt</code>
- for a list of configurable flags and environment variables accepted by the wrapper.
-</p>
-
-<h4 id="java_binary_implicit_outputs">Implicit output targets</h4>
-<ul>
- <li><code><var>name</var>.jar</code>: A Java archive, containing the class files and other
- resources corresponding to the binary's direct dependencies.</li>
- <li><code><var>name</var>-src.jar</code>: An archive containing the sources ("source
- jar").</li>
- <li><code><var>name</var>_deploy.jar</code>: A Java archive suitable for deployment (only
- built if explicitly requested).
- <p>
- Building the <code><<var>name</var>>_deploy.jar</code> target for your rule
- creates a self-contained jar file with a manifest that allows it to be run with the
- <code>java -jar</code> command or with the wrapper script's <code>--singlejar</code>
- option. Using the wrapper script is preferred to <code>java -jar</code> because it
- also passes the <a href="${link java_binary.jvm_flags}">JVM flags</a> and the options
- to load native libraries.
- </p>
- <p>
- The deploy jar contains all the classes that would be found by a classloader that
- searched the classpath from the binary's wrapper script from beginning to end. It also
- contains the native libraries needed for dependencies. These are automatically loaded
- into the JVM at runtime.
- </p>
- <p>If your target specifies a <a href="#java_binary.launcher">launcher</a>
- attribute, then instead of being a normal JAR file, the _deploy.jar will be a
- native binary. This will contain the launcher plus any native (C++) dependencies of
- your rule, all linked into a static binary. The actual jar file's bytes will be
- appended to that native binary, creating a single binary blob containing both the
- executable and the Java code. You can execute the resulting jar file directly
- like you would execute any native binary.</p>
- </li>
- <li><code><var>name</var>_deploy-src.jar</code>: An archive containing the sources
- collected from the transitive closure of the target. These will match the classes in the
- <code>deploy.jar</code> except where jars have no matching source jar.</li>
-</ul>
-
-<p>
-It is good practice to use the name of the source file that is the main entry point of the
-application (minus the extension). For example, if your entry point is called
-<code>Main.java</code>, then your name could be <code>Main</code>.
-</p>
-
-<p>
- A <code>deps</code> attribute is not allowed in a <code>java_binary</code> rule without
- <a href="${link java_binary.srcs}"><code>srcs</code></a>; such a rule requires a
- <a href="${link java_binary.main_class}"><code>main_class</code></a> provided by
- <a href="${link java_binary.runtime_deps}"><code>runtime_deps</code></a>.
-</p>
-
-<p>The following code snippet illustrates a common mistake:</p>
-
-<pre class="code">
-<code class="lang-starlark">
-java_binary(
- name = "DontDoThis",
- srcs = [
- <var>...</var>,
- <code class="deprecated">"GeneratedJavaFile.java"</code>, # a generated .java file
- ],
- deps = [<code class="deprecated">":generating_rule",</code>], # rule that generates that file
-)
-</code>
-</pre>
-
-<p>Do this instead:</p>
-
-<pre class="code">
-<code class="lang-starlark">
-java_binary(
- name = "DoThisInstead",
- srcs = [
- <var>...</var>,
- ":generating_rule",
- ],
-)
-</code>
-</pre>
- """,
- attrs = merge_attrs(
- _BASE_BINARY_ATTRS,
- ({} if executable else {
- "args": attr.string_list(),
- "output_licenses": attr.license() if hasattr(attr, "license") else attr.string_list(),
- }),
- ),
- executable = executable,
- )
-
-java_binary = make_java_binary(executable = True)
-
-def _java_test_initializer(**kwargs):
- if "stamp" in kwargs and type(kwargs["stamp"]) == type(True):
- kwargs["stamp"] = 1 if kwargs["stamp"] else 0
- if "use_launcher" in kwargs and not kwargs["use_launcher"]:
- kwargs["launcher"] = None
- else:
- # If launcher is not set or None, set it to config flag
- if "launcher" not in kwargs or not kwargs["launcher"]:
- kwargs["launcher"] = semantics.LAUNCHER_FLAG_LABEL
- return kwargs
-
-java_test = _make_binary_rule(
- _bazel_java_test_impl,
- doc = """
-<p>
-A <code>java_test()</code> rule compiles a Java test. A test is a binary wrapper around your
-test code. The test runner's main method is invoked instead of the main class being compiled.
-</p>
-
-<h4 id="java_test_implicit_outputs">Implicit output targets</h4>
-<ul>
- <li><code><var>name</var>.jar</code>: A Java archive.</li>
- <li><code><var>name</var>_deploy.jar</code>: A Java archive suitable
- for deployment. (Only built if explicitly requested.) See the description of the
- <code><var>name</var>_deploy.jar</code> output from
- <a href="#java_binary">java_binary</a> for more details.</li>
-</ul>
-
-<p>
-See the section on <code>java_binary()</code> arguments. This rule also
-supports all <a href="${link common-definitions#common-attributes-tests}">attributes common
-to all test rules (*_test)</a>.
-</p>
-
-<h4 id="java_test_examples">Examples</h4>
-
-<pre class="code">
-<code class="lang-starlark">
-
-java_library(
- name = "tests",
- srcs = glob(["*.java"]),
- deps = [
- "//java/com/foo/base:testResources",
- "//java/com/foo/testing/util",
- ],
-)
-
-java_test(
- name = "AllTests",
- size = "small",
- runtime_deps = [
- ":tests",
- "//util/mysql",
- ],
-)
-</code>
-</pre>
- """,
- attrs = merge_attrs(
- BASE_TEST_ATTRIBUTES,
- _BASE_BINARY_ATTRS,
- {
- "_lcov_merger": attr.label(
- cfg = "exec",
- default = configuration_field(
- fragment = "coverage",
- name = "output_generator",
- ),
- ),
- "_collect_cc_coverage": attr.label(
- cfg = "exec",
- allow_single_file = True,
- default = "@bazel_tools//tools/test:collect_cc_coverage",
- ),
- },
- override_attrs = {
- "use_testrunner": attr.bool(
- default = True,
- doc = semantics.DOCS.for_attribute("use_testrunner") + """
-<br/>
-You can use this to override the default
-behavior, which is to use test runner for
-<code>java_test</code> rules,
-and not use it for <code>java_binary</code> rules. It is unlikely
-you will want to do this. One use is for <code>AllTest</code>
-rules that are invoked by another rule (to set up a database
-before running the tests, for example). The <code>AllTest</code>
-rule must be declared as a <code>java_binary</code>, but should
-still use the test runner as its main entry point.
-
-The name of a test runner class can be overridden with <code>main_class</code> attribute.
- """,
- ),
- "stamp": attr.int(
- default = 0,
- values = [-1, 0, 1],
- doc = """
-Whether to encode build information into the binary. Possible values:
-<ul>
-<li>
- <code>stamp = 1</code>: Always stamp the build information into the binary, even in
- <a href="${link user-manual#flag--stamp}"><code>--nostamp</code></a> builds. <b>This
- setting should be avoided</b>, since it potentially kills remote caching for the
- binary and any downstream actions that depend on it.
-</li>
-<li>
- <code>stamp = 0</code>: Always replace build information by constant values. This
- gives good build result caching.
-</li>
-<li>
- <code>stamp = -1</code>: Embedding of build information is controlled by the
- <a href="${link user-manual#flag--stamp}"><code>--[no]stamp</code></a> flag.
-</li>
-</ul>
-<p>Stamped binaries are <em>not</em> rebuilt unless their dependencies change.</p>
- """,
- ),
- },
- remove_attrs = ["deploy_env"],
- ),
- test = True,
- initializer = _java_test_initializer,
-)
diff --git a/src/main/starlark/builtins_bzl/bazel/java/bazel_java_binary_nonexec.bzl b/src/main/starlark/builtins_bzl/bazel/java/bazel_java_binary_nonexec.bzl
deleted file mode 100644
index e0b8e37..0000000
--- a/src/main/starlark/builtins_bzl/bazel/java/bazel_java_binary_nonexec.bzl
+++ /dev/null
@@ -1,26 +0,0 @@
-# Copyright 2022 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.
-
-"""Defines a java_binary rule class that is non-executable.
-
-There are three physical rule classes for java_binary and we want all of them
-to have a name string of "java_binary" because various tooling expects that.
-But we also need the rule classes to be defined in separate files. That way the
-hash of their bzl environments will be different. See http://b/226379109,
-specifically #20, for details.
-"""
-
-load(":bazel/java/bazel_java_binary.bzl", "make_java_binary")
-
-java_binary = make_java_binary(executable = False)
diff --git a/src/main/starlark/builtins_bzl/bazel/java/bazel_java_binary_wrapper.bzl b/src/main/starlark/builtins_bzl/bazel/java/bazel_java_binary_wrapper.bzl
deleted file mode 100644
index 7f1a2ca..0000000
--- a/src/main/starlark/builtins_bzl/bazel/java/bazel_java_binary_wrapper.bzl
+++ /dev/null
@@ -1,36 +0,0 @@
-# Copyright 2022 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.
-
-"""Macro encapsulating the java_binary implementation
-
-This is needed since the `executable` nature of the target must be computed from
-the supplied value of the `create_executable` attribute.
-"""
-
-load(":bazel/java/bazel_java_binary.bzl", java_bin_exec = "java_binary")
-load(":bazel/java/bazel_java_binary_nonexec.bzl", java_bin_nonexec = "java_binary")
-load(":common/java/java_binary_wrapper.bzl", "register_java_binary_rules", "register_legacy_java_binary_rules")
-
-def java_binary(**kwargs):
- if _builtins.internal.java_common_internal_do_not_use.incompatible_disable_non_executable_java_binary():
- register_java_binary_rules(
- java_bin_exec,
- **kwargs
- )
- else:
- register_legacy_java_binary_rules(
- java_bin_exec,
- java_bin_nonexec,
- **kwargs
- )
diff --git a/src/main/starlark/builtins_bzl/common/java/android_lint.bzl b/src/main/starlark/builtins_bzl/common/java/android_lint.bzl
deleted file mode 100644
index 2d14b2a..0000000
--- a/src/main/starlark/builtins_bzl/common/java/android_lint.bzl
+++ /dev/null
@@ -1,143 +0,0 @@
-# Copyright 2021 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.
-
-"""Creates the android lint action for java rules"""
-
-load(":common/java/java_helper.bzl", "helper")
-load(":common/java/java_semantics.bzl", "semantics")
-
-def _tokenize_opts(opts_depset):
- return helper.tokenize_javacopts(ctx = None, opts = opts_depset)
-
-def _android_lint_action(ctx, source_files, source_jars, compilation_info):
- """
- Creates an action that runs Android lint against Java source files.
-
- You need to add `ANDROID_LINT_IMPLICIT_ATTRS` to any rule or aspect using this call.
-
- To lint generated source jars (java_info.java_outputs.gen_source_jar)
- add them to the `source_jar` parameter.
-
- `compilation_info` parameter should supply the classpath and Javac options
- that were used during Java compilation.
-
- The Android lint tool is obtained from Java toolchain.
-
- Args:
- ctx: (RuleContext) Used to register the action.
- source_files: (list[File]) A list of .java source files
- source_jars: (list[File]) A list of .jar or .srcjar files containing
- source files. It should also include generated source jars.
- compilation_info: (struct) Information about compilation.
-
- Returns:
- (None|File) The Android lint output file or None if no source files were
- present.
- """
-
- # assuming that linting is enabled for all java rules i.e.
- # --experimental_limit_android_lint_to_android_constrained_java=false
-
- # --experimental_run_android_lint_on_java_rules= is checked in basic_java_library.bzl
-
- if not (source_files or source_jars):
- return None
-
- toolchain = semantics.find_java_toolchain(ctx)
- java_runtime = toolchain.java_runtime
- linter = toolchain._android_linter
- if not linter:
- # TODO(hvd): enable after enabling in tests
- # fail("android linter not set in java_toolchain")
- return None
-
- args = ctx.actions.args()
-
- executable = linter.tool.executable
- transitive_inputs = []
- if executable.extension != "jar":
- tools = [linter.tool]
- transitive_inputs.append(linter.data)
- args_list = [args]
- else:
- jvm_args = ctx.actions.args()
- jvm_args.add_all(toolchain.jvm_opt)
- jvm_args.add_all(linter.jvm_opts)
- jvm_args.add("-jar", executable)
- executable = java_runtime.java_executable_exec_path
- tools = [java_runtime.files, linter.tool.executable]
- transitive_inputs.append(linter.data)
- args_list = [jvm_args, args]
-
- classpath = compilation_info.compilation_classpath
-
- # TODO(hvd): get from toolchain if we need this - probably android only
- bootclasspath_aux = []
- if bootclasspath_aux:
- classpath = depset(transitive = [classpath, bootclasspath_aux])
- transitive_inputs.append(classpath)
-
- bootclasspath = toolchain.bootclasspath
- transitive_inputs.append(bootclasspath)
-
- transitive_inputs.append(compilation_info.plugins.processor_jars)
- transitive_inputs.append(compilation_info.plugins.processor_data)
- args.add_all("--sources", source_files)
- args.add_all("--source_jars", source_jars)
- args.add_all("--bootclasspath", bootclasspath)
- args.add_all("--classpath", classpath)
- args.add_all("--lint_rules", compilation_info.plugins.processor_jars)
- args.add("--target_label", ctx.label)
-
- javac_opts = compilation_info.javac_options
- if javac_opts:
- # wrap in a list so that map_each passes the depset to _tokenize_opts
- args.add_all("--javacopts", [javac_opts], map_each = _tokenize_opts)
- args.add("--")
-
- args.add("--lintopts")
- args.add_all(linter.lint_opts)
-
- for package_config in linter.package_config:
- if package_config.matches(ctx.label):
- # wrap in a list so that map_each passes the depset to _tokenize_opts
- package_opts = [package_config.javac_opts]
- args.add_all(package_opts, map_each = _tokenize_opts)
- transitive_inputs.append(package_config.data)
-
- android_lint_out = ctx.actions.declare_file("%s_android_lint_output.xml" % ctx.label.name)
- args.add("--xml", android_lint_out)
-
- args.set_param_file_format(format = "multiline")
- args.use_param_file(param_file_arg = "@%s", use_always = True)
- ctx.actions.run(
- mnemonic = "AndroidLint",
- progress_message = semantics.LINT_PROGRESS_MESSAGE,
- executable = executable,
- inputs = depset(
- # TODO(b/213551463) benchmark using a transitive depset instead
- source_files + source_jars,
- transitive = transitive_inputs,
- ),
- outputs = [android_lint_out],
- tools = tools,
- arguments = args_list,
- execution_requirements = {"supports-workers": "1"},
- )
- return android_lint_out
-
-android_lint_subrule = subrule(
- implementation = _android_lint_action,
- toolchains = [semantics.JAVA_TOOLCHAIN_TYPE],
-)
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
deleted file mode 100644
index febc1d1..0000000
--- a/src/main/starlark/builtins_bzl/common/java/basic_java_library.bzl
+++ /dev/null
@@ -1,280 +0,0 @@
-# Copyright 2021 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.
-
-"""
-Common code for reuse across java_* rules
-"""
-
-load(":common/cc/cc_info.bzl", "CcInfo")
-load(":common/java/android_lint.bzl", "android_lint_subrule")
-load(":common/java/boot_class_path_info.bzl", "BootClassPathInfo")
-load(":common/java/compile_action.bzl", "compile_action")
-load(":common/java/java_common.bzl", "java_common")
-load(":common/java/java_common_internal_for_builtins.bzl", "target_kind")
-load(":common/java/java_info.bzl", "JavaInfo", "JavaPluginInfo")
-load(":common/java/java_semantics.bzl", "semantics")
-load(":common/java/proguard_validation.bzl", "validate_proguard_specs")
-load(":common/rule_util.bzl", "merge_attrs")
-
-coverage_common = _builtins.toplevel.coverage_common
-
-def _filter_srcs(srcs, ext):
- return [f for f in srcs if f.extension == ext]
-
-def _filter_provider(provider, *attrs):
- return [dep[provider] for attr in attrs for dep in attr if provider in dep]
-
-def _get_attr_safe(ctx, attr, default):
- return getattr(ctx.attr, attr) if hasattr(ctx.attr, attr) else default
-
-# TODO(b/11285003): disallow jar files in deps, require java_import instead
-def _filter_javainfo_and_legacy_jars(attr):
- dep_list = []
-
- # Native code collected data into a NestedSet, using add for legacy jars and
- # addTransitive for JavaInfo. This resulted in legacy jars being first in the list.
- for dep in attr:
- kind = target_kind(dep)
- if not JavaInfo in dep or kind == "java_binary" or kind == "java_test":
- for file in dep[DefaultInfo].files.to_list():
- if file.extension == "jar":
- # Native doesn't construct JavaInfo
- java_info = JavaInfo(output_jar = file, compile_jar = file)
- dep_list.append(java_info)
-
- for dep in attr:
- if JavaInfo in dep:
- dep_list.append(dep[JavaInfo])
- return dep_list
-
-def basic_java_library(
- ctx,
- srcs,
- deps = [],
- runtime_deps = [],
- plugins = [],
- exports = [],
- exported_plugins = [],
- resources = [],
- resource_jars = [],
- classpath_resources = [],
- javacopts = [],
- neverlink = False,
- enable_compile_jar_action = True,
- coverage_config = None,
- proguard_specs = None,
- add_exports = [],
- add_opens = [],
- bootclasspath = None,
- javabuilder_jvm_flags = None):
- """
- Creates actions that compile and lint Java sources, sets up coverage and returns JavaInfo, InstrumentedFilesInfo and output groups.
-
- The call creates actions and providers needed and shared by `java_library`,
- `java_plugin`,`java_binary`, and `java_test` rules and it is primarily
- intended to be used in those rules.
-
- Before compilation coverage.runner is added to the dependencies and if
- present plugins are extended with the value of `--plugin` flag.
-
- Args:
- ctx: (RuleContext) Used to register the actions.
- srcs: (list[File]) The list of source files that are processed to create the target.
- deps: (list[Target]) The list of other libraries to be linked in to the target.
- runtime_deps: (list[Target]) Libraries to make available to the final binary or test at runtime only.
- plugins: (list[Target]) Java compiler plugins to run at compile-time.
- exports: (list[Target]) Exported libraries.
- exported_plugins: (list[Target]) The list of `java_plugin`s (e.g. annotation
- processors) to export to libraries that directly depend on this library.
- resources: (list[File]) A list of data files to include in a Java jar.
- resource_jars: (list[File]) A list of jar files to unpack and include in a
- Java jar.
- classpath_resources: (list[File])
- javacopts: (list[str])
- neverlink: (bool) Whether this library should only be used for compilation and not at runtime.
- enable_compile_jar_action: (bool) Enables header compilation or ijar creation.
- coverage_config: (struct{runner:JavaInfo, support_files:list[File]|depset[File], env:dict[str,str]})
- Coverage configuration. `runner` is added to dependencies during
- compilation, `support_files` and `env` is returned in InstrumentedFilesInfo.
- proguard_specs: (list[File]) Files to be used as Proguard specification.
- Proguard validation is done only when the parameter is set.
- add_exports: (list[str]) Allow this library to access the given <module>/<package>.
- add_opens: (list[str]) Allow this library to reflectively access the given <module>/<package>.
- bootclasspath: (Target) The JDK APIs to compile this library against.
- javabuilder_jvm_flags: (list[str]) Additional JVM flags to pass to JavaBuilder.
- Returns:
- (dict[str, Provider],
- {files_to_build: list[File],
- runfiles: list[File],
- output_groups: dict[str,list[File]]})
- """
- source_files = _filter_srcs(srcs, "java")
- source_jars = _filter_srcs(srcs, "srcjar")
-
- plugins_javaplugininfo = _collect_plugins(plugins)
- plugins_javaplugininfo.append(ctx.attr._java_plugins[JavaPluginInfo])
-
- properties = _filter_srcs(srcs, "properties")
- if properties:
- resources = list(resources)
- resources.extend(properties)
-
- java_info, compilation_info = compile_action(
- ctx,
- ctx.outputs.classjar,
- ctx.outputs.sourcejar,
- source_files,
- source_jars,
- collect_deps(deps) + ([coverage_config.runner] if coverage_config and coverage_config.runner else []),
- collect_deps(runtime_deps),
- plugins_javaplugininfo,
- collect_deps(exports),
- _collect_plugins(exported_plugins),
- resources,
- resource_jars,
- classpath_resources,
- _collect_native_libraries(deps, runtime_deps, exports),
- javacopts,
- neverlink,
- ctx.fragments.java.strict_java_deps,
- enable_compile_jar_action,
- add_exports = add_exports,
- add_opens = add_opens,
- bootclasspath = bootclasspath[BootClassPathInfo] if bootclasspath else None,
- javabuilder_jvm_flags = javabuilder_jvm_flags,
- )
- target = {"JavaInfo": java_info}
-
- output_groups = dict(
- compilation_outputs = compilation_info.files_to_build,
- _source_jars = java_info.transitive_source_jars,
- _direct_source_jars = java_info.source_jars,
- )
-
- if ctx.fragments.java.run_android_lint:
- generated_source_jars = [
- output.generated_source_jar
- for output in java_info.java_outputs
- if output.generated_source_jar != None
- ]
- lint_output = android_lint_subrule(
- source_files,
- source_jars + generated_source_jars,
- compilation_info,
- )
- if lint_output:
- output_groups["_validation"] = [lint_output]
-
- target["InstrumentedFilesInfo"] = coverage_common.instrumented_files_info(
- ctx,
- source_attributes = ["srcs"],
- dependency_attributes = ["deps", "data", "resources", "resource_jars", "exports", "runtime_deps", "jars"],
- coverage_support_files = coverage_config.support_files if coverage_config else depset(),
- coverage_environment = coverage_config.env if coverage_config else {},
- )
-
- if proguard_specs != None:
- target["ProguardSpecProvider"] = validate_proguard_specs(
- ctx,
- proguard_specs,
- [deps, runtime_deps, exports],
- )
- output_groups["_hidden_top_level_INTERNAL_"] = target["ProguardSpecProvider"].specs
-
- return target, struct(
- files_to_build = compilation_info.files_to_build,
- runfiles = compilation_info.runfiles,
- output_groups = output_groups,
- )
-
-def _collect_plugins(plugins):
- """Collects plugins from an attribute.
-
- Use this call to collect plugins from `plugins` or `exported_plugins` attribute.
-
- The call simply extracts JavaPluginInfo provider.
-
- Args:
- plugins: (list[Target]) Attribute to collect plugins from.
- Returns:
- (list[JavaPluginInfo]) The plugins.
- """
- return _filter_provider(JavaPluginInfo, plugins)
-
-def collect_deps(deps):
- """Collects dependencies from an attribute.
-
- Use this call to collect plugins from `deps`, `runtime_deps`, or `exports` attribute.
-
- The call extracts JavaInfo and additionaly also "legacy jars". "legacy jars"
- are wrapped into a JavaInfo.
-
- Args:
- deps: (list[Target]) Attribute to collect dependencies from.
- Returns:
- (list[JavaInfo]) The dependencies.
- """
- return _filter_javainfo_and_legacy_jars(deps)
-
-def _collect_native_libraries(*attrs):
- """Collects native libraries from a list of attributes.
-
- Use this call to collect native libraries from `deps`, `runtime_deps`, or `exports` attributes.
-
- The call simply extracts CcInfo provider.
- Args:
- *attrs: (*list[Target]) Attribute to collect native libraries from.
- Returns:
- (list[CcInfo]) The native library dependencies.
- """
- return _filter_provider(CcInfo, *attrs)
-
-def construct_defaultinfo(ctx, files_to_build, files, neverlink, *extra_attrs):
- """Constructs DefaultInfo for Java library like rule.
-
- Args:
- ctx: (RuleContext) Used to construct the runfiles.
- files_to_build: (list[File]) List of the files built by the rule.
- files: (list[File]) List of the files include in runfiles.
- neverlink: (bool) When true empty runfiles are constructed.
- *extra_attrs: (list[Target]) Extra attributes to merge runfiles from.
-
- Returns:
- (DefaultInfo) DefaultInfo provider.
- """
- if neverlink:
- runfiles = None
- else:
- runfiles = ctx.runfiles(files = files, collect_default = True)
- runfiles = runfiles.merge_all([dep[DefaultInfo].default_runfiles for attr in extra_attrs for dep in attr])
- default_info = DefaultInfo(
- files = depset(files_to_build),
- runfiles = runfiles,
- )
- return default_info
-
-BASIC_JAVA_LIBRARY_IMPLICIT_ATTRS = merge_attrs(
- {
- "_java_plugins": attr.label(
- default = semantics.JAVA_PLUGINS_FLAG_ALIAS_LABEL,
- providers = [JavaPluginInfo],
- ),
- # TODO(b/245144242): Used by IDE integration, remove when toolchains are used
- "_java_toolchain": attr.label(
- default = semantics.JAVA_TOOLCHAIN_LABEL,
- providers = [java_common.JavaToolchainInfo],
- ),
- "_use_auto_exec_groups": attr.bool(default = True),
- },
-)
diff --git a/src/main/starlark/builtins_bzl/common/java/compile_action.bzl b/src/main/starlark/builtins_bzl/common/java/compile_action.bzl
deleted file mode 100644
index 7b9c20a..0000000
--- a/src/main/starlark/builtins_bzl/common/java/compile_action.bzl
+++ /dev/null
@@ -1,172 +0,0 @@
-# Copyright 2021 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.
-
-"""
-Java compile action
-"""
-
-load(":common/java/java_common_internal_for_builtins.bzl", _compile_private_for_builtins = "compile")
-load(":common/java/java_semantics.bzl", "semantics")
-
-def _filter_strict_deps(mode):
- return "error" if mode in ["strict", "default"] else mode
-
-def _collect_plugins(deps, plugins):
- transitive_processor_jars = []
- transitive_processor_data = []
- for plugin in plugins:
- transitive_processor_jars.append(plugin.plugins.processor_jars)
- transitive_processor_data.append(plugin.plugins.processor_data)
- for dep in deps:
- transitive_processor_jars.append(dep.plugins.processor_jars)
- transitive_processor_data.append(dep.plugins.processor_data)
- return struct(
- processor_jars = depset(transitive = transitive_processor_jars),
- processor_data = depset(transitive = transitive_processor_data),
- )
-
-def compile_action(
- ctx,
- output_class_jar,
- output_source_jar,
- source_files = [],
- source_jars = [],
- deps = [],
- runtime_deps = [],
- plugins = [],
- exports = [],
- exported_plugins = [],
- resources = [],
- resource_jars = [],
- classpath_resources = [],
- native_libraries = [],
- javacopts = [],
- neverlink = False,
- strict_deps = "ERROR",
- enable_compile_jar_action = True,
- add_exports = [],
- add_opens = [],
- bootclasspath = None,
- javabuilder_jvm_flags = None):
- """
- Creates actions that compile Java sources, produce source jar, and produce header jar and returns JavaInfo.
-
- Use this call when you need the most basic and consistent Java compilation.
-
- Most parameters correspond to attributes on a java_library (srcs, deps,
- plugins, resources ...) except they are more strict, for example:
-
- - Where java_library's srcs attribute allows mixing of .java, .srcjar, and
- .properties files the arguments accepted by this call should be strictly
- separated into source_files, source_jars, and resources parameter.
- - deps parameter accepts only JavaInfo providers and plugins parameter only
- JavaPluginInfo
-
- The call creates following actions and files:
- - compiling Java sources to a class jar (output_class_jar parameter)
- - a source jar (output_source_jar parameter)
- - optionally a jar containing plugin generated classes when plugins are present
- - optionally a jar containing plugin generated sources
- - jdeps file containing dependencies used during compilation
- - other files used to speed up incremental builds:
- - a header jar - a jar containing only method signatures without implementation
- - compile jdeps - dependencies used during header compilation
-
- The returned JavaInfo provider may be used as a "fully-qualified" dependency
- to a java_library.
-
- Args:
- ctx: (RuleContext) Used to register the actions.
- output_class_jar: (File) Output class .jar file. The file needs to be declared.
- output_source_jar: (File) Output source .jar file. The file needs to be declared.
- source_files: (list[File]) A list of .java source files to compile.
- At least one of source_files or source_jars parameter must be specified.
- source_jars: (list[File]) A list of .jar or .srcjar files containing
- source files to compile.
- At least one of source_files or source_jars parameter must be specified.
- deps: (list[JavaInfo]) A list of dependencies.
- runtime_deps: (list[JavaInfo]) A list of runtime dependencies.
- plugins: (list[JavaPluginInfo]) A list of plugins.
- exports: (list[JavaInfo]) A list of exports.
- exported_plugins: (list[JavaInfo]) A list of exported plugins.
- resources: (list[File]) A list of resources.
- resource_jars: (list[File]) A list of jars to unpack.
- classpath_resources: (list[File]) A list of classpath resources.
- native_libraries: (list[CcInfo]) C++ native library dependencies that are
- needed for this library.
- javacopts: (list[str]) A list of the desired javac options. The options
- may contain `$(location ..)` templates that will be expanded.
- neverlink: (bool) Whether or not this library should be used only for
- compilation and not at runtime.
- strict_deps: (str) A string that specifies how to handle strict deps.
- Possible values: 'OFF', 'ERROR', 'WARN' and 'DEFAULT'. For more details
- see https://bazel.build/docs/user-manual#strict-java-deps.
- By default 'ERROR'.
- 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.
- add_exports: (list[str]) Allow this library to access the given <module>/<package>.
- add_opens: (list[str]) Allow this library to reflectively access the given <module>/<package>.
- bootclasspath: (BootClassPathInfo) The set of JDK APIs to compile this library against.
- javabuilder_jvm_flags: (list[str]) Additional JVM flags to pass to JavaBuilder.
-
- Returns:
- ((JavaInfo, {files_to_build: list[File],
- runfiles: list[File],
- compilation_classpath: list[File],
- plugins: {processor_jars,
- processor_data: depset[File]}}))
- A tuple with JavaInfo provider and additional compilation info.
-
- Files_to_build may include an empty .jar file when there are no sources
- or resources present, whereas runfiles in this case are empty.
- """
-
- 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,
- resource_jars = resource_jars,
- classpath_resources = classpath_resources,
- plugins = plugins,
- deps = deps,
- native_libraries = native_libraries,
- runtime_deps = runtime_deps,
- exports = exports,
- exported_plugins = exported_plugins,
- javac_opts = [ctx.expand_location(opt) for opt in javacopts],
- neverlink = neverlink,
- output_source_jar = output_source_jar,
- strict_deps = _filter_strict_deps(strict_deps),
- enable_compile_jar_action = enable_compile_jar_action,
- add_exports = add_exports,
- add_opens = add_opens,
- bootclasspath = bootclasspath,
- javabuilder_jvm_flags = javabuilder_jvm_flags,
- )
-
- compilation_info = struct(
- files_to_build = [output_class_jar],
- runfiles = [output_class_jar] if source_files or source_jars or resources else [],
- # TODO(ilist): collect compile_jars from JavaInfo in deps & exports
- compilation_classpath = java_info.compilation_info.compilation_classpath,
- javac_options = java_info.compilation_info.javac_options,
- plugins = _collect_plugins(deps, plugins),
- )
-
- return java_info, compilation_info
diff --git a/src/main/starlark/builtins_bzl/common/java/import_deps_check.bzl b/src/main/starlark/builtins_bzl/common/java/import_deps_check.bzl
deleted file mode 100644
index cfdce3b..0000000
--- a/src/main/starlark/builtins_bzl/common/java/import_deps_check.bzl
+++ /dev/null
@@ -1,79 +0,0 @@
-# Copyright 2022 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.
-
-"""Creates the import deps checker for java rules"""
-
-load(":common/java/java_semantics.bzl", "semantics")
-
-def import_deps_check(
- ctx,
- jars_to_check,
- declared_deps,
- transitive_deps,
- rule_class):
- """
- Creates actions that checks import deps for java rules.
-
- Args:
- ctx: (RuleContext) Used to register the actions.
- jars_to_check: (list[File]) A list of jars files to check.
- declared_deps: (list[File]) A list of direct dependencies.
- transitive_deps: (list[File]) A list of transitive dependencies.
- rule_class: (String) Rule class.
-
- Returns:
- (File) Output file of the created action.
- """
- java_toolchain = semantics.find_java_toolchain(ctx)
- deps_checker = java_toolchain._deps_checker
- if deps_checker == None:
- return None
-
- jdeps_output = ctx.actions.declare_file("_%s/%s/jdeps.proto" % (rule_class, ctx.label.name))
-
- args = ctx.actions.args()
- args.add("-jar", deps_checker)
- args.add_all(jars_to_check, before_each = "--input")
- args.add_all(declared_deps, before_each = "--directdep")
- args.add_all(
- depset(order = "preorder", transitive = [declared_deps, transitive_deps]),
- before_each = "--classpath_entry",
- )
- args.add_all(java_toolchain.bootclasspath, before_each = "--bootclasspath_entry")
- args.add("--checking_mode=error")
- args.add("--jdeps_output", jdeps_output)
- args.add("--rule_label", ctx.label)
-
- inputs = depset(
- jars_to_check,
- transitive = [
- declared_deps,
- transitive_deps,
- java_toolchain.bootclasspath,
- ],
- )
- tools = [deps_checker, java_toolchain.java_runtime.files]
-
- ctx.actions.run(
- mnemonic = "ImportDepsChecker",
- progress_message = "Checking the completeness of the deps for %s" % jars_to_check,
- executable = java_toolchain.java_runtime.java_executable_exec_path,
- arguments = [args],
- inputs = inputs,
- outputs = [jdeps_output],
- tools = tools,
- toolchain = semantics.JAVA_TOOLCHAIN_TYPE,
- )
-
- return jdeps_output
diff --git a/src/main/starlark/builtins_bzl/common/java/java_binary.bzl b/src/main/starlark/builtins_bzl/common/java/java_binary.bzl
deleted file mode 100644
index 888044c..0000000
--- a/src/main/starlark/builtins_bzl/common/java/java_binary.bzl
+++ /dev/null
@@ -1,817 +0,0 @@
-# Copyright 2022 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.
-
-""" Implementation of java_binary for bazel """
-
-load(":common/cc/cc_common.bzl", "cc_common")
-load(":common/cc/cc_info.bzl", "CcInfo")
-load(":common/cc/semantics.bzl", cc_semantics = "semantics")
-load(":common/java/basic_java_library.bzl", "BASIC_JAVA_LIBRARY_IMPLICIT_ATTRS", "basic_java_library", "collect_deps")
-load(":common/java/boot_class_path_info.bzl", "BootClassPathInfo")
-load(":common/java/java_binary_deploy_jar.bzl", "create_deploy_archive")
-load(":common/java/java_common.bzl", "java_common")
-load(
- ":common/java/java_common_internal_for_builtins.bzl",
- "collect_native_deps_dirs",
- "get_runtime_classpath_for_archive",
-)
-load(":common/java/java_helper.bzl", "helper")
-load(":common/java/java_info.bzl", "JavaCompilationInfo", "JavaInfo", "JavaPluginInfo", "to_java_binary_info")
-load(":common/java/java_semantics.bzl", "semantics")
-load(":common/paths.bzl", "paths")
-load(":common/proto/proto_info.bzl", "ProtoInfo")
-load(":common/rule_util.bzl", "merge_attrs")
-
-CcLauncherInfo = _builtins.internal.cc_internal.launcher_provider
-
-InternalDeployJarInfo = provider(
- "Provider for passing info to deploy jar rule",
- fields = [
- "java_attrs",
- "strip_as_default",
- "add_exports",
- "add_opens",
- ],
-)
-
-def basic_java_binary(
- ctx,
- deps,
- runtime_deps,
- resources,
- main_class,
- coverage_main_class,
- coverage_config,
- launcher_info,
- executable,
- strip_as_default,
- extension_registry_provider = None,
- is_test_rule_class = False):
- """Creates actions for compiling and linting java sources, coverage support, and sources jar (_deploy-src.jar).
-
- Args:
- ctx: (RuleContext) The rule context
- deps: (list[Target]) The list of other targets to be compiled with
- runtime_deps: (list[Target]) The list of other targets to be linked in
- resources: (list[File]) The list of data files to be included in the class jar
- main_class: (String) FQN of the java main class
- coverage_main_class: (String) FQN of the actual main class if coverage is enabled
- coverage_config: (Struct|None) If coverage is enabled, a struct with fields (runner, manifest, env, support_files), None otherwise
- launcher_info: (Struct) Structure with fields (launcher, unstripped_launcher, runfiles, runtime_jars, jvm_flags, classpath_resources)
- executable: (File) The executable output of the rule
- strip_as_default: (bool) Whether this target outputs a stripped launcher and deploy jar
- extension_registry_provider: (GeneratedExtensionRegistryProvider) internal param, do not use
- is_test_rule_class: (bool) Whether this rule is a test rule
-
- Returns:
- Tuple(
- dict[str, Provider], // providers
- Struct( // default info
- files_to_build: depset(File),
- runfiles: Runfiles,
- executable: File
- ),
- list[String] // jvm flags
- )
-
- """
- if not ctx.attr.create_executable and (ctx.attr.launcher and cc_common.launcher_provider in ctx.attr.launcher):
- fail("launcher specified but create_executable is false")
- if not ctx.attr.use_launcher and (ctx.attr.launcher and ctx.attr.launcher.label != semantics.LAUNCHER_FLAG_LABEL):
- fail("launcher specified but use_launcher is false")
-
- if not ctx.attr.srcs and ctx.attr.deps:
- fail("deps not allowed without srcs; move to runtime_deps?")
-
- module_flags = [dep[JavaInfo].module_flags_info for dep in runtime_deps if JavaInfo in dep]
- add_exports = depset(ctx.attr.add_exports, transitive = [m.add_exports for m in module_flags])
- add_opens = depset(ctx.attr.add_opens, transitive = [m.add_opens for m in module_flags])
-
- classpath_resources = []
- classpath_resources.extend(launcher_info.classpath_resources)
- if hasattr(ctx.files, "classpath_resources"):
- classpath_resources.extend(ctx.files.classpath_resources)
-
- toolchain = semantics.find_java_toolchain(ctx)
- timezone_data = [toolchain._timezone_data] if toolchain._timezone_data else []
- target, common_info = basic_java_library(
- ctx,
- srcs = ctx.files.srcs,
- deps = deps,
- runtime_deps = runtime_deps,
- plugins = ctx.attr.plugins,
- resources = resources,
- resource_jars = timezone_data,
- classpath_resources = classpath_resources,
- javacopts = ctx.attr.javacopts,
- neverlink = ctx.attr.neverlink,
- enable_compile_jar_action = False,
- coverage_config = coverage_config,
- add_exports = ctx.attr.add_exports,
- add_opens = ctx.attr.add_opens,
- bootclasspath = ctx.attr.bootclasspath,
- )
- java_info = target["JavaInfo"]
- compilation_info = java_info.compilation_info
- runtime_classpath = depset(
- order = "preorder",
- transitive = [
- java_info.transitive_runtime_jars
- for java_info in (
- collect_deps(ctx.attr.runtime_deps + deps) +
- ([coverage_config.runner] if coverage_config and coverage_config.runner else [])
- )
- ],
- )
- if extension_registry_provider:
- runtime_classpath = depset(order = "preorder", direct = [extension_registry_provider.class_jar], transitive = [runtime_classpath])
- java_info = java_common.merge(
- [
- java_info,
- JavaInfo(
- output_jar = extension_registry_provider.class_jar,
- compile_jar = None,
- source_jar = extension_registry_provider.src_jar,
- ),
- ],
- )
- compilation_info = JavaCompilationInfo(
- compilation_classpath = compilation_info.compilation_classpath,
- runtime_classpath = runtime_classpath,
- boot_classpath = compilation_info.boot_classpath,
- javac_options = compilation_info.javac_options,
- )
-
- java_attrs = _collect_attrs(ctx, runtime_classpath, classpath_resources)
-
- jvm_flags = []
-
- jvm_flags.extend(launcher_info.jvm_flags)
-
- native_libs_depsets = []
- for dep in runtime_deps:
- if JavaInfo in dep:
- native_libs_depsets.append(dep[JavaInfo].transitive_native_libraries)
- if CcInfo in dep:
- native_libs_depsets.append(dep[CcInfo].transitive_native_libraries())
- native_libs_dirs = collect_native_deps_dirs(depset(transitive = native_libs_depsets))
- if native_libs_dirs:
- prefix = "${JAVA_RUNFILES}/" + ctx.workspace_name + "/"
- jvm_flags.append("-Djava.library.path=%s" % (
- ":".join([prefix + d for d in native_libs_dirs])
- ))
-
- jvm_flags.extend(ctx.fragments.java.default_jvm_opts)
- jvm_flags.extend([ctx.expand_make_variables(
- "jvm_flags",
- ctx.expand_location(flag, ctx.attr.data, short_paths = True),
- {},
- ) for flag in ctx.attr.jvm_flags])
-
- # TODO(cushon): make string formatting lazier once extend_template support is added
- # https://github.com/bazelbuild/proposals#:~:text=2022%2D04%2D25,Starlark
- jvm_flags.extend(["--add-exports=%s=ALL-UNNAMED" % x for x in add_exports.to_list()])
- jvm_flags.extend(["--add-opens=%s=ALL-UNNAMED" % x for x in add_opens.to_list()])
-
- files_to_build = []
-
- if executable:
- files_to_build.append(executable)
-
- output_groups = common_info.output_groups
-
- if coverage_config:
- _generate_coverage_manifest(ctx, coverage_config.manifest, java_attrs.runtime_classpath)
- files_to_build.append(coverage_config.manifest)
-
- if extension_registry_provider:
- files_to_build.append(extension_registry_provider.class_jar)
- output_groups["_direct_source_jars"] = (
- output_groups["_direct_source_jars"] + [extension_registry_provider.src_jar]
- )
- output_groups["_source_jars"] = depset(
- direct = [extension_registry_provider.src_jar],
- transitive = [output_groups["_source_jars"]],
- )
-
- if (ctx.fragments.java.one_version_enforcement_on_java_tests or not is_test_rule_class):
- one_version_output = _create_one_version_check(ctx, java_attrs.runtime_classpath, is_test_rule_class)
- else:
- one_version_output = None
-
- validation_outputs = [one_version_output] if one_version_output else []
-
- _create_deploy_sources_jar(ctx, output_groups["_source_jars"])
-
- files = depset(files_to_build + common_info.files_to_build)
-
- transitive_runfiles_artifacts = depset(transitive = [
- files,
- java_attrs.runtime_classpath,
- depset(transitive = launcher_info.runfiles),
- ])
-
- runfiles = ctx.runfiles(
- transitive_files = transitive_runfiles_artifacts,
- collect_default = True,
- )
-
- if launcher_info.launcher:
- default_launcher = helper.filter_launcher_for_target(ctx)
- default_launcher_artifact = helper.launcher_artifact_for_target(ctx)
- default_launcher_runfiles = default_launcher[DefaultInfo].default_runfiles
- if default_launcher_artifact == launcher_info.launcher:
- runfiles = runfiles.merge(default_launcher_runfiles)
- else:
- # N.B. The "default launcher" referred to here is the launcher target specified through
- # an attribute or flag. We wish to retain the runfiles of the default launcher, *except*
- # for the original cc_binary artifact, because we've swapped it out with our custom
- # launcher. Hence, instead of calling builder.addTarget(), or adding an odd method
- # to Runfiles.Builder, we "unravel" the call and manually add things to the builder.
- # Because the NestedSet representing each target's launcher runfiles is re-built here,
- # we may see increased memory consumption for representing the target's runfiles.
- runfiles = runfiles.merge(
- ctx.runfiles(
- files = [launcher_info.launcher],
- transitive_files = depset([
- file
- for file in default_launcher_runfiles.files.to_list()
- if file != default_launcher_artifact
- ]),
- symlinks = default_launcher_runfiles.symlinks,
- root_symlinks = default_launcher_runfiles.root_symlinks,
- ),
- )
-
- runfiles = runfiles.merge_all([
- dep[DefaultInfo].default_runfiles
- for dep in ctx.attr.runtime_deps
- if DefaultInfo in dep
- ])
-
- if validation_outputs:
- output_groups["_validation"] = output_groups.get("_validation", []) + validation_outputs
-
- _filter_validation_output_group(ctx, output_groups)
-
- java_binary_info = to_java_binary_info(java_info, compilation_info)
-
- internal_deploy_jar_info = InternalDeployJarInfo(
- java_attrs = java_attrs,
- strip_as_default = strip_as_default,
- add_exports = add_exports,
- add_opens = add_opens,
- )
-
- # "temporary" workaround for https://github.com/bazelbuild/intellij/issues/5845
- extra_files = []
- if is_test_rule_class and ctx.fragments.java.auto_create_java_test_deploy_jars():
- extra_files.append(_auto_create_deploy_jar(ctx, internal_deploy_jar_info, launcher_info, main_class, coverage_main_class))
-
- default_info = struct(
- files = depset(extra_files, transitive = [files]),
- runfiles = runfiles,
- executable = executable,
- )
-
- return {
- "OutputGroupInfo": OutputGroupInfo(**output_groups),
- "JavaInfo": java_binary_info,
- "InstrumentedFilesInfo": target["InstrumentedFilesInfo"],
- "JavaRuntimeClasspathInfo": java_common.JavaRuntimeClasspathInfo(runtime_classpath = java_info.transitive_runtime_jars),
- "InternalDeployJarInfo": internal_deploy_jar_info,
- }, default_info, jvm_flags
-
-def _collect_attrs(ctx, runtime_classpath, classpath_resources):
- deploy_env_jars = depset(transitive = [
- dep[java_common.JavaRuntimeClasspathInfo].runtime_classpath
- for dep in ctx.attr.deploy_env
- ]) if hasattr(ctx.attr, "deploy_env") else depset()
-
- runtime_classpath_for_archive = get_runtime_classpath_for_archive(runtime_classpath, deploy_env_jars)
- runtime_jars = [ctx.outputs.classjar]
-
- resources = [p for p in ctx.files.srcs if p.extension == "properties"]
- transitive_resources = []
- for r in ctx.attr.resources:
- transitive_resources.append(
- r[ProtoInfo].transitive_sources if ProtoInfo in r else r.files,
- )
-
- resource_names = dict()
- for r in classpath_resources:
- if r.basename in resource_names:
- fail("entries must have different file names (duplicate: %s)" % r.basename)
- resource_names[r.basename] = None
-
- return struct(
- runtime_jars = depset(runtime_jars),
- runtime_classpath_for_archive = runtime_classpath_for_archive,
- classpath_resources = depset(classpath_resources),
- runtime_classpath = depset(order = "preorder", direct = runtime_jars, transitive = [runtime_classpath]),
- resources = depset(resources, transitive = transitive_resources),
- )
-
-def _generate_coverage_manifest(ctx, output, runtime_classpath):
- ctx.actions.write(
- output = output,
- content = "\n".join([file.short_path for file in runtime_classpath.to_list()]),
- )
-
-def _create_one_version_check(ctx, inputs, is_test_rule_class):
- one_version_level = ctx.fragments.java.one_version_enforcement_level
- if one_version_level == "OFF":
- return None
- tool = helper.check_and_get_one_version_attribute(ctx, "_one_version_tool")
-
- if is_test_rule_class:
- toolchain = semantics.find_java_toolchain(ctx)
- allowlist = toolchain._one_version_allowlist_for_tests
- else:
- allowlist = helper.check_and_get_one_version_attribute(ctx, "_one_version_allowlist")
-
- if not tool: # On Mac oneversion tool is not available
- return None
-
- output = ctx.actions.declare_file("%s-one-version.txt" % ctx.label.name)
-
- args = ctx.actions.args()
- args.set_param_file_format("shell").use_param_file("@%s", use_always = True)
-
- one_version_inputs = []
- args.add("--output", output)
- if allowlist:
- args.add("--allowlist", allowlist)
- one_version_inputs.append(allowlist)
- if one_version_level == "WARNING":
- args.add("--succeed_on_found_violations")
- args.add_all(
- "--inputs",
- inputs,
- map_each = helper.jar_and_target_arg_mapper,
- )
-
- ctx.actions.run(
- mnemonic = "JavaOneVersion",
- progress_message = "Checking for one-version violations in %{label}",
- executable = tool,
- toolchain = semantics.JAVA_TOOLCHAIN_TYPE,
- inputs = depset(one_version_inputs, transitive = [inputs]),
- tools = [tool],
- outputs = [output],
- arguments = [args],
- )
-
- return output
-
-def _create_deploy_sources_jar(ctx, sources):
- helper.create_single_jar(
- ctx.actions,
- toolchain = semantics.find_java_toolchain(ctx),
- output = ctx.outputs.deploysrcjar,
- sources = sources,
- )
-
-def _filter_validation_output_group(ctx, output_group):
- to_exclude = depset(transitive = [
- dep[OutputGroupInfo]._validation
- for dep in ctx.attr.deploy_env
- if OutputGroupInfo in dep and hasattr(dep[OutputGroupInfo], "_validation")
- ]) if hasattr(ctx.attr, "deploy_env") else depset()
- if to_exclude:
- transitive_validations = depset(transitive = [
- _get_validations_from_attr(ctx, attr_name)
- for attr_name in dir(ctx.attr)
- # we also exclude implicit, cfg=host/exec and tool attributes
- if not attr_name.startswith("_") and
- attr_name not in [
- "deploy_env",
- "applicable_licenses",
- "package_metadata",
- "plugins",
- "translations",
- # special ignored attributes
- "compatible_with",
- "restricted_to",
- "exec_compatible_with",
- "target_compatible_with",
- ]
- ])
- if not ctx.attr.create_executable:
- excluded_set = {x: None for x in to_exclude.to_list()}
- transitive_validations = [
- x
- for x in transitive_validations.to_list()
- if x not in excluded_set
- ]
- output_group["_validation_transitive"] = transitive_validations
-
-def _get_validations_from_attr(ctx, attr_name):
- attr = getattr(ctx.attr, attr_name)
- if type(attr) == "list":
- return depset(transitive = [_get_validations_from_target(t) for t in attr])
- else:
- return _get_validations_from_target(attr)
-
-def _get_validations_from_target(target):
- if (
- type(target) == "Target" and
- OutputGroupInfo in target and
- hasattr(target[OutputGroupInfo], "_validation")
- ):
- return target[OutputGroupInfo]._validation
- else:
- return depset()
-
-# TODO: bazelbuild/intellij/issues/5845 - remove this once no longer required
-# this need not be completely identical to the regular deploy jar since we only
-# care about packaging the classpath
-def _auto_create_deploy_jar(ctx, info, launcher_info, main_class, coverage_main_class):
- output = ctx.actions.declare_file(ctx.label.name + "_auto_deploy.jar")
- java_attrs = info.java_attrs
- runtime_classpath = depset(
- direct = launcher_info.runtime_jars,
- transitive = [
- java_attrs.runtime_jars,
- java_attrs.runtime_classpath_for_archive,
- ],
- order = "preorder",
- )
- create_deploy_archive(
- ctx,
- launcher = launcher_info.launcher,
- main_class = main_class,
- coverage_main_class = coverage_main_class,
- resources = java_attrs.resources,
- classpath_resources = java_attrs.classpath_resources,
- runtime_classpath = runtime_classpath,
- manifest_lines = info.manifest_lines,
- build_info_files = [],
- build_target = str(ctx.label),
- output = output,
- one_version_level = ctx.fragments.java.one_version_enforcement_level,
- one_version_allowlist = helper.check_and_get_one_version_attribute(ctx, "_one_version_allowlist"),
- multi_release = ctx.fragments.java.multi_release_deploy_jars,
- hermetic = hasattr(ctx.attr, "hermetic") and ctx.attr.hermetic,
- add_exports = info.add_exports,
- add_opens = info.add_opens,
- )
- return output
-
-BASIC_JAVA_BINARY_ATTRIBUTES = merge_attrs(
- BASIC_JAVA_LIBRARY_IMPLICIT_ATTRS,
- {
- "srcs": attr.label_list(
- allow_files = [".java", ".srcjar", ".properties"] + semantics.EXTRA_SRCS_TYPES,
- flags = ["DIRECT_COMPILE_TIME_INPUT", "ORDER_INDEPENDENT"],
- doc = """
-The list of source files that are processed to create the target.
-This attribute is almost always required; see exceptions below.
-<p>
-Source files of type <code>.java</code> are compiled. In case of generated
-<code>.java</code> files it is generally advisable to put the generating rule's name
-here instead of the name of the file itself. This not only improves readability but
-makes the rule more resilient to future changes: if the generating rule generates
-different files in the future, you only need to fix one place: the <code>outs</code> of
-the generating rule. You should not list the generating rule in <code>deps</code>
-because it is a no-op.
-</p>
-<p>
-Source files of type <code>.srcjar</code> are unpacked and compiled. (This is useful if
-you need to generate a set of <code>.java</code> files with a genrule.)
-</p>
-<p>
-Rules: if the rule (typically <code>genrule</code> or <code>filegroup</code>) generates
-any of the files listed above, they will be used the same way as described for source
-files.
-</p>
-
-<p>
-This argument is almost always required, except if a
-<a href="#java_binary.main_class"><code>main_class</code></a> attribute specifies a
-class on the runtime classpath or you specify the <code>runtime_deps</code> argument.
-</p>
- """,
- ),
- "deps": attr.label_list(
- allow_files = [".jar"],
- allow_rules = semantics.ALLOWED_RULES_IN_DEPS + semantics.ALLOWED_RULES_IN_DEPS_WITH_WARNING,
- providers = [
- [CcInfo],
- [JavaInfo],
- ],
- flags = ["SKIP_ANALYSIS_TIME_FILETYPE_CHECK"],
- doc = """
-The list of other libraries to be linked in to the target.
-See general comments about <code>deps</code> at
-<a href="common-definitions.html#typical-attributes">Typical attributes defined by
-most build rules</a>.
- """,
- ),
- "resources": attr.label_list(
- allow_files = True,
- flags = ["SKIP_CONSTRAINTS_OVERRIDE", "ORDER_INDEPENDENT"],
- doc = """
-A list of data files to include in a Java jar.
-
-<p>
-Resources may be source files or generated files.
-</p>
- """ + semantics.DOCS.for_attribute("resources"),
- ),
- "runtime_deps": attr.label_list(
- allow_files = [".jar"],
- allow_rules = semantics.ALLOWED_RULES_IN_DEPS,
- providers = [[CcInfo], [JavaInfo]],
- flags = ["SKIP_ANALYSIS_TIME_FILETYPE_CHECK"],
- doc = """
-Libraries to make available to the final binary or test at runtime only.
-Like ordinary <code>deps</code>, these will appear on the runtime classpath, but unlike
-them, not on the compile-time classpath. Dependencies needed only at runtime should be
-listed here. Dependency-analysis tools should ignore targets that appear in both
-<code>runtime_deps</code> and <code>deps</code>.
- """,
- ),
- "data": attr.label_list(
- allow_files = True,
- flags = ["SKIP_CONSTRAINTS_OVERRIDE"],
- doc = """
-The list of files needed by this library at runtime.
-See general comments about <code>data</code>
-at <a href="${link common-definitions#typical-attributes}">Typical attributes defined by
-most build rules</a>.
- """ + semantics.DOCS.for_attribute("data"),
- ),
- "plugins": attr.label_list(
- providers = [JavaPluginInfo],
- allow_files = True,
- cfg = "exec",
- doc = """
-Java compiler plugins to run at compile-time.
-Every <code>java_plugin</code> specified in this attribute will be run whenever this rule
-is built. A library may also inherit plugins from dependencies that use
-<code><a href="#java_library.exported_plugins">exported_plugins</a></code>. Resources
-generated by the plugin will be included in the resulting jar of this rule.
- """,
- ),
- "deploy_env": attr.label_list(
- providers = [java_common.JavaRuntimeClasspathInfo],
- allow_files = False,
- doc = """
-A list of other <code>java_binary</code> targets which represent the deployment
-environment for this binary.
-Set this attribute when building a plugin which will be loaded by another
-<code>java_binary</code>.<br/> Setting this attribute excludes all dependencies from
-the runtime classpath (and the deploy jar) of this binary that are shared between this
-binary and the targets specified in <code>deploy_env</code>.
- """,
- ),
- "launcher": attr.label(
- # TODO(b/295221112): add back CcLauncherInfo
- allow_files = False,
- doc = """
-Specify a binary that will be used to run your Java program instead of the
-normal <code>bin/java</code> program included with the JDK.
-The target must be a <code>cc_binary</code>. Any <code>cc_binary</code> that
-implements the
-<a href="http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/invocation.html">
-Java Invocation API</a> can be specified as a value for this attribute.
-
-<p>By default, Bazel will use the normal JDK launcher (bin/java or java.exe).</p>
-
-<p>The related <a href="${link user-manual#flag--java_launcher}"><code>
---java_launcher</code></a> Bazel flag affects only those
-<code>java_binary</code> and <code>java_test</code> targets that have
-<i>not</i> specified a <code>launcher</code> attribute.</p>
-
-<p>Note that your native (C++, SWIG, JNI) dependencies will be built differently
-depending on whether you are using the JDK launcher or another launcher:</p>
-
-<ul>
-<li>If you are using the normal JDK launcher (the default), native dependencies are
-built as a shared library named <code>{name}_nativedeps.so</code>, where
-<code>{name}</code> is the <code>name</code> attribute of this java_binary rule.
-Unused code is <em>not</em> removed by the linker in this configuration.</li>
-
-<li>If you are using any other launcher, native (C++) dependencies are statically
-linked into a binary named <code>{name}_nativedeps</code>, where <code>{name}</code>
-is the <code>name</code> attribute of this java_binary rule. In this case,
-the linker will remove any code it thinks is unused from the resulting binary,
-which means any C++ code accessed only via JNI may not be linked in unless
-that <code>cc_library</code> target specifies <code>alwayslink = 1</code>.</li>
-</ul>
-
-<p>When using any launcher other than the default JDK launcher, the format
-of the <code>*_deploy.jar</code> output changes. See the main
-<a href="#java_binary">java_binary</a> docs for details.</p>
- """,
- ),
- "bootclasspath": attr.label(
- providers = [BootClassPathInfo],
- flags = ["SKIP_CONSTRAINTS_OVERRIDE"],
- doc = "Restricted API, do not use!",
- ),
- "neverlink": attr.bool(),
- "javacopts": attr.string_list(
- doc = """
-Extra compiler options for this binary.
-Subject to <a href="make-variables.html">"Make variable"</a> substitution and
-<a href="common-definitions.html#sh-tokenization">Bourne shell tokenization</a>.
-<p>These compiler options are passed to javac after the global compiler options.</p>
- """,
- ),
- "add_exports": attr.string_list(
- doc = """
-Allow this library to access the given <code>module</code> or <code>package</code>.
-<p>
-This corresponds to the javac and JVM --add-exports= flags.
- """,
- ),
- "add_opens": attr.string_list(
- doc = """
-Allow this library to reflectively access the given <code>module</code> or
-<code>package</code>.
-<p>
-This corresponds to the javac and JVM --add-opens= flags.
- """,
- ),
- "main_class": attr.string(
- doc = """
-Name of class with <code>main()</code> method to use as entry point.
-If a rule uses this option, it does not need a <code>srcs=[...]</code> list.
-Thus, with this attribute one can make an executable from a Java library that already
-contains one or more <code>main()</code> methods.
-<p>
-The value of this attribute is a class name, not a source file. The class must be
-available at runtime: it may be compiled by this rule (from <code>srcs</code>) or
-provided by direct or transitive dependencies (through <code>runtime_deps</code> or
-<code>deps</code>). If the class is unavailable, the binary will fail at runtime; there
-is no build-time check.
-</p>
- """,
- ),
- "jvm_flags": attr.string_list(
- doc = """
-A list of flags to embed in the wrapper script generated for running this binary.
-Subject to <a href="${link make-variables#location}">$(location)</a> and
-<a href="make-variables.html">"Make variable"</a> substitution, and
-<a href="common-definitions.html#sh-tokenization">Bourne shell tokenization</a>.
-
-<p>The wrapper script for a Java binary includes a CLASSPATH definition
-(to find all the dependent jars) and invokes the right Java interpreter.
-The command line generated by the wrapper script includes the name of
-the main class followed by a <code>"$@"</code> so you can pass along other
-arguments after the classname. However, arguments intended for parsing
-by the JVM must be specified <i>before</i> the classname on the command
-line. The contents of <code>jvm_flags</code> are added to the wrapper
-script before the classname is listed.</p>
-
-<p>Note that this attribute has <em>no effect</em> on <code>*_deploy.jar</code>
-outputs.</p>
- """,
- ),
- "deploy_manifest_lines": attr.string_list(
- doc = """
-A list of lines to add to the <code>META-INF/manifest.mf</code> file generated for the
-<code>*_deploy.jar</code> target. The contents of this attribute are <em>not</em> subject
-to <a href="make-variables.html">"Make variable"</a> substitution.
- """,
- ),
- "stamp": attr.int(
- default = -1,
- values = [-1, 0, 1],
- doc = """
-Whether to encode build information into the binary. Possible values:
-<ul>
-<li>
- <code>stamp = 1</code>: Always stamp the build information into the binary, even in
- <a href="${link user-manual#flag--stamp}"><code>--nostamp</code></a> builds. <b>This
- setting should be avoided</b>, since it potentially kills remote caching for the
- binary and any downstream actions that depend on it.
-</li>
-<li>
- <code>stamp = 0</code>: Always replace build information by constant values. This
- gives good build result caching.
-</li>
-<li>
- <code>stamp = -1</code>: Embedding of build information is controlled by the
- <a href="${link user-manual#flag--stamp}"><code>--[no]stamp</code></a> flag.
-</li>
-</ul>
-<p>Stamped binaries are <em>not</em> rebuilt unless their dependencies change.</p>
- """,
- ),
- "use_testrunner": attr.bool(
- default = False,
- doc = semantics.DOCS.for_attribute("use_testrunner") + """
-<br/>
-You can use this to override the default
-behavior, which is to use test runner for
-<code>java_test</code> rules,
-and not use it for <code>java_binary</code> rules. It is unlikely
-you will want to do this. One use is for <code>AllTest</code>
-rules that are invoked by another rule (to set up a database
-before running the tests, for example). The <code>AllTest</code>
-rule must be declared as a <code>java_binary</code>, but should
-still use the test runner as its main entry point.
-
-The name of a test runner class can be overridden with <code>main_class</code> attribute.
- """,
- ),
- "use_launcher": attr.bool(
- default = True,
- doc = """
-Whether the binary should use a custom launcher.
-
-<p>If this attribute is set to false, the
-<a href="${link java_binary.launcher}">launcher</a> attribute and the related
-<a href="${link user-manual#flag--java_launcher}"><code>--java_launcher</code></a> flag
-will be ignored for this target.
- """,
- ),
- "env": attr.string_dict(),
- "classpath_resources": attr.label_list(
- allow_files = True,
- doc = """
-<em class="harmful">DO NOT USE THIS OPTION UNLESS THERE IS NO OTHER WAY)</em>
-<p>
-A list of resources that must be located at the root of the java tree. This attribute's
-only purpose is to support third-party libraries that require that their resources be
-found on the classpath as exactly <code>"myconfig.xml"</code>. It is only allowed on
-binaries and not libraries, due to the danger of namespace conflicts.
-</p>
- """,
- ),
- "licenses": attr.license() if hasattr(attr, "license") else attr.string_list(),
- "_stub_template": attr.label(
- default = semantics.JAVA_STUB_TEMPLATE_LABEL,
- allow_single_file = True,
- ),
- "_java_toolchain_type": attr.label(default = semantics.JAVA_TOOLCHAIN_TYPE),
- "_windows_constraints": attr.label_list(
- default = ["@" + paths.join(cc_semantics.get_platforms_root(), "os:windows")],
- ),
- "_build_info_translator": attr.label(default = semantics.BUILD_INFO_TRANSLATOR_LABEL),
- } | ({} if _builtins.internal.java_common_internal_do_not_use.incompatible_disable_non_executable_java_binary() else {"create_executable": attr.bool(default = True, doc = "Deprecated, use <code>java_single_jar</code> instead.")}),
-)
-
-BASE_TEST_ATTRIBUTES = {
- "test_class": attr.string(
- doc = """
-The Java class to be loaded by the test runner.<br/>
-<p>
- By default, if this argument is not defined then the legacy mode is used and the
- test arguments are used instead. Set the <code>--nolegacy_bazel_java_test</code> flag
- to not fallback on the first argument.
-</p>
-<p>
- This attribute specifies the name of a Java class to be run by
- this test. It is rare to need to set this. If this argument is omitted,
- it will be inferred using the target's <code>name</code> and its
- source-root-relative path. If the test is located outside a known
- source root, Bazel will report an error if <code>test_class</code>
- is unset.
-</p>
-<p>
- For JUnit3, the test class needs to either be a subclass of
- <code>junit.framework.TestCase</code> or it needs to have a public
- static <code>suite()</code> method that returns a
- <code>junit.framework.Test</code> (or a subclass of <code>Test</code>).
- For JUnit4, the class needs to be annotated with
- <code>org.junit.runner.RunWith</code>.
-</p>
-<p>
- This attribute allows several <code>java_test</code> rules to
- share the same <code>Test</code>
- (<code>TestCase</code>, <code>TestSuite</code>, ...). Typically
- additional information is passed to it
- (e.g. via <code>jvm_flags=['-Dkey=value']</code>) so that its
- behavior differs in each case, such as running a different
- subset of the tests. This attribute also enables the use of
- Java tests outside the <code>javatests</code> tree.
-</p>
- """,
- ),
- "env_inherit": attr.string_list(),
- "_apple_constraints": attr.label_list(
- default = [
- "@" + paths.join(cc_semantics.get_platforms_root(), "os:ios"),
- "@" + paths.join(cc_semantics.get_platforms_root(), "os:macos"),
- "@" + paths.join(cc_semantics.get_platforms_root(), "os:tvos"),
- "@" + paths.join(cc_semantics.get_platforms_root(), "os:visionos"),
- "@" + paths.join(cc_semantics.get_platforms_root(), "os:watchos"),
- ],
- ),
- "_legacy_any_type_attrs": attr.string_list(default = ["stamp"]),
-}
diff --git a/src/main/starlark/builtins_bzl/common/java/java_binary_deploy_jar.bzl b/src/main/starlark/builtins_bzl/common/java/java_binary_deploy_jar.bzl
deleted file mode 100644
index 92242a9..0000000
--- a/src/main/starlark/builtins_bzl/common/java/java_binary_deploy_jar.bzl
+++ /dev/null
@@ -1,254 +0,0 @@
-# Copyright 2022 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.
-
-"""Auxiliary rule to create the deploy archives for java_binary"""
-
-load(":common/java/java_helper.bzl", "helper")
-load(":common/java/java_semantics.bzl", "semantics")
-
-InstrumentedFilesInfo = _builtins.toplevel.InstrumentedFilesInfo
-
-def _stamping_enabled(ctx, stamp):
- if ctx.configuration.is_tool_configuration():
- stamp = 0
- return (stamp == 1) or (stamp == -1 and ctx.configuration.stamp_binaries())
-
-def get_build_info(ctx, stamp):
- if _stamping_enabled(ctx, stamp):
- # Makes the target depend on BUILD_INFO_KEY, which helps to discover stamped targets
- # See b/326620485 for more details.
- ctx.version_file # buildifier: disable=no-effect
- return ctx.attr._build_info_translator[OutputGroupInfo].non_redacted_build_info_files.to_list()
- else:
- return ctx.attr._build_info_translator[OutputGroupInfo].redacted_build_info_files.to_list()
-
-def create_deploy_archives(
- ctx,
- java_attrs,
- launcher_info,
- main_class,
- coverage_main_class,
- strip_as_default,
- hermetic = False,
- add_exports = depset(),
- add_opens = depset(),
- shared_archive = None,
- one_version_level = "OFF",
- one_version_allowlist = None,
- extra_args = [],
- extra_manifest_lines = []):
- """ Registers actions for _deploy.jar and _deploy.jar.unstripped
-
- Args:
- ctx: (RuleContext) The rule context
- java_attrs: (Struct) Struct of (classpath_resources, runtime_jars, runtime_classpath_for_archive, resources)
- launcher_info: (Struct) Struct of (runtime_jars, launcher, unstripped_launcher)
- main_class: (String) FQN of the entry point for execution
- coverage_main_class: (String) FQN of the entry point for coverage collection
- build_target: (String) Name of the build target for stamping
- strip_as_default: (bool) Whether to create unstripped deploy jar
- hermetic: (bool)
- add_exports: (depset)
- add_opens: (depset)
- shared_archive: (File) Optional .jsa artifact
- one_version_level: (String) Optional one version check level, default OFF
- one_version_allowlist: (File) Optional allowlist for one version check
- extra_args: (list[Args]) Optional arguments for the deploy jar action
- extra_manifest_lines: (list[String]) Optional lines added to the jar manifest
- """
- classpath_resources = java_attrs.classpath_resources
-
- runtime_classpath = depset(
- direct = launcher_info.runtime_jars,
- transitive = [
- java_attrs.runtime_jars,
- java_attrs.runtime_classpath_for_archive,
- ],
- order = "preorder",
- )
- multi_release = ctx.fragments.java.multi_release_deploy_jars
- build_info_files = get_build_info(ctx, ctx.attr.stamp)
- build_target = str(ctx.label)
- manifest_lines = ctx.attr.deploy_manifest_lines + extra_manifest_lines
- create_deploy_archive(
- ctx,
- launcher_info.launcher,
- main_class,
- coverage_main_class,
- java_attrs.resources,
- classpath_resources,
- runtime_classpath,
- manifest_lines,
- build_info_files,
- build_target,
- output = ctx.outputs.deployjar,
- shared_archive = shared_archive,
- one_version_level = one_version_level,
- one_version_allowlist = one_version_allowlist,
- multi_release = multi_release,
- hermetic = hermetic,
- add_exports = add_exports,
- add_opens = add_opens,
- extra_args = extra_args,
- )
-
- if strip_as_default:
- create_deploy_archive(
- ctx,
- launcher_info.unstripped_launcher,
- main_class,
- coverage_main_class,
- java_attrs.resources,
- classpath_resources,
- runtime_classpath,
- manifest_lines,
- build_info_files,
- build_target,
- output = ctx.outputs.unstrippeddeployjar,
- multi_release = multi_release,
- hermetic = hermetic,
- add_exports = add_exports,
- add_opens = add_opens,
- extra_args = extra_args,
- )
- else:
- ctx.actions.write(ctx.outputs.unstrippeddeployjar, "")
-
-def create_deploy_archive(
- ctx,
- launcher,
- main_class,
- coverage_main_class,
- resources,
- classpath_resources,
- runtime_classpath,
- manifest_lines,
- build_info_files,
- build_target,
- output,
- shared_archive = None,
- one_version_level = "OFF",
- one_version_allowlist = None,
- multi_release = False,
- hermetic = False,
- add_exports = [],
- add_opens = [],
- extra_args = []):
- """ Creates a deploy jar
-
- Requires a Java runtime toolchain if and only if hermetic is True.
-
- Args:
- ctx: (RuleContext) The rule context
- launcher: (File) the launcher artifact
- main_class: (String) FQN of the entry point for execution
- coverage_main_class: (String) FQN of the entry point for coverage collection
- resources: (Depset) resource inputs
- classpath_resources: (Depset) classpath resource inputs
- runtime_classpath: (Depset) source files to add to the jar
- build_target: (String) Name of the build target for stamping
- manifest_lines: (list[String]) Optional lines added to the jar manifest
- build_info_files: (list[File]) build info files for stamping
- build_target: (String) the owner build target label name string
- output: (File) the output jar artifact
- shared_archive: (File) Optional .jsa artifact
- one_version_level: (String) Optional one version check level, default OFF
- one_version_allowlist: (File) Optional allowlist for one version check
- multi_release: (bool)
- hermetic: (bool)
- add_exports: (depset)
- add_opens: (depset)
- extra_args: (list[Args]) Optional arguments for the deploy jar action
- """
- input_files = []
- input_files.extend(build_info_files)
-
- transitive_input_files = [
- resources,
- classpath_resources,
- runtime_classpath,
- ]
-
- single_jar = semantics.find_java_toolchain(ctx).single_jar
-
- manifest_lines = list(manifest_lines)
- if ctx.configuration.coverage_enabled:
- manifest_lines.append("Coverage-Main-Class: %s" % coverage_main_class)
-
- args = ctx.actions.args()
- args.set_param_file_format("shell").use_param_file("@%s", use_always = True)
-
- args.add("--output", output)
- args.add("--build_target", build_target)
- args.add("--normalize")
- args.add("--compression")
- if main_class:
- args.add("--main_class", main_class)
- args.add_all("--deploy_manifest_lines", manifest_lines)
- args.add_all(build_info_files, before_each = "--build_info_file")
- if launcher:
- input_files.append(launcher)
- args.add("--java_launcher", launcher)
- args.add_all("--classpath_resources", classpath_resources)
- args.add_all(
- "--sources",
- runtime_classpath,
- map_each = helper.jar_and_target_arg_mapper,
- )
-
- if one_version_level != "OFF" and one_version_allowlist:
- input_files.append(one_version_allowlist)
- args.add("--enforce_one_version")
- args.add("--one_version_allowlist", one_version_allowlist)
- if one_version_level == "WARNING":
- args.add("--succeed_on_found_violations")
-
- if multi_release:
- args.add("--multi_release")
-
- if hermetic:
- runtime = ctx.toolchains["@//tools/jdk/hermetic:hermetic_runtime_toolchain_type"].java_runtime
- if runtime.lib_modules != None:
- java_home = runtime.java_home
- lib_modules = runtime.lib_modules
- hermetic_files = runtime.hermetic_files
- args.add("--hermetic_java_home", java_home)
- args.add("--jdk_lib_modules", lib_modules)
- args.add_all("--resources", hermetic_files)
- input_files.append(lib_modules)
- transitive_input_files.append(hermetic_files)
-
- if shared_archive == None:
- shared_archive = runtime.default_cds
-
- if shared_archive:
- input_files.append(shared_archive)
- args.add("--cds_archive", shared_archive)
-
- args.add_all("--add_exports", add_exports)
- args.add_all("--add_opens", add_opens)
-
- inputs = depset(input_files, transitive = transitive_input_files)
-
- ctx.actions.run(
- mnemonic = "JavaDeployJar",
- progress_message = "Building deploy jar %s" % output.short_path,
- executable = single_jar,
- inputs = inputs,
- tools = [single_jar],
- outputs = [output],
- arguments = [args] + extra_args,
- use_default_shell_env = True,
- toolchain = semantics.JAVA_TOOLCHAIN_TYPE,
- )
diff --git a/src/main/starlark/builtins_bzl/common/java/java_binary_wrapper.bzl b/src/main/starlark/builtins_bzl/common/java/java_binary_wrapper.bzl
deleted file mode 100644
index d00d962..0000000
--- a/src/main/starlark/builtins_bzl/common/java/java_binary_wrapper.bzl
+++ /dev/null
@@ -1,71 +0,0 @@
-# Copyright 2022 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.
-
-"""Macro encapsulating the java_binary implementation
-
-This is needed since the `executable` nature of the target must be computed from
-the supplied value of the `create_executable` attribute.
-"""
-
-load(":common/java/java_semantics.bzl", "semantics")
-
-def register_legacy_java_binary_rules(
- rule_exec,
- rule_nonexec,
- **kwargs):
- """Registers the correct java_binary rule and deploy jar rule
-
- Args:
- rule_exec: (Rule) The executable java_binary rule
- rule_nonexec: (Rule) The non-executable java_binary rule
- **kwargs: Actual args to instantiate the rule
- """
-
- create_executable = "create_executable" not in kwargs or kwargs["create_executable"]
-
- # TODO(hvd): migrate depot to integers / maybe use decompose_select_list()
- if "stamp" in kwargs and type(kwargs["stamp"]) == type(True):
- kwargs["stamp"] = 1 if kwargs["stamp"] else 0
- if not create_executable:
- rule_nonexec(**kwargs)
- else:
- if "use_launcher" in kwargs and not kwargs["use_launcher"]:
- kwargs["launcher"] = None
- else:
- # If launcher is not set or None, set it to config flag
- if "launcher" not in kwargs or not kwargs["launcher"]:
- kwargs["launcher"] = semantics.LAUNCHER_FLAG_LABEL
- rule_exec(**kwargs)
-
-def register_java_binary_rules(
- java_binary,
- **kwargs):
- """Creates a java_binary rule and a deploy jar rule
-
- Args:
- java_binary: (Rule) The executable java_binary rule
- **kwargs: Actual args to instantiate the rule
- """
-
- # TODO(hvd): migrate depot to integers / maybe use decompose_select_list()
- if "stamp" in kwargs and type(kwargs["stamp"]) == type(True):
- kwargs["stamp"] = 1 if kwargs["stamp"] else 0
-
- if "use_launcher" in kwargs and not kwargs["use_launcher"]:
- kwargs["launcher"] = None
- else:
- # If launcher is not set or None, set it to config flag
- if "launcher" not in kwargs or not kwargs["launcher"]:
- kwargs["launcher"] = semantics.LAUNCHER_FLAG_LABEL
- java_binary(**kwargs)
diff --git a/src/main/starlark/builtins_bzl/common/java/java_import.bzl b/src/main/starlark/builtins_bzl/common/java/java_import.bzl
deleted file mode 100644
index 925f768..0000000
--- a/src/main/starlark/builtins_bzl/common/java/java_import.bzl
+++ /dev/null
@@ -1,354 +0,0 @@
-# Copyright 2021 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.
-
-"""
-Definition of java_import rule.
-"""
-
-load(":common/cc/cc_info.bzl", "CcInfo")
-load(":common/java/basic_java_library.bzl", "construct_defaultinfo")
-load(":common/java/import_deps_check.bzl", "import_deps_check")
-load(":common/java/java_common.bzl", "java_common")
-load(":common/java/java_common_internal_for_builtins.bzl", _run_ijar_private_for_builtins = "run_ijar")
-load(":common/java/java_info.bzl", "JavaInfo")
-load(":common/java/java_semantics.bzl", "semantics")
-load(":common/java/proguard_validation.bzl", "validate_proguard_specs")
-
-PackageSpecificationInfo = _builtins.toplevel.PackageSpecificationInfo
-
-def _filter_provider(provider, *attrs):
- return [dep[provider] for attr in attrs for dep in attr if provider in dep]
-
-def _collect_jars(ctx, jars):
- jars_dict = {}
- for info in jars:
- if JavaInfo in info:
- fail("'jars' attribute cannot contain labels of Java targets")
- for jar in info.files.to_list():
- jar_path = jar.dirname + jar.basename
- if jars_dict.get(jar_path) != None:
- fail("in jars attribute of java_import rule //" + ctx.label.package + ":" + ctx.attr.name + ": " + jar.basename + " is a duplicate")
- jars_dict[jar_path] = jar
- return [jar_tuple[1] for jar_tuple in jars_dict.items()] if len(jars_dict.items()) > 0 else []
-
-def _process_with_ijars_if_needed(jars, ctx):
- file_dict = {}
- use_ijars = ctx.fragments.java.use_ijars()
- for jar in jars:
- interface_jar = jar
- if use_ijars:
- ijar_basename = jar.short_path.removeprefix("../").removesuffix("." + jar.extension) + "-ijar.jar"
- interface_jar_directory = "_ijar/" + ctx.label.name + "/" + ijar_basename
-
- interface_jar = ctx.actions.declare_file(interface_jar_directory)
- _run_ijar_private_for_builtins(
- ctx.actions,
- target_label = ctx.label,
- jar = jar,
- output = interface_jar,
- java_toolchain = semantics.find_java_toolchain(ctx),
- )
- file_dict[jar] = interface_jar
-
- return file_dict
-
-def _check_export_error(ctx, exports):
- not_in_allowlist = hasattr(ctx.attr, "_allowlist_java_import_exports") and not getattr(ctx.attr, "_allowlist_java_import_exports")[PackageSpecificationInfo].contains(ctx.label)
- disallow_java_import_exports = ctx.fragments.java.disallow_java_import_exports()
-
- if len(exports) != 0 and (disallow_java_import_exports or not_in_allowlist):
- fail("java_import.exports is no longer supported; use java_import.deps instead")
-
-def _check_empty_jars_error(ctx, jars):
- # TODO(kotlaja): Remove temporary incompatible flag [disallow_java_import_empty_jars] once migration is done.
- not_in_allowlist = hasattr(ctx.attr, "_allowlist_java_import_empty_jars") and not getattr(ctx.attr, "_allowlist_java_import_empty_jars")[PackageSpecificationInfo].contains(ctx.label)
- disallow_java_import_empty_jars = ctx.fragments.java.disallow_java_import_empty_jars()
-
- if len(jars) == 0 and disallow_java_import_empty_jars and not_in_allowlist:
- fail("empty java_import.jars is no longer supported " + ctx.label.package)
-
-def _create_java_info_with_dummy_output_file(ctx, srcjar, all_deps, exports, runtime_deps_list, neverlink, cc_info_list, add_exports, add_opens):
- dummy_jar = ctx.actions.declare_file(ctx.label.name + "_dummy.jar")
- dummy_src_jar = srcjar
- if dummy_src_jar == None:
- dummy_src_jar = ctx.actions.declare_file(ctx.label.name + "_src_dummy.java")
- ctx.actions.write(dummy_src_jar, "")
- return java_common.compile(
- ctx,
- output = dummy_jar,
- java_toolchain = semantics.find_java_toolchain(ctx),
- source_files = [dummy_src_jar],
- deps = all_deps,
- runtime_deps = runtime_deps_list,
- neverlink = neverlink,
- exports = [export[JavaInfo] for export in exports if JavaInfo in export], # Watchout, maybe you need to add them there manually.
- native_libraries = cc_info_list,
- add_exports = add_exports,
- add_opens = add_opens,
- )
-
-def bazel_java_import_rule(
- ctx,
- jars = [],
- srcjar = None,
- deps = [],
- runtime_deps = [],
- exports = [],
- neverlink = False,
- proguard_specs = [],
- add_exports = [],
- add_opens = []):
- """Implements java_import.
-
- This rule allows the use of precompiled .jar files as libraries in other Java rules.
-
- Args:
- ctx: (RuleContext) Used to register the actions.
- jars: (list[Artifact]) List of output jars.
- srcjar: (Artifact) The jar containing the sources.
- deps: (list[Target]) The list of dependent libraries.
- runtime_deps: (list[Target]) Runtime dependencies to attach to the rule.
- exports: (list[Target]) The list of exported libraries.
- neverlink: (bool) Whether this rule should only be used for compilation and not at runtime.
- constraints: (list[String]) Rule constraints.
- proguard_specs: (list[File]) Files to be used as Proguard specification.
- add_exports: (list[str]) Allow this library to access the given <module>/<package>.
- add_opens: (list[str]) Allow this library to reflectively access the given <module>/<package>.
-
- Returns:
- (list[provider]) A list containing DefaultInfo, JavaInfo,
- OutputGroupsInfo, ProguardSpecProvider providers.
- """
-
- _check_empty_jars_error(ctx, jars)
- _check_export_error(ctx, exports)
-
- collected_jars = _collect_jars(ctx, jars)
- all_deps = _filter_provider(JavaInfo, deps, exports)
-
- jdeps_artifact = None
- merged_java_info = java_common.merge(all_deps)
- not_in_allowlist = hasattr(ctx.attr, "_allowlist_java_import_deps_checking") and not ctx.attr._allowlist_java_import_deps_checking[PackageSpecificationInfo].contains(ctx.label)
- if len(collected_jars) > 0 and not_in_allowlist and "incomplete-deps" not in ctx.attr.tags:
- jdeps_artifact = import_deps_check(
- ctx,
- collected_jars,
- merged_java_info.compile_jars,
- merged_java_info.transitive_compile_time_jars,
- "java_import",
- )
-
- compilation_to_runtime_jar_map = _process_with_ijars_if_needed(collected_jars, ctx)
- runtime_deps_list = [runtime_dep[JavaInfo] for runtime_dep in runtime_deps if JavaInfo in runtime_dep]
- cc_info_list = [dep[CcInfo] for dep in deps if CcInfo in dep]
- java_info = None
- if len(collected_jars) > 0:
- java_infos = []
- for jar in collected_jars:
- java_infos.append(JavaInfo(
- output_jar = jar,
- compile_jar = compilation_to_runtime_jar_map[jar],
- deps = all_deps,
- runtime_deps = runtime_deps_list,
- neverlink = neverlink,
- source_jar = srcjar,
- exports = [export[JavaInfo] for export in exports if JavaInfo in export], # Watchout, maybe you need to add them there manually.
- native_libraries = cc_info_list,
- add_exports = add_exports,
- add_opens = add_opens,
- ))
- java_info = java_common.merge(java_infos)
- else:
- # TODO(kotlaja): Remove next line once all java_import targets with empty jars attribute are cleaned from depot (b/246559727).
- java_info = _create_java_info_with_dummy_output_file(ctx, srcjar, all_deps, exports, runtime_deps_list, neverlink, cc_info_list, add_exports, add_opens)
-
- target = {"JavaInfo": java_info}
-
- target["ProguardSpecProvider"] = validate_proguard_specs(
- ctx,
- proguard_specs,
- [deps, runtime_deps, exports],
- )
-
- # TODO(kotlaja): Revise if collected_runtimes can be added into construct_defaultinfo directly.
- collected_runtimes = []
- for runtime_dep in ctx.attr.runtime_deps:
- collected_runtimes.extend(runtime_dep.files.to_list())
-
- target["DefaultInfo"] = construct_defaultinfo(
- ctx,
- collected_jars,
- collected_jars + collected_runtimes,
- neverlink,
- exports,
- )
-
- output_group_src_jars = depset() if srcjar == None else depset([srcjar])
- target["OutputGroupInfo"] = OutputGroupInfo(
- **{
- "_source_jars": output_group_src_jars,
- "_direct_source_jars": output_group_src_jars,
- "_validation": depset() if jdeps_artifact == None else depset([jdeps_artifact]),
- "_hidden_top_level_INTERNAL_": target["ProguardSpecProvider"].specs,
- }
- )
- return target
-
-def _proxy(ctx):
- return bazel_java_import_rule(
- ctx,
- ctx.attr.jars,
- ctx.file.srcjar,
- ctx.attr.deps,
- ctx.attr.runtime_deps,
- ctx.attr.exports,
- ctx.attr.neverlink,
- ctx.files.proguard_specs,
- ctx.attr.add_exports,
- ctx.attr.add_opens,
- ).values()
-
-_ALLOWED_RULES_IN_DEPS_FOR_JAVA_IMPORT = [
- "java_library",
- "java_import",
- "cc_library",
- "cc_binary",
-]
-
-JAVA_IMPORT_ATTRS = {
- "data": attr.label_list(
- allow_files = True,
- flags = ["SKIP_CONSTRAINTS_OVERRIDE"],
- doc = """
-The list of files needed by this rule at runtime.
- """,
- ),
- "deps": attr.label_list(
- providers = [JavaInfo],
- allow_rules = _ALLOWED_RULES_IN_DEPS_FOR_JAVA_IMPORT,
- doc = """
-The list of other libraries to be linked in to the target.
-See <a href="${link java_library.deps}">java_library.deps</a>.
- """,
- ),
- "exports": attr.label_list(
- providers = [JavaInfo],
- allow_rules = _ALLOWED_RULES_IN_DEPS_FOR_JAVA_IMPORT,
- doc = """
-Targets to make available to users of this rule.
-See <a href="${link java_library.exports}">java_library.exports</a>.
- """,
- ),
- "runtime_deps": attr.label_list(
- allow_files = [".jar"],
- allow_rules = _ALLOWED_RULES_IN_DEPS_FOR_JAVA_IMPORT,
- providers = [[CcInfo], [JavaInfo]],
- flags = ["SKIP_ANALYSIS_TIME_FILETYPE_CHECK"],
- doc = """
-Libraries to make available to the final binary or test at runtime only.
-See <a href="${link java_library.runtime_deps}">java_library.runtime_deps</a>.
- """,
- ),
- # JavaImportBazeRule attr
- "jars": attr.label_list(
- allow_files = [".jar"],
- mandatory = True,
- doc = """
-The list of JAR files provided to Java targets that depend on this target.
- """,
- ),
- "srcjar": attr.label(
- allow_single_file = [".srcjar", ".jar"],
- flags = ["DIRECT_COMPILE_TIME_INPUT"],
- doc = """
-A JAR file that contains source code for the compiled JAR files.
- """,
- ),
- "neverlink": attr.bool(
- default = False,
- doc = """
-Only use this library for compilation and not at runtime.
-Useful if the library will be provided by the runtime environment
-during execution. Examples of libraries like this are IDE APIs
-for IDE plug-ins or <code>tools.jar</code> for anything running on
-a standard JDK.
- """,
- ),
- "constraints": attr.string_list(
- doc = """
-Extra constraints imposed on this rule as a Java library.
- """,
- ),
- # ProguardLibraryRule attr
- "proguard_specs": attr.label_list(
- allow_files = True,
- doc = """
-Files to be used as Proguard specification.
-These will describe the set of specifications to be used by Proguard. If specified,
-they will be added to any <code>android_binary</code> target depending on this library.
-
-The files included here must only have idempotent rules, namely -dontnote, -dontwarn,
-assumenosideeffects, and rules that start with -keep. Other options can only appear in
-<code>android_binary</code>'s proguard_specs, to ensure non-tautological merges.
- """,
- ),
- # Additional attrs
- "add_exports": attr.string_list(
- doc = """
-Allow this library to access the given <code>module</code> or <code>package</code>.
-<p>
-This corresponds to the javac and JVM --add-exports= flags.
- """,
- ),
- "add_opens": attr.string_list(
- doc = """
-Allow this library to reflectively access the given <code>module</code> or
-<code>package</code>.
-<p>
-This corresponds to the javac and JVM --add-opens= flags.
- """,
- ),
- "licenses": attr.license() if hasattr(attr, "license") else attr.string_list(),
- "_java_toolchain_type": attr.label(default = semantics.JAVA_TOOLCHAIN_TYPE),
-}
-
-java_import = rule(
- _proxy,
- doc = """
-<p>
- This rule allows the use of precompiled <code>.jar</code> files as
- libraries for <code><a href="${link java_library}">java_library</a></code> and
- <code>java_binary</code> rules.
-</p>
-
-<h4 id="java_import_examples">Examples</h4>
-
-<pre class="code">
-<code class="lang-starlark">
- java_import(
- name = "maven_model",
- jars = [
- "maven_model/maven-aether-provider-3.2.3.jar",
- "maven_model/maven-model-3.2.3.jar",
- "maven_model/maven-model-builder-3.2.3.jar",
- ],
- )
-</code>
-</pre>
- """,
- attrs = JAVA_IMPORT_ATTRS,
- provides = [JavaInfo],
- fragments = ["java", "cpp"],
- toolchains = [semantics.JAVA_TOOLCHAIN],
-)
diff --git a/src/main/starlark/builtins_bzl/common/java/java_library.bzl b/src/main/starlark/builtins_bzl/common/java/java_library.bzl
deleted file mode 100644
index a93cd2c..0000000
--- a/src/main/starlark/builtins_bzl/common/java/java_library.bzl
+++ /dev/null
@@ -1,387 +0,0 @@
-# Copyright 2021 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.
-
-"""
-Definition of java_library rule.
-"""
-
-load(":common/cc/cc_info.bzl", "CcInfo")
-load(":common/java/android_lint.bzl", "android_lint_subrule")
-load(":common/java/basic_java_library.bzl", "BASIC_JAVA_LIBRARY_IMPLICIT_ATTRS", "basic_java_library", "construct_defaultinfo")
-load(":common/java/boot_class_path_info.bzl", "BootClassPathInfo")
-load(":common/java/java_info.bzl", "JavaInfo", "JavaPluginInfo")
-load(":common/java/java_semantics.bzl", "semantics")
-load(":common/rule_util.bzl", "merge_attrs")
-
-def bazel_java_library_rule(
- ctx,
- srcs = [],
- deps = [],
- runtime_deps = [],
- plugins = [],
- exports = [],
- exported_plugins = [],
- resources = [],
- javacopts = [],
- neverlink = False,
- proguard_specs = [],
- add_exports = [],
- add_opens = [],
- bootclasspath = None,
- javabuilder_jvm_flags = None):
- """Implements java_library.
-
- Use this call when you need to produce a fully fledged java_library from
- another rule's implementation.
-
- Args:
- ctx: (RuleContext) Used to register the actions.
- srcs: (list[File]) The list of source files that are processed to create the target.
- deps: (list[Target]) The list of other libraries to be linked in to the target.
- runtime_deps: (list[Target]) Libraries to make available to the final binary or test at runtime only.
- plugins: (list[Target]) Java compiler plugins to run at compile-time.
- exports: (list[Target]) Exported libraries.
- exported_plugins: (list[Target]) The list of `java_plugin`s (e.g. annotation
- processors) to export to libraries that directly depend on this library.
- resources: (list[File]) A list of data files to include in a Java jar.
- javacopts: (list[str]) Extra compiler options for this library.
- neverlink: (bool) Whether this library should only be used for compilation and not at runtime.
- proguard_specs: (list[File]) Files to be used as Proguard specification.
- add_exports: (list[str]) Allow this library to access the given <module>/<package>.
- add_opens: (list[str]) Allow this library to reflectively access the given <module>/<package>.
- bootclasspath: (Target) The JDK APIs to compile this library against.
- javabuilder_jvm_flags: (list[str]) Additional JVM flags to pass to JavaBuilder.
- Returns:
- (dict[str, provider]) A list containing DefaultInfo, JavaInfo,
- InstrumentedFilesInfo, OutputGroupsInfo, ProguardSpecProvider providers.
- """
- if not srcs and deps:
- fail("deps not allowed without srcs; move to runtime_deps?")
-
- target, base_info = basic_java_library(
- ctx,
- srcs,
- deps,
- runtime_deps,
- plugins,
- exports,
- exported_plugins,
- resources,
- [], # resource_jars
- [], # class_pathresources
- javacopts,
- neverlink,
- proguard_specs = proguard_specs,
- add_exports = add_exports,
- add_opens = add_opens,
- bootclasspath = bootclasspath,
- javabuilder_jvm_flags = javabuilder_jvm_flags,
- )
-
- target["DefaultInfo"] = construct_defaultinfo(
- ctx,
- base_info.files_to_build,
- base_info.runfiles,
- neverlink,
- exports,
- runtime_deps,
- )
- target["OutputGroupInfo"] = OutputGroupInfo(**base_info.output_groups)
-
- return target
-
-def _proxy(ctx):
- return bazel_java_library_rule(
- ctx,
- ctx.files.srcs,
- ctx.attr.deps,
- ctx.attr.runtime_deps,
- ctx.attr.plugins,
- ctx.attr.exports,
- ctx.attr.exported_plugins,
- ctx.files.resources,
- ctx.attr.javacopts,
- ctx.attr.neverlink,
- ctx.files.proguard_specs,
- ctx.attr.add_exports,
- ctx.attr.add_opens,
- ctx.attr.bootclasspath,
- ctx.attr.javabuilder_jvm_flags,
- ).values()
-
-JAVA_LIBRARY_IMPLICIT_ATTRS = BASIC_JAVA_LIBRARY_IMPLICIT_ATTRS
-
-JAVA_LIBRARY_ATTRS = merge_attrs(
- JAVA_LIBRARY_IMPLICIT_ATTRS,
- {
- "srcs": attr.label_list(
- allow_files = [".java", ".srcjar", ".properties"] + semantics.EXTRA_SRCS_TYPES,
- flags = ["DIRECT_COMPILE_TIME_INPUT", "ORDER_INDEPENDENT"],
- doc = """
-The list of source files that are processed to create the target.
-This attribute is almost always required; see exceptions below.
-<p>
-Source files of type <code>.java</code> are compiled. In case of generated
-<code>.java</code> files it is generally advisable to put the generating rule's name
-here instead of the name of the file itself. This not only improves readability but
-makes the rule more resilient to future changes: if the generating rule generates
-different files in the future, you only need to fix one place: the <code>outs</code> of
-the generating rule. You should not list the generating rule in <code>deps</code>
-because it is a no-op.
-</p>
-<p>
-Source files of type <code>.srcjar</code> are unpacked and compiled. (This is useful if
-you need to generate a set of <code>.java</code> files with a genrule.)
-</p>
-<p>
-Rules: if the rule (typically <code>genrule</code> or <code>filegroup</code>) generates
-any of the files listed above, they will be used the same way as described for source
-files.
-</p>
-<p>
-Source files of type <code>.properties</code> are treated as resources.
-</p>
-
-<p>All other files are ignored, as long as there is at least one file of a
-file type described above. Otherwise an error is raised.</p>
-
-<p>
-This argument is almost always required, except if you specify the <code>runtime_deps</code> argument.
-</p>
- """,
- ),
- "data": attr.label_list(
- allow_files = True,
- flags = ["SKIP_CONSTRAINTS_OVERRIDE"],
- doc = """
-The list of files needed by this library at runtime.
-See general comments about <code>data</code> at
-<a href="${link common-definitions#typical-attributes}">Typical attributes defined by
-most build rules</a>.
-<p>
- When building a <code>java_library</code>, Bazel doesn't put these files anywhere; if the
- <code>data</code> files are generated files then Bazel generates them. When building a
- test that depends on this <code>java_library</code> Bazel copies or links the
- <code>data</code> files into the runfiles area.
-</p>
- """ + semantics.DOCS.for_attribute("data"),
- ),
- "resources": attr.label_list(
- allow_files = True,
- flags = ["SKIP_CONSTRAINTS_OVERRIDE", "ORDER_INDEPENDENT"],
- doc = """
-A list of data files to include in a Java jar.
-<p>
-Resources may be source files or generated files.
-</p>
- """ + semantics.DOCS.for_attribute("resources"),
- ),
- "plugins": attr.label_list(
- providers = [JavaPluginInfo],
- allow_files = True,
- cfg = "exec",
- doc = """
-Java compiler plugins to run at compile-time.
-Every <code>java_plugin</code> specified in this attribute will be run whenever this rule
-is built. A library may also inherit plugins from dependencies that use
-<code><a href="#java_library.exported_plugins">exported_plugins</a></code>. Resources
-generated by the plugin will be included in the resulting jar of this rule.
- """,
- ),
- "deps": attr.label_list(
- allow_files = [".jar"],
- allow_rules = semantics.ALLOWED_RULES_IN_DEPS + semantics.ALLOWED_RULES_IN_DEPS_WITH_WARNING,
- providers = [
- [CcInfo],
- [JavaInfo],
- ],
- flags = ["SKIP_ANALYSIS_TIME_FILETYPE_CHECK"],
- doc = """
-The list of libraries to link into this library.
-See general comments about <code>deps</code> at
-<a href="${link common-definitions#typical-attributes}">Typical attributes defined by
-most build rules</a>.
-<p>
- The jars built by <code>java_library</code> rules listed in <code>deps</code> will be on
- the compile-time classpath of this rule. Furthermore the transitive closure of their
- <code>deps</code>, <code>runtime_deps</code> and <code>exports</code> will be on the
- runtime classpath.
-</p>
-<p>
- By contrast, targets in the <code>data</code> attribute are included in the runfiles but
- on neither the compile-time nor runtime classpath.
-</p>
- """,
- ),
- "runtime_deps": attr.label_list(
- allow_files = [".jar"],
- allow_rules = semantics.ALLOWED_RULES_IN_DEPS,
- providers = [[CcInfo], [JavaInfo]],
- flags = ["SKIP_ANALYSIS_TIME_FILETYPE_CHECK"],
- doc = """
-Libraries to make available to the final binary or test at runtime only.
-Like ordinary <code>deps</code>, these will appear on the runtime classpath, but unlike
-them, not on the compile-time classpath. Dependencies needed only at runtime should be
-listed here. Dependency-analysis tools should ignore targets that appear in both
-<code>runtime_deps</code> and <code>deps</code>.
- """,
- ),
- "exports": attr.label_list(
- allow_rules = semantics.ALLOWED_RULES_IN_DEPS,
- providers = [[JavaInfo], [CcInfo]],
- doc = """
-Exported libraries.
-<p>
- Listing rules here will make them available to parent rules, as if the parents explicitly
- depended on these rules. This is not true for regular (non-exported) <code>deps</code>.
-</p>
-<p>
- Summary: a rule <i>X</i> can access the code in <i>Y</i> if there exists a dependency
- path between them that begins with a <code>deps</code> edge followed by zero or more
- <code>exports</code> edges. Let's see some examples to illustrate this.
-</p>
-<p>
- Assume <i>A</i> depends on <i>B</i> and <i>B</i> depends on <i>C</i>. In this case
- C is a <em>transitive</em> dependency of A, so changing C's sources and rebuilding A will
- correctly rebuild everything. However A will not be able to use classes in C. To allow
- that, either A has to declare C in its <code>deps</code>, or B can make it easier for A
- (and anything that may depend on A) by declaring C in its (B's) <code>exports</code>
- attribute.
-</p>
-<p>
- The closure of exported libraries is available to all direct parent rules. Take a slightly
- different example: A depends on B, B depends on C and D, and also exports C but not D.
- Now A has access to C but not to D. Now, if C and D exported some libraries, C' and D'
- respectively, A could only access C' but not D'.
-</p>
-<p>
- Important: an exported rule is not a regular dependency. Sticking to the previous example,
- if B exports C and wants to also use C, it has to also list it in its own
- <code>deps</code>.
-</p>
- """,
- ),
- "exported_plugins": attr.label_list(
- providers = [JavaPluginInfo],
- cfg = "exec",
- doc = """
-The list of <code><a href="#${link java_plugin}">java_plugin</a></code>s (e.g. annotation
-processors) to export to libraries that directly depend on this library.
-<p>
- The specified list of <code>java_plugin</code>s will be applied to any library which
- directly depends on this library, just as if that library had explicitly declared these
- labels in <code><a href="${link java_library.plugins}">plugins</a></code>.
-</p>
- """,
- ),
- "bootclasspath": attr.label(
- providers = [BootClassPathInfo],
- flags = ["SKIP_CONSTRAINTS_OVERRIDE"],
- doc = """Restricted API, do not use!""",
- ),
- "javabuilder_jvm_flags": attr.string_list(doc = """Restricted API, do not use!"""),
- "javacopts": attr.string_list(
- doc = """
-Extra compiler options for this library.
-Subject to <a href="make-variables.html">"Make variable"</a> substitution and
-<a href="common-definitions.html#sh-tokenization">Bourne shell tokenization</a>.
-<p>These compiler options are passed to javac after the global compiler options.</p>
- """,
- ),
- "neverlink": attr.bool(
- doc = """
-Whether this library should only be used for compilation and not at runtime.
-Useful if the library will be provided by the runtime environment during execution. Examples
-of such libraries are the IDE APIs for IDE plug-ins or <code>tools.jar</code> for anything
-running on a standard JDK.
-<p>
- Note that <code>neverlink = 1</code> does not prevent the compiler from inlining material
- from this library into compilation targets that depend on it, as permitted by the Java
- Language Specification (e.g., <code>static final</code> constants of <code>String</code>
- or of primitive types). The preferred use case is therefore when the runtime library is
- identical to the compilation library.
-</p>
-<p>
- If the runtime library differs from the compilation library then you must ensure that it
- differs only in places that the JLS forbids compilers to inline (and that must hold for
- all future versions of the JLS).
-</p>
- """,
- ),
- "resource_strip_prefix": attr.string(
- doc = """
-The path prefix to strip from Java resources.
-<p>
-If specified, this path prefix is stripped from every file in the <code>resources</code>
-attribute. It is an error for a resource file not to be under this directory. If not
-specified (the default), the path of resource file is determined according to the same
-logic as the Java package of source files. For example, a source file at
-<code>stuff/java/foo/bar/a.txt</code> will be located at <code>foo/bar/a.txt</code>.
-</p>
- """,
- ),
- "proguard_specs": attr.label_list(
- allow_files = True,
- doc = """
-Files to be used as Proguard specification.
-These will describe the set of specifications to be used by Proguard. If specified,
-they will be added to any <code>android_binary</code> target depending on this library.
-
-The files included here must only have idempotent rules, namely -dontnote, -dontwarn,
-assumenosideeffects, and rules that start with -keep. Other options can only appear in
-<code>android_binary</code>'s proguard_specs, to ensure non-tautological merges.
- """,
- ),
- "add_exports": attr.string_list(
- doc = """
-Allow this library to access the given <code>module</code> or <code>package</code>.
-<p>
-This corresponds to the javac and JVM --add-exports= flags.
- """,
- ),
- "add_opens": attr.string_list(
- doc = """
-Allow this library to reflectively access the given <code>module</code> or
-<code>package</code>.
-<p>
-This corresponds to the javac and JVM --add-opens= flags.
- """,
- ),
- "licenses": attr.license() if hasattr(attr, "license") else attr.string_list(),
- "_java_toolchain_type": attr.label(default = semantics.JAVA_TOOLCHAIN_TYPE),
- },
-)
-
-java_library = rule(
- _proxy,
- doc = """
-<p>This rule compiles and links sources into a <code>.jar</code> file.</p>
-
-<h4>Implicit outputs</h4>
-<ul>
- <li><code>lib<var>name</var>.jar</code>: A Java archive containing the class files.</li>
- <li><code>lib<var>name</var>-src.jar</code>: An archive containing the sources ("source
- jar").</li>
-</ul>
- """,
- attrs = JAVA_LIBRARY_ATTRS,
- provides = [JavaInfo],
- outputs = {
- "classjar": "lib%{name}.jar",
- "sourcejar": "lib%{name}-src.jar",
- },
- fragments = ["java", "cpp"],
- toolchains = [semantics.JAVA_TOOLCHAIN],
- subrules = [android_lint_subrule],
-)
diff --git a/src/main/starlark/builtins_bzl/common/java/java_plugin.bzl b/src/main/starlark/builtins_bzl/common/java/java_plugin.bzl
deleted file mode 100644
index 60d4f26..0000000
--- a/src/main/starlark/builtins_bzl/common/java/java_plugin.bzl
+++ /dev/null
@@ -1,182 +0,0 @@
-# Copyright 2021 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.
-
-"""
-Definition of java_plugin rule.
-"""
-
-load(":common/java/android_lint.bzl", "android_lint_subrule")
-load(":common/java/basic_java_library.bzl", "basic_java_library", "construct_defaultinfo")
-load(":common/java/java_info.bzl", "JavaPluginInfo")
-load(":common/java/java_library.bzl", "JAVA_LIBRARY_ATTRS", "JAVA_LIBRARY_IMPLICIT_ATTRS")
-load(":common/java/java_semantics.bzl", "semantics")
-load(":common/rule_util.bzl", "merge_attrs")
-
-def bazel_java_plugin_rule(
- ctx,
- srcs = [],
- data = [],
- generates_api = False,
- processor_class = "",
- deps = [],
- plugins = [],
- resources = [],
- javacopts = [],
- neverlink = False,
- proguard_specs = [],
- add_exports = [],
- add_opens = []):
- """Implements java_plugin rule.
-
- Use this call when you need to produce a fully fledged java_plugin from
- another rule's implementation.
-
- Args:
- ctx: (RuleContext) Used to register the actions.
- srcs: (list[File]) The list of source files that are processed to create the target.
- data: (list[File]) The list of files needed by this plugin at runtime.
- generates_api: (bool) This attribute marks annotation processors that generate API code.
- processor_class: (str) The processor class is the fully qualified type of
- the class that the Java compiler should use as entry point to the annotation processor.
- deps: (list[Target]) The list of other libraries to be linked in to the target.
- plugins: (list[Target]) Java compiler plugins to run at compile-time.
- resources: (list[File]) A list of data files to include in a Java jar.
- javacopts: (list[str]) Extra compiler options for this library.
- neverlink: (bool) Whether this library should only be used for compilation and not at runtime.
- proguard_specs: (list[File]) Files to be used as Proguard specification.
- add_exports: (list[str]) Allow this library to access the given <module>/<package>.
- add_opens: (list[str]) Allow this library to reflectively access the given <module>/<package>.
- Returns:
- (list[provider]) A list containing DefaultInfo, JavaInfo,
- InstrumentedFilesInfo, OutputGroupsInfo, ProguardSpecProvider providers.
- """
- target, base_info = basic_java_library(
- ctx,
- srcs,
- deps,
- [], # runtime_deps
- plugins,
- [], # exports
- [], # exported_plugins
- resources,
- [], # resource_jars
- [], # classpath_resources
- javacopts,
- neverlink,
- proguard_specs = proguard_specs,
- add_exports = add_exports,
- add_opens = add_opens,
- )
- java_info = target.pop("JavaInfo")
-
- # Replace JavaInfo with JavaPluginInfo
- target["JavaPluginInfo"] = JavaPluginInfo(
- runtime_deps = [java_info],
- processor_class = processor_class if processor_class else None, # ignore empty string (default)
- data = data,
- generates_api = generates_api,
- )
- target["DefaultInfo"] = construct_defaultinfo(
- ctx,
- base_info.files_to_build,
- base_info.runfiles,
- neverlink,
- )
- target["OutputGroupInfo"] = OutputGroupInfo(**base_info.output_groups)
-
- return target
-
-def _proxy(ctx):
- return bazel_java_plugin_rule(
- ctx,
- ctx.files.srcs,
- ctx.files.data,
- ctx.attr.generates_api,
- ctx.attr.processor_class,
- ctx.attr.deps,
- ctx.attr.plugins,
- ctx.files.resources,
- ctx.attr.javacopts,
- ctx.attr.neverlink,
- ctx.files.proguard_specs,
- ctx.attr.add_exports,
- ctx.attr.add_opens,
- ).values()
-
-JAVA_PLUGIN_ATTRS = merge_attrs(
- JAVA_LIBRARY_ATTRS,
- {
- "generates_api": attr.bool(doc = """
-This attribute marks annotation processors that generate API code.
-<p>If a rule uses an API-generating annotation processor, other rules
-depending on it can refer to the generated code only if their
-compilation actions are scheduled after the generating rule. This
-attribute instructs Bazel to introduce scheduling constraints when
---java_header_compilation is enabled.
-<p><em class="harmful">WARNING: This attribute affects build
-performance, use it only if necessary.</em></p>
- """),
- "processor_class": attr.string(doc = """
-The processor class is the fully qualified type of the class that the Java compiler should
-use as entry point to the annotation processor. If not specified, this rule will not
-contribute an annotation processor to the Java compiler's annotation processing, but its
-runtime classpath will still be included on the compiler's annotation processor path. (This
-is primarily intended for use by
-<a href="https://errorprone.info/docs/plugins">Error Prone plugins</a>, which are loaded
-from the annotation processor path using
-<a href="https://docs.oracle.com/javase/8/docs/api/java/util/ServiceLoader.html">
-java.util.ServiceLoader</a>.)
- """),
- "output_licenses": attr.license() if hasattr(attr, "license") else attr.string_list(),
- },
- remove_attrs = ["runtime_deps", "exports", "exported_plugins"],
-)
-
-JAVA_PLUGIN_IMPLICIT_ATTRS = JAVA_LIBRARY_IMPLICIT_ATTRS
-
-java_plugin = rule(
- _proxy,
- doc = """
-<p>
- <code>java_plugin</code> defines plugins for the Java compiler run by Bazel. The
- only supported kind of plugins are annotation processors. A <code>java_library</code> or
- <code>java_binary</code> rule can run plugins by depending on them via the <code>plugins</code>
- attribute. A <code>java_library</code> can also automatically export plugins to libraries that
- directly depend on it using
- <code><a href="${link java_library.exported_plugins}">exported_plugins</a></code>.
-</p>
-
-<h4 id="java_plugin_implicit_outputs">Implicit output targets</h4>
- <ul>
- <li><code><var>libname</var>.jar</code>: A Java archive.</li>
- </ul>
-
-<p>
- Arguments are identical to <a href="${link java_library}"><code>java_library</code></a>, except
- for the addition of the <code>processor_class</code> argument.
-</p>
- """,
- attrs = merge_attrs(
- JAVA_PLUGIN_ATTRS,
- JAVA_PLUGIN_IMPLICIT_ATTRS,
- ),
- provides = [JavaPluginInfo],
- outputs = {
- "classjar": "lib%{name}.jar",
- "sourcejar": "lib%{name}-src.jar",
- },
- fragments = ["java", "cpp"],
- toolchains = [semantics.JAVA_TOOLCHAIN],
- subrules = [android_lint_subrule],
-)
diff --git a/src/main/starlark/builtins_bzl/common/java/proguard_validation.bzl b/src/main/starlark/builtins_bzl/common/java/proguard_validation.bzl
deleted file mode 100644
index d054990..0000000
--- a/src/main/starlark/builtins_bzl/common/java/proguard_validation.bzl
+++ /dev/null
@@ -1,70 +0,0 @@
-# Copyright 2021 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.
-
-"""
-Proguard
-"""
-
-load(":common/java/java_semantics.bzl", "semantics")
-
-ProguardSpecProvider = _builtins.toplevel.ProguardSpecProvider
-
-def _filter_provider(provider, *attrs):
- return [dep[provider] for attr in attrs for dep in attr if provider in dep]
-
-def _validate_spec(ctx, spec_file):
- validated_proguard_spec = ctx.actions.declare_file(
- "validated_proguard/%s/%s_valid" % (ctx.label.name, spec_file.path),
- )
-
- toolchain = semantics.find_java_toolchain(ctx)
-
- args = ctx.actions.args()
- args.add("--path", spec_file)
- args.add("--output", validated_proguard_spec)
-
- ctx.actions.run(
- mnemonic = "ValidateProguard",
- progress_message = "Validating proguard configuration %{input}",
- executable = toolchain.proguard_allowlister,
- arguments = [args],
- inputs = [spec_file],
- outputs = [validated_proguard_spec],
- toolchain = Label(semantics.JAVA_TOOLCHAIN_TYPE),
- )
-
- return validated_proguard_spec
-
-def validate_proguard_specs(ctx, proguard_specs = [], transitive_attrs = []):
- """
- Creates actions that validate Proguard specification and returns ProguardSpecProvider.
-
- Use transtive_attrs parameter to collect Proguard validations from `deps`,
- `runtime_deps`, `exports`, `plugins`, and `exported_plugins` attributes.
-
- Args:
- ctx: (RuleContext) Used to register the actions.
- proguard_specs: (list[File]) List of Proguard specs files.
- transitive_attrs: (list[list[Target]]) Attributes to collect transitive
- proguard validations from.
- Returns:
- (ProguardSpecProvider) A ProguardSpecProvider.
- """
- proguard_validations = _filter_provider(ProguardSpecProvider, *transitive_attrs)
- return ProguardSpecProvider(
- depset(
- [_validate_spec(ctx, spec_file) for spec_file in proguard_specs],
- transitive = [validation.specs for validation in proguard_validations],
- ),
- )
diff --git a/src/main/starlark/builtins_bzl/common/rule_util.bzl b/src/main/starlark/builtins_bzl/common/rule_util.bzl
deleted file mode 100644
index 200bf34..0000000
--- a/src/main/starlark/builtins_bzl/common/rule_util.bzl
+++ /dev/null
@@ -1,49 +0,0 @@
-# Copyright 2021 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.
-
-"""Defines rule utilities"""
-
-def merge_attrs(*attribute_dicts, override_attrs = {}, remove_attrs = []):
- """Merges attributes together.
-
- Attributes are first merged, then overridden and removed.
-
- If there are duplicate definitions of an attribute, the last one is used.
- (Current API doesn't let us compare)
-
- Overridden and removed attributes need to be present.
-
- Args:
- *attribute_dicts: (*dict[str,Attribute]) A list of attribute dictionaries
- to merge together.
- override_attrs: (dict[str,Attribute]) A dictionary of attributes to override
- remove_attrs: (list[str]) A list of attributes to remove.
- Returns:
- (dict[str,Attribute]) The merged attributes dictionary.
- """
- all_attributes = {}
- for attribute_dict in attribute_dicts:
- for key, attr in attribute_dict.items():
- all_attributes.setdefault(key, attr)
- for key, attr in override_attrs.items():
- if all_attributes.get(key) == None:
- fail("Trying to override attribute %s where there is none." % key)
- all_attributes[key] = attr
- for key in remove_attrs:
- if key in override_attrs:
- fail("Trying to remove overridden attribute %s." % key)
- if key not in all_attributes:
- fail("Trying to remove non-existent attribute %s." % key)
- all_attributes.pop(key)
- return all_attributes
diff --git a/src/test/shell/bazel/bazel_rules_java_test.sh b/src/test/shell/bazel/bazel_rules_java_test.sh
index ecb340f..e61eb42 100755
--- a/src/test/shell/bazel/bazel_rules_java_test.sh
+++ b/src/test/shell/bazel/bazel_rules_java_test.sh
@@ -93,12 +93,17 @@
|| fail "Build failed unexpectedly"
}
+# Formerly --experimental_java_library_export
+function test_java_library_extension_support() {
+ add_rules_java "MODULE.bazel"
+ write_default_bazelrc
-function test_experimental_java_library_export_do_not_use() {
mkdir -p java
cat >java/java_library.bzl <<EOF
+load("@rules_java//java/common/rules/impl:bazel_java_library_impl.bzl", "bazel_java_library_rule")
+load("@rules_java//java/common/rules:java_library.bzl", "JAVA_LIBRARY_ATTRS")
def _impl(ctx):
- return experimental_java_library_export_do_not_use.bazel_java_library_rule(
+ return bazel_java_library_rule(
ctx,
ctx.files.srcs,
ctx.attr.deps,
@@ -116,7 +121,7 @@
java_library = rule(
implementation = _impl,
- attrs = experimental_java_library_export_do_not_use.JAVA_LIBRARY_ATTRS,
+ attrs = JAVA_LIBRARY_ATTRS,
provides = [JavaInfo],
outputs = {
"classjar": "lib%{name}.jar",
@@ -141,8 +146,7 @@
}
EOF
- bazel build //java:hello_library &> $TEST_log && fail "build succeeded"
- bazel build --experimental_java_library_export //java:hello_library &> $TEST_log || fail "build failed"
+ bazel build //java:hello_library &> $TEST_log || fail "build failed"
}
run_suite "rules_java tests"