forked from facebook/buck2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_exe.bzl
More file actions
150 lines (126 loc) · 4.36 KB
/
Copy pathexport_exe.bzl
File metadata and controls
150 lines (126 loc) · 4.36 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# 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//python:manifest.bzl", "create_manifest_for_entries")
load("@prelude//unix:providers.bzl", "UnixEnv", "create_unix_env_info")
def _export_exe_impl(ctx: AnalysisContext) -> list[Provider]:
if ctx.attrs.src and ctx.attrs.exe:
fail("Must supply one of src or exe to export_exe")
src = ctx.attrs.src if ctx.attrs.src else ctx.attrs.exe
providers = []
providers.append(DefaultInfo(other_outputs = ctx.attrs.resources))
providers.append(RunInfo(args = cmd_args(src, hidden = ctx.attrs.resources)))
if ctx.attrs.src != None:
providers.append(
create_unix_env_info(
actions = ctx.actions,
env = UnixEnv(
label = ctx.label,
binaries = [
create_manifest_for_entries(
ctx = ctx,
name = "unix_env",
entries = [
(ctx.attrs.src.basename, ctx.attrs.src, ""),
],
),
],
),
),
)
return providers
_export_exe = rule(
doc = """Exports a file as an executable, for use in $(exe) macros or as a valid target for an exec_dep().
Accepts either a string `src`, which is a relative path to a file that will be directly referenced,
or an arg `exe` which should be a path to an executable relative to a $(location) macro.
The first form is a more ergonomic replacement for export_file + command_alias. Eg. Instead of
export_file(
name = "script_sh",
src = "bin/script.sh",
)
command_alias(
name = "script.sh"
exe = ":script_sh",
)
You can write
export_exe(
name = "script.sh",
src = "bin/script.sh",
)
The latter form allows executing checked in binaries with required resources (eg. runtime shared libraries)
without unnecessary indirection via another rule which allows args, like command_alias. Eg. instead of
export_file(
name = "bin"
src = "bin",
mode = "reference",
)
export_file(
name = "exec.sh",
src = "exec.sh",
)
command_alias(
name = "compiler",
exe = ":exec.sh", # Just calls exec $@
args = [
"$(location :bin)/compiler",
],
)
You can write
export_file(
name = "bin",
src = "bin",
mode = "reference",
)
export_exe(
name = "compiler",
exe = "$(location :bin)/compiler",
)
To ensure additional resources are materialized in Remote Execution (e.g., internal
compiler components that the executable invokes), use the `resources` parameter:
export_file(
name = "bin",
src = "bin",
mode = "reference",
)
export_file(
name = "libexec",
src = "libexec",
mode = "reference",
)
export_exe(
name = "compiler",
exe = "$(location :bin)/compiler",
resources = [":bin", ":libexec"],
)
""",
impl = _export_exe_impl,
attrs = {
"exe": attrs.option(attrs.arg(), default = None, doc = "arg which should evaluate to a path to an executable binary"),
"resources": attrs.list(attrs.source(), default = [], doc = "Additional artifacts to materialize alongside the executable (for Remote Execution)"),
"src": attrs.option(attrs.source(), default = None, doc = "path to an executable binary relative to this package"),
},
)
def export_exe(name, exe = None, src = None, **kwargs):
# If neither `exe` nor `src` is passed, treat the target's name as the src.
#
# export_exe(
# name = "script.sh",
# )
#
# is equivalent to:
#
# export_exe(
# name = "script.sh",
# src = "script.sh",
# )
#
_export_exe(
name = name,
exe = exe,
src = src if (exe or src) else name,
**kwargs
)