Skip to content

Commit

Permalink
Merge branch 'master' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
rockwotj authored Aug 20, 2024
2 parents e78b2d2 + f3029e2 commit 60ef975
Show file tree
Hide file tree
Showing 13 changed files with 298 additions and 160 deletions.
3 changes: 2 additions & 1 deletion docs/go/extras/extras.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ This rule has moved. See [gazelle rule] in the Gazelle repository.

<pre>
gomock(<a href="#gomock-name">name</a>, <a href="#gomock-out">out</a>, <a href="#gomock-library">library</a>, <a href="#gomock-source_importpath">source_importpath</a>, <a href="#gomock-source">source</a>, <a href="#gomock-interfaces">interfaces</a>, <a href="#gomock-package">package</a>, <a href="#gomock-self_package">self_package</a>, <a href="#gomock-aux_files">aux_files</a>,
<a href="#gomock-mockgen_tool">mockgen_tool</a>, <a href="#gomock-imports">imports</a>, <a href="#gomock-copyright_file">copyright_file</a>, <a href="#gomock-mock_names">mock_names</a>, <a href="#gomock-kwargs">kwargs</a>)
<a href="#gomock-mockgen_tool">mockgen_tool</a>, <a href="#gomock-imports">imports</a>, <a href="#gomock-copyright_file">copyright_file</a>, <a href="#gomock-mock_names">mock_names</a>, <a href="#gomock-typed">typed</a>, <a href="#gomock-kwargs">kwargs</a>)
</pre>

