Skip to content

Commit

Permalink
Move coverage code to coverage.bzl
Browse files Browse the repository at this point in the history
Summary:
Organize better.

#build_rule_type[go_library,go_binary,go_test]

Reviewed By: podtserkovskiy

Differential Revision: D68812433

fbshipit-source-id: 3a6f5a01a2f345a8f9c3be8ba3b85f7d362db570
  • Loading branch information
echistyakov authored and facebook-github-bot committed Jan 29, 2025
1 parent 4e684d1 commit 21b0372
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 59 deletions.
64 changes: 63 additions & 1 deletion go/coverage.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# License, Version 2.0 found in the LICENSE-APACHE file in the root directory
# of this source tree.

load(":toolchain.bzl", "GoToolchainInfo")
load(":toolchain.bzl", "GoToolchainInfo", "get_toolchain_env_vars")

GoCoverageMode = enum(
"set",
Expand Down Expand Up @@ -47,3 +47,65 @@ def cover_srcs(ctx: AnalysisContext, pkg_name: str, mode: GoCoverageMode, srcs:
srcs = cmd_args(out_srcs_argsfile, format = "@{}", hidden = [out_covered_src_dir, srcs]),
variables = cmd_args(out_coverage_vars_argsfile, format = "@{}"),
)

def cover_srcs_v2(
ctx: AnalysisContext,
pkg_name: str,
pkg_import_path: str,
go_files: list[Artifact],
coverage_mode: GoCoverageMode | None) -> (list[Artifact], str | cmd_args, Artifact | None):
if coverage_mode == None:
return go_files, "", None

if len(go_files) == 0:
return go_files, "", None

go_toolchain = ctx.attrs._go_toolchain[GoToolchainInfo]
env = get_toolchain_env_vars(go_toolchain)

cover_meta_file = ctx.actions.declare_output("cover_meta.json")
out_config_file = ctx.actions.declare_output("out_config.json")

# Based on https://pkg.go.dev/cmd/internal/cov/covcmd#CoverPkgConfig
pkgcfg = {
"EmitMetaFile": cover_meta_file,
"Granularity": "perblock",
"Local": False,
"ModulePath": "",
"OutConfig": out_config_file,
"PkgName": pkg_name,
"PkgPath": pkg_import_path,
}
pkgcfg_file = ctx.actions.write_json("pkg.cfg", pkgcfg)

var = "Var_" + sha256(pkg_import_path)
instrum_vars_file = ctx.actions.declare_output("with_instrumentation", "instrum_vars.go")
instrum_go_files = [
ctx.actions.declare_output("with_instrumentation", go_file.short_path)
for go_file in go_files
]
instrum_all_files = [instrum_vars_file] + instrum_go_files
file_to_var = {
go_file.short_path: var
for go_file in go_files
}
outfilelist = ctx.actions.write("outfilelist.txt", cmd_args(instrum_all_files, ""))

cover_cmd = [
go_toolchain.cover,
["-mode", coverage_mode.value],
["-var", var],
cmd_args(["-outfilelist", outfilelist], hidden = [f.as_output() for f in instrum_all_files]),
cmd_args(["-pkgcfg", pkgcfg_file], hidden = [cover_meta_file.as_output(), out_config_file.as_output()]),
go_files,
]

ctx.actions.run(cover_cmd, env = env, category = "go_cover", identifier = pkg_import_path)

coverage_vars_out = ""
if len(file_to_var) > 0:
# convert file_to_var to argsfile for compatibility with python implementation
cover_pkg = "{}:{}".format(pkg_import_path, ",".join(["{}={}".format(name, var) for name, var in file_to_var.items()]))
coverage_vars_out = cmd_args("--cover-pkgs", cover_pkg)

return instrum_all_files, coverage_vars_out, out_config_file
60 changes: 2 additions & 58 deletions go/package_builder.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ load(":compile.bzl", "get_inherited_compile_pkgs", "infer_package_root")
load(
":coverage.bzl",
"GoCoverageMode", # @Unused used as type
"cover_srcs_v2",
)
load(":go_list.bzl", "go_list", "parse_go_list_out")
load(":packages.bzl", "GoPackageInfo", "GoPkg", "make_importcfg", "merge_pkgs")
Expand Down Expand Up @@ -79,7 +80,7 @@ def build_package(
ctx.actions.write(outputs[test_go_files_argsfile], cmd_args((all_test_go_files if with_tests else []), ""))

go_files_to_cover = go_list.go_files + cgo_go_files + (all_test_go_files if with_tests else [])
covered_go_files, coverage_vars_out, coveragecfg = _cover(ctx, go_list_pkg_name, pkg_name, go_files_to_cover, coverage_mode)
covered_go_files, coverage_vars_out, coveragecfg = cover_srcs_v2(ctx, go_list_pkg_name, pkg_name, go_files_to_cover, coverage_mode)
ctx.actions.write(outputs[coverage_vars_argsfile], coverage_vars_out)

symabis = _symabis(ctx, pkg_name, main, go_list.s_files, go_list.h_files, assembler_flags)
Expand Down Expand Up @@ -298,60 +299,3 @@ def _asm_args(ctx: AnalysisContext, pkg_name: str, main: bool, shared: bool):
["-D", "GOARCH_" + go_toolchain.env_go_arch] if go_toolchain.env_go_arch else [],
["-shared"] if shared else [],
]

def _cover(ctx: AnalysisContext, pkg_name: str, pkg_import_path: str, go_files: list[Artifact], coverage_mode: GoCoverageMode | None) -> (list[Artifact], str | cmd_args, Artifact | None):
if coverage_mode == None:
return go_files, "", None

if len(go_files) == 0:
return go_files, "", None

go_toolchain = ctx.attrs._go_toolchain[GoToolchainInfo]
env = get_toolchain_env_vars(go_toolchain)

cover_meta_file = ctx.actions.declare_output("cover_meta.json")
out_config_file = ctx.actions.declare_output("out_config.json")

# Based on https://pkg.go.dev/cmd/internal/cov/covcmd#CoverPkgConfig
pkgcfg = {
"EmitMetaFile": cover_meta_file,
"Granularity": "perblock",
"Local": False,
"ModulePath": "",
"OutConfig": out_config_file,
"PkgName": pkg_name,
"PkgPath": pkg_import_path,
}
pkgcfg_file = ctx.actions.write_json("pkg.cfg", pkgcfg)

var = "Var_" + sha256(pkg_import_path)
instrum_vars_file = ctx.actions.declare_output("with_instrumentation", "instrum_vars.go")
instrum_go_files = [
ctx.actions.declare_output("with_instrumentation", go_file.short_path)
for go_file in go_files
]
instrum_all_files = [instrum_vars_file] + instrum_go_files
file_to_var = {
go_file.short_path: var
for go_file in go_files
}
outfilelist = ctx.actions.write("outfilelist.txt", cmd_args(instrum_all_files, ""))

cover_cmd = [
go_toolchain.cover,
["-mode", coverage_mode.value],
["-var", var],
cmd_args(["-outfilelist", outfilelist], hidden = [f.as_output() for f in instrum_all_files]),
cmd_args(["-pkgcfg", pkgcfg_file], hidden = [cover_meta_file.as_output(), out_config_file.as_output()]),
go_files,
]

ctx.actions.run(cover_cmd, env = env, category = "go_cover", identifier = pkg_import_path)

coverage_vars_out = ""
if len(file_to_var) > 0:
# convert file_to_var to argsfile for compatibility with python implementation
cover_pkg = "{}:{}".format(pkg_import_path, ",".join(["{}={}".format(name, var) for name, var in file_to_var.items()]))
coverage_vars_out = cmd_args("--cover-pkgs", cover_pkg)

return instrum_all_files, coverage_vars_out, out_config_file

0 comments on commit 21b0372

Please sign in to comment.