Skip to content

Commit

Permalink
Update repo using latest gazelle. (#281)
Browse files Browse the repository at this point in the history
* upgrade rule_go; run gazelle everywhere

* add go_proto_checkedin_test to deal with latest go_proto_library and run update_generated.sh to regenerate files

* format build_defs.bzl with buildifier -type bzl

* add buildifier_integration_test to tests suite

* revert x_defs for linkstamp until it works -- in a follow up PR.
  • Loading branch information
pmbethe09 committed Apr 19, 2018
1 parent 8b22880 commit 49d4d32
Show file tree
Hide file tree
Showing 31 changed files with 457 additions and 546 deletions.
7 changes: 7 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("@bazel_gazelle//:def.bzl", "gazelle")
load("@io_bazel_rules_go//go:def.bzl", "go_prefix")

go_prefix("github.com/bazelbuild/buildtools")
Expand All @@ -13,6 +14,7 @@ test_suite(
"//api_proto:api.gen.pb.go_checkshtest",
"//build:go_default_test",
"//build_proto:build.gen.pb.go_checkshtest",
"//buildifier:buildifier_integration_test",
"//deps_proto:deps.gen.pb.go_checkshtest",
"//edit:go_default_test",
"//extra_actions_base_proto:extra_actions_base.gen.pb.go_checkshtest",
Expand All @@ -21,3 +23,8 @@ test_suite(
"//wspace:go_default_test",
],
)

gazelle(
name = "gazelle",
prefix = "github.com/bazelbuild/buildtools",
)
15 changes: 12 additions & 3 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@ workspace(name = "com_github_bazelbuild_buildtools")

git_repository(
name = "io_bazel_rules_go",
commit = "9b3a85e62cc8c00d4b356bfb2035ca0657703187",
# 0.11.1
commit = "12fa5fd88725c9033fc7c37ec0f04b64a9519f49",
remote = "https://github.com/bazelbuild/rules_go.git",
)

http_archive(
name = "bazel_gazelle",
sha256 = "92a3c59734dad2ef85dc731dbcb2bc23c4568cded79d4b87ebccd787eb89e8d0",
url = "https://github.com/bazelbuild/bazel-gazelle/releases/download/0.11.0/bazel-gazelle-0.11.0.tar.gz",
)

load(
"@io_bazel_rules_go//go:def.bzl",
"go_rules_dependencies",
"go_register_toolchains",
"go_repository",
)
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")

gazelle_dependencies()

go_rules_dependencies()

Expand All @@ -30,6 +39,6 @@ http_archive(

go_repository(
name = "skylark_syntax",
commit = "f11011f2887ba17f71cf974fc319dbb550a48ed5",
commit = "ede9b31f30c07f7081ae3c112b223d024c7f7a15",
importpath = "github.com/google/skylark",
)
26 changes: 21 additions & 5 deletions api_proto/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
load("@io_bazel_rules_go//proto:go_proto_library.bzl", "go_proto_library")
load("//build:build_defs.bzl", "genfile_check_test")
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
load("//build:build_defs.bzl", "go_proto_checkedin_test")

genfile_check_test(
# gazelle:exclude api.gen.pb.go

go_proto_checkedin_test(
src = "api.gen.pb.go",
gen = "api.pb.go",
)

proto_library(
name = "api_proto_proto",
srcs = ["api.proto"],
visibility = ["//visibility:public"],
)

go_proto_library(
name = "api_proto_go_proto",
importpath = "github.com/bazelbuild/buildtools/api_proto",
proto = ":api_proto_proto",
visibility = ["//visibility:public"],
)

go_library(
name = "go_default_library",
srcs = ["api.proto"],
embed = [":api_proto_go_proto"],
importpath = "github.com/bazelbuild/buildtools/api_proto",
visibility = ["//visibility:public"],
)
55 changes: 27 additions & 28 deletions api_proto/api.gen.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions api_proto/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ syntax = "proto3";

package devtools.buildozer;

option go_package = "api_proto";

message Output {
repeated Record records = 1;
message Record {
Expand Down
4 changes: 3 additions & 1 deletion build/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ go_library(
"syntax.go",
"walk.go",
],
importpath = "github.com/bazelbuild/buildtools/build",
visibility = ["//visibility:public"],
deps = ["//tables:go_default_library"],
)
Expand All @@ -40,5 +41,6 @@ go_test(
"parse.y.baz.go",
"parse.y.go",
],
library = ":go_default_library",
embed = [":go_default_library"],
deps = ["//tables:go_default_library"],
)
108 changes: 78 additions & 30 deletions build/build_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,86 @@ distributed under the License is distributed on an "AS IS" BASIS,
limitations under the License.
"""

load(
"@io_bazel_rules_go//go/private:providers.bzl",
"GoSource",
)

_GO_YACC_TOOL = "@org_golang_x_tools//cmd/goyacc"

def go_yacc(src, out, visibility=None):
"""Runs go tool yacc -o $out $src."""
native.genrule(
name = src + ".go_yacc",
srcs = [src],
outs = [out],
tools = [_GO_YACC_TOOL],
cmd = ("export GOROOT=$$(dirname $(location " + _GO_YACC_TOOL + "))/..;" +
" $(location " + _GO_YACC_TOOL + ") " +
" -o $(location " + out + ") $(SRCS)"),
visibility = visibility,
local = 1,
)
def go_yacc(src, out, visibility = None):
"""Runs go tool yacc -o $out $src."""
native.genrule(
name = src + ".go_yacc",
srcs = [src],
outs = [out],
tools = [_GO_YACC_TOOL],
cmd = ("export GOROOT=$$(dirname $(location " + _GO_YACC_TOOL + "))/..;" +
" $(location " + _GO_YACC_TOOL + ") " +
" -o $(location " + out + ") $(SRCS)"),
visibility = visibility,
local = 1,
)

def _extract_go_src(ctx):
"""Thin rule that exposes the GoSource from a go_library."""
return [DefaultInfo(files = depset(ctx.attr.library[GoSource].srcs))]

extract_go_src = rule(
implementation = _extract_go_src,
attrs = {
"library": attr.label(
providers = [GoSource],
),
},
)

def genfile_check_test(src, gen):
"""Asserts that any checked-in generated code matches regen."""
if not src:
fail("src is required", "src")
if not gen:
fail("gen is required", "gen")
native.genrule(
name = src + "_checksh",
outs = [src + "_check.sh"],
cmd = "echo 'diff $$@' > $@",
)
native.sh_test(
name = src + "_checkshtest",
size = "small",
srcs = [src + "_check.sh"],
data = [src, gen],
args = ["$(location " + src + ")", "$(location " + gen + ")"],
)
"""Asserts that any checked-in generated code matches bazel gen."""
if not src:
fail("src is required", "src")
if not gen:
fail("gen is required", "gen")
native.genrule(
name = src + "_checksh",
outs = [src + "_check.sh"],
cmd = "echo 'diff $$@' > $@",
)
native.sh_test(
name = src + "_checkshtest",
size = "small",
srcs = [src + "_check.sh"],
data = [src, gen],
args = ["$(location " + src + ")", "$(location " + gen + ")"],
)

# magic copy rule used to update the checked-in version
native.genrule(
name = src + "_copysh",
srcs = [gen],
outs = [src + "copy.sh"],
cmd = "echo 'cp $${BUILD_WORKSPACE_DIRECTORY}/$(location " + gen +
") $${BUILD_WORKSPACE_DIRECTORY}/" + native.package_name() + "/" + src + "' > $@",
)
native.sh_binary(
name = src + "_copy",
srcs = [src + "_copysh"],
data = [gen],
)

def go_proto_checkedin_test(src, proto = "go_default_library"):
"""Asserts that any checked-in .pb.go code matches bazel gen."""
genfile = src + "_genfile"
extract_go_src(
name = genfile + "go",
library = proto,
)

# TODO(pmbethe09): why is the extra copy needed?
native.genrule(
name = genfile,
srcs = [genfile + "go"],
outs = [genfile + ".go"],
cmd = "cp $< $@",
)
genfile_check_test(src, genfile)
22 changes: 15 additions & 7 deletions build_proto/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
load("@io_bazel_rules_go//proto:go_proto_library.bzl", "go_proto_library")
load("//build:build_defs.bzl", "genfile_check_test")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
load("//build:build_defs.bzl", "go_proto_checkedin_test")

genfile_check_test(
# gazelle:exclude build.gen.pb.go

go_proto_checkedin_test(
src = "build.gen.pb.go",
gen = "build.pb.go",
)

genrule(
name = "copy_and_fix",
srcs = ["@io_bazel//src/main/protobuf:srcs"],
outs = ["build.proto"],
cmd = ("SRCS=($(locations @io_bazel//src/main/protobuf:srcs));" +
"cp $$(dirname $$SRCS)/build.proto $@"),
cmd = "SRCS=($(locations @io_bazel//src/main/protobuf:srcs));" +
"cp $$(dirname $$SRCS)/build.proto $@",
)

proto_library(
name = "blaze_query_proto",
srcs = ["build.proto"],
visibility = ["//visibility:public"],
)

go_proto_library(
name = "go_default_library",
srcs = ["build.proto"],
importpath = "github.com/bazelbuild/buildtools/build_proto",
proto = ":blaze_query_proto",
visibility = ["//visibility:public"],
)
Loading

0 comments on commit 49d4d32

Please sign in to comment.