Skip to content

Commit 82e658c

Browse files
Copilotphlax
andauthored
Add bzlmod extension for sanitizer_libs (#3404)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: phlax <454682+phlax@users.noreply.github.com>
1 parent dd3e129 commit 82e658c

File tree

4 files changed

+109
-1
lines changed

4 files changed

+109
-1
lines changed

bazel/MODULE.bazel

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@ sysroot_ext = use_extension("//sysroot:extensions.bzl", "sysroot_extension", dev
6868
sysroot_ext.setup()
6969
use_repo(sysroot_ext, "sysroot_linux_amd64", "sysroot_linux_arm64")
7070

71+
# Setup sanitizer libraries (MSAN and TSAN) - example for downstream consumers
72+
# Uncomment to use in your MODULE.bazel:
73+
# sanitizer_ext = use_extension("@envoy_toolshed//compile:extensions.bzl", "sanitizer_extension")
74+
# sanitizer_ext.setup() # Uses default versions from VERSIONS
75+
# # Or with custom versions:
76+
# # sanitizer_ext.setup(
77+
# # msan_version = "0.1.34",
78+
# # msan_sha256 = "534e5e6893f177f891d78d6e85a80c680c84f0abd64681f8ddbf2f5457e97a52",
79+
# # tsan_version = "0.1.34",
80+
# # tsan_sha256 = "2cd571a07014972ff9bc0f189c5725c2ea121aeab0daa4c27ef171842ea13985",
81+
# # )
82+
# use_repo(sanitizer_ext, "msan_libs", "tsan_libs")
83+
7184
# Configure LLVM toolchain
7285
llvm = use_extension("@toolchains_llvm//toolchain/extensions:llvm.bzl", "llvm", dev_dependency=True)
7386
llvm.toolchain(

bazel/compile/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ load("//:versions.bzl", "VERSIONS")
55

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

8-
exports_files(["sanitizer_libs.bzl"])
8+
exports_files(["sanitizer_libs.bzl", "extensions.bzl"])
99

1010
SANITIZER_ENV = {
1111
"CXXFLAGS": "-nostdinc++ -nostdlib++",

bazel/compile/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,41 @@ The sanitizer libraries are automatically built and published to GitHub releases
3838
"msan_libs_sha256": "...", # Add actual SHA256
3939
"tsan_libs_sha256": "...", # Add actual SHA256
4040
```
41+
42+
## Using with WORKSPACE
43+
44+
In your WORKSPACE file:
45+
46+
```starlark
47+
load("@envoy_toolshed//compile:sanitizer_libs.bzl", "setup_sanitizer_libs")
48+
49+
setup_sanitizer_libs()
50+
```
51+
52+
This will create `@msan_libs` and `@tsan_libs` repositories you can use in your builds.
53+
54+
## Using with bzlmod (MODULE.bazel)
55+
56+
In your MODULE.bazel file:
57+
58+
```starlark
59+
bazel_dep(name = "envoy_toolshed", version = "0.3.12")
60+
61+
# Setup sanitizer libraries
62+
sanitizer_ext = use_extension("@envoy_toolshed//compile:extensions.bzl", "sanitizer_extension")
63+
sanitizer_ext.setup() # Uses default versions
64+
use_repo(sanitizer_ext, "msan_libs", "tsan_libs")
65+
```
66+
67+
Or with custom versions:
68+
69+
```starlark
70+
sanitizer_ext = use_extension("@envoy_toolshed//compile:extensions.bzl", "sanitizer_extension")
71+
sanitizer_ext.setup(
72+
msan_version = "0.1.34",
73+
msan_sha256 = "534e5e6893f177f891d78d6e85a80c680c84f0abd64681f8ddbf2f5457e97a52",
74+
tsan_version = "0.1.34",
75+
tsan_sha256 = "2cd571a07014972ff9bc0f189c5725c2ea121aeab0daa4c27ef171842ea13985",
76+
)
77+
use_repo(sanitizer_ext, "msan_libs", "tsan_libs")
78+
```

bazel/compile/extensions.bzl

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""Module extension for sanitizer libraries configuration in bzlmod."""
2+
3+
load(":sanitizer_libs.bzl", "setup_sanitizer_libs")
4+
5+
def _sanitizer_libs_impl(module_ctx):
6+
"""Implementation of the sanitizer_libs module extension.
7+
8+
This extension allows configuring sanitizer libraries in MODULE.bazel using
9+
the same setup_sanitizer_libs() function used in WORKSPACE.
10+
"""
11+
12+
# Collect all setup tags from all modules
13+
# Only use the first tag found (sanitizer repos have fixed names)
14+
setup_tag = None
15+
for mod in module_ctx.modules:
16+
for tag in mod.tags.setup:
17+
if setup_tag == None:
18+
setup_tag = tag
19+
else:
20+
# Fail if multiple tags are found
21+
fail("Multiple setup() calls found for sanitizer_extension. Only one configuration is allowed since repository names are fixed to @msan_libs and @tsan_libs.")
22+
23+
# Call setup_sanitizer_libs once with the configuration
24+
if setup_tag:
25+
setup_sanitizer_libs(
26+
msan_version = setup_tag.msan_version,
27+
msan_sha256 = setup_tag.msan_sha256,
28+
tsan_version = setup_tag.tsan_version,
29+
tsan_sha256 = setup_tag.tsan_sha256,
30+
)
31+
else:
32+
# Use default configuration if no tags specified
33+
setup_sanitizer_libs()
34+
35+
_setup = tag_class(
36+
attrs = {
37+
"msan_version": attr.string(
38+
doc = "Version of MSAN release to use (default: VERSIONS['bins_release'] from //:versions.bzl)",
39+
),
40+
"msan_sha256": attr.string(
41+
doc = "SHA256 hash of the MSAN libs archive (default: VERSIONS['msan_libs_sha256'] from //:versions.bzl)",
42+
),
43+
"tsan_version": attr.string(
44+
doc = "Version of TSAN release to use (default: VERSIONS['bins_release'] from //:versions.bzl)",
45+
),
46+
"tsan_sha256": attr.string(
47+
doc = "SHA256 hash of the TSAN libs archive (default: VERSIONS['tsan_libs_sha256'] from //:versions.bzl)",
48+
),
49+
},
50+
)
51+
52+
sanitizer_extension = module_extension(
53+
implementation = _sanitizer_libs_impl,
54+
tag_classes = {
55+
"setup": _setup,
56+
},
57+
)

0 commit comments

Comments
 (0)