From fcd086dee43301e3b1398952c0525c280678b404 Mon Sep 17 00:00:00 2001 From: Nigel Stewart Date: Mon, 30 Nov 2026 19:58:31 +1000 Subject: [PATCH 1/4] CMake: standalone build with check and check-valgrind targets Allow the library to be configured on its own, outside the Boost superproject, using an installed Boost (find_package(Boost 1.85 CONFIG)). - When this is the top-level project and BUILD_TESTING is on, include CTest and add two targets: check run the unit tests (ctest --output-on-failure) check-valgrind run them under valgrind (ctest -T memcheck), only when valgrind is found - Pass -C to ctest only when a configuration is set, so the targets work with single-config generators and an empty CMAKE_BUILD_TYPE (a bare "-C" made ctest fail). - Run valgrind with --error-exitcode=1, --leak-check=full and --errors-for-leak-kinds=definite, so memory errors and definite leaks fail the test instead of only being listed in the memcheck summary. - test/CMakeLists.txt: list the tests directly (base_test, constant_erase_test, soft_iterator_invalidation, space_optimized_test) instead of reading them from Jamfile.v2. The tests are built with C++11 and -Wall -Wextra -Wpedantic (/W4 on MSVC), and a "tests" target builds them all. The library target no longer lists its Boost dependencies. --- CMakeLists.txt | 46 +++++++++++++++++++++++++++++++++++---------- test/CMakeLists.txt | 39 +++++++++++++++++++++++++++++++------- 2 files changed, 68 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9ff26a9a..750427e2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,16 +12,42 @@ add_library(Boost::circular_buffer ALIAS boost_circular_buffer) target_include_directories(boost_circular_buffer INTERFACE include) -target_link_libraries(boost_circular_buffer - INTERFACE - Boost::assert - Boost::concept_check - Boost::config - Boost::core - Boost::move - Boost::throw_exception - Boost::type_traits -) +# Self-contained cmake build + +if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) + + message(STATUS "Using CMake version ${CMAKE_VERSION}") + + set(Boost_DEBUG ON) + find_package(Boost 1.85 CONFIG REQUIRED) + + if(BUILD_TESTING) + + # --error-exitcode makes a test fail on any valgrind error, including definite leaks. + # (must be set before CTest include) + set(MEMORYCHECK_COMMAND_OPTIONS "--error-exitcode=1 --leak-check=full --errors-for-leak-kinds=definite --track-origins=yes") + + include(CTest) + + # check - run the unit tests + + add_custom_target(check + COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure "$<$>:-C;$>" + COMMAND_EXPAND_LISTS) + add_dependencies(check tests) + + # check-valgrind - run the unit tests with valgrind + + if(MEMORYCHECK_COMMAND) + add_custom_target(check-valgrind + COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure -T memcheck "$<$>:-C;$>" + COMMAND_EXPAND_LISTS) + add_dependencies(check-valgrind tests) + endif() + + endif() + +endif() if(BUILD_TESTING AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/test/CMakeLists.txt") diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 5d2db5d4..07938b5c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,11 +1,36 @@ -# Copyright 2018, 2019 Peter Dimov -# Distributed under the Boost Software License, Version 1.0. -# See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt - -include(BoostTestJamfile OPTIONAL RESULT_VARIABLE HAVE_BOOST_TEST) +if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") + # warning level 4 + add_compile_options(/W4) +endif() -if(HAVE_BOOST_TEST) +if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + add_compile_options(-Wall -Wextra -Wpedantic) +endif() -boost_test_jamfile(FILE Jamfile.v2 LINK_LIBRARIES Boost::circular_buffer Boost::core) +if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + add_compile_options(-Wall -Wextra -Wpedantic) +endif() +if(NOT TARGET tests) + add_custom_target(tests) endif() + +set(PREFIX "boost_circular_buffer_") +set(LINK_LIBRARIES Boost::circular_buffer) +set(COMPILE_FEATURES cxx_std_11) + +function(boost_circular_buffer_add_test name source) + set(pname "${PREFIX}${name}") + add_executable(${pname} ${source}) + target_link_libraries(${pname} PRIVATE ${LINK_LIBRARIES}) + target_compile_features(${pname} PRIVATE ${COMPILE_FEATURES}) + add_test(NAME ${pname} COMMAND ${pname}) + add_dependencies(tests ${pname}) +endfunction() + +# + +boost_circular_buffer_add_test(base_test base_test.cpp) +boost_circular_buffer_add_test(constant_erase_test constant_erase_test.cpp) +boost_circular_buffer_add_test(soft_iterator_invalidation soft_iterator_invalidation.cpp) +boost_circular_buffer_add_test(space_optimized_test space_optimized_test.cpp) From c1be1d320d79258a31ec2d55229cba07badbf1d7 Mon Sep 17 00:00:00 2001 From: Nigel Stewart Date: Mon, 30 Nov 2026 20:07:51 +1000 Subject: [PATCH 2/4] CMake: require 3.10 and exclude tests from the default build - Raise the cmake_minimum_required lower bound from 3.8 to 3.10. CMake 3.31 and later warn that compatibility with versions older than 3.10 is deprecated. - Add the test subdirectory with EXCLUDE_FROM_ALL. A plain build no longer compiles the tests, following the Boost convention. They are built through the "tests" target, which check and check-valgrind depend on. --- CMakeLists.txt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 750427e2..2f8e7da5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ # Distributed under the Boost Software License, Version 1.0. # https://www.boost.org/LICENSE_1_0.txt -cmake_minimum_required(VERSION 3.8...3.31) +cmake_minimum_required(VERSION 3.10...3.31) project(boost_circular_buffer VERSION "${BOOST_SUPERPROJECT_VERSION}" LANGUAGES CXX) @@ -51,6 +51,8 @@ endif() if(BUILD_TESTING AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/test/CMakeLists.txt") - add_subdirectory(test) + # Follow the Boost convention: don't build test targets by default, + # and only when explicitly requested by building target tests + add_subdirectory(test EXCLUDE_FROM_ALL) endif() From 960e6bfb27e9e314c9edde141f0def6710efd954 Mon Sep 17 00:00:00 2001 From: Nigel Stewart Date: Mon, 30 Nov 2026 20:22:45 +1000 Subject: [PATCH 3/4] CMake: restore the dependency list for the standalone build The library target's Boost dependencies had been removed because the standalone build failed with them. A b2-installed Boost (for example the distro packages) provides only Boost::headers and none of the per-library targets, so the link interface named targets that did not exist ("The link interface of target boost_circular_buffer contains: Boost::assert but the target was not found"). Without the list, Boost::circular_buffer did not pass on its dependencies' include paths in the superproject or in test/cmake_test. - Put the dependencies back, in a BOOST_CIRCULAR_BUFFER_DEPS list that is passed to target_link_libraries. - In the standalone build, after find_package(Boost), make each dependency target that does not exist an alias of an interface library that links Boost::headers. Targets that already exist, from the superproject or a CMake-installed Boost, are left alone. - The file is now maintained by hand rather than generated by boostdep, so drop the "Generated by" line. CLAUDE.md: - Describe the hand-maintained dependency list and the Boost::headers fallback, and stop telling people to regenerate CMakeLists.txt with boostdep. - test/CMakeLists.txt lists its tests separately from Jamfile.v2 and must be kept in sync with it. cmake_install_test and cmake_subdir_test are replaced by cmake_test. - Add instructions for the standalone CMake build (CMake 3.10 or newer, Boost 1.85 or newer, -DBUILD_TESTING=ON, check and check-valgrind). --- CLAUDE.md | 33 ++++++++++++++++++++++++++++----- CMakeLists.txt | 24 +++++++++++++++++++++++- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 17b0fcbd..5e35a44f 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -34,11 +34,13 @@ test/ base_test.cpp circular_buffer: #define CB_CONTAINER circular_buffer + common.ipp + extras space_optimized_test.cpp circular_buffer_space_optimized: same pattern soft_iterator_invalidation.cpp, constant_erase_test.cpp, bounded_buffer_comparison.cpp - Jamfile.v2 source of truth for which tests run; CMake reuses it via boost_test_jamfile - cmake_install_test/, cmake_subdir_test/ CMake integration tests (CI only) + Jamfile.v2 tests run by b2 (and CI) + CMakeLists.txt tests run by CMake, listed separately. Keep it in sync with Jamfile.v2 + cmake_test/ CMake install and add_subdirectory integration test (CI only) example/ examples; snippets are imported into the docs doc/circular_buffer.qbk Quickbook documentation -CMakeLists.txt generated by `boostdep --cmake circular_buffer`. Do not hand-edit the dependency list +CMakeLists.txt library target, dependency list (BOOST_CIRCULAR_BUFFER_DEPS) and standalone build. + Started from `boostdep --cmake` output, now maintained by hand. Don't regenerate it build.jam b2 project and dependency list ``` @@ -93,8 +95,11 @@ build.jam b2 project and dependency list compiler workarounds. - **Dependencies**: keep them minimal. Current direct dependencies are assert, concept_check, config, core, move, throw_exception and type_traits. - If you add one, update `CMakeLists.txt`, `build.jam` and - `test/cmake_subdir_test/CMakeLists.txt` together. + If you add one, update `CMakeLists.txt` (the `BOOST_CIRCULAR_BUFFER_DEPS` + list), `build.jam` and `test/cmake_test/CMakeLists.txt` together. + The standalone CMake build reads the same list. When an installed Boost + doesn't provide one of these targets, it uses `Boost::headers` instead. + So the standalone build needs no separate update. - **Self-contained headers**: each header must `#include` what it uses, both Boost and standard headers. - **Formatting**: 4-space indent, 120 columns, K&R braces (opening brace on @@ -141,6 +146,24 @@ runs them: ./b2 libs/circular_buffer/test toolset=gcc cxxstd=03,11,17 variant=debug,release ``` +A standalone CMake build uses an installed Boost (1.85 or newer, found with +`find_package(Boost CONFIG)`) and requires CMake 3.10 or newer: + +```sh +cmake -S . -B build -G Ninja -DBUILD_TESTING=ON +ninja -C build check # build and run the tests +ninja -C build check-valgrind # the same under valgrind (ctest -T memcheck) +``` + +- Pass `-DBUILD_TESTING=ON` explicitly. The `check` targets are defined only + when this is the top-level project and `BUILD_TESTING` is on. +- The tests are `EXCLUDE_FROM_ALL`, so a plain `ninja` doesn't build them. + Use `ninja tests` or the `check` targets. +- `check-valgrind` exists only if valgrind is found. A memory error or a + definite leak fails the test (`--error-exitcode=1`). Logs go to + `build/Testing/Temporary/MemoryChecker..log`. +- Don't use `check-valgrind` on a sanitizer build. + For a quick standalone run against a separate Boost checkout at `$BOOST` (this repo's `include/` must come first, so that it shadows the superproject's copy): diff --git a/CMakeLists.txt b/CMakeLists.txt index 2f8e7da5..98f48083 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ -# Generated by `boostdep --cmake circular_buffer` # Copyright 2020, 2021 Peter Dimov +# Copyright 2026 Nigel Stewart # Distributed under the Boost Software License, Version 1.0. # https://www.boost.org/LICENSE_1_0.txt @@ -12,6 +12,18 @@ add_library(Boost::circular_buffer ALIAS boost_circular_buffer) target_include_directories(boost_circular_buffer INTERFACE include) +set(BOOST_CIRCULAR_BUFFER_DEPS + Boost::assert + Boost::concept_check + Boost::config + Boost::core + Boost::move + Boost::throw_exception + Boost::type_traits +) + +target_link_libraries(boost_circular_buffer INTERFACE ${BOOST_CIRCULAR_BUFFER_DEPS}) + # Self-contained cmake build if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) @@ -21,6 +33,16 @@ if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) set(Boost_DEBUG ON) find_package(Boost 1.85 CONFIG REQUIRED) + # A b2-installed Boost (e.g. distro packages) provides only Boost::headers, + # not per-library targets. Stand in for any missing dependency with it. + foreach(_dep IN LISTS BOOST_CIRCULAR_BUFFER_DEPS) + if(_dep MATCHES "^Boost::(.+)$" AND NOT TARGET ${_dep}) + add_library(boost_circular_buffer_dep_${CMAKE_MATCH_1} INTERFACE) + target_link_libraries(boost_circular_buffer_dep_${CMAKE_MATCH_1} INTERFACE Boost::headers) + add_library(${_dep} ALIAS boost_circular_buffer_dep_${CMAKE_MATCH_1}) + endif() + endforeach() + if(BUILD_TESTING) # --error-exitcode makes a test fail on any valgrind error, including definite leaks. From eea9736f7cbe77156999896152cb6d25a3f79846 Mon Sep 17 00:00:00 2001 From: Nigel Stewart Date: Mon, 30 Nov 2026 20:27:58 +1000 Subject: [PATCH 4/4] Add license header to test/CMakeLists.txt The header was lost when the file was rewritten to list the tests directly. Restore the Boost Software License notice. --- test/CMakeLists.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 07938b5c..db967f9e 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,3 +1,6 @@ +# Distributed under the Boost Software License, Version 1.0. +# See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt + if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") # warning level 4 add_compile_options(/W4)