Skip to content

Commit

Permalink
Add coverage instrumentation flags to compile commands
Browse files Browse the repository at this point in the history
Summary:
Add coverage instrumentation flags to the compilation of C++ source files
to instrument them.

The only difference between executables and libraries is that libraries consider non-exported dependencies to determine if they need to be instrumented or not, but they only propagate the information of exported deps.

Reviewed By: Nekitosss

Differential Revision: D58779016

fbshipit-source-id: ed0e30a297dcce8518383888fa67b0c91dcabb21
  • Loading branch information
fgasperij authored and facebook-github-bot committed Jun 19, 2024
1 parent 8e4c241 commit d2ad220
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
5 changes: 4 additions & 1 deletion cxx/compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ def create_compile_cmds(
ctx: AnalysisContext,
impl_params: CxxRuleConstructorParams,
own_preprocessors: list[CPreprocessor],
inherited_preprocessor_infos: list[CPreprocessorInfo]) -> CxxCompileCommandOutput:
inherited_preprocessor_infos: list[CPreprocessorInfo],
add_coverage_instrumentation_compiler_flags: bool) -> CxxCompileCommandOutput:
"""
Forms the CxxSrcCompileCommand to use for each source file based on it's extension
and optional source file flags. Returns CxxCompileCommandOutput containing an array
Expand Down Expand Up @@ -335,6 +336,8 @@ def create_compile_cmds(
for src in srcs_with_flags:
src_args = []
src_args.extend(src.flags)
if add_coverage_instrumentation_compiler_flags:
src_args.extend(ctx.attrs.coverage_instrumentation_compiler_flags)

ext = get_source_extension(src, extension_for_plain_headers)

Expand Down
5 changes: 5 additions & 0 deletions cxx/cxx_executable.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ load(
"cxx_objects_sub_targets",
)
load(":cxx_context.bzl", "get_cxx_platform_info", "get_cxx_toolchain_info")
load(
":cxx_instrumentation.bzl",
"is_coverage_enabled_by_any_dep",
)
load(
":cxx_library_utility.bzl",
"OBJECTS_SUBTARGET",
Expand Down Expand Up @@ -216,6 +220,7 @@ def cxx_executable(ctx: AnalysisContext, impl_params: CxxRuleConstructorParams,
impl_params,
[own_preprocessor_info] + test_preprocessor_infos,
inherited_preprocessor_infos,
is_coverage_enabled_by_any_dep(ctx, preprocessor_deps),
)
cxx_outs = compile_cxx(ctx, compile_cmd_output.src_compile_cmds, pic = link_strategy != LinkStrategy("static"))

Expand Down
13 changes: 12 additions & 1 deletion cxx/cxx_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ load(
"cxx_objects_sub_targets",
)
load(":cxx_context.bzl", "get_cxx_platform_info", "get_cxx_toolchain_info")
load(
":cxx_instrumentation.bzl",
"build_exported_needs_coverage",
"needs_coverage",
)
load(
":cxx_library_utility.bzl",
"OBJECTS_SUBTARGET",
Expand Down Expand Up @@ -370,18 +375,22 @@ def cxx_library_parameterized(ctx: AnalysisContext, impl_params: CxxRuleConstruc

preferred_linkage = cxx_attr_preferred_linkage(ctx)

exported_needs_coverage = build_exported_needs_coverage(ctx, exported_deps + non_exported_deps)
compiled_srcs = cxx_compile_srcs(
ctx = ctx,
impl_params = impl_params,
own_preprocessors = own_preprocessors,
inherited_non_exported_preprocessor_infos = inherited_non_exported_preprocessor_infos,
inherited_exported_preprocessor_infos = inherited_exported_preprocessor_infos,
preferred_linkage = preferred_linkage,
add_coverage_instrumentation_compiler_flags = needs_coverage(exported_needs_coverage),
)

sub_targets = {}
providers = []

providers.append(exported_needs_coverage)

if len(ctx.attrs.tests) > 0 and impl_params.generate_providers.preprocessor_for_tests:
providers.append(
CPreprocessorForTestsInfo(
Expand Down Expand Up @@ -931,7 +940,8 @@ def cxx_compile_srcs(
own_preprocessors: list[CPreprocessor],
inherited_non_exported_preprocessor_infos: list[CPreprocessorInfo],
inherited_exported_preprocessor_infos: list[CPreprocessorInfo],
preferred_linkage: Linkage) -> _CxxCompiledSourcesOutput:
preferred_linkage: Linkage,
add_coverage_instrumentation_compiler_flags: bool) -> _CxxCompiledSourcesOutput:
"""
Compile objects we'll need for archives and shared libraries.
"""
Expand All @@ -942,6 +952,7 @@ def cxx_compile_srcs(
impl_params = impl_params,
own_preprocessors = own_preprocessors,
inherited_preprocessor_infos = inherited_non_exported_preprocessor_infos + inherited_exported_preprocessor_infos,
add_coverage_instrumentation_compiler_flags = add_coverage_instrumentation_compiler_flags,
)

# Define object files.
Expand Down
1 change: 1 addition & 0 deletions go/cgo_builder.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ def build_cgo(ctx: AnalysisContext, cgo_files: list[Artifact], h_files: list[Art
inherited_pre,
[],
linkage,
False, # add_coverage_instrumentation_compiler_flags
)

compiled_objects = c_compile_cmds.pic.objects
Expand Down

0 comments on commit d2ad220

Please sign in to comment.