diff --git a/go/tools/gazelle/gazelle/diff.go b/go/tools/gazelle/gazelle/diff.go index c39b486833..14f6463448 100644 --- a/go/tools/gazelle/gazelle/diff.go +++ b/go/tools/gazelle/gazelle/diff.go @@ -24,7 +24,7 @@ import ( ) func diffFile(file *bzl.File) error { - f, err := ioutil.TempFile("", "BUILD") + f, err := ioutil.TempFile("", *buildName) if err != nil { return err } diff --git a/go/tools/gazelle/gazelle/main.go b/go/tools/gazelle/gazelle/main.go index cea56893f1..5aaa7ac8b1 100644 --- a/go/tools/gazelle/gazelle/main.go +++ b/go/tools/gazelle/gazelle/main.go @@ -33,9 +33,15 @@ import ( ) var ( - goPrefix = flag.String("go_prefix", "", "go_prefix of the target workspace") - repoRoot = flag.String("repo_root", "", "path to a directory which corresponds to go_prefix, otherwise gazelle searches for it.") - mode = flag.String("mode", "fix", "print: prints all of the updated BUILD files\n\tfix: rewrites all of the BUILD files in place\n\tdiff: computes the rewrite but then just does a diff") + goPrefix = flag.String("go_prefix", "", "go_prefix of the target workspace") + repoRoot = flag.String("repo_root", "", "path to a directory which corresponds to go_prefix, otherwise gazelle searches for it.") + mode = flag.String("mode", "fix", "print: prints all of the updated BUILD files\n\tfix: rewrites all of the BUILD files in place\n\tdiff: computes the rewrite but then just does a diff") + buildName = flag.String("build_name", "BUILD", "name of output build files to generate, defaults to 'BUILD'") + + validBuildNames = map[string]bool{ + "BUILD": true, + "BUILD.bazel": true, + } ) func init() { @@ -51,7 +57,7 @@ var modeFromName = map[string]func(*bzl.File) error{ } func run(dirs []string, emit func(*bzl.File) error) error { - g, err := generator.New(*repoRoot, *goPrefix) + g, err := generator.New(*repoRoot, *goPrefix, *buildName) if err != nil { return err } @@ -120,6 +126,10 @@ func main() { } } + if !validBuildNames[*buildName] { + log.Fatalf("-build_name %q must be in: %v", *buildName, validBuildNames) + } + emit := modeFromName[*mode] if emit == nil { log.Fatalf("unrecognized mode %s", *mode) @@ -135,9 +145,23 @@ func main() { } } +func readBuild(root string) (data []byte, location string, _ error) { + for n := range validBuildNames { + p := filepath.Join(root, n) + b, err := ioutil.ReadFile(p) + if err != nil { + if os.IsNotExist(err) { + continue + } + return nil, "", err + } + return b, p, nil + } + return nil, "", fmt.Errorf("no build files found at %q", root) +} + func loadGoPrefix(repo string) (string, error) { - p := filepath.Join(repo, "BUILD") - b, err := ioutil.ReadFile(p) + b, p, err := readBuild(repo) if err != nil { return "", err } diff --git a/go/tools/gazelle/generator/generator.go b/go/tools/gazelle/generator/generator.go index b27fd5c595..677d61c5c7 100644 --- a/go/tools/gazelle/generator/generator.go +++ b/go/tools/gazelle/generator/generator.go @@ -39,10 +39,11 @@ var ( // Generator generates BUILD files for a Go repository. type Generator struct { - repoRoot string - goPrefix string - bctx build.Context - g rules.Generator + repoRoot string + goPrefix string + buildName string + bctx build.Context + g rules.Generator } // New returns a new Generator which is responsible for a Go repository. @@ -50,7 +51,7 @@ type Generator struct { // "repoRoot" is a path to the root directory of the repository. // "goPrefix" is the go_prefix corresponding to the repository root directory. // See also https://github.com/bazelbuild/rules_go#go_prefix. -func New(repoRoot, goPrefix string) (*Generator, error) { +func New(repoRoot, goPrefix, buildName string) (*Generator, error) { bctx := build.Default // Ignore source files in $GOROOT and $GOPATH bctx.GOROOT = "" @@ -66,10 +67,11 @@ func New(repoRoot, goPrefix string) (*Generator, error) { return nil, err } return &Generator{ - repoRoot: filepath.Clean(repoRoot), - goPrefix: goPrefix, - bctx: bctx, - g: rules.NewGenerator(goPrefix), + repoRoot: filepath.Clean(repoRoot), + goPrefix: goPrefix, + buildName: buildName, + bctx: bctx, + g: rules.NewGenerator(goPrefix), }, nil } @@ -99,7 +101,7 @@ func (g *Generator) Generate(dir string) ([]*bzl.File, error) { if len(files) == 0 && rel != "" { // "dir" was not a buildable Go package but still need a BUILD file // for go_prefix. - files = append(files, emptyToplevel(g.goPrefix)) + files = append(files, emptyToplevel(g.goPrefix, g.buildName)) } file, err := g.generateOne(rel, pkg) @@ -116,9 +118,9 @@ func (g *Generator) Generate(dir string) ([]*bzl.File, error) { return files, nil } -func emptyToplevel(goPrefix string) *bzl.File { +func emptyToplevel(goPrefix, buildName string) *bzl.File { return &bzl.File{ - Path: "BUILD", + Path: buildName, Stmt: []bzl.Expr{ loadExpr("go_prefix"), &bzl.CallExpr{ @@ -137,7 +139,7 @@ func (g *Generator) generateOne(rel string, pkg *build.Package) (*bzl.File, erro return nil, err } - file := &bzl.File{Path: filepath.Join(rel, "BUILD")} + file := &bzl.File{Path: filepath.Join(rel, g.buildName)} for _, r := range rs { file.Stmt = append(file.Stmt, r.Call) } diff --git a/go/tools/gazelle/generator/generator_test.go b/go/tools/gazelle/generator/generator_test.go index 2ba594e76b..2a3f63e5a1 100644 --- a/go/tools/gazelle/generator/generator_test.go +++ b/go/tools/gazelle/generator/generator_test.go @@ -33,6 +33,14 @@ var ( ) func TestGenerator(t *testing.T) { + testGenerator(t, "BUILD") +} + +func TestGeneratorDotBazel(t *testing.T) { + testGenerator(t, "BUILD.bazel") +} + +func testGenerator(t *testing.T, buildName string) { stub := stubRuleGen{ goFiles: make(map[string][]string), cFiles: make(map[string][]string), @@ -153,7 +161,7 @@ func TestGenerator(t *testing.T) { } repo := filepath.Join(testdata.Dir(), "repo") - g, err := New(repo, "example.com/repo") + g, err := New(repo, "example.com/repo", buildName) if err != nil { t.Errorf(`New(%q, "example.com/repo") failed with %v; want success`, repo, err) return @@ -172,7 +180,7 @@ func TestGenerator(t *testing.T) { want := []*bzl.File{ { - Path: "BUILD", + Path: buildName, Stmt: []bzl.Expr{ loadExpr("go_prefix"), &bzl.CallExpr{ @@ -184,7 +192,7 @@ func TestGenerator(t *testing.T) { }, }, { - Path: "lib/BUILD", + Path: "lib/" + buildName, Stmt: []bzl.Expr{ loadExpr("go_prefix", "go_library"), stub.fixtures["lib"][0].Call, @@ -192,7 +200,7 @@ func TestGenerator(t *testing.T) { }, }, { - Path: "lib/internal/deep/BUILD", + Path: "lib/internal/deep/" + buildName, Stmt: []bzl.Expr{ loadExpr("go_library", "go_test"), stub.fixtures["lib/internal/deep"][0].Call, @@ -200,14 +208,14 @@ func TestGenerator(t *testing.T) { }, }, { - Path: "lib/relativeimporter/BUILD", + Path: "lib/relativeimporter/" + buildName, Stmt: []bzl.Expr{ loadExpr("go_library"), stub.fixtures["lib/relativeimporter"][0].Call, }, }, { - Path: "bin/BUILD", + Path: "bin/" + buildName, Stmt: []bzl.Expr{ loadExpr("go_library", "go_binary"), stub.fixtures["bin"][0].Call, @@ -215,7 +223,7 @@ func TestGenerator(t *testing.T) { }, }, { - Path: "bin_with_tests/BUILD", + Path: "bin_with_tests/" + buildName, Stmt: []bzl.Expr{ loadExpr("go_library", "go_binary", "go_test"), stub.fixtures["bin_with_tests"][0].Call, @@ -224,7 +232,7 @@ func TestGenerator(t *testing.T) { }, }, { - Path: "cgolib/BUILD", + Path: "cgolib/" + buildName, Stmt: []bzl.Expr{ loadExpr("go_library", "go_test", "cgo_library"), stub.fixtures["cgolib"][0].Call, @@ -233,7 +241,7 @@ func TestGenerator(t *testing.T) { }, }, { - Path: "cgolib_with_build_tags/BUILD", + Path: "cgolib_with_build_tags/" + buildName, Stmt: []bzl.Expr{ loadExpr("go_library", "go_test", "cgo_library"), stub.fixtures["cgolib"][0].Call, @@ -242,7 +250,7 @@ func TestGenerator(t *testing.T) { }, }, { - Path: "allcgolib/BUILD", + Path: "allcgolib/" + buildName, Stmt: []bzl.Expr{ loadExpr("go_library", "go_test", "cgo_library"), stub.fixtures["cgolib"][0].Call,