diff --git a/go/private/actions/compilepkg.bzl b/go/private/actions/compilepkg.bzl index 3310976e1..5bca2464d 100644 --- a/go/private/actions/compilepkg.bzl +++ b/go/private/actions/compilepkg.bzl @@ -110,13 +110,15 @@ def emit_compilepkg( uniquify = True, expand_directories = False, ) + cover_mode = None if cover and go.coverdata: inputs.append(go.coverdata.data.export_file) args.add("-arc", _archive(go.coverdata)) if go.mode.race: - args.add("-cover_mode", "atomic") + cover_mode = "atomic" else: - args.add("-cover_mode", "set") + cover_mode = "set" + args.add("-cover_mode", cover_mode) args.add("-cover_format", go.cover_format) args.add_all(cover, before_each = "-cover") args.add_all(archives, before_each = "-arc", map_each = _archive) @@ -207,6 +209,7 @@ def emit_compilepkg( importmap = importmap, archives = archives, recompile_internal_deps = recompile_internal_deps, + cover_mode = cover_mode, cgo_go_srcs = cgo_go_srcs, out_facts = out_facts, out_log = out_nogo_log, @@ -222,6 +225,7 @@ def _run_nogo( importmap, archives, recompile_internal_deps, + cover_mode, cgo_go_srcs, out_facts, out_log, @@ -237,7 +241,10 @@ def _run_nogo( args = go.builder_args(go, "nogo", use_path_mapping = True) args.add_all(sources, before_each = "-src") if cgo_go_srcs: + inputs.append(cgo_go_srcs) args.add_all([cgo_go_srcs], before_each = "-src") + if cover_mode: + args.add("-cover_mode", cover_mode) if recompile_internal_deps: args.add_all(recompile_internal_deps, before_each = "-recompile_internal_deps") args.add_all(archives, before_each = "-arc", map_each = _archive) diff --git a/go/tools/builders/compilepkg.go b/go/tools/builders/compilepkg.go index d968efa52..02fe6c59c 100644 --- a/go/tools/builders/compilepkg.go +++ b/go/tools/builders/compilepkg.go @@ -38,7 +38,7 @@ func compilePkg(args []string) error { fs := flag.NewFlagSet("GoCompilePkg", flag.ExitOnError) goenv := envFlags(fs) var unfilteredSrcs, coverSrcs, embedSrcs, embedLookupDirs, embedRoots, recompileInternalDeps multiFlag - var deps, facts archiveMultiFlag + var deps archiveMultiFlag var importPath, packagePath, packageListPath, coverMode string var outLinkobjPath, outInterfacePath, cgoExportHPath, cgoGoSrcsPath string var testFilter string @@ -51,7 +51,6 @@ func compilePkg(args []string) error { fs.Var(&embedLookupDirs, "embedlookupdir", "Root-relative paths to directories relative to which //go:embed directives are resolved") fs.Var(&embedRoots, "embedroot", "Bazel output root under which a file passed via -embedsrc resides") fs.Var(&deps, "arc", "Import path, package path, and file name of a direct dependency, separated by '='") - fs.Var(&facts, "facts", "Import path, package path, and file name of a direct dependency's nogo facts file, separated by '='") fs.StringVar(&importPath, "importpath", "", "The import path of the package being compiled. Not passed to the compiler, but may be displayed in debug data.") fs.StringVar(&packagePath, "p", "", "The package path (importmap) of the package being compiled") fs.Var(&gcFlags, "gcflags", "Go compiler flags") @@ -131,7 +130,6 @@ func compilePkg(args []string) error { packagePath, srcs, deps, - facts, coverMode, coverSrcs, embedSrcs, @@ -163,7 +161,6 @@ func compileArchive( packagePath string, srcs archiveSrcs, deps []archive, - facts []archive, coverMode string, coverSrcs []string, embedSrcs []string, diff --git a/go/tools/builders/nogo.go b/go/tools/builders/nogo.go index d4d127ee2..711a5b07b 100644 --- a/go/tools/builders/nogo.go +++ b/go/tools/builders/nogo.go @@ -2,6 +2,7 @@ package main import ( "bytes" + "errors" "flag" "fmt" "os" @@ -23,6 +24,7 @@ func nogo(args []string) error { var deps, facts archiveMultiFlag var importPath, packagePath, nogoPath, packageListPath string var outFactsPath, outLogPath string + var coverMode string fs.Var(&unfilteredSrcs, "src", ".go, .c, .cc, .m, .mm, .s, or .S file to be filtered and compiled") fs.Var(&deps, "arc", "Import path, package path, and file name of a direct dependency, separated by '='") fs.Var(&facts, "facts", "Import path, package path, and file name of a direct dependency's nogo facts file, separated by '='") @@ -30,6 +32,7 @@ func nogo(args []string) error { fs.StringVar(&packagePath, "p", "", "The package path (importmap) of the package being compiled") fs.StringVar(&packageListPath, "package_list", "", "The file containing the list of standard library packages") fs.Var(&recompileInternalDeps, "recompile_internal_deps", "The import path of the direct dependencies that needs to be recompiled.") + fs.StringVar(&coverMode, "cover_mode", "", "The coverage mode to use. Empty if coverage instrumentation should not be added.") fs.StringVar(&nogoPath, "nogo", "", "The nogo binary") fs.StringVar(&outFactsPath, "out_facts", "", "The file to emit serialized nogo facts to") fs.StringVar(&outLogPath, "out_log", "", "The file to emit nogo logs into") @@ -76,6 +79,23 @@ func nogo(args []string) error { imports["syscall"] = nil imports["unsafe"] = nil } + if coverMode != "" { + if coverMode == "atomic" { + imports["sync/atomic"] = nil + } + const coverdataPath = "github.com/bazelbuild/rules_go/go/tools/coverdata" + var coverdata *archive + for i := range deps { + if deps[i].importPath == coverdataPath { + coverdata = &deps[i] + break + } + } + if coverdata == nil { + return errors.New("coverage requested but coverdata dependency not provided") + } + imports[coverdataPath] = coverdata + } importcfgPath, err := buildImportcfgFileForCompile(imports, goenv.installSuffix, filepath.Dir(outFactsPath)) if err != nil {