-
Notifications
You must be signed in to change notification settings - Fork 331
Expand file tree
/
Copy pathcommand_alias.bzl
More file actions
264 lines (228 loc) · 10.3 KB
/
command_alias.bzl
File metadata and controls
264 lines (228 loc) · 10.3 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# 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//os_lookup:defs.bzl", "Os", "OsLookup", "ScriptLanguage")
load("@prelude//utils:arglike.bzl", "ArgLike") # @unused: Used as a type
def command_alias_impl(ctx: AnalysisContext):
target_os = ctx.attrs._target_os_type[OsLookup]
if target_os.os == Os("fat_mac_linux") and len(ctx.attrs.platform_exe) > 0:
base = {
"Darwin": _get_os_base(ctx, Os("macos")),
"Linux": _get_os_base(ctx, Os("linux")),
}
else:
base = _get_os_base(ctx, target_os.os)
output = command_alias(
actions = ctx.actions,
path = ctx.attrs.executable_name,
target_os = target_os,
base = base,
args = cmd_args(ctx.attrs.args),
env = ctx.attrs.env,
labels = ctx.attrs.labels,
)
default_info = DefaultInfo(
default_output = output.output.default_outputs[0],
other_outputs = list(output.output.other_outputs) + ctx.attrs.resources,
)
# FIXME(JakobDegen): We should not accept `platform_exe` as meaning `run_using_single_arg`, but
# there are things that depend on that
#
# FIXME(JakobDegen): It's easy to end up depending on either one of these behaviors. Life would
# probably be easier if we just always went the `output.cmd` route
if output.maybe_directly_runnable == None or \
ctx.attrs.run_using_single_arg or \
ctx.attrs.executable_name != None or \
(len(ctx.attrs.platform_exe) > 0 and target_os.script == ScriptLanguage("sh")):
run_info = RunInfo(args = output.cmd)
else:
run_info = RunInfo(args = output.maybe_directly_runnable)
run_info_with_resources = RunInfo(
args = cmd_args(run_info, hidden = ctx.attrs.resources),
)
return [default_info, run_info_with_resources]
def _get_os_base(ctx: AnalysisContext, os: Os) -> RunInfo:
exe = ctx.attrs.platform_exe.get(os.value)
if exe == None:
exe = ctx.attrs.exe
if exe == None:
return RunInfo()
if isinstance(exe, Artifact):
return RunInfo(args = cmd_args(exe))
run_info = exe.get(RunInfo)
if run_info == None:
run_info = RunInfo(
args = exe[DefaultInfo].default_outputs,
)
return run_info
CommandAliasOutput = record(
# A batch or sh script representing the command alias
#
# `DefaultInfo` instead of `Artifact` because the `other_outputs` will usually need to be
# included as hidden somewhere
output = DefaultInfo,
# The output wrapped into a `cmd_args` in the obvious way
cmd = cmd_args,
# For some values of the arguments to `command_alias` it is possible to represent the
# command_alias entirely within a `cmd_args`, without needing to write out a script. If that is
# the case, those args are made available here.
maybe_directly_runnable = cmd_args | None,
)
def command_alias(
*,
actions: AnalysisActions,
# The path at which to write the output to, without an extension - that will be added
path: str | None,
# The target where this script should be able to run (this may actually be your exec platform)
target_os: OsLookup,
# Either the `RunInfo` to use, or in the case of a fat platform, the choice of `RunInfo`
# depending on `uname`
base: RunInfo | dict[str, RunInfo],
args: cmd_args,
env: dict[str, ArgLike],
labels: list[str],
has_content_based_path: bool = False) -> CommandAliasOutput:
if path == "":
fail("Path cannot be empty string")
if path != None:
# Custom paths are usually provided for binaries that are searched for with a particular
# name. On Windows, the .bat extension is required, and well-behaved tools will respect the
# PATHEXT environment variable (which contains that extension). On Unix, no extension is
# expected for executables, and searches would likely fail if we added one.
unix_trampoline_path = path
windows_trampoline_path = path + ".bat"
else:
# Preserve the `.sh` extension in the default path, since many places have it hard-coded.
unix_trampoline_path = "__command_alias_trampoline.sh"
windows_trampoline_path = "__command_alias_trampoline.bat"
if target_os.script == ScriptLanguage("sh"):
trampoline, hidden = _command_alias_write_trampoline_unix(actions, unix_trampoline_path, base, args, env, has_content_based_path)
elif target_os.script == ScriptLanguage("bat"):
trampoline, hidden = _command_alias_write_trampoline_windows(actions, windows_trampoline_path, base, args, env, labels, has_content_based_path)
else:
fail("Unsupported script language: {}".format(target_os.script))
if len(env) > 0 or isinstance(base, dict):
maybe_directly_runnable = None
else:
maybe_directly_runnable = cmd_args(base.args, args)
return CommandAliasOutput(
output = DefaultInfo(
default_output = trampoline,
other_outputs = [hidden],
),
cmd = cmd_args(trampoline, hidden = hidden),
maybe_directly_runnable = maybe_directly_runnable,
)
def _command_alias_write_trampoline_unix(
actions: AnalysisActions,
path: str,
base: RunInfo | dict[str, RunInfo],
args: cmd_args,
env: dict[str, ArgLike],
has_content_based_path: bool) -> (Artifact, cmd_args):
trampoline_args = cmd_args()
trampoline_args.add("#!/usr/bin/env bash")
trampoline_args.add("set -euo pipefail")
if isinstance(base, dict):
trampoline_args.add('case "$(uname)" in')
for uname, run_info in base.items():
trampoline_args.add(" {})".format(uname))
_add_args_declaration_to_trampoline_args(trampoline_args, run_info, args)
trampoline_args.add(" ;;")
trampoline_args.add("esac")
else:
_add_args_declaration_to_trampoline_args(trampoline_args, base, args)
# We can't use cwd relative paths (since we don't know the cwd when this script is run) and so
# we instead use paths relative to the script itself. However, we can't just naively stick a
# `$(...)` into `absolute_prefix`, since we must also shell quote paths, and that expression
# would be shell quoted.
#
# Instead, we use `BUCK_COMMAND_ALIAS_ABSOLUTE_PREFIX/`, verbatim, as an absolute prefix on the
# cmd_args, and then replace that with the actual path of the script at runtime
#
# Resolve symlinks first to handle execution via symlinks (e.g., in link-trees)
trampoline_args.add(
"""
SCRIPT_PATH="$0"
if [ -L "$SCRIPT_PATH" ]; then
TARGET="$(readlink "$SCRIPT_PATH")"
SCRIPT_PATH="$(dirname "$SCRIPT_PATH")/$TARGET"
fi
""",
)
# Calculate the base path and process arguments with the resolved path
trampoline_args.add(
"""
BASE=$(cd -- "$(dirname "$SCRIPT_PATH")" >/dev/null 2>&1 ; pwd -P)
R_ARGS=()
for arg in "${ARGS[@]}"; do
R_ARGS+=("${arg//BUCK_COMMAND_ALIAS_ABSOLUTE_PREFIX/$BASE}")
done
""",
)
for (k, v) in env.items():
# TODO(akozhevnikov): maybe check environment variable is not conflicting with pre-existing one
trampoline_args.add(cmd_args("export ", k, "=", cmd_args(v, quote = "shell"), delimiter = ""))
trampoline_args.add(cmd_args("export ", k, '="${', k, '//BUCK_COMMAND_ALIAS_ABSOLUTE_PREFIX/$BASE}"', delimiter = ""))
trampoline_args.add('exec "${R_ARGS[@]}" "$@"')
trampoline = actions.declare_output(path, has_content_based_path = has_content_based_path)
trampoline_args = cmd_args(
trampoline_args,
relative_to = (trampoline, 1),
absolute_prefix = "BUCK_COMMAND_ALIAS_ABSOLUTE_PREFIX/",
)
actions.write(
trampoline.as_output(),
trampoline_args,
allow_args = True,
is_executable = True,
has_content_based_path = has_content_based_path,
)
return trampoline, trampoline_args
def _command_alias_write_trampoline_windows(
actions: AnalysisActions,
path: str,
base: RunInfo,
args: cmd_args,
env: dict[str, ArgLike],
labels: list[str],
has_content_based_path: bool) -> (Artifact, cmd_args):
trampoline_args = cmd_args()
trampoline_args.add("@echo off")
if "close_stdin" in labels:
# Avoids waiting for input on the "Terminate batch job (Y/N)?" prompt.
# The prompt itself is unavoidable, but we can avoid having to wait for input.
# This will call the same trampoline batch file with stdin disabled
trampoline_args.add("if not defined STDIN_CLOSED (set STDIN_CLOSED=1 & CALL <NUL %0 %* & GOTO :EOF)")
# Set BUCK_COMMAND_ALIAS_ABSOLUTE to the drive and full path of the script being created here
# We use this below to prefix any artifacts being referenced in the script
trampoline_args.add("set BUCK_COMMAND_ALIAS_ABSOLUTE=%~dp0")
# Handle envs
for (k, v) in env.items():
# TODO(akozhevnikov): maybe check environment variable is not conflicting with pre-existing one
trampoline_args.add(cmd_args(["set ", k, "=", v], delimiter = ""))
# FIXME(JakobDegen): This should be batch quoting, not shell quoting
cmd = cmd_args(cmd_args(base.args, args, quote = "shell"), "%*", delimiter = " ")
trampoline_args.add(cmd)
trampoline = actions.declare_output(path, has_content_based_path = has_content_based_path)
trampoline_args = cmd_args(
trampoline_args,
relative_to = (trampoline, 1),
absolute_prefix = "%BUCK_COMMAND_ALIAS_ABSOLUTE%/",
)
actions.write(
trampoline.as_output(),
trampoline_args,
allow_args = True,
is_executable = True,
has_content_based_path = has_content_based_path,
)
return trampoline, trampoline_args
def _add_args_declaration_to_trampoline_args(trampoline_args: cmd_args, base: RunInfo, args: cmd_args):
trampoline_args.add("ARGS=(")
trampoline_args.add(cmd_args(base.args, args, quote = "shell"))
trampoline_args.add(")")