Skip to content

Commit

Permalink
Use depset for SDK files
Browse files Browse the repository at this point in the history
  • Loading branch information
dzbarsky committed Aug 10, 2024
1 parent 64759ee commit f1685ce
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 75 deletions.
8 changes: 3 additions & 5 deletions extras/gomock.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,14 @@ def _gomock_source_impl(ctx):
needed_files.append(aux)
args += ["-aux_files", ",".join(aux_files)]

inputs = (
needed_files +
go_ctx.sdk.headers + go_ctx.sdk.srcs + go_ctx.sdk.tools
) + [source]
inputs_direct = needed_files + [source]
inputs_transitive = [go_ctx.sdk.tools, go_ctx.sdk.headers, go_ctx.sdk.srcs]

# 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 = [ctx.outputs.out],
inputs = inputs,
inputs = depset(inputs_direct, transitive = inputs_transitive),
tools = [
ctx.file.mockgen_tool,
go_ctx.go,
Expand Down
29 changes: 15 additions & 14 deletions go/private/actions/compilepkg.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,10 @@ def emit_compilepkg(
if bool(nogo) != bool(out_nogo_validation):
fail("nogo must be specified if and only if out_nogo_validation is specified")

inputs = (sources + embedsrcs + [go.package_list] +
[archive.data.export_file for archive in archives] +
go.sdk.tools + go.sdk.headers + go.stdlib.libs)
sdk = go.sdk
inputs_direct = (sources + embedsrcs + [sdk.package_list] +
[archive.data.export_file for archive in archives])
inputs_transitive = [sdk.headers, sdk.tools, go.stdlib.libs]
outputs = [out_lib, out_export]

args = go.builder_args(go, "compilepkg", use_path_mapping = True)
Expand All @@ -114,7 +115,7 @@ def emit_compilepkg(
cover_archive = None
if cover and go.coverdata:
cover_archive = go.coverdata
inputs.append(cover_archive.data.export_file)
inputs_direct.append(cover_archive.data.export_file)
args.add("-arc", _archive(cover_archive))
if go.mode.race:
cover_mode = "atomic"
Expand Down Expand Up @@ -172,8 +173,8 @@ def emit_compilepkg(
cgo_go_srcs_for_nogo = go.declare_directory(go, path = out_lib.basename + ".cgo")
outputs.append(cgo_go_srcs_for_nogo)
args.add("-cgo_go_srcs", cgo_go_srcs_for_nogo.path)
inputs.extend(cgo_inputs.to_list()) # OPT: don't expand depset
inputs.extend(go.cc_toolchain_files)
inputs_transitive.append(cgo_inputs)
inputs_transitive.append(go.cc_toolchain_files)
env["CC"] = go.cgo_tools.c_compiler_path
if cppopts:
args.add("-cppflags", quote_opts(cppopts))
Expand All @@ -190,10 +191,10 @@ def emit_compilepkg(

if go.mode.pgoprofile:
args.add("-pgoprofile", go.mode.pgoprofile)
inputs.append(go.mode.pgoprofile)
inputs_direct.append(go.mode.pgoprofile)

go.actions.run(
inputs = inputs,
inputs = depset(inputs_direct, transitive = inputs_transitive),
outputs = outputs,
mnemonic = "GoCompilePkgExternal" if is_external_pkg else "GoCompilePkg",
executable = go.toolchain._builder,
Expand Down Expand Up @@ -234,16 +235,16 @@ def _run_nogo(
out_validation,
nogo):
"""Runs nogo on Go source files, including those generated by cgo."""
inputs = (sources + [nogo, go.package_list] +
[archive.data.facts_file for archive in archives if archive.data.facts_file] +
[archive.data.export_file for archive in archives] +
go.sdk.tools + go.sdk.headers + go.stdlib.libs)
inputs_direct = (sources + [nogo, go.package_list] +
[archive.data.facts_file for archive in archives if archive.data.facts_file] +
[archive.data.export_file for archive in archives])
inputs_transitive = [go.sdk.tools, go.sdk.headers, go.stdlib.libs]
outputs = [out_facts, out_log]

args = go.builder_args(go, "nogo", use_path_mapping = True)
args.add_all(sources, before_each = "-src")
if cgo_go_srcs:
inputs.append(cgo_go_srcs)
inputs_direct.append(cgo_go_srcs)
args.add_all([cgo_go_srcs], before_each = "-src")
if cover_mode:
args.add("-cover_mode", cover_mode)
Expand Down Expand Up @@ -271,7 +272,7 @@ def _run_nogo(
# analyzers with --sandbox_debug. Users can set debug = True on the nogo target to have it fail
# on findings to get the same debugging experience as with other failures.
go.actions.run(
inputs = inputs,
inputs = depset(inputs_direct, transitive = inputs_transitive),
outputs = outputs,
mnemonic = "RunNogo",
executable = go.toolchain._builder,
Expand Down
7 changes: 3 additions & 4 deletions go/private/actions/link.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ load(
load(
"//go/private:common.bzl",
"GO_TOOLCHAIN_LABEL",
"as_set",
"count_group_matches",
"has_shared_lib_extension",
)
Expand Down Expand Up @@ -201,9 +200,9 @@ def emit_link(
inputs_transitive = [
archive.libs,
archive.cgo_deps,
as_set(go.cc_toolchain_files),
as_set(go.sdk.tools),
as_set(go.stdlib.libs),
go.cc_toolchain_files,
go.sdk.tools,
go.stdlib.libs,
]
inputs = depset(direct = inputs_direct, transitive = inputs_transitive)

Expand Down
27 changes: 14 additions & 13 deletions go/private/actions/stdlib.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def _should_use_sdk_stdlib(go):
if version and version[0] <= 1 and version[1] <= 19 and go.sdk.experiments:
# The precompiled stdlib shipped with 1.19 or below doesn't have experiments
return False
return (go.sdk.libs and # go.sdk.libs is non-empty if sdk ships with precompiled .a files
return (go.sdk.libs.to_list() and # go.sdk.libs is non-empty if sdk ships with precompiled .a files
go.mode.goos == go.sdk.goos and
go.mode.goarch == go.sdk.goarch and
not go.mode.race and # TODO(jayconrod): use precompiled race
Expand All @@ -66,19 +66,22 @@ def _should_use_sdk_stdlib(go):
go.mode.link == LINKMODE_NORMAL)

def _build_stdlib_list_json(go):
sdk = go.sdk

out = go.declare_file(go, "stdlib.pkg.json")
cache_dir = go.declare_directory(go, "gocache")
args = go.builder_args(go, "stdliblist")
args.add("-sdk", go.sdk.root_file.dirname)
args.add("-sdk", sdk.root_file.dirname)
args.add("-out", out)
args.add("-cache", cache_dir.path)

inputs = go.sdk_files
inputs_direct = [sdk.go]
inputs_transitive = [sdk.headers, sdk.srcs, sdk.libs, sdk.tools]
if not go.mode.pure:
inputs += go.cc_toolchain_files
inputs_transitive.append(go.cc_toolchain_files)

go.actions.run(
inputs = inputs,
inputs = depset(inputs_direct, transitive = inputs_transitive),
outputs = [out, cache_dir],
mnemonic = "GoStdlibList",
executable = go.toolchain._builder,
Expand Down Expand Up @@ -140,19 +143,17 @@ def _build_stdlib(go):

args.add("-gcflags", quote_opts(go.mode.gc_goopts))

inputs = (go.sdk.srcs +
go.sdk.headers +
go.sdk.tools +
[go.sdk.go, go.sdk.package_list, go.sdk.root_file] +
go.cc_toolchain_files)
sdk = go.sdk
inputs_direct = [sdk.go, sdk.package_list, sdk.root_file]
inputs_transitive = [sdk.headers, sdk.srcs, sdk.tools, go.cc_toolchain_files]

if go.mode.pgoprofile:
args.add("-pgoprofile", go.mode.pgoprofile)
inputs.append(go.mode.pgoprofile)
inputs_direct.append(go.mode.pgoprofile)

outputs = [pkg]
go.actions.run(
inputs = inputs,
inputs = depset(direct = inputs_direct, transitive = inputs_transitive),
outputs = outputs,
mnemonic = "GoStdlib",
executable = go.toolchain._builder,
Expand All @@ -163,6 +164,6 @@ def _build_stdlib(go):
)
return GoStdLib(
_list_json = _build_stdlib_list_json(go),
libs = [pkg],
libs = depset([pkg]),
root_file = pkg,
)
10 changes: 0 additions & 10 deletions go/private/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -210,16 +210,6 @@ def as_tuple(v):
return tuple(v.to_list())
fail("as_tuple failed on {}".format(v))

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

_STRUCT_TYPE = type(struct())

def is_struct(v):
Expand Down
13 changes: 2 additions & 11 deletions go/private/context.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ def go_context(ctx, attr = None):
env["GOARM"] = mode.arm

if not cgo_context_info:
cc_toolchain_files = []
cc_toolchain_files = depset()
cgo_tools = None
else:
env.update(cgo_context_info.env)
Expand Down Expand Up @@ -558,14 +558,6 @@ def go_context(ctx, attr = None):
paths.append("/usr/bin")
env["PATH"] = ctx.configuration.host_path_separator.join(paths)

# TODO(jayconrod): remove this. It's way too broad. Everything should
# depend on more specific lists.
sdk_files = ([toolchain.sdk.go] +
toolchain.sdk.srcs +
toolchain.sdk.headers +
toolchain.sdk.libs +
toolchain.sdk.tools)

_check_importpaths(ctx)
importpath, importmap, pathtype = _infer_importpath(ctx, attr)
importpath_aliases = tuple(getattr(attr, "importpath_aliases", ()))
Expand All @@ -579,7 +571,6 @@ def go_context(ctx, attr = None):
go = binary,
stdlib = stdlib,
sdk_root = toolchain.sdk.root_file,
sdk_files = sdk_files,
sdk_tools = toolchain.sdk.tools,
actions = ctx.actions,
exe_extension = goos_to_extension(mode.goos),
Expand Down Expand Up @@ -846,7 +837,7 @@ def _cgo_context_data_impl(ctx):
)

return [CgoContextInfo(
cc_toolchain_files = cc_toolchain.all_files.to_list(),
cc_toolchain_files = cc_toolchain.all_files,
tags = tags,
env = env,
cgo_tools = struct(
Expand Down
8 changes: 4 additions & 4 deletions go/private/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ GoSDK = provider(
"goarch": "The host architecture the SDK was built for.",
"experiments": "Go experiments to enable via GOEXPERIMENT.",
"root_file": "A file in the SDK root directory",
"libs": ("List of pre-compiled .a files for the standard library " +
"libs": ("Depset of pre-compiled .a files for the standard library " +
"built for the execution platform."),
"headers": ("List of .h files from pkg/include that may be included " +
"headers": ("Depset of .h files from pkg/include that may be included " +
"in assembly sources."),
"srcs": ("List of source files for importable packages in the " +
"srcs": ("Depset of source files for importable packages in the " +
"standard library. Internal, vendored, and tool packages " +
"may not be included."),
"package_list": ("A file containing a list of importable packages " +
"in the standard library."),
"tools": ("List of executable files in the SDK built for " +
"tools": ("Depset of executable files in the SDK built for " +
"the execution platform, excluding the go binary file"),
"go": "The go binary file",
"version": "The Go SDK version",
Expand Down
10 changes: 8 additions & 2 deletions go/private/rules/binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,10 @@ exit /b %GO_EXIT_CODE%
)
ctx.actions.run(
executable = bat,
inputs = sdk.headers + sdk.tools + sdk.srcs + ctx.files.srcs + [sdk.go],
inputs = depset(
ctx.files.srcs + [sdk.go],
transitive = [sdk.headers, sdk.srcs, sdk.tools],
),
outputs = [out, gotmp],
mnemonic = "GoToolchainBinaryBuild",
)
Expand All @@ -479,7 +482,10 @@ exit /b %GO_EXIT_CODE%
)
ctx.actions.run_shell(
command = cmd,
inputs = sdk.headers + sdk.tools + sdk.srcs + sdk.libs + ctx.files.srcs + [sdk.go],
inputs = depset(
ctx.files.srcs + [sdk.go],
transitive = [sdk.headers, sdk.srcs, sdk.libs, sdk.tools],
),
outputs = [out],
mnemonic = "GoToolchainBinaryBuild",
)
Expand Down
5 changes: 4 additions & 1 deletion go/private/rules/go_bin_for_host.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ def _go_bin_for_host_impl(ctx):
_ensure_target_cfg(ctx)

sdk = ctx.toolchains[GO_TOOLCHAIN].sdk
sdk_files = ctx.runfiles([sdk.go] + sdk.headers + sdk.libs + sdk.srcs + sdk.tools)
sdk_files = ctx.runfiles(
[sdk.go],
transitive_files = depset(transitive = [sdk.headers, sdk.srcs, sdk.libs, sdk.tools]),
)

return [
DefaultInfo(
Expand Down
2 changes: 1 addition & 1 deletion go/private/rules/info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def _go_info_impl(ctx):
args = go.builder_args(go)
args.add("-out", report)
go.actions.run(
inputs = go.sdk_files,
inputs = depset([go.sdk.go], transitive = [go.sdk.tools]),
outputs = [report],
mnemonic = "GoInfo",
executable = ctx.executable._go_info,
Expand Down
8 changes: 4 additions & 4 deletions go/private/rules/sdk.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ def _go_sdk_impl(ctx):
experiments = ctx.attr.experiments,
root_file = ctx.file.root_file,
package_list = package_list,
libs = ctx.files.libs,
headers = ctx.files.headers,
srcs = ctx.files.srcs,
tools = ctx.files.tools,
libs = depset(ctx.files.libs),
headers = depset(ctx.files.headers),
srcs = depset(ctx.files.srcs),
tools = depset(ctx.files.tools),
go = ctx.executable.go,
version = ctx.attr.version,
)]
Expand Down
8 changes: 4 additions & 4 deletions go/providers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -397,16 +397,16 @@ GoSDK
+--------------------------------+-----------------------------------------------------------------+
| A file in the SDK root directory. Used to determine ``GOROOT``. |
+--------------------------------+-----------------------------------------------------------------+
| :param:`libs` | :type:`list of File` |
| :param:`libs` | :type:`depset of File` |
+--------------------------------+-----------------------------------------------------------------+
| Pre-compiled .a files for the standard library, built for the |
| execution platform. |
+--------------------------------+-----------------------------------------------------------------+
| :param:`headers` | :type:`list of File` |
| :param:`headers` | :type:`depset of File` |
+--------------------------------+-----------------------------------------------------------------+
| .h files from pkg/include that may be included in assembly sources. |
+--------------------------------+-----------------------------------------------------------------+
| :param:`srcs` | :type:`list of File` |
| :param:`srcs` | :type:`depset of File` |
+--------------------------------+-----------------------------------------------------------------+
| Source files for importable packages in the standard library. |
| Internal, vendored, and tool packages might not be included. |
Expand All @@ -415,7 +415,7 @@ GoSDK
+--------------------------------+-----------------------------------------------------------------+
| A file containing a list of importable packages in the standard library. |
+--------------------------------+-----------------------------------------------------------------+
| :param:`tools` | :type:`list of File` |
| :param:`tools` | :type:`depset of File` |
+--------------------------------+-----------------------------------------------------------------+
| Executable files from pkg/tool built for the execution platform. |
+--------------------------------+-----------------------------------------------------------------+
Expand Down
4 changes: 2 additions & 2 deletions tests/core/stdlib/stdlib_files.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ def _stdlib_files_impl(ctx):
# ctx.attr._stdlib is a list of Target.
stdlib = ctx.attr._stdlib[0][GoStdLib]
libs = stdlib.libs
runfiles = ctx.runfiles(files = libs)
runfiles = ctx.runfiles(transitive_files = libs)
return [DefaultInfo(
files = depset(libs + [stdlib._list_json]),
files = depset([stdlib._list_json], transitive = [libs]),
runfiles = runfiles,
)]

Expand Down

0 comments on commit f1685ce

Please sign in to comment.