Skip to content
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 .bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.1.0
4.2.1
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ coverage:
dep:
go mod tidy
go mod vendor
bazelisk run //:gazelle -- update-repos -from_file=go.mod -prune -to_macro=repositories.bzl%go_repositories
bazelisk run //:gazelle -- update-repos -from_file=go.mod -prune -build_file_proto_mode=disable -to_macro=repositories.bzl%go_repositories

.PHONY: gazelle
gazelle:
Expand Down
24 changes: 13 additions & 11 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ load(
### Rules_go and gazelle
http_archive(
name = "io_bazel_rules_go",
sha256 = "e0015762cdeb5a2a9c48f96fb079c6a98e001d44ec23ad4fa2ca27208c5be4fb",
sha256 = "2b1641428dff9018f9e85c0384f03ec6c10660d935b750e3fa1492a281a53b0f",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.24.14/rules_go-v0.24.14.tar.gz",
"https://github.com/bazelbuild/rules_go/releases/download/v0.24.14/rules_go-v0.24.14.tar.gz",
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.29.0/rules_go-v0.29.0.zip",
"https://github.com/bazelbuild/rules_go/releases/download/v0.29.0/rules_go-v0.29.0.zip",
],
)

Expand All @@ -31,7 +31,7 @@ load(
go_rules_dependencies()

go_register_toolchains(
go_version = "1.16.2",
go_version = "1.17.2",
)

load(
Expand All @@ -43,10 +43,10 @@ go_embed_data_dependencies()

http_archive(
name = "bazel_gazelle",
sha256 = "62ca106be173579c0a167deb23358fdfe71ffa1e4cfdddf5582af26520f1c66f",
sha256 = "de69a09dc70417580aabf20a28619bb3ef60d038470c7cf8442fafcf627c21cb",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.23.0/bazel-gazelle-v0.23.0.tar.gz",
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.23.0/bazel-gazelle-v0.23.0.tar.gz",
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.24.0/bazel-gazelle-v0.24.0.tar.gz",
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.24.0/bazel-gazelle-v0.24.0.tar.gz",
],
)

Expand All @@ -73,9 +73,12 @@ gazelle_dependencies()
### Google Protobuf
http_archive(
name = "com_google_protobuf",
sha256 = "1c744a6a1f2c901e68c5521bc275e22bdc66256eeb605c2781923365b7087e5f",
strip_prefix = "protobuf-3.13.0",
urls = ["https://github.com/protocolbuffers/protobuf/archive/v3.13.0.zip"],
sha256 = "d0f5f605d0d656007ce6c8b5a82df3037e1d8fe8b121ed42e536f569dec16113",
strip_prefix = "protobuf-3.14.0",
urls = [
"https://mirror.bazel.build/github.com/protocolbuffers/protobuf/archive/v3.14.0.tar.gz",
"https://github.com/protocolbuffers/protobuf/archive/v3.14.0.tar.gz",
],
)

load(
Expand Down Expand Up @@ -180,7 +183,6 @@ container_pull(
tag = "0.1.0",
)


### web

http_archive(
Expand Down
166 changes: 123 additions & 43 deletions bazel/gomock.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -33,45 +33,64 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_context", "go_rule")
load("@io_bazel_rules_go//go/private:providers.bzl", "GoLibrary")
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_context", "go_path")
load("@io_bazel_rules_go//go/private:providers.bzl", "GoLibrary", "GoPath")

_MOCKGEN_TOOL = "@com_github_golang_mock//mockgen"
_MOCKGEN_MODEL_LIB = "@com_github_golang_mock//mockgen/model:go_default_library"

def _gomock_source_impl(ctx):
args = ["-source", ctx.file.source.path]
if ctx.attr.package != "":
args += ["-package", ctx.attr.package]
args += [",".join(ctx.attr.interfaces)]

out = ctx.outputs.out
cmd = ctx.file.mockgen_tool
go_ctx = go_context(ctx)
inputs = go_ctx.sdk.headers + go_ctx.sdk.srcs + go_ctx.sdk.tools + [ctx.file.source]
gopath = "$(pwd)/" + ctx.bin_dir.path + "/" + ctx.attr.gopath_dep[GoPath].gopath

# passed in source needs to be in gopath to not trigger module mode
args = ["-source", gopath + "/src/" + ctx.attr.library[GoLibrary].importmap + "/"+ ctx.file.source.basename]

args, needed_files = _handle_shared_args(ctx, args)

if len(ctx.attr.aux_files) > 0:
aux_files = []
for pkg, files in ctx.attr.aux_files.items():
for f in files:
mapped_f = gopath + "/src/" + ctx.attr.library[GoLibrary].importmap + "/"+ f
aux_files.append("{0}={1}".format(pkg, mapped_f))
args += ["-aux_files", ",".join(aux_files)]

inputs = (
ctx.attr.gopath_dep.files.to_list() + needed_files +
go_ctx.sdk.headers + go_ctx.sdk.srcs + go_ctx.sdk.tools
) + [ctx.file.source]

# We can use the go binary from the stdlib for most of the environment
# variables, but our GOPATH is specific to the library target we were given.
ctx.actions.run_shell(
outputs = [out],
outputs = [ctx.outputs.out],
inputs = inputs,
tools = [
cmd,
ctx.file.mockgen_tool,
go_ctx.go,
],
command = """
source <($PWD/{godir}/go env) &&
export PATH=$GOROOT/bin:$PWD/{godir}:$PATH &&
export GOPATH={gopath} &&
mkdir -p .gocache &&
export GOCACHE=$PWD/.gocache &&
{cmd} {args} > {out}
""".format(
godir = go_ctx.go.path[:-1 - len(go_ctx.go.basename)],
cmd = "$(pwd)/" + cmd.path,
gopath = gopath,
cmd = "$(pwd)/" + ctx.file.mockgen_tool.path,
args = " ".join(args),
out = out.path,
out = ctx.outputs.out.path,
mnemonic = "GoMockSourceGen",
),
env = {
"GO111MODULE": "off", # explicitly relying on passed in go_path to not download modules while doing codegen
},
)

_gomock_source = go_rule(
_gomock_source = rule(
_gomock_source_impl,
attrs = {
"library": attr.label(
Expand All @@ -90,24 +109,49 @@ _gomock_source = go_rule(
),
"interfaces": attr.string_list(
allow_empty = False,
doc = "The names of the Go interfaces to generate mocks for. If not set, all of the interfaces in the library or source file will have mocks generated for them.",
doc = "Ignored. If `source` is not set, this would be the list of Go interfaces to generate mocks for.",
mandatory = True,
),
"aux_files": attr.string_list_dict(
default = {},
doc = "A map from packages to auxilliary Go source files to load for those packages.",
),
"package": attr.string(
doc = "The name of the package the generated mocks should be in. If not specified, uses mockgen's default.",
),
"self_package": attr.string(
doc = "The full package import path for the generated code. The purpose of this flag is to prevent import cycles in the generated code by trying to include its own package. This can happen if the mock's package is set to one of its inputs (usually the main one) and the output is stdio so mockgen cannot detect the final output package. Setting this flag will then tell mockgen which import to exclude.",
),
"imports": attr.string_dict(
doc = "Dictionary of name-path pairs of explicit imports to use.",
),
"mock_names": attr.string_dict(
doc = "Dictionary of interface name to mock name pairs to change the output names of the mock objects. Mock names default to 'Mock' prepended to the name of the interface.",
default = {},
),
"copyright_file": attr.label(
doc = "Optional file containing copyright to prepend to the generated contents.",
allow_single_file = True,
mandatory = False,
),
"gopath_dep": attr.label(
doc = "The go_path label to use to create the GOPATH for the given library. Will be set correctly by the gomock macro, so you don't need to set it.",
providers = [GoPath],
mandatory = False,
),
"mockgen_tool": attr.label(
doc = "The mockgen tool to run",
default = Label(_MOCKGEN_TOOL),
allow_single_file = True,
executable = True,
cfg = "host",
cfg = "exec",
mandatory = False,
),
"_go_context_data": attr.label(
default = "@io_bazel_rules_go//:go_context_data",
),
},
toolchains = ["@io_bazel_rules_go//go:toolchain"],
)

def gomock(name, library, out, **kwargs):
Expand All @@ -116,23 +160,28 @@ def gomock(name, library, out, **kwargs):
mockgen_tool = kwargs["mockgen_tool"]

if kwargs.get("source", None):
gopath_name = name + "_gomock_gopath"
go_path(
name = gopath_name,
deps = [library, mockgen_tool],
)
_gomock_source(
name = name,
library = library,
gopath_dep = gopath_name,
out = out,
**kwargs
)
**kwargs)
else:
_gomock_reflect(
name = name,
library = library,
out = out,
mockgen_tool = mockgen_tool,
**kwargs
)
**kwargs)

def _gomock_reflect(name, library, out, mockgen_tool, **kwargs):
interfaces = kwargs.get("interfaces", None)
interfaces = kwargs.pop("interfaces", None)

mockgen_model_lib = _MOCKGEN_MODEL_LIB
if kwargs.get("mockgen_model_library", None):
mockgen_model_lib = kwargs["mockgen_model_library"]
Expand All @@ -143,7 +192,6 @@ def _gomock_reflect(name, library, out, mockgen_tool, **kwargs):
name = prog_src,
interfaces = interfaces,
library = library,
package = kwargs.get("package", None),
out = prog_src_out,
mockgen_tool = mockgen_tool,
)
Expand All @@ -157,17 +205,13 @@ def _gomock_reflect(name, library, out, mockgen_tool, **kwargs):
name = name,
interfaces = interfaces,
library = library,
package = kwargs.get("package", None),
out = out,
prog_bin = prog_bin,
mockgen_tool = mockgen_tool,
self_package = kwargs.get("self_package", None),
)
**kwargs)

