forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bazel: generate proto targets, remove old protos when generating new …
…ones This is what the Makefile did. It was painful to have the old onces because they'd lead to spurious diffs. This commit also automates the generation of the protobuf sources into a bzl file which is now an unexported input to the rule. Release note: None
- Loading branch information
Showing
6 changed files
with
264 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") | ||
|
||
go_library( | ||
name = "genbzl_lib", | ||
srcs = ["main.go"], | ||
importpath = "github.com/cockroachdb/cockroach/pkg/gen/genbzl", | ||
visibility = ["//visibility:private"], | ||
deps = [ | ||
"//pkg/cli/exit", | ||
"@com_github_cockroachdb_errors//:errors", | ||
], | ||
) | ||
|
||
go_binary( | ||
name = "genbzl", | ||
embed = [":genbzl_lib"], | ||
visibility = ["//visibility:public"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
// Copyright 2022 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
// Command genbzl is used to generate bazel files which then get imported by | ||
// the gen package's BUILD.bazel to facilitate hoisting these generated files | ||
// back into the source tree. | ||
// | ||
// It's all a bit meta. The flow is that we invoke this binary inside the | ||
// bazelutil/bazel-generate.sh script which writes out some bzl files with | ||
// lists of targets which are then depended on in gen.bzl | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"sort" | ||
"strings" | ||
"text/template" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/cli/exit" | ||
"github.com/cockroachdb/errors" | ||
) | ||
|
||
var ( | ||
outDir = flag.String("out-dir", "", "directory in which to place the generated files") | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
if err := generate(*outDir); err != nil { | ||
fmt.Fprintf(os.Stderr, "failed to generate files: %v\n", err) | ||
exit.WithCode(exit.UnspecifiedError()) | ||
} | ||
} | ||
|
||
var targets = []*target{ | ||
newTarget( | ||
"protobuf.bzl", | ||
newQuery("kind(go_proto_library, //pkg/...)"), | ||
`# Generated by genbzl | ||
PROTOBUF_SRCS = [{{ range . }} | ||
"{{ . }}",{{end}} | ||
] | ||
`), | ||
} | ||
|
||
func newQuery(q string, filters ...func(s string) (shouldKeep bool)) *query { | ||
return &query{ | ||
query: q, | ||
filters: filters, | ||
} | ||
} | ||
|
||
type target struct { | ||
filename string | ||
template *template.Template | ||
query *query | ||
} | ||
|
||
func newTarget(filename string, query *query, tmpl string) *target { | ||
return &target{ | ||
filename: filename, | ||
template: template.Must(template.New(filename).Parse(tmpl)), | ||
query: query, | ||
} | ||
} | ||
|
||
type query struct { | ||
query string | ||
filters []func(s string) bool | ||
} | ||
|
||
func (q query) exec() (results []string, _ error) { | ||
cmd := exec.Command("bazel", "query", q.query) | ||
var stdout bytes.Buffer | ||
var stderr strings.Builder | ||
cmd.Stdout = &stdout | ||
cmd.Stderr = &stderr | ||
if err := cmd.Run(); err != nil { | ||
return nil, errors.Wrapf(err, | ||
"failed to run %s: (stderr)\n%s", cmd, &stderr) | ||
} | ||
sc := bufio.NewScanner(&stdout) | ||
keep := func(s string) (shouldKeep bool) { | ||
for _, f := range q.filters { | ||
if !f(s) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
for sc.Scan() { | ||
if s := sc.Text(); keep(s) { | ||
results = append(results, s) | ||
} | ||
} | ||
sort.Strings(results) | ||
return results, nil | ||
} | ||
|
||
func generate(outDir string) error { | ||
for _, t := range targets { | ||
out, err := t.query.exec() | ||
if err != nil { | ||
return err | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
var buf bytes.Buffer | ||
if err := t.template.Execute(&buf, out); err != nil { | ||
return errors.Wrapf(err, "failed to execute template for %s", t.filename) | ||
} | ||
f, err := os.Create(filepath.Join(outDir, t.filename)) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to open file for %s", t.filename) | ||
} | ||
if _, err := io.Copy(f, &buf); err != nil { | ||
return errors.Wrapf(err, "failed to write file for %s", t.filename) | ||
} | ||
if err := f.Close(); err != nil { | ||
return errors.Wrapf(err, "failed to write file for %s", t.filename) | ||
} | ||
} | ||
return nil | ||
} |
Oops, something went wrong.