Skip to content

Commit

Permalink
Minor fixes and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
muratekici committed Sep 14, 2020
1 parent 7fb7cad commit b33f1c5
Show file tree
Hide file tree
Showing 21 changed files with 83 additions and 63 deletions.
4 changes: 2 additions & 2 deletions go/private/actions/compilepkg.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ def emit_compilepkg(
if cover and go.coverdata:
inputs.append(go.coverdata.data.export_file)
args.add("-arc", _archive(go.coverdata))
if go.mode.covermode == "func":
args.add("-cover_mode", "func")
if go.coverage_mode != "":
args.add("-cover_mode", "go.coverage_mode")
elif go.mode.race:
args.add("-cover_mode", "atomic")
else:
Expand Down
7 changes: 3 additions & 4 deletions go/private/context.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ def _library_to_source(go, attr, library, coverage_instrumented):
attr_srcs = [f for t in getattr(attr, "srcs", []) for f in as_iterable(t.files)]
generated_srcs = getattr(library, "srcs", [])
srcs = attr_srcs + generated_srcs
xx = (getattr(attr, "deps", []))
source = {
"library": library,
"mode": go.mode,
Expand All @@ -230,7 +229,7 @@ def _library_to_source(go, attr, library, coverage_instrumented):
"cppopts": getattr(attr, "cppopts", []),
"copts": getattr(attr, "copts", []),
"cxxopts": getattr(attr, "cxxopts", []),
"clinkopts": getattr(attr, "cliginkopts", []),
"clinkopts": getattr(attr, "clinkopts", []),
"cgo_deps": [],
"cgo_exports": [],
}
Expand Down Expand Up @@ -348,7 +347,6 @@ def go_context(ctx, attr = None):
"""Returns an API used to build Go code.
See /go/toolchains.rst#go-context"""

if not attr:
attr = ctx.attr
toolchain = ctx.toolchains["@io_bazel_rules_go//go:toolchain"]
Expand All @@ -357,7 +355,6 @@ def go_context(ctx, attr = None):
stdlib = None
coverdata = None
nogo = None

if hasattr(attr, "_go_context_data"):
if CgoContextInfo in attr._go_context_data:
cgo_context_info = attr._go_context_data[CgoContextInfo]
Expand Down Expand Up @@ -389,6 +386,7 @@ def go_context(ctx, attr = None):
"GOROOT": goroot,
"GOROOT_FINAL": "GOROOT",
"CGO_ENABLED": "0" if mode.pure else "1",

# If we use --action_env=GOPATH, or in other cases where environment
# variables are passed through to this builder, the SDK build will try
# to write to that GOPATH (e.g. for x/net/nettest). This will fail if
Expand Down Expand Up @@ -469,6 +467,7 @@ def go_context(ctx, attr = None):
nogo = nogo,
coverdata = coverdata,
coverage_enabled = ctx.configuration.coverage_enabled,
coverage_mode = ctx.configuration.coverage_mode,
coverage_instrumented = ctx.coverage_instrumented(),
env = env,
tags = tags,
Expand Down
2 changes: 0 additions & 2 deletions go/private/mode.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ def get_mode(ctx, go_toolchain, cgo_context_info, go_config_info):
"on" if ("msan" in ctx.features and not pure) else "auto",
go_config_info.msan if go_config_info else "off",
)
covermode = ctx.configuration.coverage_mode
strip = go_config_info.strip if go_config_info else False
stamp = go_config_info.stamp if go_config_info else False
debug = go_config_info.debug if go_config_info else False
Expand Down Expand Up @@ -117,7 +116,6 @@ def get_mode(ctx, go_toolchain, cgo_context_info, go_config_info):
goos = goos,
goarch = goarch,
tags = tags,
covermode = covermode,
)

def installsuffix(mode):
Expand Down
28 changes: 27 additions & 1 deletion go/private/sdk_list.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,24 @@
# See the License for the specific language governing permissions and
# limitations under the License.