def _gomock_prog_gen_impl(ctx):
args = ["-prog_only"]
if ctx.attr.package != "":
args += ["-package", ctx.attr.package]
args += [ctx.attr.library[GoLibrary].importpath]
args += [",".join(ctx.attr.interfaces)]

Expand All @@ -183,13 +227,14 @@ def _gomock_prog_gen_impl(ctx):
args = " ".join(args),
out = out.path,
),
mnemonic = "GoMockReflectProgOnlyGen"
)

_gomock_prog_gen = go_rule(
_gomock_prog_gen = rule(
_gomock_prog_gen_impl,
attrs = {
"library": attr.label(
doc = "The target the Go library is at to look for the interfaces in. When this is set and source is not set, mockgen will use its reflect code to generate the mocks.",
doc = "The target the Go library is at to look for the interfaces in. When this is set and source is not set, mockgen will use its reflect code to generate the mocks. If source is set, its dependencies will be included in the GOPATH that mockgen will be run in.",
providers = [GoLibrary],
mandatory = True,
),
Expand All @@ -202,34 +247,33 @@ _gomock_prog_gen = go_rule(
doc = "The names of the Go interfaces to generate mocks for. If not set, all of the interfaces in the library or source file will have mocks generated for them.",
mandatory = True,
),
"package": attr.string(
doc = "The name of the package the generated mocks should be in. If not specified, uses mockgen's default.",
),
"mockgen_tool": attr.label(
doc = "The mockgen tool to run",
default = Label(_MOCKGEN_TOOL),
allow_single_file = True,
executable = True,
cfg = "host",
cfg = "exec",
mandatory = False,
),
"_go_context_data": attr.label(
default = "@io_bazel_rules_go//:go_context_data",
),
},
toolchains = ["@io_bazel_rules_go//go:toolchain"],
)

def _gomock_prog_exec_impl(ctx):
args = ["-exec_only", ctx.file.prog_bin.path]
if ctx.attr.package != "":
args += ["-package", ctx.attr.package]

if ctx.attr.self_package != "":
args += ["-self_package", ctx.attr.self_package]
args, needed_files = _handle_shared_args(ctx, args)

# annoyingly, the interfaces join has to go after the importpath so we can't
# share those.
args += [ctx.attr.library[GoLibrary].importpath]
args += [",".join(ctx.attr.interfaces)]

ctx.actions.run_shell(
outputs = [ctx.outputs.out],
inputs = [ctx.file.prog_bin],
inputs = [ctx.file.prog_bin] + needed_files,
tools = [ctx.file.mockgen_tool],
command = """{cmd} {args} > {out}""".format(
cmd = "$(pwd)/" + ctx.file.mockgen_tool.path,
Expand All @@ -240,9 +284,10 @@ def _gomock_prog_exec_impl(ctx):
# GOCACHE is required starting in Go 1.12
"GOCACHE": "./.gocache",
},
mnemonic = "GoMockReflectExecOnlyGen",
)

_gomock_prog_exec = go_rule(
_gomock_prog_exec = rule(
_gomock_prog_exec_impl,
attrs = {
"library": attr.label(
Expand All @@ -265,20 +310,55 @@ _gomock_prog_exec = go_rule(
"self_package": attr.string(
doc = "The full package import path for the generated code. The purpose of this flag is to prevent import cycles in the generated code by trying to include its own package. This can happen if the mock's package is set to one of its inputs (usually the main one) and the output is stdio so mockgen cannot detect the final output package. Setting this flag will then tell mockgen which import to exclude.",
),
"imports": attr.string_dict(
doc = "Dictionary of name-path pairs of explicit imports to use.",
),
"mock_names": attr.string_dict(
doc = "Dictionary of interfaceName-mockName pairs of explicit mock names to use. Mock names default to 'Mock'+ interfaceName suffix.",
default = {},
),
"copyright_file": attr.label(
doc = "Optional file containing copyright to prepend to the generated contents.",
allow_single_file = True,
mandatory = False,
),
"prog_bin": attr.label(
doc = "The program binary generated by mockgen's -prog_only and compiled by bazel.",
allow_single_file = True,
executable = True,
cfg = "host",
cfg = "exec",
mandatory = True,
),
"mockgen_tool": attr.label(
doc = "The mockgen tool to run",
default = Label(_MOCKGEN_TOOL),
allow_single_file = True,
executable = True,
cfg = "host",
cfg = "exec",
mandatory = False,
),
"_go_context_data": attr.label(
default = "@io_bazel_rules_go//:go_context_data",
),
},
toolchains = ["@io_bazel_rules_go//go:toolchain"],
)

def _handle_shared_args(ctx, args):
needed_files = []

if ctx.attr.package != "":
args += ["-package", ctx.attr.package]
if ctx.attr.self_package != "":
args += ["-self_package", ctx.attr.self_package]
if len(ctx.attr.imports) > 0:
imports = ",".join(["{0}={1}".format(name, pkg) for name, pkg in ctx.attr.imports.items()])
args += ["-imports", imports]
if ctx.file.copyright_file != None:
args += ["-copyright_file", ctx.file.copyright_file.path]
needed_files.append(ctx.file.copyright_file)
if len(ctx.attr.mock_names) > 0:
mock_names = ",".join(["{0}={1}".format(name, pkg) for name, pkg in ctx.attr.mock_names.items()])
args += ["-mock_names", mock_names]

return args, needed_files
Loading