Skip to content

Commit 00ed00e

Browse files
committed
Bazel: avoid unneeded operations if no imported zips are present
1 parent e2206e6 commit 00ed00e

File tree

2 files changed

+70
-40
lines changed

2 files changed

+70
-40
lines changed

misc/bazel/internal/install.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@
2323
help="The wrapped `pkg_install` installation script rlocation")
2424
parser.add_argument("--build-file", required=True,
2525
help="BUILD.bazel rlocation relative to which the installation should take place")
26-
parser.add_argument("--ripunzip", required=True,
27-
help="ripunzip executable rlocation")
28-
parser.add_argument("--zip-manifest", required=True,
26+
parser.add_argument("--ripunzip",
27+
help="ripunzip executable rlocation. Must be provided if `--zip-manifest` is.")
28+
parser.add_argument("--zip-manifest",
2929
help="The rlocation of a file containing newline-separated `prefix:zip_file` entries")
3030
parser.add_argument("--cleanup", action=argparse.BooleanOptionalAction, default=True,
3131
help="Whether to wipe the destination directory before installing (true by default)")
3232
opts = parser.parse_args()
33+
if opts.zip_manifest and not opts.ripunzip:
34+
parser.error("Provide `--ripunzip` when specifying `--zip-manifest`")
3335

3436
build_file = runfiles.Rlocation(opts.build_file)
3537
script = runfiles.Rlocation(opts.pkg_install_script)
36-
ripunzip = runfiles.Rlocation(opts.ripunzip)
37-
zip_manifest = runfiles.Rlocation(opts.zip_manifest)
3838
destdir = pathlib.Path(build_file).resolve().parent / opts.destdir
3939

4040
if destdir.exists() and opts.cleanup:
@@ -43,10 +43,13 @@
4343
destdir.mkdir(parents=True, exist_ok=True)
4444
subprocess.run([script, "--destdir", destdir], check=True)
4545

46-
with open(zip_manifest) as manifest:
47-
for line in manifest:
48-
prefix, _, zip = line.partition(":")
49-
assert zip, f"missing prefix for {prefix}, you should use prefix:zip format"
50-
dest = destdir / prefix
51-
dest.mkdir(parents=True, exist_ok=True)
52-
subprocess.run([ripunzip, "unzip-file", zip, "-d", dest], check=True)
46+
if opts.zip_manifest:
47+
ripunzip = runfiles.Rlocation(opts.ripunzip)
48+
zip_manifest = runfiles.Rlocation(opts.zip_manifest)
49+
with open(zip_manifest) as manifest:
50+
for line in manifest:
51+
prefix, _, zip = line.partition(":")
52+
assert zip, f"missing prefix for {prefix}, you should use prefix:zip format"
53+
dest = destdir / prefix
54+
dest.mkdir(parents=True, exist_ok=True)
55+
subprocess.run([ripunzip, "unzip-file", zip, "-d", dest], check=True)

misc/bazel/pkg.bzl

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,28 @@ def _expand_path(path, platform):
2727
return ("arch", path)
2828
return ("generic", path)
2929

30-
def _detect_platform(ctx):
31-
if ctx.target_platform_has_constraint(ctx.attr._windows[platform_common.ConstraintValueInfo]):
32-
return "win64"
33-
elif ctx.target_platform_has_constraint(ctx.attr._macos[platform_common.ConstraintValueInfo]):
34-
return "osx64"
30+
def _platform_select(
31+
ctx = None,
32+
*,
33+
linux,
34+
windows,
35+
macos):
36+
if ctx:
37+
if ctx.target_platform_has_constraint(ctx.attr._windows[platform_common.ConstraintValueInfo]):
38+
return windows
39+
elif ctx.target_platform_has_constraint(ctx.attr._macos[platform_common.ConstraintValueInfo]):
40+
return macos
41+
else:
42+
return linux
3543
else:
36-
return "linux64"
44+
return select({
45+
"@platforms//os:linux": linux,
46+
"@platforms//os:macos": macos,
47+
"@platforms//os:windows": windows,
48+
})
49+
50+
def _detect_platform(ctx = None):
51+
return _platform_select(ctx, linux = "linux64", macos = "osx64", windows = "win64")
3752

3853
def codeql_pkg_files(
3954
*,
@@ -160,11 +175,11 @@ def _zipmerge_impl(ctx):
160175
platform = _detect_platform(ctx)
161176
filename = "%s-%s.zip" % (ctx.attr.zip_name, platform if ctx.attr.kind == "arch" else "generic")
162177
output = ctx.actions.declare_file(filename)
163-
args = [output.path, "--prefix=%s" % ctx.attr.zip_prefix, ctx.file.base.path]
178+
args = [output.path, "--prefix=%s" % ctx.attr.prefix, ctx.file.base.path]
164179
for zip, prefix in ctx.attr.zips.items():
165180
zip_kind, expanded_prefix = _expand_path(prefix, platform)
166181
if zip_kind == ctx.attr.kind:
167-
args.append("--prefix=%s/%s" % (ctx.attr.zip_prefix, expanded_prefix.rstrip("/")))
182+
args.append("--prefix=%s/%s" % (ctx.attr.prefix, expanded_prefix.rstrip("/")))
168183
args += [f.path for f in zip.files.to_list()]
169184
zips.append(zip.files)
170185
ctx.actions.run(
@@ -200,7 +215,7 @@ _zipmerge = rule(
200215
),
201216
"zip_name": attr.string(doc = "Prefix to use for the output file name"),
202217
"kind": attr.string(doc = "Which zip kind to consider", values = ["generic", "arch"]),
203-
"zip_prefix": attr.string(doc = "Prefix posix path to add to the zip contents in the archive"),
218+
"prefix": attr.string(doc = "Prefix posix path to add to the zip contents in the archive"),
204219
"_zipmerge": attr.label(default = "//misc/bazel/internal/zipmerge", executable = True, cfg = "exec"),
205220
} | _PLAT_DETECTION_ATTRS,
206221
)
@@ -245,25 +260,35 @@ def codeql_pack(
245260
kind = kind,
246261
visibility = ["//visibility:private"],
247262
)
248-
pkg_zip(
249-
name = internal(kind + "-zip-base"),
250-
srcs = [internal(kind)],
251-
visibility = ["//visibility:private"],
252-
)
253-
_zipmerge(
254-
name = internal(kind + "-zip"),
255-
base = internal(kind + "-zip-base"),
263+
if zips:
264+
pkg_zip(
265+
name = internal(kind + "-zip-base"),
266+
srcs = [internal(kind)],
267+
visibility = ["//visibility:private"],
268+
)
269+
_zipmerge(
270+
name = internal(kind + "-zip"),
271+
base = internal(kind + "-zip-base"),
272+
zips = zips,
273+
zip_name = zip_filename,
274+
kind = kind,
275+
prefix = name,
276+
visibility = visibility,
277+
)
278+
else:
279+
pkg_zip(
280+
name = internal(kind + "-zip"),
281+
srcs = [internal(kind)],
282+
visibility = ["//visibility:private"],
283+
package_dir = name,
284+
package_file_name = name + "-" + (_detect_platform() if kind == "arch" else "generic") + ".zip",
285+
)
286+
if zips:
287+
_imported_zips_manifest(
288+
name = internal("zip-manifest"),
256289
zips = zips,
257-
zip_name = zip_filename,
258-
zip_prefix = name, # this is prefixing the zip contents with the pack name
259-
kind = kind,
260-
visibility = visibility,
290+
visibility = ["//visibility:private"],
261291
)
262-
_imported_zips_manifest(
263-
name = internal("zip-manifest"),
264-
zips = zips,
265-
visibility = ["//visibility:private"],
266-
)
267292

268293
pkg_install(
269294
name = internal("script"),
@@ -283,18 +308,20 @@ def codeql_pack(
283308
data = [
284309
internal("build-file"),
285310
internal("script"),
311+
] + ([
286312
internal("zip-manifest"),
287313
"//misc/bazel/internal/ripunzip",
288-
],
314+
] if zips else []),
289315
deps = ["@rules_python//python/runfiles"],
290316
args = [
291317
"--build-file=$(rlocationpath %s)" % internal("build-file"),
292318
"--pkg-install-script=$(rlocationpath %s)" % internal("script"),
293319
"--destdir",
294320
install_dest,
321+
] + ([
295322
"--ripunzip=$(rlocationpath //misc/bazel/internal/ripunzip)",
296323
"--zip-manifest=$(rlocationpath %s)" % internal("zip-manifest"),
297-
],
324+
] if zips else []),
298325
visibility = visibility,
299326
)
300327
native.filegroup(

0 commit comments

Comments
 (0)