-
Notifications
You must be signed in to change notification settings - Fork 197
/
go_binary.bzl
67 lines (60 loc) · 2.29 KB
/
go_binary.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under both the MIT license found in the
# LICENSE-MIT file in the root directory of this source tree and the Apache
# License, Version 2.0 found in the LICENSE-APACHE file in the root directory
# of this source tree.
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",
"map_val",
"value_or",
)
load(":compile.bzl", "compile", "get_filtered_srcs")
load(":link.bzl", "link")
def go_binary_impl(ctx: AnalysisContext) -> list[Provider]:
lib = compile(
ctx,
"main",
get_filtered_srcs(ctx, ctx.attrs.srcs),
deps = ctx.attrs.deps,
compile_flags = ctx.attrs.compiler_flags,
race = ctx.attrs._race,
)
(bin, runtime_files, external_debug_info) = link(
ctx,
lib,
deps = ctx.attrs.deps,
link_style = value_or(map_val(LinkStyle, ctx.attrs.link_style), LinkStyle("static")),
linker_flags = ctx.attrs.linker_flags,
link_mode = ctx.attrs.link_mode,
race = ctx.attrs._race,
)
# 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),
]