Skip to content

Commit a63f0c1

Browse files
authored
feat: add support for bazel (#759)
1 parent 6325ede commit a63f0c1

File tree

7 files changed

+249
-4
lines changed

7 files changed

+249
-4
lines changed

.bazelrc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
common --enable_platform_specific_config
2+
3+
build --cxxopt=-std=c++2a
4+
5+
# Show everything when running tests.
6+
test --test_output=streamed
7+
8+
build:macos --macos_minimum_os=10.15
9+
build:macos --no@fuzztest//fuzztest:use_riegeli
10+
11+
try-import %workspace%/fuzztest.bazelrc

.github/workflows/main.yml

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ jobs:
107107
mkdir libs
108108
cp libsnmallocshim*.so libs
109109
for lib in `ls libs`; do echo; echo Testing $lib; ninja clean; LD_PRELOAD=libs/$lib ninja libsnmallocshim.so; done
110+
- uses: bazelbuild/setup-bazelisk@v3
111+
- name: Mount bazel cache # Optional
112+
uses: actions/cache@v4
113+
with:
114+
path: "~/.cache/bazel"
115+
key: bazel
116+
- run: bazel build -c opt //:snmalloc
117+
- run: bazel build -c opt //:snmalloc-rs
110118

111119
# If this looks remarkably familiar, that's because it is. Sigh.
112120
macos:
@@ -151,6 +159,14 @@ jobs:
151159
mkdir libs
152160
cp libsnmallocshim*.so libs
153161
for lib in `ls libs`; do echo; echo Testing $lib; ninja clean; LD_PRELOAD=libs/$lib ninja libsnmallocshim.so; done
162+
- uses: bazelbuild/setup-bazelisk@v3
163+
- name: Mount bazel cache # Optional
164+
uses: actions/cache@v4
165+
with:
166+
path: "~/.cache/bazel"
167+
key: bazel
168+
- run: bazel build -c opt //:snmalloc
169+
- run: bazel build -c opt //:snmalloc-rs
154170

155171

156172
# GitHub doesn't natively support *BSD, but we can run them in VMs on Mac /
@@ -448,7 +464,6 @@ jobs:
448464
run: ctest -j 2 --interactive-debug-mode 0 --output-on-failure -C ${{ matrix.build-type }} --timeout 400
449465
timeout-minutes: 20
450466

451-
452467
# Job to run clang-format and report errors
453468
format:
454469
runs-on: ubuntu-22.04
@@ -488,6 +503,13 @@ jobs:
488503
run: cmake --build ${{github.workspace}}/build --target snmalloc-fuzzer
489504
- name: Test
490505
run: ${{github.workspace}}/build/fuzzing/snmalloc-fuzzer
506+
- uses: bazelbuild/setup-bazelisk@v3
507+
- name: Mount bazel cache # Optional
508+
uses: actions/cache@v4
509+
with:
510+
path: "~/.cache/bazel"
511+
key: bazel
512+
- run: bazel test -c opt --config=asan //fuzzing:snmalloc_fuzzer
491513

492514
self-vendored:
493515
strategy:

BUILD.bazel

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake")
2+
3+
filegroup(
4+
name = "srcs",
5+
srcs = glob(
6+
[
7+
"src/snmalloc/**/*",
8+
"src/test/*.h",
9+
"CMakeLists.txt",
10+
],
11+
),
12+
visibility = ["//visibility:private"],
13+
)
14+
15+
config_setting(
16+
name = "release_with_debug",
17+
values = {
18+
"compilation_mode": "fastbuild",
19+
},
20+
)
21+
22+
config_setting(
23+
name = "release",
24+
values = {
25+
"compilation_mode": "opt",
26+
},
27+
)
28+
29+
config_setting(
30+
name = "debug",
31+
values = {
32+
"compilation_mode": "dbg",
33+
},
34+
)
35+
36+
CMAKE_FLAGS = {
37+
"CMAKE_INTERPROCEDURAL_OPTIMIZATION": "TRUE",
38+
"SNMALLOC_OPTIMISE_FOR_CURRENT_MACHINE": "ON",
39+
"SNMALLOC_USE_SELF_VENDORED_STL": "OFF",
40+
"SNMALLOC_IPO": "ON",
41+
"USE_SNMALLOC_STATS": "ON",
42+
} | select({
43+
":release_with_debug": {"CMAKE_BUILD_TYPE": "RelWithDebInfo"},
44+
":release": {"CMAKE_BUILD_TYPE": "Release"},
45+
":debug": {"CMAKE_BUILD_TYPE": "Debug"},
46+
"//conditions:default": {"CMAKE_BUILD_TYPE": "Release"},
47+
})
48+
49+
cmake(
50+
name = "snmalloc",
51+
cache_entries = CMAKE_FLAGS,
52+
generate_args = ["-G Ninja"],
53+
lib_source = ":srcs",
54+
out_shared_libs = select({
55+
"@bazel_tools//src/conditions:darwin": [
56+
"libsnmallocshim-checks-memcpy-only.dylib",
57+
"libsnmallocshim-checks.dylib",
58+
"libsnmallocshim.dylib",
59+
],
60+
"//conditions:default": [],
61+
}),
62+
out_static_libs = [
63+
"libsnmallocshim-static.a",
64+
"libsnmalloc-new-override.a",
65+
],
66+
postfix_script = "ninja",
67+
visibility = ["//visibility:public"],
68+
)
69+
70+
cmake(
71+
name = "snmalloc-rs",
72+
cache_entries = CMAKE_FLAGS | {
73+
"SNMALLOC_RUST_SUPPORT": "ON",
74+
},
75+
generate_args = ["-G Ninja"],
76+
lib_source = ":srcs",
77+
out_shared_libs = select({
78+
"@bazel_tools//src/conditions:darwin": [
79+
"libsnmallocshim-checks-memcpy-only.dylib",
80+
"libsnmallocshim-checks.dylib",
81+
"libsnmallocshim.dylib",
82+
],
83+
"//conditions:default": [],
84+
}),
85+
out_static_libs = [
86+
"libsnmallocshim-static.a",
87+
"libsnmalloc-new-override.a",
88+
],
89+
postfix_script = "ninja",
90+
visibility = ["//visibility:public"],
91+
)

CMakeLists.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -659,19 +659,24 @@ install(TARGETS EXPORT snmallocConfig DESTINATION ${CMAKE_INSTALL_LIBDIR}
659659
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/snmalloc)
660660

661661
install(DIRECTORY src/snmalloc/aal DESTINATION include/snmalloc)
662-
install(DIRECTORY src/snmalloc/ds DESTINATION include/snmalloc)
663-
install(DIRECTORY src/snmalloc/override DESTINATION include/snmalloc)
664662
install(DIRECTORY src/snmalloc/backend DESTINATION include/snmalloc)
663+
install(DIRECTORY src/snmalloc/backend_helpers DESTINATION include/snmalloc)
664+
install(DIRECTORY src/snmalloc/ds DESTINATION include/snmalloc)
665+
install(DIRECTORY src/snmalloc/ds_aal DESTINATION include/snmalloc)
666+
install(DIRECTORY src/snmalloc/ds_core DESTINATION include/snmalloc)
667+
install(DIRECTORY src/snmalloc/global DESTINATION include/snmalloc)
665668
install(DIRECTORY src/snmalloc/mem DESTINATION include/snmalloc)
669+
install(DIRECTORY src/snmalloc/override DESTINATION include/snmalloc)
666670
install(DIRECTORY src/snmalloc/pal DESTINATION include/snmalloc)
671+
install(DIRECTORY src/snmalloc/stl DESTINATION include/snmalloc)
667672
install(FILES
668673
src/test/measuretime.h
669674
src/test/opt.h
670675
src/test/setup.h
671676
src/test/usage.h
672677
src/test/xoroshiro.h
673678
DESTINATION include/snmalloc/test
674-
)
679+
)
675680
install(FILES src/snmalloc/snmalloc.h;src/snmalloc/snmalloc_core.h;src/snmalloc/snmalloc_front.h DESTINATION include/snmalloc)
676681

