2020-01-27 18:04:07 +00:00
|
|
|
"""Wrappers for common build rules.
|
|
|
|
|
|
|
|
These wrappers apply common BUILD configurations (e.g., proto_library
|
|
|
|
automagically creating cc_ and go_ proto targets) and act as a single point of
|
|
|
|
change for Google-internal and bazel-compatible rules.
|
|
|
|
"""
|
|
|
|
|
|
|
|
load("//tools/go_stateify:defs.bzl", "go_stateify")
|
|
|
|
load("//tools/go_marshal:defs.bzl", "go_marshal", "marshal_deps", "marshal_test_deps")
|
2020-10-16 21:42:02 +00:00
|
|
|
load("//tools/nogo:defs.bzl", "nogo_test")
|
2021-01-09 02:01:05 +00:00
|
|
|
load("//tools/bazeldefs:defs.bzl", _arch_genrule = "arch_genrule", _build_test = "build_test", _bzl_library = "bzl_library", _coreutil = "coreutil", _default_installer = "default_installer", _default_net_util = "default_net_util", _more_shards = "more_shards", _most_shards = "most_shards", _proto_library = "proto_library", _select_arch = "select_arch", _select_system = "select_system", _short_path = "short_path", _version = "version")
|
2020-10-16 21:42:02 +00:00
|
|
|
load("//tools/bazeldefs:cc.bzl", _cc_binary = "cc_binary", _cc_flags_supplier = "cc_flags_supplier", _cc_grpc_library = "cc_grpc_library", _cc_library = "cc_library", _cc_proto_library = "cc_proto_library", _cc_test = "cc_test", _cc_toolchain = "cc_toolchain", _gbenchmark = "gbenchmark", _grpcpp = "grpcpp", _gtest = "gtest", _vdso_linker_option = "vdso_linker_option")
|
2021-01-12 06:31:16 +00:00
|
|
|
load("//tools/bazeldefs:go.bzl", _gazelle = "gazelle", _go_binary = "go_binary", _go_embed_data = "go_embed_data", _go_grpc_and_proto_libraries = "go_grpc_and_proto_libraries", _go_library = "go_library", _go_path = "go_path", _go_proto_library = "go_proto_library", _go_rule = "go_rule", _go_test = "go_test", _select_goarch = "select_goarch", _select_goos = "select_goos")
|
2020-10-16 21:42:02 +00:00
|
|
|
load("//tools/bazeldefs:pkg.bzl", _pkg_deb = "pkg_deb", _pkg_tar = "pkg_tar")
|
2020-02-21 23:05:20 +00:00
|
|
|
load("//tools/bazeldefs:platforms.bzl", _default_platform = "default_platform", _platforms = "platforms")
|
2020-02-20 20:32:31 +00:00
|
|
|
load("//tools/bazeldefs:tags.bzl", "go_suffixes")
|
2020-01-27 18:04:07 +00:00
|
|
|
|
2020-10-16 21:42:02 +00:00
|
|
|
# Core rules.
|
2020-12-09 08:42:53 +00:00
|
|
|
arch_genrule = _arch_genrule
|
2020-04-28 21:45:06 +00:00
|
|
|
build_test = _build_test
|
2020-08-06 19:08:12 +00:00
|
|
|
bzl_library = _bzl_library
|
2020-10-16 21:42:02 +00:00
|
|
|
default_installer = _default_installer
|
|
|
|
default_net_util = _default_net_util
|
|
|
|
select_arch = _select_arch
|
|
|
|
select_system = _select_system
|
|
|
|
short_path = _short_path
|
|
|
|
coreutil = _coreutil
|
2020-12-03 08:58:36 +00:00
|
|
|
more_shards = _more_shards
|
|
|
|
most_shards = _most_shards
|
2021-01-09 02:01:05 +00:00
|
|
|
version = _version
|
2020-10-16 21:42:02 +00:00
|
|
|
|
|
|
|
# C++ rules.
|
2020-01-27 18:04:07 +00:00
|
|
|
cc_binary = _cc_binary
|
2020-03-17 15:52:14 +00:00
|
|
|
cc_flags_supplier = _cc_flags_supplier
|
|
|
|
cc_grpc_library = _cc_grpc_library
|
2020-01-27 18:04:07 +00:00
|
|
|
cc_library = _cc_library
|
|
|
|
cc_test = _cc_test
|
|
|
|
cc_toolchain = _cc_toolchain
|
2020-03-17 15:52:14 +00:00
|
|
|
gbenchmark = _gbenchmark
|
2020-10-16 21:42:02 +00:00
|
|
|
gtest = _gtest
|
|
|
|
grpcpp = _grpcpp
|
|
|
|
vdso_linker_option = _vdso_linker_option
|
|
|
|
|
|
|
|
# Go rules.
|
2020-04-28 21:45:06 +00:00
|
|
|
gazelle = _gazelle
|
|
|
|
go_path = _go_path
|
2020-10-16 18:24:52 +00:00
|
|
|
select_goos = _select_goos
|
|
|
|
select_goarch = _select_goarch
|
2021-01-12 06:31:16 +00:00
|
|
|
go_embed_data = _go_embed_data
|
2020-10-16 21:42:02 +00:00
|
|
|
|
|
|
|
# Packaging rules.
|
|
|
|
pkg_deb = _pkg_deb
|
|
|
|
pkg_tar = _pkg_tar
|
2020-03-17 15:52:14 +00:00
|
|
|
|
2020-04-21 03:57:02 +00:00
|
|
|
# Platform options.
|
2020-02-21 23:05:20 +00:00
|
|
|
default_platform = _default_platform
|
2020-03-17 15:52:14 +00:00
|
|
|
platforms = _platforms
|
2020-01-27 18:04:07 +00:00
|
|
|
|
2021-01-12 06:31:16 +00:00
|
|
|
def _go_add_tags(ctx):
|
|
|
|
""" Adds tags to the given source file. """
|
|
|
|
output = ctx.outputs.out
|
|
|
|
runner = ctx.actions.declare_file(ctx.label.name + ".sh")
|
|
|
|
lines = ["#!/bin/bash"]
|
|
|
|
lines += ["echo '// +build %s' >> %s" % (tag, output.path) for tag in ctx.attr.go_tags]
|
|
|
|
lines.append("echo '' >> %s" % output.path)
|
|
|
|
lines += ["cat %s >> %s" % (f.path, output.path) for f in ctx.files.src]
|
|
|
|
lines.append("")
|
|
|
|
ctx.actions.write(runner, "\n".join(lines), is_executable = True)
|
|
|
|
ctx.actions.run(
|
|
|
|
inputs = ctx.files.src,
|
|
|
|
outputs = [output],
|
|
|
|
executable = runner,
|
|
|
|
)
|
|
|
|
return [DefaultInfo(
|
|
|
|
files = depset([output]),
|
|
|
|
)]
|
|
|
|
|
|
|
|
go_add_tags = _go_rule(
|
|
|
|
rule,
|
|
|
|
implementation = _go_add_tags,
|
|
|
|
attrs = {
|
|
|
|
"go_tags": attr.string_list(doc = "Go build tags to be added.", mandatory = True),
|
|
|
|
"src": attr.label(doc = "Source file.", allow_single_file = True, mandatory = True),
|
|
|
|
"out": attr.output(doc = "Output file.", mandatory = True),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2020-08-25 19:16:31 +00:00
|
|
|
def go_binary(name, nogo = True, pure = False, static = False, x_defs = None, **kwargs):
|
2020-01-27 18:04:07 +00:00
|
|
|
"""Wraps the standard go_binary.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
name: the rule name.
|
2020-08-25 19:16:31 +00:00
|
|
|
nogo: enable nogo analysis.
|
|
|
|
pure: build a pure Go (no CGo) binary.
|
|
|
|
static: build a static binary.
|
|
|
|
x_defs: additional linker definitions.
|
2020-01-27 18:04:07 +00:00
|
|
|
**kwargs: standard go_binary arguments.
|
|
|
|
"""
|
|
|
|
_go_binary(
|
|
|
|
name = name,
|
2020-08-25 19:16:31 +00:00
|
|
|
pure = pure,
|
|
|
|
static = static,
|
|
|
|
x_defs = x_defs,
|
2020-01-27 18:04:07 +00:00
|
|
|
**kwargs
|
|
|
|
)
|
2020-08-25 19:16:31 +00:00
|
|
|
if nogo:
|
|
|
|
# Note that the nogo rule applies only for go_library and go_test
|
|
|
|
# targets, therefore we construct a library from the binary sources.
|
2020-10-19 23:26:42 +00:00
|
|
|
# This is done because the binary may not be in a form that objdump
|
|
|
|
# supports (i.e. a pure Go binary).
|
2020-08-25 19:16:31 +00:00
|
|
|
_go_library(
|
|
|
|
name = name + "_nogo_library",
|
2020-10-19 23:26:42 +00:00
|
|
|
srcs = kwargs.get("srcs", []),
|
|
|
|
deps = kwargs.get("deps", []),
|
|
|
|
testonly = 1,
|
2020-08-25 19:16:31 +00:00
|
|
|
)
|
|
|
|
nogo_test(
|
|
|
|
name = name + "_nogo",
|
2020-10-26 18:09:34 +00:00
|
|
|
config = "//:nogo_config",
|
2020-10-09 19:31:35 +00:00
|
|
|
srcs = kwargs.get("srcs", []),
|
2020-10-26 18:09:34 +00:00
|
|
|
deps = [":" + name + "_nogo_library"],
|
|
|
|
tags = ["nogo"],
|
2020-08-25 19:16:31 +00:00
|
|
|
)
|
2020-01-27 18:04:07 +00:00
|
|
|
|
2020-02-04 22:36:43 +00:00
|
|
|
def calculate_sets(srcs):
|
|
|
|
"""Calculates special Go sets for templates.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
srcs: the full set of Go sources.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A dictionary of the form:
|
|
|
|
|
|
|
|
"": [src1.go, src2.go]
|
|
|
|
"suffix": [src3suffix.go, src4suffix.go]
|
|
|
|
|
|
|
|
Note that suffix will typically start with '_'.
|
|
|
|
"""
|
|
|
|
result = dict()
|
|
|
|
for file in srcs:
|
|
|
|
if not file.endswith(".go"):
|
|
|
|
continue
|
|
|
|
target = ""
|
|
|
|
for suffix in go_suffixes:
|
|
|
|
if file.endswith(suffix + ".go"):
|
|
|
|
target = suffix
|
|
|
|
if not target in result:
|
|
|
|
result[target] = [file]
|
|
|
|
else:
|
|
|
|
result[target].append(file)
|
|
|
|
return result
|
|
|
|
|
2020-02-06 06:45:44 +00:00
|
|
|
def go_imports(name, src, out):
|
|
|
|
"""Simplify a single Go source file by eliminating unused imports."""
|
|
|
|
native.genrule(
|
|
|
|
name = name,
|
|
|
|
srcs = [src],
|
|
|
|
outs = [out],
|
|
|
|
tools = ["@org_golang_x_tools//cmd/goimports:goimports"],
|
|
|
|
cmd = ("$(location @org_golang_x_tools//cmd/goimports:goimports) $(SRCS) > $@"),
|
|
|
|
)
|
|
|
|
|
2020-06-12 00:32:42 +00:00
|
|
|
def go_library(name, srcs, deps = [], imports = [], stateify = True, marshal = False, marshal_debug = False, nogo = True, **kwargs):
|
2020-01-27 18:04:07 +00:00
|
|
|
"""Wraps the standard go_library and does stateification and marshalling.
|
|
|
|
|
|
|
|
The recommended way is to use this rule with mostly identical configuration as the native
|
|
|
|
go_library rule.
|
|
|
|
|
|
|
|
These definitions provide additional flags (stateify, marshal) that can be used
|
|
|
|
with the generators to automatically supplement the library code.
|
|
|
|
|
|
|
|
load("//tools:defs.bzl", "go_library")
|
|
|
|
|
|
|
|
go_library(
|
|
|
|
name = "foo",
|
|
|
|
srcs = ["foo.go"],
|
|
|
|
)
|
|
|
|
|
|
|
|
Args:
|
|
|
|
name: the rule name.
|
|
|
|
srcs: the library sources.
|
|
|
|
deps: the library dependencies.
|
|
|
|
imports: imports required for stateify.
|
|
|
|
stateify: whether statify is enabled (default: true).
|
|
|
|
marshal: whether marshal is enabled (default: false).
|
2020-02-21 00:22:45 +00:00
|
|
|
marshal_debug: whether the gomarshal tools emits debugging output (default: false).
|
2020-08-25 19:16:31 +00:00
|
|
|
nogo: enable nogo analysis.
|
2020-01-27 18:04:07 +00:00
|
|
|
**kwargs: standard go_library arguments.
|
|
|
|
"""
|
2020-02-04 22:36:43 +00:00
|
|
|
all_srcs = srcs
|
|
|
|
all_deps = deps
|
2020-02-11 19:40:51 +00:00
|
|
|
dirname, _, _ = native.package_name().rpartition("/")
|
|
|
|
full_pkg = dirname + "/" + name
|
2020-01-27 18:04:07 +00:00
|
|
|
if stateify:
|
|
|
|
# Only do stateification for non-state packages without manual autogen.
|
2020-02-04 22:36:43 +00:00
|
|
|
# First, we need to segregate the input files via the special suffixes,
|
|
|
|
# and calculate the final output set.
|
|
|
|
state_sets = calculate_sets(srcs)
|
2020-02-14 11:26:42 +00:00
|
|
|
for (suffix, src_subset) in state_sets.items():
|
2020-02-04 22:36:43 +00:00
|
|
|
go_stateify(
|
2020-02-06 06:45:44 +00:00
|
|
|
name = name + suffix + "_state_autogen_with_imports",
|
2020-02-14 11:26:42 +00:00
|
|
|
srcs = src_subset,
|
2020-02-04 22:36:43 +00:00
|
|
|
imports = imports,
|
2020-02-11 19:40:51 +00:00
|
|
|
package = full_pkg,
|
2020-02-06 06:45:44 +00:00
|
|
|
out = name + suffix + "_state_autogen_with_imports.go",
|
|
|
|
)
|
|
|
|
go_imports(
|
|
|
|
name = name + suffix + "_state_autogen",
|
|
|
|
src = name + suffix + "_state_autogen_with_imports.go",
|
2020-02-04 22:36:43 +00:00
|
|
|
out = name + suffix + "_state_autogen.go",
|
|
|
|
)
|
|
|
|
all_srcs = all_srcs + [
|
|
|
|
name + suffix + "_state_autogen.go"
|
|
|
|
for suffix in state_sets.keys()
|
|
|
|
]
|
2020-12-09 08:42:53 +00:00
|
|
|
|
2020-02-04 22:36:43 +00:00
|
|
|
if "//pkg/state" not in all_deps:
|
|
|
|
all_deps = all_deps + ["//pkg/state"]
|
|
|
|
|
2020-01-27 18:04:07 +00:00
|
|
|
if marshal:
|
2020-02-04 22:36:43 +00:00
|
|
|
# See above.
|
|
|
|
marshal_sets = calculate_sets(srcs)
|
2020-02-14 11:26:42 +00:00
|
|
|
for (suffix, src_subset) in marshal_sets.items():
|
2020-02-04 22:36:43 +00:00
|
|
|
go_marshal(
|
|
|
|
name = name + suffix + "_abi_autogen",
|
2020-02-14 11:26:42 +00:00
|
|
|
srcs = src_subset,
|
2020-02-21 00:22:45 +00:00
|
|
|
debug = select({
|
|
|
|
"//tools/go_marshal:marshal_config_verbose": True,
|
|
|
|
"//conditions:default": marshal_debug,
|
|
|
|
}),
|
2020-02-04 22:36:43 +00:00
|
|
|
imports = imports,
|
|
|
|
package = name,
|
|
|
|
)
|
2020-01-27 18:04:07 +00:00
|
|
|
extra_deps = [
|
|
|
|
dep
|
|
|
|
for dep in marshal_deps
|
|
|
|
if not dep in all_deps
|
|
|
|
]
|
|
|
|
all_deps = all_deps + extra_deps
|
2020-02-04 22:36:43 +00:00
|
|
|
all_srcs = all_srcs + [
|
|
|
|
name + suffix + "_abi_autogen_unsafe.go"
|
|
|
|
for suffix in marshal_sets.keys()
|
|
|
|
]
|
2020-01-27 18:04:07 +00:00
|
|
|
|
|
|
|
_go_library(
|
|
|
|
name = name,
|
|
|
|
srcs = all_srcs,
|
|
|
|
deps = all_deps,
|
|
|
|
**kwargs
|
|
|
|
)
|
2020-04-21 03:57:02 +00:00
|
|
|
if nogo:
|
|
|
|
nogo_test(
|
|
|
|
name = name + "_nogo",
|
2020-10-26 18:09:34 +00:00
|
|
|
config = "//:nogo_config",
|
2020-10-09 19:31:35 +00:00
|
|
|
srcs = all_srcs,
|
2020-10-26 18:09:34 +00:00
|
|
|
deps = [":" + name],
|
|
|
|
tags = ["nogo"],
|
2020-04-21 03:57:02 +00:00
|
|
|
)
|
2020-01-27 18:04:07 +00:00
|
|
|
|
|
|
|
if marshal:
|
|
|
|
# Ignore importpath for go_test.
|
|
|
|
kwargs.pop("importpath", None)
|
|
|
|
|
2020-02-04 22:36:43 +00:00
|
|
|
# See above.
|
|
|
|
marshal_sets = calculate_sets(srcs)
|
2020-02-14 11:26:42 +00:00
|
|
|
for (suffix, _) in marshal_sets.items():
|
2020-02-04 22:36:43 +00:00
|
|
|
_go_test(
|
|
|
|
name = name + suffix + "_abi_autogen_test",
|
2020-09-30 00:27:16 +00:00
|
|
|
srcs = [
|
|
|
|
name + suffix + "_abi_autogen_test.go",
|
|
|
|
name + suffix + "_abi_autogen_unconditional_test.go",
|
|
|
|
],
|
2020-02-14 11:26:42 +00:00
|
|
|
library = ":" + name,
|
2020-02-04 22:36:43 +00:00
|
|
|
deps = marshal_test_deps,
|
|
|
|
**kwargs
|
|
|
|
)
|
2020-01-27 18:04:07 +00:00
|
|
|
|
2020-08-25 19:16:31 +00:00
|
|
|
def go_test(name, nogo = True, **kwargs):
|
|
|
|
"""Wraps the standard go_test.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
name: the rule name.
|
|
|
|
nogo: enable nogo analysis.
|
|
|
|
**kwargs: standard go_test arguments.
|
|
|
|
"""
|
|
|
|
_go_test(
|
|
|
|
name = name,
|
|
|
|
**kwargs
|
|
|
|
)
|
|
|
|
if nogo:
|
|
|
|
nogo_test(
|
|
|
|
name = name + "_nogo",
|
2020-10-26 18:09:34 +00:00
|
|
|
config = "//:nogo_config",
|
2020-10-09 19:31:35 +00:00
|
|
|
srcs = kwargs.get("srcs", []),
|
2020-10-26 18:09:34 +00:00
|
|
|
deps = [":" + name],
|
|
|
|
tags = ["nogo"],
|
2020-08-25 19:16:31 +00:00
|
|
|
)
|
|
|
|
|
2020-03-17 15:52:14 +00:00
|
|
|
def proto_library(name, srcs, deps = None, has_services = 0, **kwargs):
|
2020-01-27 18:04:07 +00:00
|
|
|
"""Wraps the standard proto_library.
|
|
|
|
|
2020-03-17 15:52:14 +00:00
|
|
|
Given a proto_library named "foo", this produces up to five different
|
|
|
|
targets:
|
2020-01-27 18:04:07 +00:00
|
|
|
- foo_proto: proto_library rule.
|
|
|
|
- foo_go_proto: go_proto_library rule.
|
|
|
|
- foo_cc_proto: cc_proto_library rule.
|
2020-03-17 15:52:14 +00:00
|
|
|
- foo_go_grpc_proto: go_grpc_library rule.
|
|
|
|
- foo_cc_grpc_proto: cc_grpc_library rule.
|
2020-01-27 18:04:07 +00:00
|
|
|
|
|
|
|
Args:
|
2020-03-17 15:52:14 +00:00
|
|
|
name: the name to which _proto, _go_proto, etc, will be appended.
|
2020-01-27 18:04:07 +00:00
|
|
|
srcs: the proto sources.
|
2020-03-17 15:52:14 +00:00
|
|
|
deps: for the proto library and the go_proto_library.
|
|
|
|
has_services: 1 to build gRPC code, otherwise 0.
|
2020-01-27 18:04:07 +00:00
|
|
|
**kwargs: standard proto_library arguments.
|
|
|
|
"""
|
|
|
|
_proto_library(
|
|
|
|
name = name + "_proto",
|
|
|
|
srcs = srcs,
|
|
|
|
deps = deps,
|
2020-03-17 15:52:14 +00:00
|
|
|
has_services = has_services,
|
2020-01-27 18:04:07 +00:00
|
|
|
**kwargs
|
|
|
|
)
|
2020-03-17 15:52:14 +00:00
|
|
|
if has_services:
|
|
|
|
_go_grpc_and_proto_libraries(
|
|
|
|
name = name,
|
|
|
|
deps = deps,
|
|
|
|
**kwargs
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
_go_proto_library(
|
|
|
|
name = name,
|
|
|
|
deps = deps,
|
|
|
|
**kwargs
|
|
|
|
)
|
2020-01-27 18:04:07 +00:00
|
|
|
_cc_proto_library(
|
|
|
|
name = name + "_cc_proto",
|
|
|
|
deps = [":" + name + "_proto"],
|
|
|
|
**kwargs
|
|
|
|
)
|
2020-03-17 15:52:14 +00:00
|
|
|
if has_services:
|
|
|
|
_cc_grpc_library(
|
|
|
|
name = name + "_cc_grpc_proto",
|
|
|
|
srcs = [":" + name + "_proto"],
|
|
|
|
deps = [":" + name + "_cc_proto"],
|
|
|
|
**kwargs
|
|
|
|
)
|