-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
252 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,15 @@ | ||
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 = ["@com_github_pkg_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,131 @@ | ||
// 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/ioutil" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"sort" | ||
"strings" | ||
"text/template" | ||
|
||
"github.com/pkg/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) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
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.Errorf( | ||
"failed to run %s: %v\n(stderr)\n%s", cmd, err, &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 { | ||
tmpDir, err := ioutil.TempDir("", "") | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { _ = os.RemoveAll(tmpDir) }() | ||
for _, t := range targets { | ||
out, err := t.query.exec() | ||
if err != nil { | ||
return err | ||
} | ||
fp := filepath.Join(tmpDir, t.filename) | ||
f, err := os.Create(fp) | ||
if err != nil { | ||
return err | ||
} | ||
if err := t.template.Execute(f, out); err != nil { | ||
return errors.Wrapf(err, "failed to execute template for %s", t.filename) | ||
} | ||
if err := f.Close(); err != nil { | ||
return errors.Wrapf(err, "failed to write file for %s", t.filename) | ||
} | ||
if err := os.Rename(fp, filepath.Join(outDir, t.filename)); err != nil { | ||
return errors.Wrapf(err, "failed to rename file for %s", t.filename) | ||
} | ||
} | ||
return nil | ||
} |
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,64 @@ | ||
# Generated by genbzl | ||
|
||
PROTOBUF_SRCS = [ | ||
"//pkg/acceptance/cluster:cluster_go_proto", | ||
"//pkg/blobs/blobspb:blobspb_go_proto", | ||
"//pkg/build:build_go_proto", | ||
"//pkg/ccl/backupccl:backupccl_go_proto", | ||
"//pkg/ccl/baseccl:baseccl_go_proto", | ||
"//pkg/ccl/sqlproxyccl/tenant:tenant_go_proto", | ||
"//pkg/ccl/storageccl/engineccl/enginepbccl:enginepbccl_go_proto", | ||
"//pkg/ccl/streamingccl/streampb:streampb_go_proto", | ||
"//pkg/ccl/utilccl/licenseccl:licenseccl_go_proto", | ||
"//pkg/clusterversion:clusterversion_go_proto", | ||
"//pkg/config/zonepb:zonepb_go_proto", | ||
"//pkg/config:config_go_proto", | ||
"//pkg/geo/geoindex:geoindex_go_proto", | ||
"//pkg/geo/geopb:geopb_go_proto", | ||
"//pkg/gossip:gossip_go_proto", | ||
"//pkg/jobs/jobspb:jobspb_go_proto", | ||
"//pkg/kv/kvnemesis:kvnemesis_go_proto", | ||
"//pkg/kv/kvserver/closedts/ctpb:ctpb_go_proto", | ||
"//pkg/kv/kvserver/concurrency/lock:lock_go_proto", | ||
"//pkg/kv/kvserver/kvserverpb:kvserverpb_go_proto", | ||
"//pkg/kv/kvserver/liveness/livenesspb:livenesspb_go_proto", | ||
"//pkg/kv/kvserver/loqrecovery/loqrecoverypb:loqrecoverypb_go_proto", | ||
"//pkg/kv/kvserver/protectedts/ptpb:ptpb_go_proto", | ||
"//pkg/kv/kvserver/protectedts/ptstorage:ptstorage_go_proto", | ||
"//pkg/kv/kvserver/readsummary/rspb:rspb_go_proto", | ||
"//pkg/kv/kvserver:kvserver_go_proto", | ||
"//pkg/roachpb:roachpb_go_proto", | ||
"//pkg/rpc:rpc_go_proto", | ||
"//pkg/server/diagnostics/diagnosticspb:diagnosticspb_go_proto", | ||
"//pkg/server/serverpb:serverpb_go_proto", | ||
"//pkg/server/status/statuspb:statuspb_go_proto", | ||
"//pkg/sql/catalog/catpb:catpb_go_proto", | ||
"//pkg/sql/catalog/descpb:descpb_go_proto", | ||
"//pkg/sql/contentionpb:contentionpb_go_proto", | ||
"//pkg/sql/execinfrapb:execinfrapb_go_proto", | ||
"//pkg/sql/inverted:inverted_go_proto", | ||
"//pkg/sql/pgwire/pgerror:pgerror_go_proto", | ||
"//pkg/sql/protoreflect/test:protoreflecttest_go_proto", | ||
"//pkg/sql/rowenc/rowencpb:rowencpb_go_proto", | ||
"//pkg/sql/schemachanger/scpb:scpb_go_proto", | ||
"//pkg/sql/sessiondatapb:sessiondatapb_go_proto", | ||
"//pkg/sql/sqlstats/persistedsqlstats:persistedsqlstats_go_proto", | ||
"//pkg/sql/stats:stats_go_proto", | ||
"//pkg/sql/types:types_go_proto", | ||
"//pkg/startupmigrations/leasemanager:leasemanager_go_proto", | ||
"//pkg/storage/enginepb:enginepb_go_proto", | ||
"//pkg/testutils/grpcutils:grpcutils_go_proto", | ||
"//pkg/ts/catalog:catalog_go_proto", | ||
"//pkg/ts/tspb:tspb_go_proto", | ||
"//pkg/util/duration:duration_go_proto", | ||
"//pkg/util/hlc:hlc_go_proto", | ||
"//pkg/util/log/eventpb:eventpb_go_proto", | ||
"//pkg/util/log/logpb:logpb_go_proto", | ||
"//pkg/util/metric:metric_go_proto", | ||
"//pkg/util/optional:optional_go_proto", | ||
"//pkg/util/protoutil:protoutil_go_proto", | ||
"//pkg/util/timeutil/pgdate:pgdate_go_proto", | ||
"//pkg/util/tracing/tracingpb:tracingpb_go_proto", | ||
"//pkg/util/tracing/tracingservicepb:tracingservicepb_go_proto", | ||
"//pkg/util:util_go_proto", | ||
] |