Skip to content

Commit b19bf2f

Browse files
committed
Bazel/C#: avoid zipmerge
1 parent 731b941 commit b19bf2f

File tree

3 files changed

+46
-12
lines changed

3 files changed

+46
-12
lines changed

csharp/BUILD.bazel

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
load("@rules_pkg//pkg:mappings.bzl", "pkg_filegroup", "pkg_files")
2-
load("@semmle_code//:common.bzl", "zipmerge")
32
load("@semmle_code//:dist.bzl", "dist")
3+
load("//misc/bazel:pkg.bzl", "codeql_pkg_files_overlay")
44

55
package(default_visibility = ["//visibility:public"])
66

@@ -50,15 +50,18 @@ pkg_files(
5050
],
5151
)
5252

53-
# See `csharp.bzl` for an explanation of why we need zipmerge here
54-
zipmerge(
55-
name = "extractor-arch",
53+
codeql_pkg_files_overlay(
54+
name = "extractor-arch-overlay",
5655
srcs = [
57-
"//csharp/autobuilder/Semmle.Autobuild.CSharp:Semmle.Autobuild.CSharp.zip",
58-
"//csharp/extractor/Semmle.Extraction.CSharp.Driver:Semmle.Extraction.CSharp.Driver.zip",
59-
"//csharp/extractor/Semmle.Extraction.CSharp.Standalone:Semmle.Extraction.CSharp.Standalone.zip",
56+
"//csharp/autobuilder/Semmle.Autobuild.CSharp",
57+
"//csharp/extractor/Semmle.Extraction.CSharp.Driver",
58+
"//csharp/extractor/Semmle.Extraction.CSharp.Standalone",
6059
],
61-
out = "extractor-arch.zip",
60+
)
61+
62+
dist(
63+
name = "extractor-arch",
64+
srcs = [":extractor-arch-overlay"],
6265
)
6366

6467
dist(

misc/bazel/csharp.bzl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,10 @@ def codeql_csharp_binary(name, **kwargs):
6060
),
6161
)
6262

63-
# we need to declare individual zip targets for each binary, as `self_contained=True` means that every binary target
64-
# contributes the same runtime files. Bazel (rightfully) complains about this, so we work around this by creating individual zip files,
65-
# and using zipmerge to produce the final extractor-arch file. For overlapping files, zipmerge chooses just one.
6663
pack_zip(
6764
name = name,
6865
srcs = [publish_binary_target],
6966
prefix = "csharp/tools/" + codeql_platform,
7067
strip_prefix = strip_prefix.files_only(),
71-
compression_level = 0,
7268
visibility = visibility,
7369
)

misc/bazel/pkg.bzl

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
load("@rules_pkg//pkg:providers.bzl", "PackageFilegroupInfo", "PackageFilesInfo")
2+
3+
def _pkg_overlay_impl(ctx):
4+
destinations = {}
5+
files = []
6+
7+
for src in reversed(ctx.attr.srcs):
8+
pfi = src[PackageFilesInfo]
9+
dest_src_map = {k: v for k, v in pfi.dest_src_map.items() if k not in destinations}
10+
destinations.update({k: True for k in dest_src_map})
11+
if dest_src_map:
12+
new_pfi = PackageFilesInfo(
13+
dest_src_map = dest_src_map,
14+
attributes = pfi.attributes,
15+
)
16+
files.append((new_pfi, src.label))
17+
return [
18+
PackageFilegroupInfo(
19+
pkg_files = reversed(files),
20+
pkg_dirs = [],
21+
pkg_symlinks = [],
22+
),
23+
DefaultInfo(
24+
files = depset(transitive = [src[DefaultInfo].files for src in ctx.attr.srcs]),
25+
),
26+
]
27+
28+
codeql_pkg_files_overlay = rule(
29+
implementation = _pkg_overlay_impl,
30+
doc = "Combine `pkg_files` targets so that later targets overwrite earlier ones without warnings",
31+
attrs = {
32+
# this could be updated to handle PackageFilegroupInfo as well if we ever need it
33+
"srcs": attr.label_list(providers = [PackageFilesInfo, DefaultInfo]),
34+
},
35+
)

0 commit comments

Comments
 (0)