Skip to content

Commit cde71a9

Browse files
committed
Bazel: address review comments
1 parent 2f95944 commit cde71a9

File tree

2 files changed

+70
-51
lines changed

2 files changed

+70
-51
lines changed

misc/bazel/internal/install.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@
1919
parser = argparse.ArgumentParser(description=__doc__)
2020
parser.add_argument("--destdir", type=pathlib.Path, required=True,
2121
help="Desination directory, relative to `--build-file`")
22-
parser.add_argument("--script", required=True,
22+
parser.add_argument("--pkg-install-script", required=True,
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")
2626
parser.add_argument("--ripunzip", required=True,
2727
help="ripunzip executable rlocation")
28-
parser.add_argument("--zip-manifest", action="append", default=[], dest="zip_manifests",
28+
parser.add_argument("--zip-manifest", required=True,
2929
help="The rlocation of a file containing newline-separated `prefix:zip_file` entries")
3030
opts = parser.parse_args()
3131

3232
build_file = runfiles.Rlocation(opts.build_file)
33-
script = runfiles.Rlocation(opts.script)
33+
script = runfiles.Rlocation(opts.pkg_install_script)
3434
ripunzip = runfiles.Rlocation(opts.ripunzip)
35-
zip_manifests = [runfiles.Rlocation(z) for z in opts.zip_manifests]
35+
zip_manifest = runfiles.Rlocation(opts.zip_manifest)
3636
destdir = pathlib.Path(build_file).resolve().parent / opts.destdir
3737

3838
if destdir.exists():
@@ -41,11 +41,10 @@
4141
destdir.mkdir(parents=True)
4242
subprocess.run([script, "--destdir", destdir], check=True)
4343

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

misc/bazel/pkg.bzl

+59-39
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ _PLAT_DETECTION_ATTRS = {
2121

2222
_PLAT_PLACEHOLDER = "{CODEQL_PLATFORM}"
2323

24-
def _process_path(path, plat):
24+
def _process_path(path, platform):
2525
if _PLAT_PLACEHOLDER in path:
26-
path = path.replace(_PLAT_PLACEHOLDER, plat)
26+
path = path.replace(_PLAT_PLACEHOLDER, platform)
2727
return ("arch", path)
2828
return ("generic", path)
2929

30-
def _detect_plat(ctx):
30+
def _detect_platform(ctx):
3131
if ctx.target_platform_has_constraint(ctx.attr._windows[platform_common.ConstraintValueInfo]):
32-
return "windows64"
32+
return "win64"
3333
elif ctx.target_platform_has_constraint(ctx.attr._macos[platform_common.ConstraintValueInfo]):
3434
return "osx64"
3535
else:
@@ -80,7 +80,7 @@ def codeql_pkg_files(
8080

8181
def _extract_pkg_filegroup_impl(ctx):
8282
src = ctx.attr.src[PackageFilegroupInfo]
83-
plat = _detect_plat(ctx)
83+
platform = _detect_platform(ctx)
8484

8585
if src.pkg_dirs or src.pkg_symlinks:
8686
fail("`pkg_dirs` and `pkg_symlinks` are not supported for codeql packaging rules")
@@ -89,7 +89,7 @@ def _extract_pkg_filegroup_impl(ctx):
8989
for pfi, origin in src.pkg_files:
9090
dest_src_map = {}
9191
for dest, file in pfi.dest_src_map.items():
92-
file_kind, dest = _process_path(dest, plat)
92+
file_kind, dest = _process_path(dest, platform)
9393
if file_kind == ctx.attr.kind:
9494
dest_src_map[dest] = file
9595
if dest_src_map:
@@ -101,25 +101,33 @@ def _extract_pkg_filegroup_impl(ctx):
101101
DefaultInfo(files = depset(transitive = files)),
102102
]
103103

104-
_extrac_pkg_filegroup = rule(
104+
_extract_pkg_filegroup = rule(
105105
implementation = _extract_pkg_filegroup_impl,
106+
doc = """
107+
This internal rule extracts the arch or generic part of a `PackageFilegroupInfo` source, returning a
108+
`PackageFilegroupInfo` that is a subset of the provided `src`, while expanding `{CODEQL_PLATFORM}` in
109+
destination paths to the relevant codeql platform (linux64, win64 or osx64).
110+
The distinction between generic and arch contents is given on a per-file basis depending on the install path
111+
containing {CODEQL_PLATFORM}, which will typically have been added by a `prefix` attribute to a `pkg_*` rule.
112+
No `pkg_dirs` or `pkg_symlink` must have been used for assembling the source mapping information: we could
113+
easily add support for that, but we don't require it for now.
114+
""",
106115
attrs = {
107116
"src": attr.label(providers = [PackageFilegroupInfo, DefaultInfo]),
108-
"kind": attr.string(doc = "generic or arch", values = ["generic", "arch"]),
117+
"kind": attr.string(doc = "What part to extract", values = ["generic", "arch"]),
109118
} | _PLAT_DETECTION_ATTRS,
110119
)
111120

112121
def _imported_zips_manifest_impl(ctx):
113-
plat = _detect_plat(ctx)
122+
platform = _detect_platform(ctx)
114123

115124
manifest = []
116125
files = []
117126
for zip, prefix in ctx.attr.zips.items():
118-
zip_kind, prefix = _process_path(prefix, plat)
119-
if zip_kind == ctx.attr.kind:
120-
zip_files = zip.files.to_list()
121-
manifest += ["%s:%s" % (prefix, f.short_path) for f in zip_files]
122-
files += zip_files
127+
_, prefix = _process_path(prefix, platform)
128+
zip_files = zip.files.to_list()
129+
manifest += ["%s:%s" % (prefix, f.short_path) for f in zip_files]
130+
files += zip_files
123131

124132
output = ctx.actions.declare_file(ctx.label.name + ".params")
125133
ctx.actions.write(
@@ -133,21 +141,24 @@ def _imported_zips_manifest_impl(ctx):
133141

134142
_imported_zips_manifest = rule(
135143
implementation = _imported_zips_manifest_impl,
144+
doc = """
145+
This internal rule prints a zip manifest file that `misc/bazel/internal/install.py` understands.
146+
{CODEQL_PLATFORM} can be used as zip prefixes and will be expanded to the relevant codeql platform.
147+
""",
136148
attrs = {
137-
"zips": attr.label_keyed_string_dict(allow_files = True),
138-
"kind": attr.string(doc = "generic or arch", values = ["generic", "arch"]),
149+
"zips": attr.label_keyed_string_dict(doc = "mapping from zip files to install prefixes", allow_files = True),
139150
} | _PLAT_DETECTION_ATTRS,
140151
)
141152

142153
def _zipmerge_impl(ctx):
143154
zips = []
144155
filename = ctx.attr.zip_name + "-"
145-
plat = _detect_plat(ctx)
146-
filename = "%s-%s.zip" % (ctx.attr.zip_name, plat if ctx.attr.kind == "arch" else "generic")
156+
platform = _detect_platform(ctx)
157+
filename = "%s-%s.zip" % (ctx.attr.zip_name, platform if ctx.attr.kind == "arch" else "generic")
147158
output = ctx.actions.declare_file(filename)
148159
args = [output.path, "--prefix=%s" % ctx.attr.zip_prefix, ctx.file.base.path]
149160
for zip, prefix in ctx.attr.zips.items():
150-
zip_kind, prefix = _process_path(prefix, plat)
161+
zip_kind, prefix = _process_path(prefix, platform)
151162
if zip_kind == ctx.attr.kind:
152163
args.append("--prefix=%s/%s" % (ctx.attr.zip_prefix, prefix.rstrip("/")))
153164
args += [f.path for f in zip.files.to_list()]
@@ -165,12 +176,24 @@ def _zipmerge_impl(ctx):
165176

166177
_zipmerge = rule(
167178
implementation = _zipmerge_impl,
179+
doc = """
180+
This internal rule merges a `base` zip file with the ones indicated by the `zips` mapping where the prefix
181+
indicates a matching kind between arch and generic. An imported zip file will be considered arch-specific
182+
if its prefix contains `{CODEQL_PLATFORM}` (and this prefix will have that expanded to the appropriate
183+
platform).
184+
185+
The output filename will be either `{zip_name}-generic.zip` or `{zip_name}-{CODEQL_PLATFORM}.zip`, depending on
186+
the requested `kind`.
187+
""",
168188
attrs = {
169-
"base": attr.label(allow_single_file = True),
170-
"zips": attr.label_keyed_string_dict(allow_files = True),
171-
"zip_name": attr.string(),
172-
"kind": attr.string(doc = "generic or arch", values = ["generic", "arch"]),
173-
"zip_prefix": attr.string(),
189+
"base": attr.label(
190+
doc = "Base zip file to which zips from `zips` will be merged with",
191+
allow_single_file = True,
192+
),
193+
"zips": attr.label_keyed_string_dict(doc = "mapping from zip files to install prefixes", allow_files = True),
194+
"zip_name": attr.string(doc = "Prefix to use for the output file name"),
195+
"kind": attr.string(doc = "Which zip kind to consider", values = ["generic", "arch"]),
196+
"zip_prefix": attr.string(doc = "Prefix posix path to add to the zip contents in the archive"),
174197
"_zipmerge": attr.label(default = "//misc/bazel/internal/zipmerge", executable = True, cfg = "exec"),
175198
} | _PLAT_DETECTION_ATTRS,
176199
)
@@ -190,14 +213,14 @@ def codeql_pack(
190213
* defines a `<name>-generic-zip` target creating a `<zip_filename>-generic.zip` archive with the generic bits,
191214
prefixed with `name`
192215
* defines a `<name>-arch-zip` target creating a `<zip_filename>-<codeql_platform>.zip` archive with the
193-
arch-specific bits, prefixed with `zip_prefix` (`name` by default)
216+
arch-specific bits, prefixed with `name`
194217
* defines a runnable `<name>-installer` target that will install the pack in `install_dest`, relative to where the
195218
rule is used. The install destination can be overridden appending `-- --destdir=...` to the `bazel run`
196-
invocation. This installation does not use the `zip_prefix`.
219+
invocation. This installation _does not_ prefix the contents with `name`.
197220
198221
The distinction between arch-specific and generic contents is made based on whether the paths (including possible
199222
prefixes added by rules) contain the special `{CODEQL_PLATFORM}` placeholder, which in case it is present will also
200-
be replaced by the appropriate platform (`linux64`, `windows64` or `osx64`).
223+
be replaced by the appropriate platform (`linux64`, `win64` or `osx64`).
201224
"""
202225
internal = _make_internal(name)
203226
zip_filename = zip_filename or name
@@ -209,7 +232,7 @@ def codeql_pack(
209232
**kwargs
210233
)
211234
for kind in ("generic", "arch"):
212-
_extrac_pkg_filegroup(
235+
_extract_pkg_filegroup(
213236
name = internal(kind),
214237
src = internal("base"),
215238
kind = kind,
@@ -229,12 +252,11 @@ def codeql_pack(
229252
kind = kind,
230253
visibility = visibility,
231254
)
232-
_imported_zips_manifest(
233-
name = internal(kind + "-zip-manifest"),
234-
zips = zips,
235-
kind = kind,
236-
visibility = ["//visibility:private"],
237-
)
255+
_imported_zips_manifest(
256+
name = internal("zip-manifest"),
257+
zips = zips,
258+
visibility = ["//visibility:private"],
259+
)
238260

239261
pkg_install(
240262
name = internal("script"),
@@ -254,19 +276,17 @@ def codeql_pack(
254276
data = [
255277
internal("build-file"),
256278
internal("script"),
257-
internal("generic-zip-manifest"),
258-
internal("arch-zip-manifest"),
279+
internal("zip-manifest"),
259280
"//misc/bazel/internal/ripunzip",
260281
],
261282
deps = ["@rules_python//python/runfiles"],
262283
args = [
263284
"--build-file=$(rlocationpath %s)" % internal("build-file"),
264-
"--script=$(rlocationpath %s)" % internal("script"),
285+
"--pkg-install-script=$(rlocationpath %s)" % internal("script"),
265286
"--destdir",
266287
install_dest,
267288
"--ripunzip=$(rlocationpath //misc/bazel/internal/ripunzip)",
268-
"--zip-manifest=$(rlocationpath %s)" % internal("generic-zip-manifest"),
269-
"--zip-manifest=$(rlocationpath %s)" % internal("arch-zip-manifest"),
289+
"--zip-manifest=$(rlocationpath %s)" % internal("zip-manifest"),
270290
],
271291
visibility = visibility,
272292
)

0 commit comments

Comments
 (0)