Skip to content

Create baseline coverage report if supported #304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
14 changes: 14 additions & 0 deletions java/common/rules/impl/basic_java_library_impl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
Common code for reuse across java_* rules
"""

load("@bazel_features//:features.bzl", "bazel_features")
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
load("//java/common/rules:android_lint.bzl", "android_lint_subrule")
load("//java/private:boot_class_path_info.bzl", "BootClassPathInfo")
Expand Down Expand Up @@ -124,6 +125,14 @@ def basic_java_library(
resources = list(resources)
resources.extend(properties)

baseline_coverage_file = None
if (
bazel_features.rules.instrumented_files_info_has_baseline_coverage_files and
ctx.coverage_instrumented() and
source_files
):
baseline_coverage_file = ctx.actions.declare_file(ctx.label.name + "_baseline_coverage.dat")

java_info, compilation_info = compile_action(
ctx,
output_class_jar = ctx.outputs.classjar,
Expand All @@ -147,6 +156,7 @@ def basic_java_library(
add_opens = add_opens,
bootclasspath = bootclasspath[BootClassPathInfo] if bootclasspath else None,
javabuilder_jvm_flags = javabuilder_jvm_flags,
baseline_coverage_file = baseline_coverage_file,
)
target = {"JavaInfo": java_info}

Expand Down Expand Up @@ -178,12 +188,16 @@ def basic_java_library(
if validation_outputs:
output_groups["_validation"] = depset(transitive = validation_outputs)

instrumented_files_info_kwargs = {}
if baseline_coverage_file:
instrumented_files_info_kwargs["baseline_coverage_files"] = [baseline_coverage_file]
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 {},
**instrumented_files_info_kwargs
)

if proguard_specs != None:
Expand Down
5 changes: 4 additions & 1 deletion java/common/rules/impl/compile_action.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ def compile_action(
add_exports = [],
add_opens = [],
bootclasspath = None,
javabuilder_jvm_flags = None):
javabuilder_jvm_flags = None,
baseline_coverage_file = None):
"""
Creates actions that compile Java sources, produce source jar, and produce header jar and returns JavaInfo.

Expand Down Expand Up @@ -123,6 +124,7 @@ def compile_action(
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.
baseline_coverage_file: (File) Baseline coverage report to generate during compilation.

Returns:
((JavaInfo, {files_to_build: list[File],
Expand Down Expand Up @@ -160,6 +162,7 @@ def compile_action(
add_opens = add_opens,
bootclasspath = bootclasspath,
javabuilder_jvm_flags = javabuilder_jvm_flags,
baseline_coverage_file = baseline_coverage_file,
)

compilation_info = struct(
Expand Down
5 changes: 4 additions & 1 deletion java/private/java_common_internal.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ def compile(
include_compilation_info = True,
classpath_resources = [],
resource_jars = [],
injecting_rule_kind = None):
injecting_rule_kind = None,
baseline_coverage_file = None):
"""Compiles Java source files/jars from the implementation of a Starlark rule

The result is a provider that represents the results of the compilation and can be added to the
Expand Down Expand Up @@ -115,6 +116,7 @@ def compile(
enable_jspecify: (bool)
include_compilation_info: (bool)
injecting_rule_kind: (str|None)
baseline_coverage_file: (File)
add_exports: ([str]) Allow this library to access the given <module>/<package>. Optional.
add_opens: ([str]) Allow this library to reflectively access the given <module>/<package>.
Optional.
Expand Down Expand Up @@ -309,6 +311,7 @@ def compile(
enable_direct_classpath,
annotation_processor_additional_inputs,
annotation_processor_additional_outputs,
baseline_coverage_file,
)

create_output_source_jar = len(source_files) > 0 or source_jars != [output_source_jar]
Expand Down