Add prefix feature to codeql_pack_group.

Turns out we need this for our production targets.
This commit is contained in:
Cornelius Riemenschneider
2024-06-11 17:05:32 +02:00
parent 3cf719cb39
commit 92957a63ad

View File

@@ -205,6 +205,7 @@ _CODEQL_PACK_GROUP_EXTRACT_ATTRS = {
"srcs": attr.label_list(providers = [_CodeQLPackInfo], mandatory = True, doc = "List of `_codeql_pack_info` rules (generated by `codeql_pack`)."),
"apply_pack_prefix": attr.bool(doc = "Set to `False` to skip adding the per-pack prefix to all file paths.", default = True),
"kind": attr.string(doc = "Extract only the commmon, arch-specific, or all files from the pack group.", values = ["common", "arch", "all"]),
"prefix": attr.string(doc = "Prefix to add to all files, is prefixed after the per-pack prefix has been applied.", default = ""),
} | OS_DETECTION_ATTRS
# common option parsing for _codeql_pack_group_extract_* rules
@@ -222,7 +223,7 @@ def _codeql_pack_group_extract_files_impl(ctx):
src = src[_CodeQLPackInfo]
if src.files.pkg_dirs or src.files.pkg_symlinks:
fail("`pkg_dirs` and `pkg_symlinks` are not supported for codeql packaging rules")
pack_prefix = src.pack_prefix if apply_pack_prefix else ""
prefix = paths.join(ctx.attr.prefix, src.pack_prefix) if apply_pack_prefix else ctx.attr.prefix
arch_overrides = src.arch_overrides
@@ -230,7 +231,7 @@ def _codeql_pack_group_extract_files_impl(ctx):
for pfi, origin in src.files.pkg_files:
dest_src_map = {}
for dest, file in pfi.dest_src_map.items():
pack_dest = paths.join(pack_prefix, dest)
pack_dest = paths.join(prefix, dest)
file_kind, expanded_dest = _expand_path(pack_dest, platform)
if file_kind == "common" and dest in arch_overrides:
file_kind = "arch"
@@ -263,11 +264,11 @@ def _codeql_pack_group_extract_zips_impl(ctx):
platform, apply_pack_prefix, include_all_files = _codeql_pack_group_extract_options(ctx)
for src in ctx.attr.srcs:
src = src[_CodeQLPackInfo]
pack_prefix = src.pack_prefix if apply_pack_prefix else ""
prefix = paths.join(ctx.attr.prefix, src.pack_prefix) if apply_pack_prefix else ctx.attr.prefix
# for each zip file, resolve whether it's filtered out or not by the current kind, and add the pack prefix
for zip, prefix in src.zips.zips_to_prefixes.items():
zip_kind, expanded_prefix = _expand_path(paths.join(pack_prefix, prefix), platform)
for zip, zip_prefix in src.zips.zips_to_prefixes.items():
zip_kind, expanded_prefix = _expand_path(paths.join(prefix, zip_prefix), platform)
if include_all_files or zip_kind == ctx.attr.kind:
zips_to_prefixes[zip] = expanded_prefix
@@ -286,7 +287,7 @@ _codeql_pack_group_extract_zips = rule(
provides = [_ZipInfo],
)
def _codeql_pack_install(name, srcs, install_dest = None, build_file_label = None, apply_pack_prefix = True):
def _codeql_pack_install(name, srcs, install_dest = None, build_file_label = None, prefix = "", apply_pack_prefix = True):
"""
Create a runnable target `name` that installs the list of codeql packs given in `srcs` in `install_dest`,
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
_codeql_pack_group_extract_files(
name = internal("all-files"),
srcs = srcs,
prefix = prefix,
kind = "all",
apply_pack_prefix = apply_pack_prefix,
visibility = ["//visibility:private"],
@@ -309,6 +311,7 @@ def _codeql_pack_install(name, srcs, install_dest = None, build_file_label = Non
name = internal("all-extra-zips"),
kind = "all",
srcs = srcs,
prefix = prefix,
apply_pack_prefix = apply_pack_prefix,
visibility = ["//visibility:private"],
)
@@ -351,7 +354,7 @@ def _codeql_pack_install(name, srcs, install_dest = None, build_file_label = Non
(["--destdir", "\"%s\"" % install_dest] if install_dest else []),
)
def codeql_pack_group(name, srcs, visibility = None, skip_installer = False, install_dest = None, build_file_label = None, compression_level = 6):
def codeql_pack_group(name, srcs, visibility = None, skip_installer = False, prefix = "", install_dest = None, build_file_label = None, compression_level = 6):
"""
Create a group of codeql packs of name `name`.
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
The install destination can be overridden appending `-- --destdir=...` to the `bazel run` invocation.
The installer target will be omitted if `skip_installer` is set to `True`.
Prefixes all paths in the pack group with `prefix`.
The compression level of the generated zip files can be set with `compression_level`. Note that this doesn't affect the compression
level of extra zip files that are added to a pack, as thes files will not be re-compressed.
"""
@@ -373,6 +378,7 @@ def codeql_pack_group(name, srcs, visibility = None, skip_installer = False, ins
name = internal(kind),
srcs = srcs,
kind = kind,
prefix = prefix,
visibility = ["//visibility:private"],
)
pkg_zip(
@@ -385,6 +391,7 @@ def codeql_pack_group(name, srcs, visibility = None, skip_installer = False, ins
name = internal(kind, "extra-zips"),
kind = kind,
srcs = srcs,
prefix = prefix,
visibility = ["//visibility:private"],
)
_zipmerge(
@@ -394,7 +401,7 @@ def codeql_pack_group(name, srcs, visibility = None, skip_installer = False, ins
visibility = visibility,
)
if not skip_installer:
_codeql_pack_install(name, srcs, build_file_label = build_file_label, install_dest = install_dest, apply_pack_prefix = True)
_codeql_pack_install(name, srcs, build_file_label = build_file_label, install_dest = install_dest, prefix = prefix, apply_pack_prefix = True)
def codeql_pack(
*,