DEFAULT_VERSION = "1.15.1"
DEFAULT_VERSION = "1.15.2"

MIN_SUPPORTED_VERSION = "1.13"

SDK_REPOSITORIES = {
"1.15.2": {
"darwin_amd64": ("go1.15.2.darwin-amd64.tar.gz", "9bd39600d9fa1fa4a5ccce8761d249f7421cffe671376f791293c4138f3d7c62"),
"freebsd_386": ("go1.15.2.freebsd-386.tar.gz", "286beeb2d2c57707d9b3f3dcace0ff0f5b804dda8916daa4e712b9174fd270e4"),
"freebsd_amd64": ("go1.15.2.freebsd-amd64.tar.gz", "53a03ac989666be77e26d2720721021a3f29a22d93a1db86a4240369799043e0"),
"linux_386": ("go1.15.2.linux-386.tar.gz", "5a91080469df6b91f1022bdfb0ca75e01ca50387950b13518def3d0a7f6af9f1"),
"linux_amd64": ("go1.15.2.linux-amd64.tar.gz", "b49fda1ca29a1946d6bb2a5a6982cf07ccd2aba849289508ee0f9918f6bb4552"),
"linux_arm64": ("go1.15.2.linux-arm64.tar.gz", "c8ec460cc82d61604b048f9439c06bd591722efce5cd48f49e19b5f6226bd36d"),
"linux_arm": ("go1.15.2.linux-armv6l.tar.gz", "c12e2afdcb21e530d332d4994919f856dd2a676e9d67034c7d6fefcb241412d9"),
"linux_ppc64le": ("go1.15.2.linux-ppc64le.tar.gz", "85fc130046f41105b50a0647d1da395d615eac0aefca8776be148800c872c8db"),
"linux_s390x": ("go1.15.2.linux-s390x.tar.gz", "30f7856323965c6bf8886f8e5ae19d250cc73a10d382948fa78ecb3ace3980b9"),
"windows_386": ("go1.15.2.windows-386.zip", "f61e54e558e23bf2626a34e3ae2f8b73c2411a9ee29ceeb39ffb712695098138"),
"windows_amd64": ("go1.15.2.windows-amd64.zip", "e72782cc6de233188c75b06849368826eaa1b8bd9e1cd766db9466a12b7138ca"),
},
"1.15.1": {
"darwin_amd64": ("go1.15.1.darwin-amd64.tar.gz", "b33341df847b4a48da40d957437c87642d221dde28c6f810b1ce26b74be2f661"),
"freebsd_386": ("go1.15.1.freebsd-386.tar.gz", "042b5f2a9eabac75f9dbd0f385e11b418fdec5fbd15b920cba5e7055d196b32b"),
Expand All @@ -43,6 +56,19 @@ SDK_REPOSITORIES = {
"windows_386": ("go1.15.windows-386.zip", "e0c747610a73030cadba6f6ac319bca33cfff19b1f58c010727ea55fb2b0cab1"),
"windows_amd64": ("go1.15.windows-amd64.zip", "dc491314dff5b87ad50bf1cf56715de8f8c54489be30f3e19239bc2ad1af25e3"),
},
"1.14.9": {
"darwin_amd64": ("go1.14.9.darwin-amd64.tar.gz", "957926fd883998f3e212ccd422d4282be957204f89eefcf13ee2fdb730e1bab7"),
"freebsd_386": ("go1.14.9.freebsd-386.tar.gz", "11b00fecbfaf34074194b095995c71a24205bbc1e8e23fd8c624bb34e55b655c"),
"freebsd_amd64": ("go1.14.9.freebsd-amd64.tar.gz", "4dae9ca431d3c9da9834c59041a1e63eec8ad9c7f75d50e6636f92cb064a3784"),
"linux_386": ("go1.14.9.linux-386.tar.gz", "14982ef997ec323023a11cffe1a4afc3aacd1b5edebf70a00e17b67f888d8cdb"),
"linux_amd64": ("go1.14.9.linux-amd64.tar.gz", "f0d26ff572c72c9823ae752d3c81819a81a60c753201f51f89637482531c110a"),
"linux_arm64": ("go1.14.9.linux-arm64.tar.gz", "65e6cef5c474a3514e754f6a7987c49388bb85a7b370370c1318087ac35427fa"),
"linux_arm": ("go1.14.9.linux-armv6l.tar.gz", "e85dc09608dc9fc245ebc5daea0826898ac0eb0d48ed24e2300427850876c442"),
"linux_ppc64le": ("go1.14.9.linux-ppc64le.tar.gz", "5880a37faf93b2396edc3ff231e0f8df14d0520505cc13d01116e24d7d1d0147"),
"linux_s390x": ("go1.14.9.linux-s390x.tar.gz", "381fc24aff153c4affcb00f4547683212157af29b8f9e3de5952d78ac35f5a0f"),
"windows_386": ("go1.14.9.windows-386.zip", "2d2abfe9b00f490678542691b18b5666d87289888df4f1b712ffd47a2f8e69ca"),
"windows_amd64": ("go1.14.9.windows-amd64.zip", "16bb44448c4423740c5fd751bad28061d5ec44cd08272d4e1efdd16cdf8221e9"),
},
"1.14.8": {
"darwin_amd64": ("go1.14.8.darwin-amd64.tar.gz", "6fbfdca4e876477f1cb54df09b310e95f335e1d49556fe5c75bca02159b94c63"),
"freebsd_386": ("go1.14.8.freebsd-386.tar.gz", "e610d966f639344e4b7415ea1a38aa088ae7cf9532d16ed2e8f1e887f4b28b98"),
Expand Down
4 changes: 3 additions & 1 deletion go/toolchains.rst
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,9 @@ dependencies.
implementation = _my_rule_impl,
attrs = {
...
"_go_context_data": attr.label("@io_bazel_rules_go//:go_context_data"),
"_go_context_data": attr.label(
default = "@io_bazel_rules_go//:go_context_data",
),
},
toolchains = ["@io_bazel_rules_go//go:toolchain"],
)
Expand Down
10 changes: 7 additions & 3 deletions go/tools/builders/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ filegroup(
"compile.go",
"compilepkg.go",
"cover.go",
"funccover_helper.go",
"funccover_instrumenter.go",
"env.go",
"filter.go",
"filter_buildid.go",
Expand All @@ -32,8 +34,6 @@ filegroup(
"pack.go",
"replicate.go",
"stdlib.go",
"funccover_helper.go",
"funccover_instrumenter.go"
] + select({
"@bazel_tools//src/conditions:windows": ["path_windows.go"],
"//conditions:default": ["path.go"],
Expand Down Expand Up @@ -71,7 +71,11 @@ go_source(

go_binary(
name = "go_path",
srcs = ["go_path.go"],
srcs = [
"env.go",
"flags.go",
"go_path.go",
],
visibility = ["//visibility:public"],
)

Expand Down
1 change: 0 additions & 1 deletion go/tools/builders/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func main() {
log.SetPrefix("builder: ")

args, err := readParamsFiles(os.Args[1:])

if err != nil {
log.Fatal(err)
}
Expand Down
25 changes: 3 additions & 22 deletions go/tools/builders/compilepkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,19 +309,6 @@ func compileArchive(
return errors.New("coverage requested but coverdata dependency not provided")
}
imports[coverdataPath] = coverdata

//////////// salak sey
const dp = "github.com/hardyantz/go-hello-world/ext2"
var cd *archive
for i := range deps {
if deps[i].importPath == dp {
cd = &deps[i]
break
}
}
if cd != nil {
imports[dp] = cd
}
}

// Build an importcfg file for the compiler.
Expand All @@ -331,15 +318,9 @@ func compileArchive(
}
defer os.Remove(importcfgPath)

// tempdir to store nogo facts and pkgdef for packaging later
xTempDir, err := ioutil.TempDir(filepath.Dir(outXPath), "x_files")
if err != nil {
return err
}
defer os.RemoveAll(xTempDir)
// Run nogo concurrently.
var nogoChan chan error
outFactsPath := filepath.Join(xTempDir, nogoFact)
outFactsPath := filepath.Join(workDir, nogoFact)
if nogoPath != "" {
ctx, cancel := context.WithCancel(context.Background())
nogoChan = make(chan error)
Expand Down Expand Up @@ -428,10 +409,10 @@ func compileArchive(
// copy the nogo facts into the .x file. Unfortunately, when building a plugin,
// the linker needs export data in the .a file. To work around this, we copy
// the export data into the .x file ourselves.
if err = extractFileFromArchive(outPath, xTempDir, pkgDef); err != nil {
if err = extractFileFromArchive(outPath, workDir, pkgDef); err != nil {
return err
}
pkgDefPath := filepath.Join(xTempDir, pkgDef)
pkgDefPath := filepath.Join(workDir, pkgDef)
if nogoStatus == nogoSucceeded {
return appendFiles(goenv, outXPath, []string{pkgDefPath, outFactsPath})
}
Expand Down
10 changes: 5 additions & 5 deletions go/tools/builders/cover.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ func cover(args []string) error {
// with the coverdata package.
func instrumentForCoverage(goenv *env, srcPath, srcName, coverVar, mode, outPath string) error {

//////////////////////////////////////////////////////////////////////////////////////////////
// Implementing a different mode (lets say mode=func) doesn't broke the current implementation
if mode == "func" {
err := instrumentForFunctionCoverage(srcPath, srcName, coverVar, outPath)
if err != nil {
Expand All @@ -80,6 +78,7 @@ func instrumentForCoverage(goenv *env, srcPath, srcName, coverVar, mode, outPath
return err
}
}

return registerCoverage(outPath, coverVar, srcName, mode)
}

Expand Down Expand Up @@ -130,10 +129,11 @@ func registerCoverage(coverSrc, varName, srcName, mode string) error {
}

// Append an init function.

if mode == "func" {
fmt.Fprintf(&buf, `
func init() {
%s.RegisterFileFuncCover(%s.SourcePath, %s.FuncNames, %s.FuncLines, %s.Counts)
%s.RegisterFileFuncCover(%s.SourcePath, %s.FuncNames, %s.FuncLines, %s.Flags)
}`, coverdataName, varName, varName, varName, varName)
} else {
fmt.Fprintf(&buf, `
Expand All @@ -142,9 +142,9 @@ func init() {
%[3]s.Count[:],
%[3]s.Pos[:],
%[3]s.NumStmt[:])
}`, coverdataName, srcName, varName)
}
`, coverdataName, srcName, varName)
}

if err := ioutil.WriteFile(coverSrc, buf.Bytes(), 0666); err != nil {
return fmt.Errorf("registerCoverage: %v", err)
}
Expand Down
5 changes: 0 additions & 5 deletions go/tools/builders/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,6 @@ func readFileInfo(bctx build.Context, input string, needPackage bool) (fileInfo,
}
}

//fmt.Println(input)

// dat, err := ioutil.ReadFile(input)
// fmt.Print(string(dat))

dir, base := filepath.Split(input)
// Check build constraints on non-cgo files.
// Skip cgo files, since they get rejected (due to leading '_') and won't
Expand Down
15 changes: 8 additions & 7 deletions go/tools/builders/funccover_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ import (
)

// FuncCoverBlock contains tha name and line of a function
// Line contains the first line of the definition in the source code
// Line contains the line of the definition in the source code
type FuncCoverBlock struct {
Name string
Line int32
}

// SaveFuncs parses given source code and returns a FuncCover instance, also returns true if main function is given
// SaveFuncs parses given source code and returns a FuncCover instance
func SaveFuncs(src string, content []byte) ([]FuncCoverBlock, error) {

fset := token.NewFileSet()
Expand Down Expand Up @@ -66,8 +66,8 @@ func astToByte(fset *token.FileSet, f *ast.File) []byte {
return buf.Bytes()
}

// Writes necessary set instrucions for instrumentation to cover variable
func addCounters(w io.Writer, content []byte, coverVar string) (bool, error) {
// Writes necessary set instrucions for instrumentation to function definitions
func InsertInstructions(w io.Writer, content []byte, coverVar string) (bool, error) {

fset := token.NewFileSet()
parsedFile, err := parser.ParseFile(fset, "", content, parser.ParseComments)
Expand Down Expand Up @@ -103,6 +103,7 @@ func addCounters(w io.Writer, content []byte, coverVar string) (bool, error) {
// ...
// }
// Also inserts defer LastCallForFunccoverReport() to the beginning of main()
// Initially this is just an empty function but handler can override it
// func main {
// defer LastCallForFunccoverReport()
// ...
Expand All @@ -112,7 +113,7 @@ func addCounters(w io.Writer, content []byte, coverVar string) (bool, error) {

for i := 0; i < contentLength; i++ {
if eventIndex < len(events) && i == events[eventIndex] {
fmt.Fprintf(w, "\n\t%s.Counts[%v] = true;", coverVar, eventIndex)
fmt.Fprintf(w, "\n\t%s.Flags[%v] = true;", coverVar, eventIndex)
eventIndex++
}
fmt.Fprintf(w, "%s", string(content[i]))
Expand Down Expand Up @@ -156,7 +157,7 @@ var {{.CoverVar}} = struct {
SourcePath string
FuncNames []string
FuncLines []int32
Counts []bool
Flags []bool
} {
SourcePath: "{{.SourceName}}",
FuncNames: []string{ {{range .FuncBlocks}}
Expand All @@ -165,7 +166,7 @@ var {{.CoverVar}} = struct {
FuncLines: []int32{ {{range .FuncBlocks}}
{{.Line}},{{end}}
},
Counts: []bool{ {{range .FuncBlocks}}
Flags: []bool{ {{range .FuncBlocks}}
false,{{end}}
},
}
Expand Down
11 changes: 6 additions & 5 deletions go/tools/builders/funccover_instrumenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//See the License for the specific language governing permissions and
//limitations under the License.

// This code implements a source file instrumentation function
// This code implements source file instrumentation function
package main

import (
Expand All @@ -31,7 +31,7 @@ type Instrumentation struct {
srcName string
}

// AddFile saves given file to instrument
// SaveFile, saves given file to instrumentation
func (h *Instrumentation) saveFile(src string) error {

if h.fset == nil {
Expand All @@ -48,11 +48,12 @@ func (h *Instrumentation) saveFile(src string) error {
return nil
}

// Instrument instruments the content saved in h
// Instrument, instruments the content saved in Instrumentation
func (h *Instrumentation) instrument() ([]byte, error) {

var funcCover = []FuncCoverBlock{}

// Saves the function data to funcCover
funcCover, err := SaveFuncs(h.srcName, h.content)
if err != nil {
return nil, err
Expand All @@ -61,7 +62,7 @@ func (h *Instrumentation) instrument() ([]byte, error) {
buf := new(bytes.Buffer)

// Inserts necessary instructions to the functions
hasMain, err := addCounters(buf, h.content, h.coverVar)
hasMain, err := InsertInstructions(buf, h.content, h.coverVar)

if err != nil {
return nil, err
Expand All @@ -81,7 +82,7 @@ func (h *Instrumentation) writeInstrumented(instrumented []byte) error {
return nil
}

//
// instrumentForFunctionCoverage instruments the file given and writes it to outPath
func instrumentForFunctionCoverage(srcPath, srcName, coverVar, outPath string) error {

var instrumentation = Instrumentation{
Expand Down
Loading

0 comments on commit b33f1c5

Please sign in to comment.