Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 18 additions & 136 deletions java/bazel/rules/bazel_java_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,27 @@ load(
"//java/common/rules:android_lint.bzl",
"android_lint_subrule",
)
load("//java/common/rules:java_binary.bzl", "BASE_TEST_ATTRIBUTES", "BASIC_JAVA_BINARY_ATTRIBUTES", "basic_java_binary")
load("//java/common/rules:java_binary_deploy_jar.bzl", "create_deploy_archives")
load("//java/common/rules:java_helper.bzl", "helper")
load("//java/common/rules:java_binary.bzl", "BASIC_JAVA_BINARY_ATTRIBUTES")
load("//java/common/rules:rule_util.bzl", "merge_attrs")
load("//java/common/rules/impl:java_binary_deploy_jar.bzl", "create_deploy_archives")
load("//java/common/rules/impl:java_binary_impl.bzl", "basic_java_binary")
load("//java/common/rules/impl:java_helper.bzl", "helper")

visibility("private")

def _bazel_java_binary_impl(ctx):
return _bazel_base_binary_impl(ctx, is_test_rule_class = False) + helper.executable_providers(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):
"""Common implementation for binaries and tests

def _bazel_base_binary_impl(ctx, is_test_rule_class):
Args:
ctx: (RuleContext)
is_test_rule_class: (bool)

Returns:
[Provider]
"""
deps = _collect_all_targets_as_deps(ctx, classpath_type = "compile_only")
runtime_deps = _collect_all_targets_as_deps(ctx)

Expand Down Expand Up @@ -288,7 +295,7 @@ def _short_path(file):
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):
def make_binary_rule(implementation, *, doc, attrs, executable = False, test = False, initializer = None):
return rule(
implementation = implementation,
initializer = initializer,
Expand All @@ -315,7 +322,7 @@ def _make_binary_rule(implementation, *, doc, attrs, executable = False, test =
subrules = [android_lint_subrule],
)

_BASE_BINARY_ATTRS = merge_attrs(
BASE_BINARY_ATTRS = merge_attrs(
BASIC_JAVA_BINARY_ATTRIBUTES,
{
"resource_strip_prefix": attr.string(
Expand Down Expand Up @@ -345,7 +352,7 @@ logic as the Java package of source files. For example, a source file at
)

def make_java_binary(executable):
return _make_binary_rule(
return make_binary_rule(
_bazel_java_binary_impl,
doc = """
<p>
Expand Down Expand Up @@ -439,7 +446,7 @@ java_binary(
</pre>
""",
attrs = merge_attrs(
_BASE_BINARY_ATTRS,
BASE_BINARY_ATTRS,
({} if executable else {
"args": attr.string_list(),
"output_licenses": attr.string_list(),
Expand All @@ -449,128 +456,3 @@ java_binary(
)

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,
)
2 changes: 1 addition & 1 deletion java/bazel/rules/bazel_java_binary_wrapper.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ load(":bazel_java_binary_nonexec.bzl", java_bin_nonexec = "java_binary")

_java_common_internal = java_common.internal_DO_NOT_USE()

visibility("private")
visibility(["//java"])

def java_binary(**kwargs):
if _java_common_internal.incompatible_disable_non_executable_java_binary():
Expand Down
68 changes: 68 additions & 0 deletions java/bazel/rules/bazel_java_import.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# 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("//java/common:java_info.bzl", "JavaInfo")
load("//java/common:java_semantics.bzl", "semantics")
load("//java/common/rules:java_import.bzl", "JAVA_IMPORT_ATTRS")
load("//java/common/rules/impl:bazel_java_import_impl.bzl", "bazel_java_import_rule")

visibility(["//java"])

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()

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],
)
67 changes: 67 additions & 0 deletions java/bazel/rules/bazel_java_library.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# 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("//java/common:java_info.bzl", "JavaInfo")
load("//java/common:java_semantics.bzl", "semantics")
load("//java/common/rules:android_lint.bzl", "android_lint_subrule")
load("//java/common/rules:java_library.bzl", "JAVA_LIBRARY_ATTRS")
load("//java/common/rules/impl:bazel_java_library_impl.bzl", "bazel_java_library_rule")

visibility(["//java/..."])

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 = 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],
)
Loading