cabal-buck2-0.6.0.0: buck2/prelude/go/go_binary.bzl
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is dual-licensed under either the MIT license found in the
# LICENSE-MIT file in the root directory of this source tree or the Apache
# License, Version 2.0 found in the LICENSE-APACHE file in the root directory
# of this source tree. You may select, at your option, one of the
# above-listed licenses.
load("@prelude//:artifacts.bzl", "single_artifact")
load("@prelude//dist:dist_info.bzl", "DistInfo")
load(
"@prelude//linking:link_info.bzl",
"LinkStyle",
)
load(
"@prelude//utils:utils.bzl",
"from_named_set",
"map_val",
"value_or",
)
load(":cgo_builder.bzl", "get_cgo_build_context")
load(":compile.bzl", "GoTestInfo")
load(":coverage.bzl", "GoCoverageMode")
load(":link.bzl", "GoBuildMode", "link")
load(":package_builder.bzl", "GoBuildConfig", "GoSourceInputs", "declare_package_build")
load(":packages.bzl", "go_attr_pkg_name")
load(":toolchain.bzl", "evaluate_cgo_enabled")
def go_binary_impl(ctx: AnalysisContext) -> list[Provider]:
pkg_import_path = go_attr_pkg_name(ctx)
cgo_enabled = evaluate_cgo_enabled(ctx.attrs._cgo_enabled)
coverage_mode = GoCoverageMode(ctx.attrs._coverage_mode) if ctx.attrs._coverage_mode else None
cgo_build_context = get_cgo_build_context(ctx)
lib, pkg_info, _ = declare_package_build(
ctx = ctx,
pkg_import_path = pkg_import_path,
main = True,
sources = GoSourceInputs(
srcs = ctx.attrs.srcs + ctx.attrs.headers,
embed_srcs = from_named_set(ctx.attrs.embed_srcs),
package_root = ctx.attrs.package_root,
),
cgo_build_context = cgo_build_context,
config = GoBuildConfig(
compiler_flags = ctx.attrs.compiler_flags,
build_tags = ctx.attrs._build_tags,
cgo_enabled = cgo_enabled,
coverage_enabled = ctx.attrs.coverage_enabled,
coverage_mode = coverage_mode,
),
deps = ctx.attrs.deps,
)
(bin, runtime_files, external_debug_info) = link(
ctx,
lib,
cgo_enabled = cgo_enabled,
deps = ctx.attrs.deps,
link_style = value_or(map_val(LinkStyle, ctx.attrs.link_style), LinkStyle("static")),
build_mode = GoBuildMode(value_or(ctx.attrs.build_mode, "exe")),
linker_flags = ctx.attrs.linker_flags,
link_mode = ctx.attrs.link_mode,
external_linker_flags = ctx.attrs.external_linker_flags,
)
# runtime_files are all the artifacts that must be present in order for this
# binary to be runnable. Notably, all of its shared library dependencies.
# This is materialized when a Go binary is executed as a genrule.
#
# other_outputs is a superset of runtime_files, adding external debuginfo
# which is necessary for a user to run this binary in a debugger. This is
# materialized when a Go binary is the end result of a build.
runtime_files = list(runtime_files)
other_outputs = runtime_files + external_debug_info
for resource in ctx.attrs.resources:
resource = single_artifact(resource)
runtime_files.append(resource.default_output)
runtime_files.extend(resource.nondebug_runtime_files)
other_outputs.append(resource.default_output)
other_outputs.extend(resource.other_outputs)
return [
DefaultInfo(
default_output = bin,
other_outputs = other_outputs,
),
RunInfo(args = cmd_args(bin, hidden = other_outputs)),
DistInfo(nondebug_runtime_files = runtime_files),
GoTestInfo(
deps = ctx.attrs.deps,
srcs = ctx.attrs.srcs,
pkg_import_path = pkg_import_path,
coverage_enabled = ctx.attrs.coverage_enabled,
),
pkg_info,
]