Skip to content

Commit 92957a6

Browse files
committed
Add prefix feature to codeql_pack_group.
Turns out we need this for our production targets.
1 parent 3cf719c commit 92957a6

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

misc/bazel/pkg.bzl

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ _CODEQL_PACK_GROUP_EXTRACT_ATTRS = {
205205
"srcs": attr.label_list(providers = [_CodeQLPackInfo], mandatory = True, doc = "List of `_codeql_pack_info` rules (generated by `codeql_pack`)."),
206206
"apply_pack_prefix": attr.bool(doc = "Set to `False` to skip adding the per-pack prefix to all file paths.", default = True),
207207
"kind": attr.string(doc = "Extract only the commmon, arch-specific, or all files from the pack group.", values = ["common", "arch", "all"]),
208+
"prefix": attr.string(doc = "Prefix to add to all files, is prefixed after the per-pack prefix has been applied.", default = ""),
208209
} | OS_DETECTION_ATTRS
209210

210211
# common option parsing for _codeql_pack_group_extract_* rules
@@ -222,15 +223,15 @@ def _codeql_pack_group_extract_files_impl(ctx):
222223
src = src[_CodeQLPackInfo]
223224
if src.files.pkg_dirs or src.files.pkg_symlinks:
224225
fail("`pkg_dirs` and `pkg_symlinks` are not supported for codeql packaging rules")
225-
pack_prefix = src.pack_prefix if apply_pack_prefix else ""
226+
prefix = paths.join(ctx.attr.prefix, src.pack_prefix) if apply_pack_prefix else ctx.attr.prefix
226227

227228
arch_overrides = src.arch_overrides
228229

229230
# for each file, resolve whether it's filtered out or not by the current kind, and add the pack prefix
230231
for pfi, origin in src.files.pkg_files:
231232
dest_src_map = {}
232233
for dest, file in pfi.dest_src_map.items():
233-
pack_dest = paths.join(pack_prefix, dest)
234+
pack_dest = paths.join(prefix, dest)
234235
file_kind, expanded_dest = _expand_path(pack_dest, platform)
235236
if file_kind == "common" and dest in arch_overrides:
236237
file_kind = "arch"
@@ -263,11 +264,11 @@ def _codeql_pack_group_extract_zips_impl(ctx):
263264
platform, apply_pack_prefix, include_all_files = _codeql_pack_group_extract_options(ctx)
264265
for src in ctx.attr.srcs:
265266
src = src[_CodeQLPackInfo]
266-
pack_prefix = src.pack_prefix if apply_pack_prefix else ""
267+
prefix = paths.join(ctx.attr.prefix, src.pack_prefix) if apply_pack_prefix else ctx.attr.prefix
267268

268269
# for each zip file, resolve whether it's filtered out or not by the current kind, and add the pack prefix
269-
for zip, prefix in src.zips.zips_to_prefixes.items():
270-
zip_kind, expanded_prefix = _expand_path(paths.join(pack_prefix, prefix), platform)
270+
for zip, zip_prefix in src.zips.zips_to_prefixes.items():
271+
zip_kind, expanded_prefix = _expand_path(paths.join(prefix, zip_prefix), platform)
271272
if include_all_files or zip_kind == ctx.attr.kind:
272273
zips_to_prefixes[zip] = expanded_prefix
273274

@@ -286,7 +287,7 @@ _codeql_pack_group_extract_zips = rule(
286287
provides = [_ZipInfo],
287288
)
288289

289-
def _codeql_pack_install(name, srcs, install_dest = None, build_file_label = None, apply_pack_prefix = True):
290+
def _codeql_pack_install(name, srcs, install_dest = None, build_file_label = None, prefix = "", apply_pack_prefix = True):
290291
"""
291292
Create a runnable target `name` that installs the list of codeql packs given in `srcs` in `install_dest`,
292293
relative to the directory where the rule is used.
@@ -301,6 +302,7 @@ def _codeql_pack_install(name, srcs, install_dest = None, build_file_label = Non
301302
_codeql_pack_group_extract_files(
302303
name = internal("all-files"),
303304
srcs = srcs,
305+
prefix = prefix,
304306
kind = "all",
305307
apply_pack_prefix = apply_pack_prefix,
306308
visibility = ["//visibility:private"],
@@ -309,6 +311,7 @@ def _codeql_pack_install(name, srcs, install_dest = None, build_file_label = Non
309311
name = internal("all-extra-zips"),
310312
kind = "all",
311313
srcs = srcs,
314+
prefix = prefix,
312315
apply_pack_prefix = apply_pack_prefix,
313316
visibility = ["//visibility:private"],
314317
)
@@ -351,7 +354,7 @@ def _codeql_pack_install(name, srcs, install_dest = None, build_file_label = Non
351354
(["--destdir", "\"%s\"" % install_dest] if install_dest else []),
352355
)
353356

354-
def codeql_pack_group(name, srcs, visibility = None, skip_installer = False, install_dest = None, build_file_label = None, compression_level = 6):
357+
def codeql_pack_group(name, srcs, visibility = None, skip_installer = False, prefix = "", install_dest = None, build_file_label = None, compression_level = 6):
355358
"""
356359
Create a group of codeql packs of name `name`.
357360
Accepts a list of `codeql_pack`s in `srcs` (essentially, `_codeql_pack_info` instantiations).
@@ -363,6 +366,8 @@ def codeql_pack_group(name, srcs, visibility = None, skip_installer = False, ins
363366
The install destination can be overridden appending `-- --destdir=...` to the `bazel run` invocation.
364367
The installer target will be omitted if `skip_installer` is set to `True`.
365368
369+
Prefixes all paths in the pack group with `prefix`.
370+
366371
The compression level of the generated zip files can be set with `compression_level`. Note that this doesn't affect the compression
367372
level of extra zip files that are added to a pack, as thes files will not be re-compressed.
368373
"""
@@ -373,6 +378,7 @@ def codeql_pack_group(name, srcs, visibility = None, skip_installer = False, ins
373378
name = internal(kind),
374379
srcs = srcs,
375380
kind = kind,
381+
prefix = prefix,
376382
visibility = ["//visibility:private"],
377383
)
378384
pkg_zip(
@@ -385,6 +391,7 @@ def codeql_pack_group(name, srcs, visibility = None, skip_installer = False, ins
385391
name = internal(kind, "extra-zips"),
386392
kind = kind,
387393
srcs = srcs,
394+
prefix = prefix,
388395
visibility = ["//visibility:private"],
389396
)
390397
_zipmerge(
@@ -394,7 +401,7 @@ def codeql_pack_group(name, srcs, visibility = None, skip_installer = False, ins
394401
visibility = visibility,
395402
)
396403
if not skip_installer:
397-
_codeql_pack_install(name, srcs, build_file_label = build_file_label, install_dest = install_dest, apply_pack_prefix = True)
404+
_codeql_pack_install(name, srcs, build_file_label = build_file_label, install_dest = install_dest, prefix = prefix, apply_pack_prefix = True)
398405

399406
def codeql_pack(
400407
*,

0 commit comments

Comments
 (0)