Skip to content

Commit

Permalink
Run cgo twice with coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Jul 27, 2024
1 parent 0faeb2f commit c089cc1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
12 changes: 6 additions & 6 deletions go/private/actions/compilepkg.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,12 @@ def emit_compilepkg(
else:
env = go.env_for_path_mapping
execution_requirements = SUPPORTS_PATH_MAPPING_REQUIREMENT
cgo_go_srcs_for_nogo = None
if cgo:
cgo_go_srcs = go.declare_directory(go, path = out_lib.basename + ".cgo")
outputs.append(cgo_go_srcs)
args.add("-cgo_go_srcs", cgo_go_srcs.path)
if nogo:
cgo_go_srcs_for_nogo = go.declare_directory(go, path = out_lib.basename + ".cgo")
outputs.append(cgo_go_srcs_for_nogo)
args.add("-cgo_go_srcs", cgo_go_srcs_for_nogo.path)
inputs.extend(cgo_inputs.to_list()) # OPT: don't expand depset
inputs.extend(go.crosstool)
env["CC"] = go.cgo_tools.c_compiler_path
Expand All @@ -185,8 +187,6 @@ def emit_compilepkg(
args.add("-objcxxflags", quote_opts(objcxxopts))
if clinkopts:
args.add("-ldflags", quote_opts(clinkopts))
else:
cgo_go_srcs = None

if go.mode.pgoprofile:
args.add("-pgoprofile", go.mode.pgoprofile)
Expand All @@ -212,7 +212,7 @@ def emit_compilepkg(
archives = archives + ([cover_archive] if cover_archive else []),
recompile_internal_deps = recompile_internal_deps,
cover_mode = cover_mode,
cgo_go_srcs = cgo_go_srcs,
cgo_go_srcs = cgo_go_srcs_for_nogo,
out_facts = out_facts,
out_log = out_nogo_log,
out_validation = out_nogo_validation,
Expand Down
8 changes: 5 additions & 3 deletions go/tools/builders/cgo2.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,11 @@ func cgo2(goenv *env, goSrcs, cgoSrcs, cSrcs, cxxSrcs, objcSrcs, objcxxSrcs, sSr
return "", nil, nil, err
}
genGoSrcs = append(genGoSrcs, cgoImportsGo)
for _, src := range genGoSrcs {
if err := copyFile(src, filepath.Join(cgoGoSrcsPath, filepath.Base(src))); err != nil {
return "", nil, nil, err
if cgoGoSrcsPath != "" {
for _, src := range genGoSrcs {
if err := copyFile(src, filepath.Join(cgoGoSrcsPath, filepath.Base(src))); err != nil {
return "", nil, nil, err
}
}
}

Expand Down
33 changes: 26 additions & 7 deletions go/tools/builders/compilepkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func compilePkg(args []string) error {
fs.StringVar(&outLinkobjPath, "lo", "", "The full output archive file required by the linker")
fs.StringVar(&outInterfacePath, "o", "", "The export-only output archive required to compile dependent packages")
fs.StringVar(&cgoExportHPath, "cgoexport", "", "The _cgo_exports.h file to write")
fs.StringVar(&cgoGoSrcsPath, "cgo_go_srcs", "", "The directory to emit cgo-generated Go sources to")
fs.StringVar(&cgoGoSrcsPath, "cgo_go_srcs", "", "The directory to emit cgo-generated Go sources for nogo consumption to")
fs.StringVar(&testFilter, "testfilter", "off", "Controls test package filtering")
fs.StringVar(&coverFormat, "cover_format", "", "Emit source file paths in coverage instrumentation suitable for the specified coverage format")
fs.Var(&recompileInternalDeps, "recompile_internal_deps", "The import path of the direct dependencies that needs to be recompiled.")
Expand Down Expand Up @@ -180,7 +180,7 @@ func compileArchive(
outLinkObj string,
outInterfacePath string,
cgoExportHPath string,
cgoGoSrcsPath string,
cgoGoSrcsForNogoPath string,
coverFormat string,
recompileInternalDeps []string,
pgoprofile string,
Expand Down Expand Up @@ -257,6 +257,12 @@ func compileArchive(
// constraints.
compilingWithCgo := haveCgo && cgoEnabled

// When coverage is set, source files will be modified during instrumentation. We should only run static analysis
// over original source files and not the modified ones.
// goSrcsNogo and cgoSrcsNogo are copies of the original source files for nogo to run static analysis.
goSrcsNogo := append([]string{}, goSrcs...)
cgoSrcsNogo := append([]string{}, cgoSrcs...)

// Instrument source files for coverage.
if coverMode != "" {
relCoverPath := make(map[string]string)
Expand Down Expand Up @@ -312,12 +318,25 @@ func compileArchive(
// C files.
var objFiles []string
if compilingWithCgo {
// If the package uses Cgo, compile .s and .S files with cgo2, not the Go assembler.
// Otherwise: the .s/.S files will be compiled with the Go assembler later
var srcDir string
srcDir, goSrcs, objFiles, err = cgo2(goenv, goSrcs, cgoSrcs, cSrcs, cxxSrcs, objcSrcs, objcxxSrcs, sSrcs, hSrcs, packagePath, packageName, cc, cppFlags, cFlags, cxxFlags, objcFlags, objcxxFlags, ldFlags, cgoExportHPath, cgoGoSrcsPath)
if err != nil {
return err
// Also run cgo on original source files, not coverage instrumented, if
// using nogo.
if coverMode != "" && cgoGoSrcsForNogoPath != "" {
srcDir, goSrcs, objFiles, err = cgo2(goenv, goSrcs, cgoSrcs, cSrcs, cxxSrcs, objcSrcs, objcxxSrcs, sSrcs, hSrcs, packagePath, packageName, cc, cppFlags, cFlags, cxxFlags, objcFlags, objcxxFlags, ldFlags, cgoExportHPath, "")
if err != nil {
return err
}
_, _, _, err = cgo2(goenv, goSrcsNogo, cgoSrcsNogo, cSrcs, cxxSrcs, objcSrcs, objcxxSrcs, sSrcs, hSrcs, packagePath, packageName, cc, cppFlags, cFlags, cxxFlags, objcFlags, objcxxFlags, ldFlags, cgoExportHPath, cgoGoSrcsForNogoPath)
if err != nil {
return err
}
} else {
// If the package uses Cgo, compile .s and .S files with cgo2, not the Go assembler.
// Otherwise: the .s/.S files will be compiled with the Go assembler later
srcDir, goSrcs, objFiles, err = cgo2(goenv, goSrcs, cgoSrcs, cSrcs, cxxSrcs, objcSrcs, objcxxSrcs, sSrcs, hSrcs, packagePath, packageName, cc, cppFlags, cFlags, cxxFlags, objcFlags, objcxxFlags, ldFlags, cgoExportHPath, cgoGoSrcsForNogoPath)
if err != nil {
return err
}
}
gcFlags = append(gcFlags, createTrimPath(gcFlags, srcDir))
} else {
Expand Down

0 comments on commit c089cc1

Please sign in to comment.