Skip to content

Commit e2facf8

Browse files
committed
Build: libbuffer's CMake follows its sibling libraries' pattern
CMake 3.16 with the CMAKE_SOURCE_DIR top-level test, one option per sanitizer (LIBBUFFER_ENABLE_ASAN, _UBSAN, _TSAN) and LIBBUFFER_ENABLE_COVERAGE in place of the LIBBUFFER_SANITIZE list, all instrumentation behind the libbuffer_build_flags target, and the presets named as theirs (dev, san, tsan, cov, rel, release). Version 0.1.2. No change to the library's code.
1 parent 0a1f06c commit e2facf8

6 files changed

Lines changed: 121 additions & 45 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@ jobs:
1616
include:
1717
- { name: "linux-gcc", os: ubuntu-latest, cc: gcc, cxx: g++, extra: "" }
1818
- { name: "linux-clang", os: ubuntu-latest, cc: clang, cxx: clang++, extra: "" }
19-
- { name: "linux-asan", os: ubuntu-latest, cc: clang, cxx: clang++,
20-
extra: "-DLIBBUFFER_SANITIZE=address;undefined" }
19+
- { name: "linux-san", os: ubuntu-latest, cc: clang, cxx: clang++,
20+
extra: "-DLIBBUFFER_ENABLE_ASAN=ON -DLIBBUFFER_ENABLE_UBSAN=ON" }
2121
- { name: "linux-tsan", os: ubuntu-latest, cc: clang, cxx: clang++,
22-
extra: "-DLIBBUFFER_SANITIZE=thread" }
22+
extra: "-DLIBBUFFER_ENABLE_TSAN=ON" }
2323
- { name: "macos", os: macos-latest, cc: clang, cxx: clang++, extra: "" }
2424
- { name: "windows", os: windows-latest, cc: "", cxx: "", extra: "" }
2525
steps:
2626
- uses: actions/checkout@v4
2727
- name: No host names
2828
run: cmake -DROOT=. -P cmake/no_host_names.cmake
2929
- name: Configure
30-
run: cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug "${{ matrix.extra }}"
30+
run: cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug ${{ matrix.extra }}
3131
shell: bash
3232
env:
3333
CC: ${{ matrix.cc }}

‎CMakeLists.txt‎

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# SPDX-License-Identifier: MIT
22
# Copyright (C) 2026 PinPoint Golf
3-
cmake_minimum_required(VERSION 3.21)
3+
cmake_minimum_required(VERSION 3.16)
44

55
project(libbuffer
6-
VERSION 0.1.1
6+
VERSION 0.1.2
77
DESCRIPTION "Lock-free multi-source capture buffer: per-source seqlock rings merged onto one timeline"
88
LANGUAGES CXX)
99

