Skip to content

Commit

Permalink
Remove some fields from Go context
Browse files Browse the repository at this point in the history
  • Loading branch information
dzbarsky authored and fmeum committed Aug 15, 2024
1 parent a32f3e1 commit 3801616
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 30 deletions.
8 changes: 5 additions & 3 deletions extras/gomock.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 = """
Expand All @@ -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,
Expand Down
12 changes: 7 additions & 5 deletions go/private/actions/compilepkg.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions go/private/actions/link.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}") +
Expand Down
26 changes: 7 additions & 19 deletions go/private/context.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -171,21 +167,22 @@ 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.
if use_path_mapping:
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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -552,25 +542,23 @@ 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,
library_to_source = _library_to_source,
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,
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 @@ -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
Expand Down

0 comments on commit 3801616

Please sign in to comment.