From f1685ce028b5b4efbdf43cdaf30b5dee54fb78c8 Mon Sep 17 00:00:00 2001 From: David Zbarsky Date: Wed, 7 Aug 2024 17:18:04 -0400 Subject: [PATCH] Use depset for SDK files --- extras/gomock.bzl | 8 +++----- go/private/actions/compilepkg.bzl | 29 ++++++++++++++-------------- go/private/actions/link.bzl | 7 +++---- go/private/actions/stdlib.bzl | 27 +++++++++++++------------- go/private/common.bzl | 10 ---------- go/private/context.bzl | 13 ++----------- go/private/providers.bzl | 8 ++++---- go/private/rules/binary.bzl | 10 ++++++++-- go/private/rules/go_bin_for_host.bzl | 5 ++++- go/private/rules/info.bzl | 2 +- go/private/rules/sdk.bzl | 8 ++++---- go/providers.rst | 8 ++++---- tests/core/stdlib/stdlib_files.bzl | 4 ++-- 13 files changed, 64 insertions(+), 75 deletions(-) diff --git a/extras/gomock.bzl b/extras/gomock.bzl index 20e06d2a4a..87058cb3e5 100644 --- a/extras/gomock.bzl +++ b/extras/gomock.bzl @@ -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, diff --git a/go/private/actions/compilepkg.bzl b/go/private/actions/compilepkg.bzl index cfb7aee697..8f31872e7d 100644 --- a/go/private/actions/compilepkg.bzl +++ b/go/private/actions/compilepkg.bzl @@ -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) @@ -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" @@ -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)) @@ -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, @@ -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) @@ -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, diff --git a/go/private/actions/link.bzl b/go/private/actions/link.bzl index f74232956e..0b12e640c4 100644 --- a/go/private/actions/link.bzl +++ b/go/private/actions/link.bzl @@ -19,7 +19,6 @@ load( load( "//go/private:common.bzl", "GO_TOOLCHAIN_LABEL", - "as_set", "count_group_matches", "has_shared_lib_extension", ) @@ -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) diff --git a/go/private/actions/stdlib.bzl b/go/private/actions/stdlib.bzl index 1a33c7544a..479d04aac7 100644 --- a/go/private/actions/stdlib.bzl +++ b/go/private/actions/stdlib.bzl @@ -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 @@ -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, @@ -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, @@ -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, ) diff --git a/go/private/common.bzl b/go/private/common.bzl index 7507811047..8f140c8a95 100644 --- a/go/private/common.bzl +++ b/go/private/common.bzl @@ -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): diff --git a/go/private/context.bzl b/go/private/context.bzl index c2c02effff..f841fac6f4 100644 --- a/go/private/context.bzl +++ b/go/private/context.bzl @@ -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) @@ -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", ())) @@ -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), @@ -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( diff --git a/go/private/providers.bzl b/go/private/providers.bzl index 16a106aa86..9c2b672aad 100644 --- a/go/private/providers.bzl +++ b/go/private/providers.bzl @@ -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", diff --git a/go/private/rules/binary.bzl b/go/private/rules/binary.bzl index 8bbe462268..5267c86add 100644 --- a/go/private/rules/binary.bzl +++ b/go/private/rules/binary.bzl @@ -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", ) @@ -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", ) diff --git a/go/private/rules/go_bin_for_host.bzl b/go/private/rules/go_bin_for_host.bzl index 50ef8be4c8..475516c32d 100644 --- a/go/private/rules/go_bin_for_host.bzl +++ b/go/private/rules/go_bin_for_host.bzl @@ -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( diff --git a/go/private/rules/info.bzl b/go/private/rules/info.bzl index e2d1324c25..0711d499a6 100644 --- a/go/private/rules/info.bzl +++ b/go/private/rules/info.bzl @@ -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, diff --git a/go/private/rules/sdk.bzl b/go/private/rules/sdk.bzl index 899cd0a7fc..2784e844ea 100644 --- a/go/private/rules/sdk.bzl +++ b/go/private/rules/sdk.bzl @@ -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, )] diff --git a/go/providers.rst b/go/providers.rst index a2361ac198..894abba2a7 100644 --- a/go/providers.rst +++ b/go/providers.rst @@ -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. | @@ -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. | +--------------------------------+-----------------------------------------------------------------+ diff --git a/tests/core/stdlib/stdlib_files.bzl b/tests/core/stdlib/stdlib_files.bzl index 35d48f3331..29be3ca476 100644 --- a/tests/core/stdlib/stdlib_files.bzl +++ b/tests/core/stdlib/stdlib_files.bzl @@ -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, )]