-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathdefs.bzl
More file actions
132 lines (121 loc) · 5.89 KB
/
defs.bzl
File metadata and controls
132 lines (121 loc) · 5.89 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
load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
load("@rules_pkg//pkg:tar.bzl", "pkg_tar")
def _extract_bazel_installation_impl(ctx):
out_dir = ctx.actions.declare_directory(ctx.attr.out_dir)
ctx.actions.run_shell(
outputs = [out_dir],
inputs = [ctx.executable.bazel],
mnemonic = "ExtractBazelInstall",
command = """
set -e
BAZEL="$PWD/$BAZEL"
OUT_DIR="$PWD/$OUT_DIR"
WORKDIR="$PWD/_extract_bazel_installation_tmp"
INSTALL_BASE="$OUT_DIR/install_base"
REPOSITORY_CACHE="$OUT_DIR/repository_cache"
OUTPUT_BASE="$WORKDIR/output_base"
OUTPUT_USER_ROOT="$WORKDIR/output_user_root"
mkdir -p "$WORKDIR"
cd "$WORKDIR"
trap 'rm -rf "$WORKDIR"' EXIT
_bazel() {
if ! USER="nobody" "$BAZEL" \\
--install_base="$INSTALL_BASE" \\
--output_user_root="$OUTPUT_USER_ROOT" \\
--output_base="$OUTPUT_BASE" \\
"$@" \\
&>"$WORKDIR/bazel.log" ; then
cat "$WORKDIR/bazel.log" >&2
exit 1
fi
}
mkdir workspace
cd workspace
touch MODULE.bazel BUILD
echo >> .bazelrc "common --repository_cache=$REPOSITORY_CACHE"
echo >> .bazelrc "common --noexperimental_convenience_symlinks"
if [ "$WARM_REPOSITORY_CACHE" = "0" ]; then
# Run bazel with no args; this should install it to the install_base.
_bazel
else
# Download dependencies of commonly used rules in tests.
# For Bazel 9+, sh_binary and sh_test are no longer native rules;
# they must be loaded from @rules_shell.
if [ "$USE_RULES_SHELL" = "1" ]; then
echo >>MODULE.bazel 'bazel_dep(name = "rules_shell", version = "0.6.1")'
echo >>BUILD 'load("@rules_shell//shell:sh_binary.bzl", "sh_binary")'
echo >>BUILD 'load("@rules_shell//shell:sh_test.bzl", "sh_test")'
fi
echo >>BUILD 'genrule(name = "genrule", outs = ["genrule.out"], cmd = "touch $@")'
echo >>BUILD 'sh_binary(name = "sh_binary", srcs = ["empty.sh"])'
echo >>BUILD 'sh_test(name = "sh_test", srcs = ["empty.sh"])'
touch empty.sh && chmod +x empty.sh
_bazel fetch //...
# Copy lockfile; needed to avoid registry requests.
cp MODULE.bazel.lock "$OUT_DIR/MODULE.bazel.lock"
fi
""",
env = {
"BAZEL": ctx.executable.bazel.path,
"OUT_DIR": out_dir.path,
"WARM_REPOSITORY_CACHE": "1" if ctx.attr.warm_repository_cache else "0",
"USE_RULES_SHELL": "1" if ctx.attr.use_rules_shell else "0",
},
)
return [DefaultInfo(files = depset([out_dir]))]
extract_bazel_installation = rule(
implementation = _extract_bazel_installation_impl,
attrs = {
"bazel": attr.label(executable = True, cfg = "exec", mandatory = True, allow_single_file = True),
"out_dir": attr.string(mandatory = True),
"warm_repository_cache": attr.bool(default = False),
"use_rules_shell": attr.bool(default = False),
},
doc = "Pre-extract a bazel installation and optionally a repository cache to the given output directory.",
)
def bazel_pkg_tar(name, versions = [], **kwargs):
"""Create a tar file containing Bazel executable for each version in versions."""
for version in versions:
copy_file(
name = "bazel-{}_crossplatform".format(version),
src = select({
"//platforms/configs:linux_x86_64": "@io_bazel_bazel-{}-linux-x86_64//file:downloaded".format(version),
"//platforms/configs:linux_arm64": "@io_bazel_bazel-{}-linux-arm64//file:downloaded".format(version),
"//platforms/configs:macos_x86_64": "@io_bazel_bazel-{}-darwin-x86_64//file:downloaded".format(version),
"//platforms/configs:macos_arm64": "@io_bazel_bazel-{}-darwin-arm64//file:downloaded".format(version),
}),
out = "bazel-{}".format(version),
allow_symlink = True,
is_executable = True,
**kwargs
)
# Pre-warm repository cache only for bazel 8+, where we've started
# using repository_cache combined with MODULE.bazel.lock to make
# bazel work without network access.
major_version = int(version.split(".")[0])
warm_repository_cache = major_version >= 8
# Always warm @rules_shell. On Bazel 9+ it's strictly required
# (sh_binary / sh_test are no longer native). On Bazel 8 it's
# optional but cheap, and lets test-written BUILD files use a
# single `load("@rules_shell//shell:...")` form that works on
# both versions.
use_rules_shell = major_version >= 8
extract_bazel_installation(
name = "bazel-{}_extract_installation".format(version),
bazel = ":bazel-{}_crossplatform".format(version),
out_dir = "bazel-{}_outdir".format(version),
warm_repository_cache = warm_repository_cache,
use_rules_shell = use_rules_shell,
# If we are warming the repository cache then we need network access
# so that this rule can download the dependencies. Targets that use
# this rule can then use the cached dependencies without needing the
# network.
exec_properties = {"dockerNetwork": "bridge"} if warm_repository_cache else {},
**kwargs
)
pkg_tar(
name = name,
srcs = [":bazel-{}_crossplatform".format(version) for version in versions],
package_dir = "/bazel",
**kwargs
)