From 380161604a7a2f042aa6ce0220a031db96aa6ce9 Mon Sep 17 00:00:00 2001 From: David Zbarsky Date: Mon, 12 Aug 2024 15:39:08 -0400 Subject: [PATCH] Remove some fields from Go context --- extras/gomock.bzl | 8 +++++--- go/private/actions/compilepkg.bzl | 12 +++++++----- go/private/actions/link.bzl | 4 ++-- go/private/context.bzl | 26 +++++++------------------- go/private/rules/test.bzl | 2 +- 5 files changed, 22 insertions(+), 30 deletions(-) diff --git a/extras/gomock.bzl b/extras/gomock.bzl index 87058cb3e..a571f2941 100644 --- a/extras/gomock.bzl +++ b/extras/gomock.bzl @@ -73,8 +73,10 @@ def _gomock_source_impl(ctx): needed_files.append(aux) args += ["-aux_files", ",".join(aux_files)] + sdk = go_ctx.sdk + inputs_direct = needed_files + [source] - inputs_transitive = [go_ctx.sdk.tools, go_ctx.sdk.headers, go_ctx.sdk.srcs] + inputs_transitive = [sdk.tools, sdk.headers, 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. @@ -83,7 +85,7 @@ def _gomock_source_impl(ctx): inputs = depset(inputs_direct, transitive = inputs_transitive), tools = [ ctx.file.mockgen_tool, - go_ctx.go, + sdk.go, ], toolchain = GO_TOOLCHAIN_LABEL, command = """ @@ -92,7 +94,7 @@ def _gomock_source_impl(ctx): {cmd} {args} > {out} """.format( gopath = gopath, - goroot = go_ctx.sdk.root_file.dirname, + goroot = sdk.root_file.dirname, cmd = "$(pwd)/" + ctx.file.mockgen_tool.path, args = " ".join(args), out = ctx.outputs.out.path, diff --git a/go/private/actions/compilepkg.bzl b/go/private/actions/compilepkg.bzl index 8ac0e5535..9298ada21 100644 --- a/go/private/actions/compilepkg.bzl +++ b/go/private/actions/compilepkg.bzl @@ -122,7 +122,7 @@ def emit_compilepkg( else: cover_mode = "set" args.add("-cover_mode", cover_mode) - args.add("-cover_format", go.cover_format) + args.add("-cover_format", go.mode.cover_format) args.add_all(cover, before_each = "-cover") args.add_all(archives, before_each = "-arc", map_each = _archive) if recompile_internal_deps: @@ -133,7 +133,7 @@ def emit_compilepkg( args.add("-importpath", go.label.name) if importmap: args.add("-p", importmap) - args.add("-package_list", go.package_list) + args.add("-package_list", sdk.package_list) args.add("-lo", out_lib) args.add("-o", out_export) @@ -237,10 +237,12 @@ def _run_nogo( out_validation, nogo): """Runs nogo on Go source files, including those generated by cgo.""" - inputs_direct = (sources + [nogo, go.package_list] + + sdk = go.sdk + + inputs_direct = (sources + [nogo, sdk.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] + inputs_transitive = [sdk.tools, sdk.headers, go.stdlib.libs] outputs = [out_facts, out_log] args = go.builder_args(go, "nogo", use_path_mapping = True) @@ -259,7 +261,7 @@ def _run_nogo( args.add("-importpath", go.label.name) if importmap: args.add("-p", importmap) - args.add("-package_list", go.package_list) + args.add("-package_list", sdk.package_list) args.add_all(archives, before_each = "-facts", map_each = _facts) args.add("-out_facts", out_facts) diff --git a/go/private/actions/link.bzl b/go/private/actions/link.bzl index 0b12e640c..55e09b787 100644 --- a/go/private/actions/link.bzl +++ b/go/private/actions/link.bzl @@ -136,7 +136,7 @@ def emit_link( not any([arc.importmap == go.coverdata.data.importmap for arc in arcs])): arcs.append(go.coverdata.data) builder_args.add_all(arcs, before_each = "-arc", map_each = _format_archive) - builder_args.add("-package_list", go.package_list) + builder_args.add("-package_list", go.sdk.package_list) # Build a list of rpaths for dynamic libraries we need to find. # rpaths are relative paths from the binary to directories where libraries @@ -156,7 +156,7 @@ def emit_link( stamp_x_defs_stable = False for k, v in archive.x_defs.items(): builder_args.add("-X", "%s=%s" % (k, v)) - if go.stamp: + if go.mode.stamp: stable_vars_count = (count_group_matches(v, "{STABLE_", "}") + v.count("{BUILD_EMBED_LABEL}") + v.count("{BUILD_USER}") + diff --git a/go/private/context.bzl b/go/private/context.bzl index de0522bf7..c1e9e30b6 100644 --- a/go/private/context.bzl +++ b/go/private/context.bzl @@ -158,10 +158,6 @@ def _declare_file(go, path = "", ext = "", name = ""): def _declare_directory(go, path = "", ext = "", name = ""): return go.actions.declare_directory(_child_name(go, path, ext, name)) -def _new_args(go): - # TODO(jayconrod): print warning. - return go.builder_args(go) - def _dirname(file): return file.dirname @@ -171,7 +167,8 @@ def _builder_args(go, command = None, use_path_mapping = False): args.set_param_file_format("shell") if command: args.add(command) - args.add("-sdk", go.sdk.root_file.dirname) + sdk_root_file = go.sdk.root_file + args.add("-sdk", sdk_root_file.dirname) # Path mapping can't map the values of environment variables, so we need to pass GOROOT to the # action via an argument instead. @@ -179,13 +176,13 @@ def _builder_args(go, command = None, use_path_mapping = False): if go.stdlib: goroot_file = go.stdlib.root_file else: - goroot_file = go.sdk_root + goroot_file = sdk_root_file # Use a file rather than goroot as the latter is just a string and thus # not subject to path mapping. args.add_all("-goroot", [goroot_file], map_each = _dirname, expand_directories = False) args.add("-installsuffix", installsuffix(go.mode)) - args.add_joined("-tags", go.tags, join_with = ",") + args.add_joined("-tags", go.mode.tags, join_with = ",") return args def _tool_args(go): @@ -468,8 +465,6 @@ def go_context(ctx, attr = None): stdlib = _flatten_possibly_transitioned_attr(attr._stdlib)[GoStdLib] mode = get_mode(ctx, toolchain, cgo_context_info, go_config_info) - tags = mode.tags - binary = toolchain.sdk.go if stdlib: goroot = stdlib.root_file.dirname @@ -533,14 +528,9 @@ def go_context(ctx, attr = None): toolchain = toolchain, sdk = toolchain.sdk, mode = mode, - root = goroot, - go = binary, stdlib = stdlib, - sdk_root = toolchain.sdk.root_file, - sdk_tools = toolchain.sdk.tools, actions = ctx.actions, cc_toolchain_files = cc_toolchain_files, - package_list = toolchain.sdk.package_list, importpath = importpath, importmap = importmap, importpath_aliases = importpath_aliases, @@ -552,18 +542,13 @@ def go_context(ctx, attr = None): coverage_instrumented = ctx.coverage_instrumented(), env = env, env_for_path_mapping = env_for_path_mapping, - tags = tags, - stamp = mode.stamp, label = ctx.label, - cover_format = mode.cover_format, - pgoprofile = mode.pgoprofile, # Action generators archive = toolchain.actions.archive, binary = toolchain.actions.binary, link = toolchain.actions.link, # Helpers - args = _new_args, # deprecated builder_args = _builder_args, tool_args = _tool_args, new_library = _new_library, @@ -571,6 +556,9 @@ def go_context(ctx, attr = None): declare_file = _declare_file, declare_directory = _declare_directory, + # TODO(zbarsky): package_list only used in gazelle, this can be removed + package_list = toolchain.sdk.package_list, + # Private # TODO: All uses of this should be removed _ctx = ctx, diff --git a/go/private/rules/test.bzl b/go/private/rules/test.bzl index c6cba0586..002a56ff1 100644 --- a/go/private/rules/test.bzl +++ b/go/private/rules/test.bzl @@ -100,7 +100,7 @@ def _go_test_impl(ctx): arguments.add("-cover_mode", "atomic") else: arguments.add("-cover_mode", "set") - arguments.add("-cover_format", go.cover_format) + arguments.add("-cover_format", go.mode.cover_format) arguments.add( # the l is the alias for the package under test, the l_test must be the # same with the test suffix