|
cpp_boilerplate 0.1.0
C++23 project template with Conan and CMake
|
Prerequisite: complete the README setup for your platform first.
Outcome: first-party code and dependencies built with matching sanitizer instrumentation, followed by the instrumented test suite.
Choose one mode:
The sanitizer run is complete when the selected target exits successfully after running the tests. If a sanitizer detects an error, it prints a stack trace, stops at the first finding, and returns a failure through CTest to the selected make target. Fix the reported source error, then rerun the same target.
CI runs make sanitize and make sanitize-tsan. On Linux it first sets vm.mmap_rnd_bits=28: the runner kernel randomizes mmap with more bits than the TSan runtime's memory layout allows. To install all four instrumented dependency graphs without running their workflows, use make bootstrap-sanitize. The default make bootstrap does not install them.
All four profiles inherit profiles/sanitize-common, which includes profiles/default, selects Debug, and defines the shared instrumentation flags and [runenv]. Each mode appends its own -fsanitize flags and shares conan.lock.
The custom compiler.sanitizer setting gives each instrumented dependency graph a distinct Conan package_id. Without it, --build=missing could reuse plain Debug dependencies. The setting defaults to null, which is omitted from package_id, so non-sanitizer builds do not change. First-party targets receive the same profile flags through the Conan toolchain.
Sanitizer runtime options such as halt_on_error and print_stacktrace live under [runenv] in profiles/sanitize-common. Conan injects them into the generated test presets inherited by the public sanitize* presets, keeping the runtime configuration out of CMakePresets.json.
Outcome: libFuzzer targets, built with ASan and UBSan, run for a fixed number of inputs by make fuzz and for a time limit by make fuzz-run.
split_fuzz splits its input at the delimiter the first byte names and checks three properties: split_views() and split_views_vec() agree, an input with n delimiters gives n + 1 fields (none when it is empty), and joining the fields with the delimiter gives the input back. A new target is a fuzz/*.cpp file with an add_fuzzer() call and a CTest entry in the fuzzers section of CMakeLists.txt, and its name in FUZZERS in the Makefile. Other presets define each fuzzer as an object library outside all, so it is in the debug compilation database and make lint checks it; only the fuzz preset links the executables. A target that includes LLVM's <fuzzer/FuzzedDataProvider.h> builds, but fails make lint in CI: the clang-tidy wheel CI pins has clang's resource headers without fuzzer/. Read the input bytes directly, as split_fuzz does.
make fuzz builds the fuzz preset in build/fuzz and runs its CTest tests, 100,000 inputs from a fixed seed. make fuzz-run then fuzzes each target for FUZZ_SECONDS, 60 by default, keeps the inputs that reach new code in build/fuzz/corpus/<target>, and writes an input that fails to build/fuzz/crashes/; build/fuzz/<target> <file> replays it. CI runs make fuzz-run on Linux and uploads any failing input.
libFuzzer comes with LLVM's Clang, so the toolchain depends on the platform:
| Platform | Toolchain |
|---|---|
| Linux | Clang and its compiler-rt; CI uses Ubuntu's clang-19 and libclang-rt-19-dev |
| macOS | Homebrew LLVM (brew install llvm), the default; AppleClang has no libFuzzer runtime |
| GCC | Not supported: GCC has no libFuzzer |
| Windows | Not supported: MSVC has /fsanitize=fuzzer, but the preset passes Clang flags |
FUZZ_CC and FUZZ_CXX name the compiler, clang and clang++ by default on Linux: make fuzz FUZZ_CC=clang-19 FUZZ_CXX=clang++-19. The preset reuses the debug Conan toolchain with the tests turned off, so the dependencies stay as the default compiler built them and only first-party code is instrumented. It compiles at -O1.
Tests are controlled by CMake's built-in BUILD_TESTING option from include(CTest). This project leaves it at the default ON, so make debug, make release, make sanitize*, and make coverage all run the GTest suite.
The main workflow presets are debug, release, sanitize, sanitize-asan, sanitize-ubsan, sanitize-tsan, coverage, and fuzz. Configure, build, and test presets use the same names. docs is configure/build only because it generates Doxygen HTML instead of compiling and testing the application.
The Conan-generated conan-* presets are internal implementation details and are not the public interface for developers or CI.
Outcome: a conan.lock with the newest versions conanfile.py allows, for reproducible builds.
conanfile.py gives each dependency a version range, and conan.lock pins the version and recipe revision every build installs. Conan reads the lock beside the recipe on its own, so a build or a conan lock create keeps each locked version its range still allows and resolves only the rest.
Update dependencies in this order:
The update is complete when the selected build and test workflow passes and conan.lock holds the intended versions. make lock-check, which CI runs, fails when the lock no longer satisfies conanfile.py or keeps entries the graph does not use; a new upstream release does not fail it.
Outcome: ccache keeps hitting when the Conan cache is rebuilt, locally and in CI.
make installs each Conan configuration with the full_deploy deployer, which copies every dependency to build/deps/<configuration>/full_deploy/ and points the generated CMake files there instead of into the Conan cache. Conan keeps a package it builds from source in a cache folder with a random name, so a rebuilt cache gives the compiler new include paths, and ccache, which hashes them, misses on every file that includes a dependency header. The copies keep the paths fixed.
Each configuration gets its own folder because full_deploy names its subfolders by build type and architecture alone, which the sanitizer configurations share with debug. A tree installed before the copies keeps the cache paths until its next install, after a lock change or a make clean.
The copies cost disk space and install time in proportion to the dependencies, a few megabytes for this template's. A project with much larger ones can drop $(CONAN_DEPLOY) from the Makefile's conan install commands and go back to the cache paths.
In CI, only pushes to main save the Conan cache and ccache; a pull request restores main's and saves nothing. The Windows job and the template check run conan install without the deployer, since neither uses ccache.
Use make format to rewrite supported files in place:
Then verify formatting and lint findings without modifying files:
The check is complete when both verification targets exit successfully. If make format-check fails, rerun make format and inspect its changes before checking again. Reported lint findings require a source fix.
make format and make format-check cover C++ sources (clang-format), CMakeLists.txt (cmake-format, from the cmakelang package), scripts/ and conanfile.py (ruff format), and tracked Markdown/JSON/YAML files (prettier; conan.lock is excluded because Conan owns its formatting). make lint runs clang-tidy against the debug compilation database, cmake-lint on CMakeLists.txt, ruff check on scripts/ and conanfile.py, and markdownlint on Markdown files. Any reported finding fails the target. CI pins all lint and format tool versions in .github/ci.env.
Prerequisite: Python 3.10+ with gcovr installed (e.g., pip install gcovr).
Build, test, and generate the coverage report:
The run is complete when the tests pass, gcovr reports a line percentage at or above the configured floor, and these files exist:
Both supported compilers emit GCov-format data (--coverage), which gcovr reports through one interface. -fprofile-update=atomic keeps the counts exact when several threads run the same code.
The report fails if line coverage falls below COVERAGE_FAIL_UNDER (default 100; override with make coverage-report COVERAGE_FAIL_UNDER=80).
Prerequisite: Doxygen installed and available on PATH.
Generate the API documentation:
The build is complete when build/docs/html/index.html exists. GitHub Pages runs the same target and publishes the result from the main branch.
Warnings are errors by default (WARNINGS_AS_ERRORS, default ON) in every preset, locally and in CI. Relax it for a single build tree with cmake --preset <name> -DWARNINGS_AS_ERRORS=OFF.
First-party targets also build with hardening flags by default (ENABLE_HARDENING, default ON; disable with -DENABLE_HARDENING=OFF on any configure preset):
First-party targets build the release preset with link-time optimization (LTO) by default (ENABLE_LTO, default ON; disable with -DENABLE_LTO=OFF). It applies only to the Release configuration (CMake's INTERPROCEDURAL_OPTIMIZATION_RELEASE property), so debug, sanitize*, and coverage builds are unaffected; when the toolchain reports no LTO support, configure logs the reason and continues without it rather than failing.
Sanitizer and coverage builds omit fortification (they build as Debug, which never defines _FORTIFY_SOURCE): fortify conflicts with the ASan interceptors, and coverage builds run at -O0 where glibc fortification warns. The other hardening flags stay on. Like the warning options, hardening covers first-party code only; dependency binaries from the Conan cache are not rebuilt with these flags.
This template does not replace the system allocator, and no preset or CMake option selects mimalloc, jemalloc, or tcmalloc. An allocator is chosen against a measured allocation profile, which a boilerplate does not have. Three constraints make it more than a link-line change here:
To add one, follow the path the recipe already uses for optional tools: declare a Conan option in conanfile.py, add the requirement under requirements(), forward the choice to CMake through tc.cache_variables next to the ccache and mold/LLD probes in generate(), and fail configuration when compiler.sanitizer is Address or AddressUndefinedBehavior. Rerun make lock afterwards.
Prerequisite: run make bootstrap so Conan generates ConanPresets.json.
Setup is complete when F5 builds the selected debug target and launches it from build/debug.
Prerequisite: run make bootstrap so Conan generates ConanPresets.json.
In CLion:
Setup is complete when CLion finishes reloading the selected public preset without a configure error.