Skip to content

Commit

Permalink
Cleanup GoArchiveData runfiles handling (#4024)
Browse files Browse the repository at this point in the history
**What type of PR is this?**
Starlark cleanup

**What does this PR do? Why is it needed?**
We don't need to make copies of the lists

**Which issues(s) does this PR fix?**

Fixes #

**Other notes for review**
  • Loading branch information
dzbarsky authored Aug 10, 2024
1 parent d01b20e commit 60f55c9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
6 changes: 2 additions & 4 deletions go/private/actions/archive.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,13 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d
out_nogo_validation = go.declare_file(go, name = source.library.name, ext = pre_ext + ".nogo")

direct = [get_archive(dep) for dep in source.deps]
runfiles = source.runfiles
data_files = runfiles.files

files = []
for a in direct:
files.append(a.runfiles)
if a.source.mode != go.mode:
fail("Archive mode does not match {} is {} expected {}".format(a.data.label, mode_string(a.source.mode), mode_string(go.mode)))
runfiles = runfiles.merge_all(files)
runfiles = source.runfiles.merge_all(files)

importmap = "main" if source.library.is_main else source.library.importmap
importpath, _ = effective_importpath_pkgpath(source.library)
Expand Down Expand Up @@ -189,7 +187,7 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d
file = out_lib,
export_file = out_export,
facts_file = out_facts,
data_files = as_tuple(data_files),
runfiles = source.runfiles,
_validation_output = out_nogo_validation,
_cgo_deps = as_tuple(cgo_deps),
)
Expand Down
2 changes: 1 addition & 1 deletion go/private/rules/test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ def _recompile_external_deps(go, external_source, internal_archive, library_labe
x_defs = dict(arc_data._x_defs),
deps = deps,
gc_goopts = as_list(arc_data._gc_goopts),
runfiles = go._ctx.runfiles(files = arc_data.data_files),
runfiles = arc_data.runfiles,
cgo = arc_data._cgo,
cdeps = as_list(arc_data._cdeps),
cppopts = as_list(arc_data._cppopts),
Expand Down
28 changes: 18 additions & 10 deletions go/private/tools/path.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,13 @@ def _go_path_impl(ctx):
importpath = importpath,
dir = "src/" + pkgpath,
srcs = as_list(archive.orig_srcs),
data = as_list(archive.data_files),
runfiles = archive.runfiles,
embedsrcs = as_list(archive._embedsrcs),
pkgs = {mode: archive.file},
)
if pkgpath in pkg_map:
_merge_pkg(pkg_map[pkgpath], pkg)
else:
pkg_map[pkgpath] = pkg
pkg = _merge_pkg(pkg_map[pkgpath], pkg)
pkg_map[pkgpath] = pkg

# Build a manifest file that includes all files to copy/link/zip.
inputs = []
Expand Down Expand Up @@ -95,7 +94,7 @@ def _go_path_impl(ctx):
_add_manifest_entry(manifest_entries, manifest_entry_map, inputs, f, dst)
if ctx.attr.include_data:
for pkg in pkg_map.values():
for f in pkg.data:
for f in pkg.runfiles.files.to_list():
parts = f.path.split("/")
if "testdata" in parts:
i = parts.index("testdata")
Expand Down Expand Up @@ -261,12 +260,21 @@ go_path = rule(

def _merge_pkg(x, y):
x_srcs = {f.path: None for f in x.srcs}
x_data = {f.path: None for f in x.data}
x_embedsrcs = {f.path: None for f in x.embedsrcs}
x.srcs.extend([f for f in y.srcs if f.path not in x_srcs])
x.data.extend([f for f in y.data if f.path not in x_data])
x.embedsrcs.extend([f for f in y.embedsrcs if f.path not in x_embedsrcs])
x.pkgs.update(y.pkgs)

# Not all bazel versions support `dict1 | dict2` yet.
pkgs = dict()
pkgs.update(x.pkgs)
pkgs.update(y.pkgs)

return struct(
importpath = x.importpath,
dir = x.dir,
srcs = x.srcs + [f for f in y.srcs if f.path not in x_srcs],
runfiles = x.runfiles.merge(y.runfiles),
embedsrcs = x.embedsrcs + [f for f in y.embedsrcs if f.path not in x_embedsrcs],
pkgs = pkgs,
)

def _add_manifest_entry(entries, entry_map, inputs, src, dst):
if dst in entry_map:
Expand Down
2 changes: 1 addition & 1 deletion go/providers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ rule. Instead, it's referenced in the ``data`` field of GoArchive_.
+--------------------------------+-----------------------------------------------------------------+
| The unmodified sources provided to the rule, including .go, .s, .h, .c files. |
+--------------------------------+-----------------------------------------------------------------+
| :param:`data_files` | :type:`tuple of File` |
| :param:`runfiles` | :type:`runfiles` |
+--------------------------------+-----------------------------------------------------------------+
| Data files that should be available at runtime to binaries and tests built |
| from this archive. |
Expand Down

0 comments on commit 60f55c9

Please sign in to comment.