Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 31 additions & 27 deletions go/private/context.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ load(
"get_source",
)

CPP_TOOLCHAIN_TYPE = Label("@bazel_tools//tools/cpp:toolchain_type")
CGO_ATTRS = {
"_cc_toolchain": attr.label(default = "@bazel_tools//tools/cpp:optional_current_cc_toolchain"),
"_xcode_config": attr.label(default = "@bazel_tools//tools/osx:current_xcode_config"),
}
CGO_TOOLCHAINS = [
# In pure mode, a C++ toolchain isn't needed when transitioning.
# But if we declare a mandatory toolchain dependency here, a cross-compiling C++ toolchain is required at toolchain resolution time.
# So we make this toolchain dependency optional, so that it's only attempted to be looked up if it's actually needed.
config_common.toolchain_type(CPP_TOOLCHAIN_TYPE, mandatory = False),
]
CGO_FRAGMENTS = ["apple", "cpp"]

# cgo requires a gcc/clang style compiler.
# We use a denylist instead of an allowlist:
# - Bazel's auto-detected toolchains used to set the compiler name to "compiler"
Expand Down Expand Up @@ -479,27 +492,28 @@ def go_context(
if go_context_data == None:
if hasattr(attr, "_go_context_data"):
go_context_data = attr._go_context_data
if CgoContextInfo in go_context_data:
cgo_context_info = go_context_data[CgoContextInfo]
go_config_info = go_context_data[GoConfigInfo]
stdlib = go_context_data[GoStdLib]
go_context_info = go_context_data[GoContextInfo]
if getattr(attr, "_cgo_context_data", None) and CgoContextInfo in attr._cgo_context_data:
cgo_context_info = attr._cgo_context_data[CgoContextInfo]
if getattr(attr, "cgo_context_data", None) and CgoContextInfo in attr.cgo_context_data:
cgo_context_info = attr.cgo_context_data[CgoContextInfo]
if hasattr(attr, "_go_config"):
go_config_info = attr._go_config[GoConfigInfo]
if hasattr(attr, "_stdlib"):
stdlib = attr._stdlib[GoStdLib]
else:
if CgoContextInfo in go_context_data:
cgo_context_info = go_context_data[CgoContextInfo]
go_config_info = go_context_data[GoConfigInfo]
stdlib = go_context_data[GoStdLib]
go_context_info = go_context_data[GoContextInfo]

if goos == "auto" and goarch == "auto" and cgo_context_info:
if getattr(attr, "_cc_toolchain", None) and CPP_TOOLCHAIN_TYPE in ctx.toolchains:
cgo_context_info = cgo_context_data_impl(ctx)
elif go_context_data and CgoContextInfo in go_context_data:
cgo_context_info = go_context_data[CgoContextInfo]
elif getattr(attr, "_cgo_context_data", None) and CgoContextInfo in attr._cgo_context_data:
cgo_context_info = attr._cgo_context_data[CgoContextInfo]
elif getattr(attr, "cgo_context_data", None) and CgoContextInfo in attr.cgo_context_data:
cgo_context_info = attr.cgo_context_data[CgoContextInfo]

if goos == "auto" and goarch == "auto" and cgo_context_info and (go_config_info == None or not go_config_info.pure):
# Fast-path to reuse the GoConfigInfo as-is
mode = go_config_info or default_go_config_info
else:
Expand Down Expand Up @@ -701,14 +715,14 @@ go_context_data = rule(
cfg = request_nogo_transition,
)

def _cgo_context_data_impl(ctx):
def cgo_context_data_impl(ctx):
# TODO(jayconrod): find a way to get a list of files that comprise the
# toolchain (to be inputs into actions that need it).
# ctx.files._cc_toolchain won't work when cc toolchain resolution
# is switched on.
cc_toolchain = find_cpp_toolchain(ctx, mandatory = False)
if not cc_toolchain or cc_toolchain.compiler in _UNSUPPORTED_C_COMPILERS:
return []
return None

feature_configuration = cc_common.configure_features(
ctx = ctx,
Expand Down Expand Up @@ -897,7 +911,7 @@ def _cgo_context_data_impl(ctx):
paths.append("/usr/bin")
env["PATH"] = ctx.configuration.host_path_separator.join(paths)

return [CgoContextInfo(
return CgoContextInfo(
cc_toolchain_files = cc_toolchain.all_files,
env = env,
cgo_tools = struct(
Expand All @@ -915,23 +929,13 @@ def _cgo_context_data_impl(ctx):
ld_dynamic_lib_options = ld_dynamic_lib_options,
ar_path = cc_toolchain.ar_executable,
),
)]
)

cgo_context_data = rule(
implementation = _cgo_context_data_impl,
attrs = {
"_cc_toolchain": attr.label(default = "@bazel_tools//tools/cpp:optional_current_cc_toolchain"),
"_xcode_config": attr.label(
default = "@bazel_tools//tools/osx:current_xcode_config",
),
},
toolchains = [
# In pure mode, a C++ toolchain isn't needed when transitioning.
# But if we declare a mandatory toolchain dependency here, a cross-compiling C++ toolchain is required at toolchain resolution time.
# So we make this toolchain dependency optional, so that it's only attempted to be looked up if it's actually needed.
config_common.toolchain_type("@bazel_tools//tools/cpp:toolchain_type", mandatory = False),
],
fragments = ["apple", "cpp"],
implementation = cgo_context_data_impl,
attrs = CGO_ATTRS,
toolchains = CGO_TOOLCHAINS,
fragments = CGO_FRAGMENTS,
doc = """Collects information about the C/C++ toolchain. The C/C++ toolchain
is needed to build cgo code, but is generally optional. Rules can't have
optional toolchains, so instead, we have an optional dependency on this
Expand Down
8 changes: 6 additions & 2 deletions go/private/rules/binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ load(
)
load(
"//go/private:context.bzl",
"CGO_ATTRS",
"CGO_FRAGMENTS",
"CGO_TOOLCHAINS",
"go_context",
"new_go_info",
)
Expand Down Expand Up @@ -453,8 +456,9 @@ def _go_binary_kwargs(go_cc_aspects = []):
"_allowlist_function_transition": attr.label(
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
),
},
"toolchains": [GO_TOOLCHAIN],
} | CGO_ATTRS,
"fragments": CGO_FRAGMENTS,
"toolchains": [GO_TOOLCHAIN] + CGO_TOOLCHAINS,
"doc": """This builds an executable from a set of source files,
which must all be in the `main` package. You can run the binary with
`bazel run`, or you can build it with `bazel build` and run it directly.<br><br>
Expand Down
13 changes: 9 additions & 4 deletions go/private/rules/library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ load(
)
load(
"//go/private:context.bzl",
"CGO_ATTRS",
"CGO_FRAGMENTS",
"CGO_TOOLCHAINS",
"go_context",
"new_go_info",
)
Expand Down Expand Up @@ -191,8 +194,9 @@ go_library = rule(
"_allowlist_function_transition": attr.label(
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
),
},
toolchains = [GO_TOOLCHAIN],
} | CGO_ATTRS,
fragments = CGO_FRAGMENTS,
toolchains = [GO_TOOLCHAIN] + CGO_TOOLCHAINS,
doc = """This builds a Go library from a set of source files that are all part of
the same package.<br><br>
***Note:*** For targets generated by Gazelle, `name` is typically the last component of the path,
Expand Down Expand Up @@ -233,8 +237,9 @@ go_tool_library = rule(
"_go_config": attr.label(default = "//:go_config"),
"_cgo_context_data": attr.label(default = "//:cgo_context_data_proxy"),
"_stdlib": attr.label(default = "//:stdlib"),
},
toolchains = [GO_TOOLCHAIN],
} | CGO_ATTRS,
fragments = CGO_FRAGMENTS,
toolchains = [GO_TOOLCHAIN] + CGO_TOOLCHAINS,
)
# This is used instead of `go_library` for dependencies of the `nogo` rule and
# packages that are depended on implicitly by code generated within the Go rules.
Expand Down
8 changes: 6 additions & 2 deletions go/private/rules/test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ load(
)
load(
"//go/private:context.bzl",
"CGO_ATTRS",
"CGO_FRAGMENTS",
"CGO_TOOLCHAINS",
"go_context",
"new_go_info",
)
Expand Down Expand Up @@ -482,10 +485,11 @@ _go_test_kwargs = {
"_allowlist_function_transition": attr.label(
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
),
},
} | CGO_ATTRS,
"executable": True,
"test": True,
"toolchains": [GO_TOOLCHAIN],
"fragments": CGO_FRAGMENTS,
"toolchains": [GO_TOOLCHAIN] + CGO_TOOLCHAINS,
"doc": """This builds a set of tests that can be run with `bazel test`.<br><br>
To run all tests in the workspace, and print output on failure (the
equivalent of `go test ./...`), run<br>
Expand Down
1 change: 1 addition & 0 deletions tests/core/go_binary/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ go_binary(
],
goarch = "amd64",
goos = "plan9",
pure = "on",
)

many_deps(name = "many_deps")
Expand Down
Loading