Calls [mockgen](https://github.com/golang/mock) to generates a Go file containing mocks from the given library.
Expand All @@ -60,6 +60,7 @@ If `source` is given, the mocks are generated in source mode; otherwise in refle
| <a id="gomock-imports"></a>imports | dictionary of name-path pairs of explicit imports to use. See [mockgen's -imports](https://github.com/golang/mock#flags) for more information. | <code>{}</code> |
| <a id="gomock-copyright_file"></a>copyright_file | optional file containing copyright to prepend to the generated contents. See [mockgen's -copyright_file](https://github.com/golang/mock#flags) for more information. | <code>None</code> |
| <a id="gomock-mock_names"></a>mock_names | 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. See [mockgen's -mock_names](https://github.com/golang/mock#flags) for more information. | <code>{}</code> |
| <a id="gomock-typed"></a>typed | generate type-safe 'Return', 'Do', 'DoAndReturn' functions. See [mockgen's -typed](https://github.com/uber-go/mock#flags) for more information. | <code>False</code> |
| <a id="gomock-kwargs"></a>kwargs | <p align="center"> - </p> | none |


17 changes: 15 additions & 2 deletions extras/gomock.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ _gomock_source = rule(
allow_single_file = True,
mandatory = False,
),
"typed": attr.bool(
doc = "Generate type-safe 'Return', 'Do', 'DoAndReturn' functions.",
mandatory = False,
),
"mockgen_tool": attr.label(
doc = "The mockgen tool to run",
default = _MOCKGEN_TOOL,
Expand All @@ -167,7 +171,7 @@ _gomock_source = rule(
toolchains = [GO_TOOLCHAIN],
)

def gomock(name, out, library = None, source_importpath = "", source = None, interfaces = [], package = "", self_package = "", aux_files = {}, mockgen_tool = _MOCKGEN_TOOL, imports = {}, copyright_file = None, mock_names = {}, **kwargs):
def gomock(name, out, library = None, source_importpath = "", source = None, interfaces = [], package = "", self_package = "", aux_files = {}, mockgen_tool = _MOCKGEN_TOOL, imports = {}, copyright_file = None, mock_names = {}, typed = False, **kwargs):
"""Calls [mockgen](https://github.com/golang/mock) to generates a Go file containing mocks from the given library.
If `source` is given, the mocks are generated in source mode; otherwise in reflective mode.
Expand All @@ -186,7 +190,8 @@ def gomock(name, out, library = None, source_importpath = "", source = None, int
imports: dictionary of name-path pairs of explicit imports to use. See [mockgen's -imports](https://github.com/golang/mock#flags) for more information.
copyright_file: optional file containing copyright to prepend to the generated contents. See [mockgen's -copyright_file](https://github.com/golang/mock#flags) for more information.
mock_names: 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. See [mockgen's -mock_names](https://github.com/golang/mock#flags) for more information.
kwargs: common attributes](https://bazel.build/reference/be/common-definitions#common-attributes) to all Bazel rules.
typed: generate type-safe 'Return', 'Do', 'DoAndReturn' functions. See [mockgen's -typed](https://github.com/uber-go/mock#flags) for more information.
kwargs: [common attributes](https://bazel.build/reference/be/common-definitions#common-attributes) to all Bazel rules.
"""
if source:
_gomock_source(
Expand All @@ -202,6 +207,7 @@ def gomock(name, out, library = None, source_importpath = "", source = None, int
imports = imports,
copyright_file = copyright_file,
mock_names = mock_names,
typed = typed,
**kwargs
)
else:
Expand All @@ -216,6 +222,7 @@ def gomock(name, out, library = None, source_importpath = "", source = None, int
imports = imports,
copyright_file = copyright_file,
mock_names = mock_names,
typed = typed,
**kwargs
)

Expand Down Expand Up @@ -360,6 +367,10 @@ _gomock_prog_exec = rule(
allow_single_file = True,
mandatory = False,
),
"typed": attr.bool(
doc = "Generate type-safe 'Return', 'Do', 'DoAndReturn' functions.",
mandatory = False,
),
"prog_bin": attr.label(
doc = "The program binary generated by mockgen's -prog_only and compiled by bazel.",
allow_single_file = True,
Expand Down Expand Up @@ -398,5 +409,7 @@ def _handle_shared_args(ctx, args):
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]
if ctx.attr.typed:
args += ["-typed"]

return args, needed_files
22 changes: 9 additions & 13 deletions go/private/actions/archive.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load(
"//go/private:common.bzl",
"as_tuple",
)
load(
"//go/private:context.bzl",
"get_nogo",
Expand Down Expand Up @@ -170,17 +166,17 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d
pathtype = source.library.pathtype,

# GoSource fields
srcs = as_tuple(source.srcs),
srcs = tuple(source.srcs),
_cover = source.cover,
_embedsrcs = as_tuple(source.embedsrcs),
_embedsrcs = tuple(source.embedsrcs),
_x_defs = tuple(source.x_defs.items()),
_gc_goopts = as_tuple(source.gc_goopts),
_gc_goopts = tuple(source.gc_goopts),
_cgo = source.cgo,
_cdeps = as_tuple(source.cdeps),
_cppopts = as_tuple(source.cppopts),
_copts = as_tuple(source.copts),
_cxxopts = as_tuple(source.cxxopts),
_clinkopts = as_tuple(source.clinkopts),
_cdeps = tuple(source.cdeps),
_cppopts = tuple(source.cppopts),
_copts = tuple(source.copts),
_cxxopts = tuple(source.cxxopts),
_clinkopts = tuple(source.clinkopts),

# Information on dependencies
_dep_labels = tuple([d.data.label for d in direct]),
Expand All @@ -192,7 +188,7 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d
facts_file = out_facts,
runfiles = source.runfiles,
_validation_output = out_nogo_validation,
_cgo_deps = as_tuple(cgo_deps),
_cgo_deps = cgo_deps,
)
x_defs = dict(source.x_defs)
for a in direct:
Expand Down
10 changes: 0 additions & 10 deletions go/private/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,6 @@ def as_iterable(v):
return v.to_list()
fail("as_iterator failed on {}".format(v))

def as_tuple(v):
"""Returns a list, tuple, or depset as a tuple."""
if type(v) == "tuple":
return v
if type(v) == "list":
return tuple(v)
if type(v) == "depset":
return tuple(v.to_list())
fail("as_tuple failed on {}".format(v))

def count_group_matches(v, prefix, suffix):
"""Counts reluctant substring matches between prefix and suffix.
Expand Down
74 changes: 45 additions & 29 deletions go/private/context.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,16 @@ def _merge_embed(source, embed):
source["x_defs"].update(s.x_defs)
source["gc_goopts"] = source["gc_goopts"] + s.gc_goopts
source["runfiles"] = source["runfiles"].merge(s.runfiles)
if s.cgo and source["cgo"]:
fail("multiple libraries with cgo enabled")
source["cgo"] = source["cgo"] or s.cgo
source["cdeps"] = source["cdeps"] or s.cdeps
source["cppopts"] = source["cppopts"] or s.cppopts
source["copts"] = source["copts"] or s.copts
source["cxxopts"] = source["cxxopts"] or s.cxxopts
source["clinkopts"] = source["clinkopts"] or s.clinkopts

if s.cgo:
if source["cgo"]:
fail("multiple libraries with cgo enabled")
source["cgo"] = s.cgo
source["cdeps"] = s.cdeps
source["cppopts"] = s.cppopts
source["copts"] = s.copts
source["cxxopts"] = s.cxxopts
source["clinkopts"] = s.clinkopts

def _dedup_archives(archives):
"""Returns a list of archives without duplicate import paths.
Expand Down Expand Up @@ -258,9 +260,7 @@ def _library_to_source(go, attr, library, coverage_instrumented):
srcs = attr_srcs + generated_srcs
embedsrcs = [f for t in getattr(attr, "embedsrcs", []) for f in as_iterable(t.files)]
attr_deps = getattr(attr, "deps", [])
generated_deps = getattr(library, "deps", [])
deps = attr_deps + generated_deps
deps = [get_archive(dep) for dep in deps]
deps = [get_archive(dep) for dep in attr_deps]
source = {
"library": library,
"mode": go.mode,
Expand Down Expand Up @@ -381,7 +381,8 @@ def go_context(
importpath = None,
importmap = None,
embed = None,
importpath_aliases = None):
importpath_aliases = None,
go_context_data = None):
"""Returns an API used to build Go code.
See /go/toolchains.rst#go-context
Expand All @@ -390,26 +391,33 @@ def go_context(
attr = ctx.attr
toolchain = ctx.toolchains[GO_TOOLCHAIN]
cgo_context_info = None
go_context_info = None
go_config_info = None
stdlib = None
coverdata = None
nogo = None
if hasattr(attr, "_go_context_data"):
go_context_data = _flatten_possibly_transitioned_attr(attr._go_context_data)

if go_context_data == None:
if hasattr(attr, "_go_context_data"):
go_context_data = _flatten_possibly_transitioned_attr(attr._go_context_data)
if CgoContextInfo in go_context_data:
cgo_context_info = go_context_data[CgoContextInfo]
go_config_info = go_context_data[GoConfigInfo]
stdlib = go_context_data[GoStdLib]
go_context_info = go_context_data[GoContextInfo]
if getattr(attr, "_cgo_context_data", None) and CgoContextInfo in attr._cgo_context_data:
cgo_context_info = attr._cgo_context_data[CgoContextInfo]
if getattr(attr, "cgo_context_data", None) and CgoContextInfo in attr.cgo_context_data:
cgo_context_info = attr.cgo_context_data[CgoContextInfo]
if hasattr(attr, "_go_config"):
go_config_info = attr._go_config[GoConfigInfo]
if hasattr(attr, "_stdlib"):
stdlib = _flatten_possibly_transitioned_attr(attr._stdlib)[GoStdLib]
else:
go_context_data = _flatten_possibly_transitioned_attr(go_context_data)
if CgoContextInfo in go_context_data:
cgo_context_info = go_context_data[CgoContextInfo]
go_config_info = go_context_data[GoConfigInfo]
stdlib = go_context_data[GoStdLib]
coverdata = go_context_data[GoContextInfo].coverdata
nogo = go_context_data[GoContextInfo].nogo
if getattr(attr, "_cgo_context_data", None) and CgoContextInfo in attr._cgo_context_data:
cgo_context_info = attr._cgo_context_data[CgoContextInfo]
if getattr(attr, "cgo_context_data", None) and CgoContextInfo in attr.cgo_context_data:
cgo_context_info = attr.cgo_context_data[CgoContextInfo]
if hasattr(attr, "_go_config"):
go_config_info = attr._go_config[GoConfigInfo]
if hasattr(attr, "_stdlib"):
stdlib = _flatten_possibly_transitioned_attr(attr._stdlib)[GoStdLib]
go_context_info = go_context_data[GoContextInfo]

mode = get_mode(ctx, toolchain, cgo_context_info, go_config_info)

Expand Down Expand Up @@ -515,8 +523,8 @@ def go_context(
importpath_aliases = importpath_aliases,
pathtype = pathtype,
cgo_tools = cgo_tools,
nogo = nogo,
coverdata = coverdata,
nogo = go_context_info.nogo if go_context_info else None,
coverdata = go_context_info.coverdata if go_context_info else None,
coverage_enabled = ctx.configuration.coverage_enabled,
coverage_instrumented = ctx.coverage_instrumented(),
env = env,
Expand Down Expand Up @@ -855,6 +863,14 @@ cgo_context_data_proxy = rule(
)

def _go_config_impl(ctx):
pgo_profiles = ctx.attr.pgoprofile.files.to_list()
if len(pgo_profiles) > 2:
fail("providing more than one pprof file to pgoprofile is not supported")
if len(pgo_profiles) == 1:
pgoprofile = pgo_profiles[0]
else:
pgoprofile = None

return [GoConfigInfo(
static = ctx.attr.static[BuildSettingInfo].value,
race = ctx.attr.race[BuildSettingInfo].value,
Expand All @@ -870,7 +886,7 @@ def _go_config_impl(ctx):
gc_goopts = ctx.attr.gc_goopts[BuildSettingInfo].value,
amd64 = ctx.attr.amd64,
arm = ctx.attr.arm,
pgoprofile = ctx.attr.pgoprofile,
pgoprofile = pgoprofile,
)]

go_config = rule(
Expand Down
Loading

0 comments on commit 60ef975

Please sign in to comment.