@@ -12,28 +12,43 @@ project(libbuffer
1212
#
1313
# ⚠ THE CACHE IS NOT DIRECTORY-SCOPED: one cache per build tree, so anything below that could reach into a
1414
# consumer's build is gated on this. Embedding libbuffer must change nothing about the consumer: not its build type,
15-
# its test registry or its install manifest.
15+
# its test registry or its install manifest. Spelled as a CMAKE_SOURCE_DIR comparison rather than
16+
# PROJECT_IS_TOP_LEVEL because that variable is CMake 3.21+ and this project promises 3.16.
1617
# ---------------------------------------------------------------------------
17-
set(LIBBUFFER_IS_TOP_LEVEL ${PROJECT_IS_TOP_LEVEL})
18+
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
19+
set(LIBBUFFER_IS_TOP_LEVEL ON)
20+
else()
21+
set(LIBBUFFER_IS_TOP_LEVEL OFF)
22+
endif()
1823

19-
option(LIBBUFFER_BUILD_TESTS "Build the automated test suite and the latency benchmark" ${LIBBUFFER_IS_TOP_LEVEL})
24+
option(LIBBUFFER_BUILD_TESTS "Build the automated test suite and the latency benchmark" ${LIBBUFFER_IS_TOP_LEVEL})
25+
option(LIBBUFFER_ENABLE_ASAN "Build with AddressSanitizer" OFF)
26+
option(LIBBUFFER_ENABLE_UBSAN "Build with UndefinedBehaviorSanitizer" OFF)
27+
option(LIBBUFFER_ENABLE_TSAN "Build with ThreadSanitizer" OFF)
28+
option(LIBBUFFER_ENABLE_COVERAGE "Build with gcov coverage instrumentation" OFF)
2029
# ⚠ -Werror is a CI gate, not a property of the code: a consumer's newer compiler must not break their build over
2130
# a warning they cannot fix.
22-
option(LIBBUFFER_WERROR "Treat warnings as errors" ${LIBBUFFER_IS_TOP_LEVEL})
23-
option(LIBBUFFER_INSTALL "Generate the install rules" ${LIBBUFFER_IS_TOP_LEVEL})
24-
# A list: "address;undefined", "thread", or empty. Applied to the library and its tests. A consumer that
25-
# instruments its own targets passes its list through, so the ring code it links is instrumented too.
26-
set(LIBBUFFER_SANITIZE "" CACHE STRING "Sanitizers for the library and its tests (address;undefined, or thread)")
31+
option(LIBBUFFER_WERROR "Treat warnings as errors" ${LIBBUFFER_IS_TOP_LEVEL})
32+
option(LIBBUFFER_INSTALL "Generate the install rules" ${LIBBUFFER_IS_TOP_LEVEL})
2733

34+
# Directory-scoped, so these reach no further than this subtree.
35+
set(CMAKE_CXX_STANDARD 20)
36+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
37+
set(CMAKE_CXX_EXTENSIONS OFF)
2838
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
39+
2940
if(LIBBUFFER_IS_TOP_LEVEL AND NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
3041
set(CMAKE_BUILD_TYPE Debug CACHE STRING "" FORCE)
3142
endif()
3243

3344
find_package(Threads REQUIRED)
3445

35-
# Warnings through an INTERFACE target: never leaked to consumers.
46+
# ---------------------------------------------------------------------------
47+
# Warning and instrumentation flags, behind an INTERFACE target so nothing leaks to a consumer. The library and its
48+
# tests link it, so a sanitizer instruments both (a consumer that instruments its own code turns the same option on).
49+
# ---------------------------------------------------------------------------
3650
add_library(libbuffer_build_flags INTERFACE)
51+
3752
if(MSVC)
3853
target_compile_options(libbuffer_build_flags INTERFACE /W4 /permissive-)
3954
if(LIBBUFFER_WERROR)
@@ -44,15 +59,23 @@ else()
4459
if(LIBBUFFER_WERROR)
4560
target_compile_options(libbuffer_build_flags INTERFACE -Werror)
4661
endif()
47-
endif()
48-
49-
# Sanitizers, for the library and (through libbuffer_build_flags) its tests.
50-
add_library(libbuffer_sanitize INTERFACE)
51-
if(LIBBUFFER_SANITIZE AND NOT MSVC)
52-
string(REPLACE ";" "," _lb_san "${LIBBUFFER_SANITIZE}")
53-
target_compile_options(libbuffer_sanitize INTERFACE -fsanitize=${_lb_san} -fno-omit-frame-pointer)
54-
target_link_options(libbuffer_sanitize INTERFACE -fsanitize=${_lb_san})
55-
unset(_lb_san)
62+
if(LIBBUFFER_ENABLE_ASAN)
63+
target_compile_options(libbuffer_build_flags INTERFACE -fsanitize=address -fno-omit-frame-pointer)
64+
target_link_options(libbuffer_build_flags INTERFACE -fsanitize=address)
65+
endif()
66+
if(LIBBUFFER_ENABLE_UBSAN)
67+
target_compile_options(libbuffer_build_flags INTERFACE -fsanitize=undefined -fno-sanitize-recover=all)
68+
target_link_options(libbuffer_build_flags INTERFACE -fsanitize=undefined)
69+
endif()
70+
# ⚠ ThreadSanitizer cannot be combined with AddressSanitizer: turn on one or the other.
71+
if(LIBBUFFER_ENABLE_TSAN)
72+
target_compile_options(libbuffer_build_flags INTERFACE -fsanitize=thread)
73+
target_link_options(libbuffer_build_flags INTERFACE -fsanitize=thread)
74+
endif()
75+
if(LIBBUFFER_ENABLE_COVERAGE)
76+
target_compile_options(libbuffer_build_flags INTERFACE --coverage -O0 -g)
77+
target_link_options(libbuffer_build_flags INTERFACE --coverage)
78+
endif()
5679
endif()
5780

5881
add_library(libbuffer STATIC
@@ -71,7 +94,7 @@ target_include_directories(libbuffer PUBLIC
7194
target_compile_features(libbuffer PUBLIC cxx_std_20)
7295
target_link_libraries(libbuffer
7396
PUBLIC Threads::Threads
74-
PRIVATE $<BUILD_INTERFACE:libbuffer_build_flags> $<BUILD_INTERFACE:libbuffer_sanitize>)
97+
PRIVATE $<BUILD_INTERFACE:libbuffer_build_flags>)
7598
if(WIN32)
7699
# The merger raises the timer resolution while capturing (timeBeginPeriod).
77100
target_link_libraries(libbuffer PRIVATE winmm)

‎CMakePresets.json‎

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,77 @@
11
{
22
"version": 3,
33
"cmakeMinimumRequired": { "major": 3, "minor": 21, "patch": 0 },
4+
"vendor": {
5+
"spdx": {
6+
"SPDX-License-Identifier": "MIT",
7+
"copyright": "Copyright (C) 2026 PinPoint Golf",
8+
"note": "JSON carries no comment syntax; `vendor` is the presets schema's own escape hatch for data CMake must ignore."
9+
}
10+
},
411
"configurePresets": [
5-
{ "name": "dev", "displayName": "Debug, warnings as errors", "binaryDir": "${sourceDir}/build/dev",
6-
"cacheVariables": { "CMAKE_BUILD_TYPE": "Debug" } },
7-
{ "name": "asan", "displayName": "Debug + ASan + UBSan", "binaryDir": "${sourceDir}/build/asan",
8-
"cacheVariables": { "CMAKE_BUILD_TYPE": "Debug", "LIBBUFFER_SANITIZE": "address;undefined" } },
9-
{ "name": "tsan", "displayName": "Debug + TSan", "binaryDir": "${sourceDir}/build/tsan",
10-
"cacheVariables": { "CMAKE_BUILD_TYPE": "Debug", "LIBBUFFER_SANITIZE": "thread" } },
11-
{ "name": "release", "displayName": "Release: the build the latency benchmark is measured in",
12-
"binaryDir": "${sourceDir}/build/release", "cacheVariables": { "CMAKE_BUILD_TYPE": "Release" } }
12+
{
13+
"name": "dev",
14+
"displayName": "Debug, warnings as errors",
15+
"binaryDir": "${sourceDir}/build/dev",
16+
"cacheVariables": { "CMAKE_BUILD_TYPE": "Debug" }
17+
},
18+
{
19+
"name": "san",
20+
"displayName": "Debug + ASan + UBSan",
21+
"binaryDir": "${sourceDir}/build/san",
22+
"cacheVariables": {
23+
"CMAKE_BUILD_TYPE": "Debug",
24+
"LIBBUFFER_ENABLE_ASAN": "ON",
25+
"LIBBUFFER_ENABLE_UBSAN": "ON"
26+
}
27+
},
28+
{
29+
"name": "tsan",
30+
"displayName": "Debug + TSan",
31+
"description": "ThreadSanitizer, on its own (it cannot be combined with ASan). The tests set TSAN_OPTIONS to tests/tsan_suppressions.txt.",
32+
"binaryDir": "${sourceDir}/build/tsan",
33+
"cacheVariables": {
34+
"CMAKE_BUILD_TYPE": "Debug",
35+
"LIBBUFFER_ENABLE_TSAN": "ON"
36+
}
37+
},
38+
{
39+
"name": "cov",
40+
"displayName": "Debug + gcov",
41+
"binaryDir": "${sourceDir}/build/cov",
42+
"cacheVariables": {
43+
"CMAKE_BUILD_TYPE": "Debug",
44+
"LIBBUFFER_ENABLE_COVERAGE": "ON"
45+
}
46+
},
47+
{
48+
"name": "rel",
49+
"displayName": "RelWithDebInfo — optimised, keeps debug info",
50+
"description": "The build to configure when an optimised fault needs a symbolised stack. Not the shipping artefact; that is `release`.",
51+
"binaryDir": "${sourceDir}/build/rel",
52+
"cacheVariables": { "CMAKE_BUILD_TYPE": "RelWithDebInfo" }
53+
},
54+
{
55+
"name": "release",
56+
"displayName": "Release — optimised, no debug info",
57+
"description": "-O3 -DNDEBUG: the build the latency benchmark is measured in.",
58+
"binaryDir": "${sourceDir}/build/release",
59+
"cacheVariables": { "CMAKE_BUILD_TYPE": "Release" }
60+
}
1361
],
1462
"buildPresets": [
15-
{ "name": "dev", "configurePreset": "dev" },
16-
{ "name": "asan", "configurePreset": "asan" },
17-
{ "name": "tsan", "configurePreset": "tsan" },
18-
{ "name": "release", "configurePreset": "release" }
63+
{ "name": "dev", "configurePreset": "dev", "jobs": 4 },
64+
{ "name": "san", "configurePreset": "san", "jobs": 4 },
65+
{ "name": "tsan", "configurePreset": "tsan", "jobs": 4 },
66+
{ "name": "cov", "configurePreset": "cov", "jobs": 4 },
67+
{ "name": "rel", "configurePreset": "rel", "jobs": 4 },
68+
{ "name": "release", "configurePreset": "release", "jobs": 4 }
1969
],
2070
"testPresets": [
2171
{ "name": "dev", "configurePreset": "dev", "output": { "outputOnFailure": true } },
22-
{ "name": "asan", "configurePreset": "asan", "output": { "outputOnFailure": true } },
23-
{ "name": "tsan", "configurePreset": "tsan", "output": { "outputOnFailure": true } }
72+
{ "name": "san", "configurePreset": "san", "output": { "outputOnFailure": true } },
73+
{ "name": "tsan", "configurePreset": "tsan", "output": { "outputOnFailure": true } },
74+
{ "name": "cov", "configurePreset": "cov", "output": { "outputOnFailure": true } },
75+
{ "name": "rel", "configurePreset": "rel", "output": { "outputOnFailure": true } }
2476
]
2577
}

‎README.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,13 @@ Embedded, the library builds itself and nothing else: no tests, no `-Werror`, no
6969

7070
```sh
7171
cmake --preset dev && cmake --build build/dev && ctest --preset dev
72-
cmake --preset asan && cmake --build build/asan && ctest --preset asan # ASan + UBSan
72+
cmake --preset san && cmake --build build/san && ctest --preset san # ASan + UBSan
7373
cmake --preset tsan && cmake --build build/tsan && ctest --preset tsan # TSan
7474
cmake --preset release && cmake --build build/release && build/release/tests/latency_benchmark
7575
```
7676

77-
`LIBBUFFER_SANITIZE` takes a list (`address;undefined`, or `thread`). The seqlock's deliberate, validated races are
77+
The sanitizers are `LIBBUFFER_ENABLE_ASAN`, `LIBBUFFER_ENABLE_UBSAN` and `LIBBUFFER_ENABLE_TSAN` (TSan on its own), and
78+
`LIBBUFFER_ENABLE_COVERAGE` builds with gcov (the `cov` preset). The seqlock's deliberate, validated races are
7879
suppressed by name in `tests/tsan_suppressions.txt`; the test targets set `TSAN_OPTIONS` for you.
7980

8081
`no_host_names` (a ctest, and a CI step) keeps the library general: it fails on the names of the application it was

‎include/libbuffer/version.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55

66
#define LIBBUFFER_VERSION_MAJOR 0
77
#define LIBBUFFER_VERSION_MINOR 1
8-
#define LIBBUFFER_VERSION_PATCH 1
9-
#define LIBBUFFER_VERSION "0.1.1"
8+
#define LIBBUFFER_VERSION_PATCH 2
9+
#define LIBBUFFER_VERSION "0.1.2"

‎tests/CMakeLists.txt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ set(LIBBUFFER_TSAN_ENV "TSAN_OPTIONS=suppressions=${CMAKE_CURRENT_SOURCE_DIR}/ts
1717

1818
function(libbuffer_add_test name)
1919
add_executable(${name} ${name}.cpp)
20-
target_link_libraries(${name} PRIVATE libbuffer libbuffer_build_flags libbuffer_sanitize GTest::gtest_main)
20+
target_link_libraries(${name} PRIVATE libbuffer libbuffer_build_flags GTest::gtest_main)
2121
add_test(NAME ${name} COMMAND ${name})
2222
set_tests_properties(${name} PROPERTIES ENVIRONMENT "${LIBBUFFER_TSAN_ENV}")
2323
endfunction()
@@ -35,7 +35,7 @@ libbuffer_add_test(thread_policy_test)
3535
# latency_benchmark: not a gtest and not a ctest (its numbers are what is read, not a pass). Run it on a Release
3636
# build (the `release` preset): build/release/tests/latency_benchmark [--merger|--e2e]
3737
add_executable(latency_benchmark latency_benchmark.cpp)
38-
target_link_libraries(latency_benchmark PRIVATE libbuffer libbuffer_build_flags libbuffer_sanitize)
38+
target_link_libraries(latency_benchmark PRIVATE libbuffer libbuffer_build_flags)
3939

4040
# No host application's names in the library (README, "Scope"): the check CI runs, as a ctest too.
4141
add_test(NAME no_host_names

0 commit comments

Comments
 (0)