Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for building using bazel #11

Merged
merged 1 commit into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ import "github.com/jordanlewis/gcassert"

func main() {
var buf strings.Builder
if err := gcassert.GCAssert(&buf, "./path/to/package", "./otherpath/to/package"); err != nil {
if err := gcassert.GCAssert(&buf, false /* useBazel */ "./path/to/package", "./otherpath/to/package"); err != nil {
// handle non-lint-failure related errors
panic(err)
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/gcassert/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import (
"github.com/jordanlewis/gcassert"
)

var useBazel = flag.Bool("use-bazel", false, "use bazel to build")

func main() {
flag.Parse()
var buf strings.Builder
err := gcassert.GCAssert(&buf, flag.Args()...)
err := gcassert.GCAssert(&buf, *useBazel, flag.Args()...)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
Expand Down
35 changes: 28 additions & 7 deletions gcassert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gcassert

import (
"bufio"
"errors"
"fmt"
"go/ast"
"go/printer"
Expand Down Expand Up @@ -38,7 +37,7 @@ func stringToDirective(s string) (assertDirective, error) {
case "noescape":
return noescape, nil
}
return noDirective, errors.New(fmt.Sprintf("no such directive %s", s))
return noDirective, fmt.Errorf("no such directive %s", s)
}

// passInfo contains info on a passed directive for directives that have
Expand Down Expand Up @@ -143,13 +142,24 @@ func (v assertVisitor) Visit(node ast.Node) (w ast.Visitor) {

// GCAssert searches through the packages at the input path and writes failures
// to comply with //gcassert directives to the given io.Writer.
func GCAssert(w io.Writer, paths ...string) error {
func GCAssert(w io.Writer, useBazel bool, paths ...string) error {
for _, path := range paths {
// Assert that all paths begin with './'
// This is needed for ensuring that packages.Load and parseDirectives work
// as expected.
if !strings.HasPrefix(path, "./") {
return fmt.Errorf("all paths should be prefixed with './': got %s", path)
}
}
fileSet := token.NewFileSet()
pkgs, err := packages.Load(&packages.Config{
Mode: packages.NeedName | packages.NeedFiles | packages.NeedSyntax | packages.NeedCompiledGoFiles |
packages.NeedTypesInfo | packages.NeedTypes,
Fset: fileSet,
}, paths...)
if err != nil {
return err
}
directiveMap, err := parseDirectives(pkgs, fileSet)
if err != nil {
return err
Expand All @@ -158,11 +168,22 @@ func GCAssert(w io.Writer, paths ...string) error {
// Next: invoke Go compiler with -m flags to get the compiler to print
// its optimization decisions.

args := []string{"build", "-gcflags=-m=2 -d=ssa/check_bce/debug=1"}
for i := range paths {
args = append(args, "./"+paths[i])
var args []string
var cmd *exec.Cmd

if useBazel {
args = []string{"build"}
for i := range paths {
args = append(args, strings.TrimPrefix(paths[i], "./"))
yuzefovich marked this conversation as resolved.
Show resolved Hide resolved
}
args = append(args, "--@io_bazel_rules_go//go/config:gc_goopts=-m=2,-d=ssa/check_bce/debug=1")
cmd = exec.Command("bazel", args...)
} else {
args = []string{"build", "-gcflags=-m=2 -d=ssa/check_bce/debug=1"}
args = append(args, paths...)
cmd = exec.Command("go", args...)
}
cmd := exec.Command("go", args...)

cwd, err := os.Getwd()
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion gcassert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestParseDirectives(t *testing.T) {

func TestGCAssert(t *testing.T) {
var w strings.Builder
err := GCAssert(&w, "./testdata", "./testdata/otherpkg")
err := GCAssert(&w, false /* useBazel */, "./testdata", "./testdata/otherpkg")
if err != nil {
t.Fatal(err)
}
Expand Down