From 2ae5358750c6de630580230b3c091c8bca1567aa Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Thu, 19 Jun 2025 13:02:11 +0200 Subject: [PATCH 1/2] Create baseline coverage report if supported --- java/common/rules/impl/basic_java_library_impl.bzl | 14 ++++++++++++++ java/common/rules/impl/compile_action.bzl | 5 ++++- java/private/java_common_internal.bzl | 5 ++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/java/common/rules/impl/basic_java_library_impl.bzl b/java/common/rules/impl/basic_java_library_impl.bzl index cf7c5c19..cd847593 100644 --- a/java/common/rules/impl/basic_java_library_impl.bzl +++ b/java/common/rules/impl/basic_java_library_impl.bzl @@ -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") @@ -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.lcov") + java_info, compilation_info = compile_action( ctx, output_class_jar = ctx.outputs.classjar, @@ -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} @@ -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: diff --git a/java/common/rules/impl/compile_action.bzl b/java/common/rules/impl/compile_action.bzl index bf5b9359..930a7705 100644 --- a/java/common/rules/impl/compile_action.bzl +++ b/java/common/rules/impl/compile_action.bzl @@ -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. @@ -123,6 +124,7 @@ def compile_action( add_opens: (list[str]) Allow this library to reflectively access the given /. 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], @@ -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( diff --git a/java/private/java_common_internal.bzl b/java/private/java_common_internal.bzl index 54f0c7bd..eb3ee8ed 100644 --- a/java/private/java_common_internal.bzl +++ b/java/private/java_common_internal.bzl @@ -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 @@ -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 /. Optional. add_opens: ([str]) Allow this library to reflectively access the given /. Optional. @@ -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] From 552d0e39f9eb7ba47b7ea2c7c60703ca3fbe86ae Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Thu, 19 Jun 2025 16:26:41 +0200 Subject: [PATCH 2/2] Update basic_java_library_impl.bzl --- java/common/rules/impl/basic_java_library_impl.bzl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/common/rules/impl/basic_java_library_impl.bzl b/java/common/rules/impl/basic_java_library_impl.bzl index cd847593..b6e95e05 100644 --- a/java/common/rules/impl/basic_java_library_impl.bzl +++ b/java/common/rules/impl/basic_java_library_impl.bzl @@ -131,7 +131,7 @@ def basic_java_library( ctx.coverage_instrumented() and source_files ): - baseline_coverage_file = ctx.actions.declare_file(ctx.label.name + "_baseline_coverage.lcov") + baseline_coverage_file = ctx.actions.declare_file(ctx.label.name + "_baseline_coverage.dat") java_info, compilation_info = compile_action( ctx,