Skip to content

Commit

Permalink
Optimize CcInfo computation
Browse files Browse the repository at this point in the history
  • Loading branch information
dzbarsky committed Aug 14, 2024
1 parent a4a7035 commit 90b4e1c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 27 deletions.
6 changes: 0 additions & 6 deletions go/private/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,6 @@ def as_tuple(v):
return tuple(v.to_list())
fail("as_tuple failed on {}".format(v))

_STRUCT_TYPE = type(struct())

def is_struct(v):
"""Returns true if v is a struct."""
return type(v) == _STRUCT_TYPE

def count_group_matches(v, prefix, suffix):
"""Counts reluctant substring matches between prefix and suffix.
Expand Down
16 changes: 0 additions & 16 deletions go/private/context.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ load(
"COVERAGE_OPTIONS_DENYLIST",
"GO_TOOLCHAIN",
"as_iterable",
"is_struct",
)
load(
":mode.bzl",
Expand Down Expand Up @@ -286,7 +285,6 @@ def _library_to_source(go, attr, library, coverage_instrumented):
"cxxopts": _expand_opts(go, "cxxopts", getattr(attr, "cxxopts", [])),
"clinkopts": _expand_opts(go, "clinkopts", getattr(attr, "clinkopts", [])),
"cgo_exports": [],
"cc_info": None,
"pgoprofile": getattr(attr, "pgoprofile", None),
}

Expand Down Expand Up @@ -314,7 +312,6 @@ def _library_to_source(go, attr, library, coverage_instrumented):
fail("source {} has C/C++ extension, but cgo was not enabled (set 'cgo = True')".format(f.path))
if library.resolve:
library.resolve(go, attr, source, _merge_embed)
source["cc_info"] = _collect_cc_infos(source["deps"], source["cdeps"])
return GoSource(**source)

def _collect_runfiles(go, data, deps):
Expand All @@ -327,19 +324,6 @@ def _collect_runfiles(go, data, deps):
[get_source(t).runfiles for t in deps],
)

def _collect_cc_infos(deps, cdeps):
cc_infos = []
for dep in cdeps:
if CcInfo in dep:
cc_infos.append(dep[CcInfo])
for dep in deps:
# dep may be a struct, which doesn't support indexing by providers.
if is_struct(dep):
continue
if GoSource in dep:
cc_infos.append(dep[GoSource].cc_info)
return cc_common.merge_cc_infos(cc_infos = cc_infos)

def _check_binary_dep(go, dep, edge):
"""Checks that this rule doesn't depend on a go_binary or go_test.
Expand Down
2 changes: 1 addition & 1 deletion go/private/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# A represenatation of the inputs to a go package.
# A representation of the inputs to a go package.
# This is a configuration independent provider.
# You must call resolve with a mode to produce a GoSource.
# See go/providers.rst#GoLibrary for full documentation.
Expand Down
29 changes: 25 additions & 4 deletions go/private/rules/binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ load(
"//go/private:providers.bzl",
"GoLibrary",
"GoSDK",
"GoSource",
)
load(
"//go/private/rules:transition.bzl",
Expand Down Expand Up @@ -94,6 +95,24 @@ def new_cc_import(
),
)

def _go_cc_aspect_impl(target, _ctx):
if GoSource in target:
deps = (
getattr(_ctx.rule.attr, "deps", []) +
getattr(_ctx.rule.attr, "cdeps", []) +
getattr(_ctx.rule.attr, "embed", [])
)
return [
cc_common.merge_cc_infos(cc_infos = [dep[CcInfo] for dep in deps]),
]

return []

_go_cc_aspect = aspect(
implementation = _go_cc_aspect_impl,
attr_aspects = ["cdeps", "deps", "embed"],
)

def _go_binary_impl(ctx):
"""go_binary_impl emits actions for compiling and linking a go executable."""
go = go_context(ctx)
Expand Down Expand Up @@ -179,10 +198,11 @@ def _go_binary_impl(ctx):
elif go.mode.link == LINKMODE_C_ARCHIVE:
cc_import_kwargs["static_library"] = executable
cc_import_kwargs["alwayslink"] = True
ccinfo = new_cc_import(go, **cc_import_kwargs)
ccinfo = cc_common.merge_cc_infos(
cc_infos = [ccinfo, source.cc_info],
)

cc_infos = [dep[CcInfo] for dep in ctx.attr.cdeps + ctx.attr.deps + ctx.attr.embed if CcInfo in dep]
cc_infos.append(new_cc_import(go, **cc_import_kwargs))

ccinfo = cc_common.merge_cc_infos(cc_infos = cc_infos)
providers.append(ccinfo)

return providers
Expand Down Expand Up @@ -211,6 +231,7 @@ _go_binary_kwargs = {
),
"deps": attr.label_list(
providers = [GoLibrary],
aspects = [_go_cc_aspect],
doc = """List of Go libraries this package imports directly.
These may be `go_library` rules or compatible rules with the [GoLibrary] provider.
""",
Expand Down

0 comments on commit 90b4e1c

Please sign in to comment.