Skip to content

Commit b744f9f

Browse files
authored
Merge pull request #16544 from github/redsun82/bazel-csharp-2
Bazel/C#: avoid zipmerge
2 parents 730d542 + 3c52e3b commit b744f9f

File tree

3 files changed

+48
-12
lines changed

3 files changed

+48
-12
lines changed

csharp/BUILD.bazel

+11-8
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

-4
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,10 @@ def codeql_csharp_binary(name, **kwargs):
6161
),
6262
)
6363

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

misc/bazel/pkg.bzl

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

0 commit comments

Comments
 (0)