677682
install(EXPORT snmallocConfig

MODULE.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module(name = "snmalloc")
2+
3+
bazel_dep(name = "rules_cc", version = "0.1.1")
4+
bazel_dep(name = "rules_foreign_cc", version = "0.14.0")
5+
bazel_dep(name = "fuzztest", version = "20250214.0")
6+
bazel_dep(name = "googletest", version = "1.16.0")

fuzzing/BUILD.bazel

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
load("@rules_cc//cc:defs.bzl", "cc_test")
2+
3+
# will raise `ERROR: AddressSanitizer: SEGV on unknown address` if run without -c opt
4+
# --config=asan adds AddressSanitizer libs
5+
# bazel test -c opt --config=asan //fuzzing:snmalloc_fuzzer
6+
cc_test(
7+
name = "snmalloc_fuzzer",
8+
srcs = ["snmalloc-fuzzer.cpp"],
9+
copts = [
10+
"-fsanitize=address",
11+
] + select({
12+
"@bazel_tools//tools/cpp:clang-cl": ["-fexperimental-library"], # needed for std::execution::unseq,
13+
"//conditions:default": ["-mcx16"],
14+
}),
15+
defines = [
16+
"SNMALLOC_USE_WAIT_ON_ADDRESS=0",
17+
"ADDRESS_SANITIZER",
18+
],
19+
linkstatic = True,
20+
malloc = "//:snmalloc",
21+
deps = [
22+
"@fuzztest//fuzztest",
23+
"@fuzztest//fuzztest:fuzztest_gtest_main",
24+
],
25+
)

fuzztest.bazelrc

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
### DO NOT EDIT. Generated file.
2+
#
3+
# To regenerate, run the following from your project's workspace:
4+
#
5+
# bazel run @com_google_fuzztest//bazel:setup_configs > fuzztest.bazelrc
6+
#
7+
# And don't forget to add the following to your project's .bazelrc:
8+
#
9+
# try-import %workspace%/fuzztest.bazelrc
10+
11+
### Common options.
12+
#
13+
# Do not use directly.
14+
15+
# Standard define for \"ifdef-ing\" any fuzz test specific code.
16+
build:fuzztest-common --copt=-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
17+
18+
# In fuzz tests, we want to catch assertion violations even in optimized builds.
19+
build:fuzztest-common --copt=-UNDEBUG
20+
21+
# Enable libc++ assertions.
22+
# See https://libcxx.llvm.org/UsingLibcxx.html#enabling-the-safe-libc-mode
23+
build:fuzztest-common --copt=-D_LIBCPP_ENABLE_ASSERTIONS=1
24+
25+
### ASan (Address Sanitizer) build configuration.
26+
#
27+
# Use with: --config=asan
28+
29+
build:asan --linkopt=-fsanitize=address
30+
build:asan --copt=-fsanitize=address
31+
32+
# We rely on the following flag instead of the compiler provided
33+
# __has_feature(address_sanitizer) to know that we have an ASAN build even in
34+
# the uninstrumented runtime.
35+
build:asan --copt=-DADDRESS_SANITIZER
36+
37+
### FuzzTest build configuration.
38+
#
39+
# Use with: --config=fuzztest
40+
#
41+
# Note that this configuration includes the ASan configuration.
42+
43+
build:fuzztest --config=asan
44+
build:fuzztest --config=fuzztest-common
45+
46+
# Link statically.
47+
build:fuzztest --dynamic_mode=off
48+
49+
# We apply coverage tracking instrumentation to everything but Centipede and the
50+
# FuzzTest framework itself (including GoogleTest and GoogleMock).
51+
build:fuzztest --copt=-fsanitize-coverage=inline-8bit-counters,trace-cmp,pc-table
52+
build:fuzztest --per_file_copt=common/.*,fuzztest/.*,centipede/.*,-centipede/.*fuzz_target,googletest/.*,googlemock/.*@-fsanitize-coverage=0
53+
54+
### Experimental FuzzTest build configuration.
55+
#
56+
# Use with: --config=fuzztest-experimental
57+
#
58+
# Use this instead of --config=fuzztest when building test binaries to run with
59+
# Centipede. Eventually, this will be consolidated with --config=fuzztest.
60+
# Note that this configuration doesn't include the ASan configuration. If you
61+
# want to use both, you can use --config=fuzztest-experimental --config=asan.
62+
63+
build:fuzztest-experimental --config=fuzztest-common
64+
build:fuzztest-experimental --@com_google_fuzztest//fuzztest:centipede_integration
65+
66+
# Generate line tables for debugging.
67+
build:fuzztest-experimental --copt=-gline-tables-only
68+
build:fuzztest-experimental --strip=never
69+
70+
# Prevent memcmp & co from being inlined.
71+
build:fuzztest-experimental --copt=-fno-builtin
72+
73+
# Disable heap checking.
74+
build:fuzztest-experimental --copt=-DHEAPCHECK_DISABLE
75+
76+
# Link statically.
77+
build:fuzztest-experimental --dynamic_mode=off
78+
79+
# We apply coverage tracking instrumentation to everything but Centipede and the
80+
# FuzzTest framework itself (including GoogleTest and GoogleMock).
81+
# TODO(b/374840534): Add -fsanitize-coverage=control-flow once we start building
82+
# with clang 16+.
83+
build:fuzztest-experimental --copt=-fsanitize-coverage=trace-pc-guard,pc-table,trace-loads,trace-cmp
84+
build:fuzztest-experimental --per_file_copt=common/.*,fuzztest/.*,centipede/.*,-centipede/.*fuzz_target,googletest/.*,googlemock/.*@-fsanitize-coverage=0
85+

0 commit comments

Comments
 (0)