From 459fbdcf4862fd29dd111860bbf64fb9551a8504 Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Sat, 10 Jan 2026 18:50:18 +0100 Subject: [PATCH 01/18] Add Windows CI build too --- .github/workflows/cmake.yml | 68 +++++++++++++++++++++++++++++++++++++ CMakeLists.txt | 4 ++- 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/cmake.yml diff --git a/.github/workflows/cmake.yml b/.github/workflows/cmake.yml new file mode 100644 index 0000000..ef7638f --- /dev/null +++ b/.github/workflows/cmake.yml @@ -0,0 +1,68 @@ +--- +name: CMake + +on: + push: + branches: ["develop"] + pull_request: + branches: ["develop"] + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: + schedule: + # run at 15:30 on day-of-month 7. + - cron: '30 15 7 * *' + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +env: + # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) + BUILD_TYPE: release + CTEST_OUTPUT_ON_FAILURE: 1 + +jobs: + build: + strategy: + fail-fast: false + + matrix: + os: [windows] + + runs-on: ${{ matrix.os }}-latest + + steps: + - uses: actions/checkout@v4 + - name: Setup build environment + uses: lukka/get-cmake@latest + with: + cmakeVersion: "~4.2.1" + ninjaVersion: "^1.13.0" + + - name: Setup MSVC + if: startsWith(matrix.os, 'windows') + uses: TheMrMilchmann/setup-msvc-dev@v3 + with: + arch: x64 + + - name: Setup Cpp + if: matrix.os != 'windows' + uses: aminya/setup-cpp@v1 + with: + compiler: llvm + + - name: Configure CMake + run: cmake --preset msvc-${{env.BUILD_TYPE}} --log-level=VERBOSE + + - name: Build + # Build your program with the given configuration + run: cmake --build --preset msvc-${{env.BUILD_TYPE}} + + # - name: Install + # # Install the project artefacts to CMAKE_INSTALL_PREFIX + # run: cmake --install ${{github.workspace}}/build/${{env.BUILD_TYPE}} --config ${{env.BUILD_TYPE}} + + - name: Test + working-directory: ${{github.workspace}}/build/${{env.BUILD_TYPE}} + # Execute tests defined by the CMake configuration, but needs to find_package(Boost)! + run: ctest -C msvc-${{env.BUILD_TYPE}} diff --git a/CMakeLists.txt b/CMakeLists.txt index 7b36f29..ce92578 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,11 +11,13 @@ project( # gersemi: off -# Modules opt in only on compilers that support g++-15 and clang-20+ +# Modules opt in only on compilers that support it: msvc, g++-15 and clang-20+ if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 20) set(CMAKE_CXX_SCAN_FOR_MODULES 1) elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 15) set(CMAKE_CXX_SCAN_FOR_MODULES 1) +elseif(MSVC) + set(CMAKE_CXX_SCAN_FOR_MODULES 1) else() set(CMAKE_CXX_SCAN_FOR_MODULES 0) endif() From f3c5c4af29d897375b1d00c7c60fee19ce05d66f Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Sat, 10 Jan 2026 19:19:15 +0100 Subject: [PATCH 02/18] Use portable C++20 check and portable C++ feature checks --- include/beman/scope/scope.hpp | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/include/beman/scope/scope.hpp b/include/beman/scope/scope.hpp index 7d33b2c..e768259 100644 --- a/include/beman/scope/scope.hpp +++ b/include/beman/scope/scope.hpp @@ -10,12 +10,30 @@ #include // clang-format off -#if __cplusplus < 202002L +#include + +#if defined(__cpp_concepts) && __cpp_concepts >= 201907L + // C++20 concepts supported +#elif __cplusplus < 202002L #error "C++20 or later is required" #endif -// clang-format on -#include //todo unconditional for unique_resource +// detect standard header first, then experimental, otherwise use local implementation +#if defined(__has_include) +# if __has_include() +# include +# define BEMAN_SCOPE_USE_STD +# elif __has_include() +# include +# define BEMAN_SCOPE_USE_STD_EXPERIMENTAL +# else +// no std scope header — fall through to local implementation below +# endif +#elif defined(__cpp_lib_scope) && __cpp_lib_scope >= 2023xxxxL +# include +# define BEMAN_SCOPE_USE_STD +#endif +// clang-format on #ifdef BEMAN_SCOPE_USE_STD_EXPERIMENTAL From 9ad29b70fba901b194f1760ff91d51b01c7cd7f0 Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Sat, 10 Jan 2026 19:35:01 +0100 Subject: [PATCH 03/18] Use C++23 on Windows --- CMakePresets.json | 2 ++ include/beman/scope/scope.hpp | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CMakePresets.json b/CMakePresets.json index 483e1a3..655058f 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -101,6 +101,7 @@ "_debug-base" ], "cacheVariables": { + "CMAKE_CXX_STANDARD": "23", "CMAKE_TOOLCHAIN_FILE": "infra/cmake/msvc-toolchain.cmake" } }, @@ -112,6 +113,7 @@ "_release-base" ], "cacheVariables": { + "CMAKE_CXX_STANDARD": "23", "CMAKE_TOOLCHAIN_FILE": "infra/cmake/msvc-toolchain.cmake" } } diff --git a/include/beman/scope/scope.hpp b/include/beman/scope/scope.hpp index e768259..d18f154 100644 --- a/include/beman/scope/scope.hpp +++ b/include/beman/scope/scope.hpp @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -67,7 +68,7 @@ namespace beman::scope { // todo temporary template -using unique_resource = std::experimental::unique_resource; +using unique_resource = std::unique_resource; // todo temporary template > From 01a4dcf6d65803bb3da33a6718da441bfdeaf3d8 Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Sat, 10 Jan 2026 20:23:31 +0100 Subject: [PATCH 04/18] Quickfix to compile with C++23 --- include/beman/scope/scope.hpp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/include/beman/scope/scope.hpp b/include/beman/scope/scope.hpp index d18f154..df45bb9 100644 --- a/include/beman/scope/scope.hpp +++ b/include/beman/scope/scope.hpp @@ -50,15 +50,15 @@ template using scope_success = std::experimental::scope_success; // todo temporary -// template -// using unique_resource = std::experimental::unique_resource; +template +using unique_resource = std::experimental::fundamentals_v3::unique_resource; -// template > -// unique_resource, std::decay_t> -// make_unique_resource_checked(R&& r, const S& invalid, D&& d) noexcept(noexcept( -// std::experimental::make_unique_resource_checked(std::forward(r), std::forward(invalid), std::forward(d)))) { -// return std::experimental::make_unique_resource_checked(std::forward(r), std::forward(invalid), std::forward(d)); -//} +template > +unique_resource, std::decay_t> +make_unique_resource_checked(R&& r, const S& invalid, D&& d) noexcept(noexcept( + std::experimental::make_unique_resource_checked(std::forward(r), std::forward(invalid), std::forward(d)))) { + return std::experimental::make_unique_resource_checked(std::forward(r), std::forward(invalid), std::forward(d)); +} } // namespace beman::scope @@ -71,11 +71,11 @@ template using unique_resource = std::unique_resource; // todo temporary -template > -unique_resource, std::decay_t > -make_unique_resource_checked(R&& r, const S& invalid, D&& d) noexcept(noexcept( - std::experimental::make_unique_resource_checked(std::forward(r), std::forward(invalid), std::forward(d)))) { - return std::experimental::make_unique_resource_checked(std::forward(r), std::forward(invalid), std::forward(d)); +template > +unique_resource, std::decay_t> +make_unique_resource_checked(R&& r, const S& invalid, D&& d) noexcept( + noexcept(std::make_unique_resource_checked(std::forward(r), std::forward(invalid), std::forward(d)))) { + return std::make_unique_resource_checked(std::forward(r), std::forward(invalid), std::forward(d)); } //================================================================================================== From 6f7560373938ac2dbe303c48a1cce99287e75dd9 Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Sun, 11 Jan 2026 21:59:08 +0100 Subject: [PATCH 05/18] Add simple BEMAN_SCOPE_USE_FALLBACK code This compiles with clang++-21 on OSX and should build on Windows too. --- include/beman/scope/scope.hpp | 17 +- include/beman/scope/scope_impl.hpp | 281 +++++++++++++++++++++++++++++ 2 files changed, 292 insertions(+), 6 deletions(-) create mode 100644 include/beman/scope/scope_impl.hpp diff --git a/include/beman/scope/scope.hpp b/include/beman/scope/scope.hpp index df45bb9..9b45233 100644 --- a/include/beman/scope/scope.hpp +++ b/include/beman/scope/scope.hpp @@ -33,6 +33,8 @@ #elif defined(__cpp_lib_scope) && __cpp_lib_scope >= 2023xxxxL # include # define BEMAN_SCOPE_USE_STD +#else +# warning "Missing feature __cpp_lib_scope" #endif // clang-format on @@ -62,20 +64,20 @@ make_unique_resource_checked(R&& r, const S& invalid, D&& d) noexcept(noexcept( } // namespace beman::scope -#else // ! BEMAN_SCOPE__USE_STD_EXPERIMENTAL +#elif defined(BEMAN_SCOPE_USE_STD) namespace beman::scope { // todo temporary template -using unique_resource = std::unique_resource; +using unique_resource = std::experimental::unique_resource; // todo temporary template > unique_resource, std::decay_t> -make_unique_resource_checked(R&& r, const S& invalid, D&& d) noexcept( - noexcept(std::make_unique_resource_checked(std::forward(r), std::forward(invalid), std::forward(d)))) { - return std::make_unique_resource_checked(std::forward(r), std::forward(invalid), std::forward(d)); +make_unique_resource_checked(R&& r, const S& invalid, D&& d) noexcept(noexcept( + std::experimental::make_unique_resource_checked(std::forward(r), std::forward(invalid), std::forward(d)))) { + return std::experimental::make_unique_resource_checked(std::forward(r), std::forward(invalid), std::forward(d)); } //================================================================================================== @@ -440,6 +442,9 @@ using scope_fail = scope_guard + +#if defined(__cpp_concepts) && __cpp_concepts >= 201907L + // C++20 concepts supported +#elif __cplusplus < 202002L +# error "C++20 or later is required" +#endif + +// detect standard header first, then experimental, otherwise use local implementation +#ifdef __has_include +# if __has_include() +# include +# define BEMAN_SCOPE_USE_STD +# elif __has_include() +# include +# define BEMAN_SCOPE_USE_STD_EXPERIMENTAL +# else +# define BEMAN_SCOPE_USE_FALLBACK +# endif +#else +# define BEMAN_SCOPE_USE_FALLBACK +#endif + +#ifdef BEMAN_SCOPE_USE_STD +# if !defined(__cpp_lib_scope_exit) +# error "Standard present but __cpp_lib_scope_exit not defined" +# endif +#endif +// clang-format on + +#ifdef BEMAN_SCOPE_USE_FALLBACK +#include +#include +#include + +namespace beman::scope { + +// TODO(CK): make a std::experimental::scope_exit::scope_exit conform +// implementation +template +class [[nodiscard]] scope_exit { + F f; + bool active = true; + + public: + constexpr explicit scope_exit(F func) noexcept(std::is_nothrow_move_constructible_v) : f(std::move(func)) {} + + // Move constructor + constexpr scope_exit(scope_exit&& other) noexcept(std::is_nothrow_move_constructible_v) + : f(std::move(other.f)), active(other.active) { + other.active = false; + } + + // Deleted copy + auto operator=(const scope_exit&) -> scope_exit& = delete; + scope_exit(const scope_exit&) = delete; + + // Move assignment + constexpr auto operator=(scope_exit&& other) noexcept(std::is_nothrow_move_assignable_v) -> scope_exit& { + if (this != &other) { + f = std::move(other.f); + active = other.active; + other.active = false; + } + return *this; + } + + // Destructor: call only if scope is exiting normally + ~scope_exit() noexcept(noexcept(f())) { + if (active) { + f(); + } + } + + // Release to prevent execution + constexpr auto release() -> void { active = false; } +}; + +// Factory helper +template +static auto make_scope_exit(F f) -> scope_exit { + return scope_exit(std::move(f)); +} + +// TODO(CK): make a std::experimental::scope_fail::scope_fail conform +// implementation +template +class [[nodiscard]] scope_fail { + F f; + bool active = true; + int exception_count{}; + + public: + // Constructor: capture current uncaught exceptions + constexpr explicit scope_fail(F func) noexcept(std::is_nothrow_move_constructible_v) + : f(std::move(func)), exception_count(std::uncaught_exceptions()) {} + + // Move constructor + constexpr scope_fail(scope_fail&& other) noexcept(std::is_nothrow_move_constructible_v) + : f(std::move(other.f)), active(other.active), exception_count(other.exception_count) { + other.active = false; + } + + // Deleted copy + scope_fail(const scope_fail&) = delete; + auto operator=(const scope_fail&) -> scope_fail& = delete; + + // Move assignment + constexpr auto operator=(scope_fail&& other) noexcept(std::is_nothrow_move_assignable_v) -> scope_fail& { + if (this != &other) { + f = std::move(other.f); + active = other.active; + exception_count = other.exception_count; + other.active = false; + } + return *this; + } + + // Destructor: call if scope is exiting due to an exception + ~scope_fail() noexcept(noexcept(f())) { + if (active && std::uncaught_exceptions() > exception_count) { + f(); + } + } + + // Release to prevent execution + constexpr auto release() -> void { active = false; } +}; + +// Factory helper +template +constexpr auto make_scope_fail(F&& f) -> scope_fail> { + return scope_fail>(std::forward(f)); +} + +// TODO(CK): make a std::experimental::scope_success::scope_success conform +// implementation +template +class [[nodiscard]] scope_success { + F f; + bool active = true; + int exception_count{}; + + public: + // Constructor: capture current uncaught exceptions + constexpr explicit scope_success(F func) noexcept(std::is_nothrow_move_constructible_v) + : f(std::move(func)), exception_count(std::uncaught_exceptions()) {} + + // Move constructor + constexpr scope_success(scope_success&& other) noexcept(std::is_nothrow_move_constructible_v) + : f(std::move(other.f)), active(other.active), exception_count(other.exception_count) { + other.active = false; + } + + // Deleted copy + scope_success(const scope_success&) = delete; + auto operator=(const scope_success&) -> scope_success& = delete; + + // Move assignment + constexpr auto operator=(scope_success&& other) noexcept(std::is_nothrow_move_assignable_v) -> scope_success& { + if (this != &other) { + f = std::move(other.f); + active = other.active; + exception_count = other.exception_count; + other.active = false; + } + return *this; + } + + // Destructor: call only if scope is exiting normally + ~scope_success() noexcept(noexcept(f())) { + if (active && std::uncaught_exceptions() == exception_count) { + f(); + } + } + + // Release to prevent execution + constexpr auto release() -> void { active = false; } +}; + +// Factory helper +template +constexpr auto make_scope_success(F&& f) -> scope_success> { + return scope_success>(std::forward(f)); +} + +template +class [[nodiscard]] unique_resource { + Resource resource; + Deleter deleter; + bool active = true; + + public: + // Constructor + constexpr unique_resource(Resource r, Deleter d) noexcept(std::is_nothrow_move_constructible_v) + : resource(std::move(r)), deleter(std::move(d)) {} + + // Move constructor + constexpr unique_resource(unique_resource&& other) noexcept(std::is_nothrow_move_constructible_v) + : resource(std::move(other.resource)), deleter(std::move(other.deleter)), active(other.active) { + other.active = false; + } + + // Move assignment + constexpr auto operator=(unique_resource&& other) noexcept(std::is_nothrow_move_assignable_v) + -> unique_resource& { + if (this != &other) { + reset(std::move(other.resource)); + deleter = std::move(other.deleter); + active = other.active; + other.active = false; + } + return *this; + } + + // Deleted copy operations + unique_resource(const unique_resource&) = delete; + auto operator=(const unique_resource&) -> unique_resource& = delete; + + // Destructor + ~unique_resource() noexcept(noexcept(deleter(resource))) { + if (active) { + deleter(resource); + } + } + + // Release ownership + constexpr void release() noexcept(noexcept(deleter(resource))) { active = false; } + + // Reset resource + constexpr void reset() noexcept(noexcept(deleter(resource))) { + if (active) { + deleter(resource); + } + active = false; + } + constexpr void reset(Resource new_resource) noexcept(noexcept(deleter(resource))) { + if (active) { + deleter(resource); + } + resource = std::move(new_resource); + active = true; + } + + // Accessors + constexpr auto get() const -> const Resource& { return resource; } + constexpr auto get() -> Resource& { return resource; } + + // operator* — only for non-void pointer resources + constexpr auto operator*() const noexcept -> std::add_lvalue_reference_t> + requires(std::is_pointer_v && !std::is_void_v>) + { + return *resource; + } + + // operator-> — only for pointer resources + constexpr auto operator->() const noexcept -> Resource + requires std::is_pointer_v + { + return resource; + } + + // Check if active + constexpr explicit operator bool() const noexcept { return active; } +}; + +// Deduction guide +template +unique_resource(Resource&&, Deleter&&) -> unique_resource, std::decay_t>; + +} // namespace beman::scope + +#endif // BEMAN_SCOPE_USE_FALLBACK + +#endif // SCOPE_IMPL_HPP From d83e3f443e82980021bfd336f7e606dca61baa06 Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Sun, 11 Jan 2026 22:23:05 +0100 Subject: [PATCH 06/18] Fix Windows CI test directory --- .github/workflows/cmake.yml | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/.github/workflows/cmake.yml b/.github/workflows/cmake.yml index ef7638f..09db9ed 100644 --- a/.github/workflows/cmake.yml +++ b/.github/workflows/cmake.yml @@ -58,11 +58,10 @@ jobs: # Build your program with the given configuration run: cmake --build --preset msvc-${{env.BUILD_TYPE}} + - name: Test + # Execute tests defined by the CMake configuration + run: ctest --preset msvc-${{env.BUILD_TYPE}} + # - name: Install # # Install the project artefacts to CMAKE_INSTALL_PREFIX - # run: cmake --install ${{github.workspace}}/build/${{env.BUILD_TYPE}} --config ${{env.BUILD_TYPE}} - - - name: Test - working-directory: ${{github.workspace}}/build/${{env.BUILD_TYPE}} - # Execute tests defined by the CMake configuration, but needs to find_package(Boost)! - run: ctest -C msvc-${{env.BUILD_TYPE}} + # run: cmake --build --preset msvc-${{env.BUILD_TYPE}} --target install From 2f5ab941ea93d03326d85a6461a7fd205bfccc74 Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Tue, 13 Jan 2026 21:05:04 +0100 Subject: [PATCH 07/18] Refactory unique_resource code to be portable Add more test for unique_resource --- CMakePresets.json | 2 + examples/CMakeLists.txt | 1 + gcovr.cfg | 21 +++ include/beman/scope/scope_impl.hpp | 61 +++++-- makefile | 65 ++++++++ tests/CMakeLists.txt | 8 +- tests/unique_resource_2.test.cpp | 254 +++++++++++++++++++++++++++++ 7 files changed, 396 insertions(+), 16 deletions(-) create mode 100644 gcovr.cfg create mode 100644 makefile create mode 100644 tests/unique_resource_2.test.cpp diff --git a/CMakePresets.json b/CMakePresets.json index 655058f..91bb5e6 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -79,6 +79,7 @@ "_debug-base" ], "cacheVariables": { + "CMAKE_CXX_STANDARD": "23", "CMAKE_TOOLCHAIN_FILE": "infra/cmake/appleclang-toolchain.cmake" } }, @@ -90,6 +91,7 @@ "_release-base" ], "cacheVariables": { + "CMAKE_CXX_STANDARD": "23", "CMAKE_TOOLCHAIN_FILE": "infra/cmake/appleclang-toolchain.cmake" } }, diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 23ffaf7..0f1d6d3 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -13,4 +13,5 @@ foreach(example ${ALL_EXAMPLES}) add_executable(${example}) target_sources(${example} PRIVATE ${example}.cpp) target_link_libraries(${example} PRIVATE beman::scope) + add_test(NAME ${example} COMMAND ${example}) endforeach() diff --git a/gcovr.cfg b/gcovr.cfg new file mode 100644 index 0000000..52add0f --- /dev/null +++ b/gcovr.cfg @@ -0,0 +1,21 @@ +root = . +search-path = build + +filter = examples/* +# filter = src/* +filter = include/* + +exclude-directories = stagedir +exclude-directories = build/*/*/_deps +exclude-directories = tests +exclude-directories = conan + +gcov-ignore-parse-errors = all +print-summary = yes + +html-details = build/coverage/index.html + +cobertura-pretty = yes +cobertura = build/cobertura.xml + +#TBD delete-gcov-files = yes diff --git a/include/beman/scope/scope_impl.hpp b/include/beman/scope/scope_impl.hpp index c03286a..e0cb393 100644 --- a/include/beman/scope/scope_impl.hpp +++ b/include/beman/scope/scope_impl.hpp @@ -17,9 +17,11 @@ # if __has_include() # include # define BEMAN_SCOPE_USE_STD +// XXX #warning "Set BEMAN_SCOPE_USE_STD" # elif __has_include() # include # define BEMAN_SCOPE_USE_STD_EXPERIMENTAL +// XXX #warning "Set BEMAN_SCOPE_USE_STD_EXPERIMENTAL" # else # define BEMAN_SCOPE_USE_FALLBACK # endif @@ -83,8 +85,9 @@ class [[nodiscard]] scope_exit { }; // Factory helper +// NOLINTNEXTLINE(misc-use-anonymous-namespace) template -static auto make_scope_exit(F f) -> scope_exit { +auto make_scope_exit(F f) -> scope_exit { return scope_exit(std::move(f)); } @@ -134,6 +137,7 @@ class [[nodiscard]] scope_fail { }; // Factory helper +// NOLINTNEXTLINE(misc-use-anonymous-namespace) template constexpr auto make_scope_fail(F&& f) -> scope_fail> { return scope_fail>(std::forward(f)); @@ -185,6 +189,7 @@ class [[nodiscard]] scope_success { }; // Factory helper +// NOLINTNEXTLINE(misc-use-anonymous-namespace) template constexpr auto make_scope_success(F&& f) -> scope_success> { return scope_success>(std::forward(f)); @@ -203,8 +208,8 @@ class [[nodiscard]] unique_resource { // Move constructor constexpr unique_resource(unique_resource&& other) noexcept(std::is_nothrow_move_constructible_v) - : resource(std::move(other.resource)), deleter(std::move(other.deleter)), active(other.active) { - other.active = false; + : resource(std::move(other.resource)), deleter(std::move(other.deleter)) { + active = std::exchange(other.active, false); } // Move assignment @@ -212,9 +217,8 @@ class [[nodiscard]] unique_resource { -> unique_resource& { if (this != &other) { reset(std::move(other.resource)); - deleter = std::move(other.deleter); - active = other.active; - other.active = false; + deleter = std::move(other.deleter); + active = std::exchange(other.active, false); } return *this; } @@ -224,22 +228,20 @@ class [[nodiscard]] unique_resource { auto operator=(const unique_resource&) -> unique_resource& = delete; // Destructor - ~unique_resource() noexcept(noexcept(deleter(resource))) { - if (active) { - deleter(resource); - } - } + ~unique_resource() noexcept(noexcept(deleter(resource))) { reset(); } // Release ownership - constexpr void release() noexcept(noexcept(deleter(resource))) { active = false; } + constexpr void release() noexcept { active = false; } // Reset resource constexpr void reset() noexcept(noexcept(deleter(resource))) { if (active) { + active = false; deleter(resource); } - active = false; } + + // Reset the resource and call deleter if engaged constexpr void reset(Resource new_resource) noexcept(noexcept(deleter(resource))) { if (active) { deleter(resource); @@ -259,14 +261,17 @@ class [[nodiscard]] unique_resource { return *resource; } - // operator-> — only for pointer resources + // Optional pointer convenience constexpr auto operator->() const noexcept -> Resource requires std::is_pointer_v { return resource; } - // Check if active + // TODO(CK): missing usecase? + constexpr auto get_deleter() const noexcept -> Deleter; + + // NOTE: check if active; not required from LWG? constexpr explicit operator bool() const noexcept { return active; } }; @@ -274,8 +279,34 @@ class [[nodiscard]] unique_resource { template unique_resource(Resource&&, Deleter&&) -> unique_resource, std::decay_t>; +// Factory: conditionally engaged +// NOLINTNEXTLINE(misc-use-anonymous-namespace) +template +constexpr auto make_unique_resource_checked(R&& r, const Invalid& invalid, D&& d) { + using resource_type = std::decay_t; + using deleter_type = std::decay_t; + + if (r == invalid) { + // Disengaged resource + unique_resource ur(resource_type{}, std::forward(d)); + ur.release(); // disengage immediately + return ur; + } + + return unique_resource(std::forward(r), std::forward(d)); +} + } // namespace beman::scope +#elifdef BEMAN_SCOPE_USE_STD_EXPERIMENTAL + +namespace beman::scope { +using std::experimental::scope_exit; +using std::experimental::scope_fail; +using std::experimental::scope_success; +using std::experimental::unique_resource; +} // namespace beman::scope + // #endif // BEMAN_SCOPE_USE_FALLBACK #endif // SCOPE_IMPL_HPP diff --git a/makefile b/makefile new file mode 100644 index 0000000..22b4fc4 --- /dev/null +++ b/makefile @@ -0,0 +1,65 @@ +# Standard stuff + +.SUFFIXES: + +MAKEFLAGS+= --no-builtin-rules # Disable the built-in implicit rules. +# MAKEFLAGS+= --warn-undefined-variables # Warn when an undefined variable is referenced. +# MAKEFLAGS+= --include-dir=$(CURDIR)/conan # Search DIRECTORY for included makefiles (*.mk). + +export hostSystemName=$(shell uname) + +ifeq (${hostSystemName},Darwin) + export LLVM_PREFIX:=$(shell brew --prefix llvm) + export LLVM_DIR:=$(shell realpath ${LLVM_PREFIX}) + export PATH:=${LLVM_DIR}/bin:${PATH} + + export CMAKE_CXX_STDLIB_MODULES_JSON=${LLVM_DIR}/lib/c++/libc++.modules.json + export CXX=clang++ + export LDFLAGS=-L$(LLVM_DIR)/lib/c++ -lc++abi # XXX -lc++ -lc++experimental + # FIXME: export GCOV="llvm-cov gcov" + + ### TODO: to test g++-15: + export GCC_PREFIX:=$(shell brew --prefix gcc) + export GCC_DIR:=$(shell realpath ${GCC_PREFIX}) + + # export CMAKE_CXX_STDLIB_MODULES_JSON=${GCC_DIR}/lib/gcc/current/libstdc++.modules.json + # export CXX:=g++-15 + # export CXXFLAGS:=-stdlib=libstdc++ + # export GCOV="gcov" +else ifeq (${hostSystemName},Linux) + export LLVM_DIR=/usr/lib/llvm-20 + export PATH:=${LLVM_DIR}/bin:${PATH} + export CXX=clang++-20 +endif + +.PHONY: all install coverage clean distclean + +all: build/compile_commands.json + ln -sf $< . + ninja -C build + +build/compile_commands.json: CMakeLists.txt makefile + cmake -S . -B build -G Ninja --log-level=DEBUG -D CMAKE_BUILD_TYPE=Release \ + -D CMAKE_EXPERIMENTAL_CXX_IMPORT_STD="d0edc3af-4c50-42ea-a356-e2862fe7a444" \ + -D CMAKE_CXX_STDLIB_MODULES_JSON=${CMAKE_CXX_STDLIB_MODULES_JSON} \ + -D CMAKE_CXX_STANDARD=23 -D CMAKE_CXX_EXTENSIONS=YES -D CMAKE_CXX_STANDARD_REQUIRED=YES \ + -D CMAKE_CXX_FLAGS='-fno-inline --coverage' \ + -D CMAKE_CXX_MODULE_STD=NO \ + -D CMAKE_INSTALL_MESSAGE=LAZY # XXX -D CMAKE_SKIP_INSTALL_RULES=YES # --fresh + +install: build/cmake_install.cmake + cmake --install build + +distclean: clean + rm -rf build + find . -name '*~' -delete + +build/coverage: test + mkdir -p $@ + +coverage: build/coverage + gcovr # XXX -v + +# Anything we don't know how to build will use this rule. +% :: + ninja -C build $(@) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a7cbcfc..6acb735 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -11,7 +11,13 @@ FetchContent_Declare( ) FetchContent_MakeAvailable(Catch2) -set(ALL_TESTNAMES scope_success scope_exit scope_fail unique_resource) +set(ALL_TESTNAMES + scope_success + scope_exit + scope_fail + unique_resource + unique_resource_2 +) # module tests will only compile with gcc15 or clang20 and above if(CMAKE_CXX_SCAN_FOR_MODULES) diff --git a/tests/unique_resource_2.test.cpp b/tests/unique_resource_2.test.cpp new file mode 100644 index 0000000..a564286 --- /dev/null +++ b/tests/unique_resource_2.test.cpp @@ -0,0 +1,254 @@ +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#include "beman/scope/scope.hpp" + +#include +#include + +namespace { + +struct Counter { + int value = 0; +}; + +struct CountingDeleter { + // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes) + Counter* counter{nullptr}; + + void operator()(int& /*unused*/) const noexcept { ++counter->value; } +}; + +} // namespace + +TEST_CASE("Construct file unique_resource", "[unique_resource]") { + bool open_file_good = false; + bool close_file_good = false; + + { + auto file = beman::scope::unique_resource(fopen("example.txt", "w"), // Acquire the FILE* + [&close_file_good](FILE* f) -> void { + if (f) { + (void)fclose(f); // Release (cleanup) the resource + close_file_good = true; + } + }); + + if (!file) { + throw std::runtime_error("file didn't open"); + } + open_file_good = true; + } + + REQUIRE(open_file_good == true); + REQUIRE(close_file_good == true); +} + +TEST_CASE("unique_resource basic construction and engagement", "[unique_resource]") { + Counter c{}; // NOLINT(misc-const-correctness) + { + beman::scope::unique_resource r(42, CountingDeleter{&c}); + + REQUIRE(static_cast(r)); + REQUIRE(r.get() == 42); + REQUIRE(c.value == 0); + } + + REQUIRE(c.value == 1); +} + +TEST_CASE("unique_resource release disengages without deleting", "[unique_resource]") { + Counter c{}; // NOLINT(misc-const-correctness) + { + beman::scope::unique_resource r(7, CountingDeleter{&c}); + + r.release(); + + REQUIRE_FALSE(r); + } + + REQUIRE(c.value == 0); +} + +TEST_CASE("unique_resource reset() destroys current resource", "[unique_resource]") { + Counter c{}; // NOLINT(misc-const-correctness) + + { + beman::scope::unique_resource r(1, CountingDeleter{&c}); + + r.reset(); + REQUIRE_FALSE(r); + REQUIRE(c.value == 1); + } + + REQUIRE(c.value == 1); +} + +TEST_CASE("unique_resource reset(new_resource) replaces resource", "[unique_resource]") { + Counter c{}; // NOLINT(misc-const-correctness) + + { + beman::scope::unique_resource r(1, CountingDeleter{&c}); + + r.reset(2); + + REQUIRE(r); + REQUIRE(r.get() == 2); + REQUIRE(c.value == 1); + } + + REQUIRE(c.value == 2); +} + +TEST_CASE("unique_resource move constructor transfers ownership", "[unique_resource]") { + Counter c{}; // NOLINT(misc-const-correctness) + + beman::scope::unique_resource r1(10, CountingDeleter{&c}); + beman::scope::unique_resource r2(std::move(r1)); + + REQUIRE_FALSE(r1); + REQUIRE(r2); + REQUIRE(r2.get() == 10); + + r2.reset(); + REQUIRE(c.value == 1); +} + +TEST_CASE("unique_resource move assignment destroys target before transfer", "[unique_resource]") { + Counter c1{}; // NOLINT(misc-const-correctness) + Counter c2{}; // NOLINT(misc-const-correctness) + + beman::scope::unique_resource r1(1, CountingDeleter{&c1}); + beman::scope::unique_resource r2(2, CountingDeleter{&c2}); + + r2 = std::move(r1); + + REQUIRE_FALSE(r1); + REQUIRE(r2); + REQUIRE(r2.get() == 1); + + REQUIRE(c2.value == 1); // old r2 destroyed + REQUIRE(c1.value == 0); + + r2.reset(); + REQUIRE(c1.value == 1); +} + +TEST_CASE("unique_resource destructor is idempotent after release", "[unique_resource]") { + Counter c{}; // NOLINT(misc-const-correctness) + + { + beman::scope::unique_resource r(99, CountingDeleter{&c}); + + r.release(); + } + + REQUIRE(c.value == 0); +} + +TEST_CASE("make_unique_resource_checked disengages on invalid", "[unique_resource]") { + Counter c{}; // NOLINT(misc-const-correctness) + + { + auto r = beman::scope::make_unique_resource_checked(-1, -1, CountingDeleter{&c}); + + REQUIRE_FALSE(r); + } + + REQUIRE(c.value == 0); +} + +TEST_CASE("make_unique_resource_checked engages on valid", "[unique_resource]") { + Counter c{}; // NOLINT(misc-const-correctness) + + { + auto r = beman::scope::make_unique_resource_checked(3, -1, CountingDeleter{&c}); + + REQUIRE(r); + } + + REQUIRE(c.value == 1); +} + +TEST_CASE("Open a nonexisting file with make_unique_resource_checked", "[unique_resource]") { + bool open_file_good = false; + bool close_file_good = false; + + { + auto file = + beman::scope::make_unique_resource_checked(fopen("nonexisting.txt", "r"), // Acquire the FILE* + nullptr, + [&close_file_good](FILE* f) -> void { + if (f) { + (void)fclose(f); // Release (cleanup) the resource + close_file_good = true; + } + }); + + if (file.get() != nullptr) { + open_file_good = true; + } + } + + REQUIRE(open_file_good == false); + REQUIRE(close_file_good == false); +} + +TEST_CASE("unique_resource supports deduction guide", "[unique_resource]") { + Counter c{}; // NOLINT(misc-const-correctness) + + beman::scope::unique_resource r(123, CountingDeleter{&c}); + + static_assert(std::is_same_v >); + + r.reset(); + REQUIRE(c.value == 1); +} + +TEST_CASE("unique_resource does not double-delete after move", "[unique_resource]") { + Counter c{}; // NOLINT(misc-const-correctness) + + { + beman::scope::unique_resource r1(1, CountingDeleter{&c}); + + { + auto r2 = std::move(r1); + } + + REQUIRE(c.value == 1); + } + + REQUIRE(c.value == 1); +} + +TEST_CASE("unique_resource operator* returns reference to resource", "[unique_resource]") { + int value = 42; + + // Define the deleter type explicitly (function pointer) + using DeleterType = void (*)(int*); + + // Empty deleter + auto empty_deleter = [](int*) {}; + + // Create unique_resource instance (modifiable) + beman::scope::unique_resource r(&value, empty_deleter); + + // operator* should return a reference + int& ref = *r; + + // Check that the reference refers to the original value + REQUIRE(&ref == &value); + REQUIRE(ref == 42); + + // Modify the value through the reference + ref = 100; + REQUIRE(value == 100); + + // Create a const unique_resource instance + const beman::scope::unique_resource r2(&value, empty_deleter); + + // operator* should return const reference + const int& cref = *r2; + REQUIRE(cref == 100); + + // Modifying through cref would fail to compile (correct) +} From 4a9894da3f221e5bfd0b661c4eff1ac77de02bb2 Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Tue, 13 Jan 2026 21:36:32 +0100 Subject: [PATCH 08/18] Prevent use of explicit operator bool() const noexcept --- tests/unique_resource_2.test.cpp | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/tests/unique_resource_2.test.cpp b/tests/unique_resource_2.test.cpp index a564286..da1d136 100644 --- a/tests/unique_resource_2.test.cpp +++ b/tests/unique_resource_2.test.cpp @@ -33,7 +33,7 @@ TEST_CASE("Construct file unique_resource", "[unique_resource]") { } }); - if (!file) { + if (file.get() == nullptr) { throw std::runtime_error("file didn't open"); } open_file_good = true; @@ -48,7 +48,7 @@ TEST_CASE("unique_resource basic construction and engagement", "[unique_resource { beman::scope::unique_resource r(42, CountingDeleter{&c}); - REQUIRE(static_cast(r)); + // XXX REQUIRE(static_cast(r)); REQUIRE(r.get() == 42); REQUIRE(c.value == 0); } @@ -63,7 +63,7 @@ TEST_CASE("unique_resource release disengages without deleting", "[unique_resour r.release(); - REQUIRE_FALSE(r); + // XXX REQUIRE_FALSE(r); } REQUIRE(c.value == 0); @@ -76,7 +76,7 @@ TEST_CASE("unique_resource reset() destroys current resource", "[unique_resource beman::scope::unique_resource r(1, CountingDeleter{&c}); r.reset(); - REQUIRE_FALSE(r); + // XXX REQUIRE_FALSE(r); REQUIRE(c.value == 1); } @@ -91,7 +91,7 @@ TEST_CASE("unique_resource reset(new_resource) replaces resource", "[unique_reso r.reset(2); - REQUIRE(r); + // XXX REQUIRE(r); REQUIRE(r.get() == 2); REQUIRE(c.value == 1); } @@ -105,8 +105,8 @@ TEST_CASE("unique_resource move constructor transfers ownership", "[unique_resou beman::scope::unique_resource r1(10, CountingDeleter{&c}); beman::scope::unique_resource r2(std::move(r1)); - REQUIRE_FALSE(r1); - REQUIRE(r2); + // XXX REQUIRE_FALSE(r1); + // XXX REQUIRE(r2); REQUIRE(r2.get() == 10); r2.reset(); @@ -122,8 +122,8 @@ TEST_CASE("unique_resource move assignment destroys target before transfer", "[u r2 = std::move(r1); - REQUIRE_FALSE(r1); - REQUIRE(r2); + // XXX REQUIRE_FALSE(r1); + // XXX REQUIRE(r2); REQUIRE(r2.get() == 1); REQUIRE(c2.value == 1); // old r2 destroyed @@ -144,14 +144,14 @@ TEST_CASE("unique_resource destructor is idempotent after release", "[unique_res REQUIRE(c.value == 0); } - +#ifdef BEMAN_SCOPE_USE_FALLBACK TEST_CASE("make_unique_resource_checked disengages on invalid", "[unique_resource]") { Counter c{}; // NOLINT(misc-const-correctness) { auto r = beman::scope::make_unique_resource_checked(-1, -1, CountingDeleter{&c}); - REQUIRE_FALSE(r); + // XXX REQUIRE_FALSE(r); } REQUIRE(c.value == 0); @@ -163,7 +163,7 @@ TEST_CASE("make_unique_resource_checked engages on valid", "[unique_resource]") { auto r = beman::scope::make_unique_resource_checked(3, -1, CountingDeleter{&c}); - REQUIRE(r); + // XXX REQUIRE(r); } REQUIRE(c.value == 1); @@ -192,6 +192,7 @@ TEST_CASE("Open a nonexisting file with make_unique_resource_checked", "[unique_ REQUIRE(open_file_good == false); REQUIRE(close_file_good == false); } +#endif TEST_CASE("unique_resource supports deduction guide", "[unique_resource]") { Counter c{}; // NOLINT(misc-const-correctness) From 7d92b09e87061f169b51a4ab21699c4d43398d50 Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Wed, 14 Jan 2026 06:37:23 +0100 Subject: [PATCH 09/18] Test appleclang on CI too --- .github/workflows/cmake.yml | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/.github/workflows/cmake.yml b/.github/workflows/cmake.yml index 09db9ed..de7a8ab 100644 --- a/.github/workflows/cmake.yml +++ b/.github/workflows/cmake.yml @@ -27,7 +27,18 @@ jobs: fail-fast: false matrix: - os: [windows] + os: [windows, macos] + include: + - { os: macos, uname: appleclang } + # - { os: ubuntu, uname: gcc } + - { os: ubuntu, uname: llvm } + - { os: windows, uname: msvc } + + # TODO(CK): + # type: [shared, static] + # include: + # - { type: shared, shared: YES } + # - { type: static, shared: NO } runs-on: ${{ matrix.os }}-latest @@ -49,19 +60,20 @@ jobs: if: matrix.os != 'windows' uses: aminya/setup-cpp@v1 with: + # compiler: ${{matrix.uname}} compiler: llvm - name: Configure CMake - run: cmake --preset msvc-${{env.BUILD_TYPE}} --log-level=VERBOSE + run: cmake --preset ${{matrix.uname}}-${{env.BUILD_TYPE}} --log-level=VERBOSE - name: Build # Build your program with the given configuration - run: cmake --build --preset msvc-${{env.BUILD_TYPE}} + run: cmake --build --preset ${{matrix.uname}}-${{env.BUILD_TYPE}} - name: Test # Execute tests defined by the CMake configuration - run: ctest --preset msvc-${{env.BUILD_TYPE}} + run: ctest --preset ${{matrix.uname}}-${{env.BUILD_TYPE}} # - name: Install # # Install the project artefacts to CMAKE_INSTALL_PREFIX - # run: cmake --build --preset msvc-${{env.BUILD_TYPE}} --target install + # run: cmake --build --preset ${{matrix.uname}}-${{env.BUILD_TYPE}} --target install From 58335ffadc8fc99e83c29566bf3ab4cf8199de85 Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Thu, 15 Jan 2026 21:35:16 +0100 Subject: [PATCH 10/18] Deleted move assignment operators scope_exit does not need to be move-assignable! --- .github/workflows/cmake.yml | 8 +++---- include/beman/scope/scope_impl.hpp | 37 +++++++++++++++++++++--------- makefile | 5 +++- tests/scope_exit.test.cpp | 19 +++++++++++++++ tests/unique_resource_2.test.cpp | 24 ++++++++++++++++++- 5 files changed, 76 insertions(+), 17 deletions(-) diff --git a/.github/workflows/cmake.yml b/.github/workflows/cmake.yml index de7a8ab..2f52cbf 100644 --- a/.github/workflows/cmake.yml +++ b/.github/workflows/cmake.yml @@ -27,10 +27,10 @@ jobs: fail-fast: false matrix: - os: [windows, macos] + os: [windows] include: - { os: macos, uname: appleclang } - # - { os: ubuntu, uname: gcc } + # XXX - { os: ubuntu, uname: gcc } - { os: ubuntu, uname: llvm } - { os: windows, uname: msvc } @@ -60,11 +60,11 @@ jobs: if: matrix.os != 'windows' uses: aminya/setup-cpp@v1 with: - # compiler: ${{matrix.uname}} + # XXX compiler: ${{matrix.uname}} compiler: llvm - name: Configure CMake - run: cmake --preset ${{matrix.uname}}-${{env.BUILD_TYPE}} --log-level=VERBOSE + run: cmake --preset ${{matrix.uname}}-${{env.BUILD_TYPE}} --log-level=VERBOSE # XXX -Wdev - name: Build # Build your program with the given configuration diff --git a/include/beman/scope/scope_impl.hpp b/include/beman/scope/scope_impl.hpp index e0cb393..31f0c10 100644 --- a/include/beman/scope/scope_impl.hpp +++ b/include/beman/scope/scope_impl.hpp @@ -63,15 +63,10 @@ class [[nodiscard]] scope_exit { auto operator=(const scope_exit&) -> scope_exit& = delete; scope_exit(const scope_exit&) = delete; - // Move assignment - constexpr auto operator=(scope_exit&& other) noexcept(std::is_nothrow_move_assignable_v) -> scope_exit& { - if (this != &other) { - f = std::move(other.f); - active = other.active; - other.active = false; - } - return *this; - } + // Deleted move assignment + // Does scope_exit need to be move-assignable? LEWG: NO! + constexpr auto operator=(scope_exit&& other) noexcept(std::is_nothrow_move_assignable_v) + -> scope_exit& = delete; // Destructor: call only if scope is exiting normally ~scope_exit() noexcept(noexcept(f())) { @@ -82,6 +77,9 @@ class [[nodiscard]] scope_exit { // Release to prevent execution constexpr auto release() -> void { active = false; } + + // Helper to tests if active + constexpr auto is_active() -> bool { return active; } }; // Factory helper @@ -114,8 +112,12 @@ class [[nodiscard]] scope_fail { scope_fail(const scope_fail&) = delete; auto operator=(const scope_fail&) -> scope_fail& = delete; + // Deleted move assignment // Move assignment - constexpr auto operator=(scope_fail&& other) noexcept(std::is_nothrow_move_assignable_v) -> scope_fail& { + constexpr auto operator=(scope_fail&& other) noexcept(std::is_nothrow_move_assignable_v) + -> scope_fail& = delete; +#if MOVE_ASSIGNMENT_NEEDED + G { if (this != &other) { f = std::move(other.f); active = other.active; @@ -124,6 +126,7 @@ class [[nodiscard]] scope_fail { } return *this; } +#endif // Destructor: call if scope is exiting due to an exception ~scope_fail() noexcept(noexcept(f())) { @@ -134,6 +137,9 @@ class [[nodiscard]] scope_fail { // Release to prevent execution constexpr auto release() -> void { active = false; } + + // Helper to tests if active + constexpr auto is_active() -> bool { return active; } }; // Factory helper @@ -166,8 +172,12 @@ class [[nodiscard]] scope_success { scope_success(const scope_success&) = delete; auto operator=(const scope_success&) -> scope_success& = delete; + // Deleted move assignment // Move assignment - constexpr auto operator=(scope_success&& other) noexcept(std::is_nothrow_move_assignable_v) -> scope_success& { + constexpr auto operator=(scope_success&& other) noexcept(std::is_nothrow_move_assignable_v) + -> scope_success& = delete; +#if MOVE_ASSIGNMENT_NEEDED + { if (this != &other) { f = std::move(other.f); active = other.active; @@ -176,6 +186,7 @@ class [[nodiscard]] scope_success { } return *this; } +#endif // Destructor: call only if scope is exiting normally ~scope_success() noexcept(noexcept(f())) { @@ -186,6 +197,9 @@ class [[nodiscard]] scope_success { // Release to prevent execution constexpr auto release() -> void { active = false; } + + // Helper to tests if active + constexpr auto is_active() -> bool { return active; } }; // Factory helper @@ -271,6 +285,7 @@ class [[nodiscard]] unique_resource { // TODO(CK): missing usecase? constexpr auto get_deleter() const noexcept -> Deleter; + // Helper to tests is_active() // NOTE: check if active; not required from LWG? constexpr explicit operator bool() const noexcept { return active; } }; diff --git a/makefile b/makefile index 22b4fc4..81041fd 100644 --- a/makefile +++ b/makefile @@ -54,11 +54,14 @@ distclean: clean rm -rf build find . -name '*~' -delete +gclean: clean + find build -name '*.gc..' -delete + build/coverage: test mkdir -p $@ coverage: build/coverage - gcovr # XXX -v + gcovr --merge-mode-functions separate # Anything we don't know how to build will use this rule. % :: diff --git a/tests/scope_exit.test.cpp b/tests/scope_exit.test.cpp index 65acf7a..51a4477 100644 --- a/tests/scope_exit.test.cpp +++ b/tests/scope_exit.test.cpp @@ -125,6 +125,25 @@ TEST_CASE("scope_exit handles nested guards in correct order", "[scope_exit][adv REQUIRE(trace == "second first "); } +// fails +// #include +// +// TEST_CASE("scope_exit move assignment transfers ownership", "[scope_exit][advanced]") { +// bool cleanup_ran = false; +// +// { +// scope_exit> guard1([&]{ cleanup_ran = true; }); +// scope_exit> guard2([&]{}); +// +// guard2 = std::move(guard1); +// +// REQUIRE_FALSE(guard1.is_active()); +// REQUIRE(guard2.is_active()); +// } +// +// REQUIRE(cleanup_ran == false); +// } + // fails // TEST_CASE("scope_exit cleanup handles custom object with side effects", "[scope_exit][advanced]") { // struct Tracer { diff --git a/tests/unique_resource_2.test.cpp b/tests/unique_resource_2.test.cpp index da1d136..b613d5c 100644 --- a/tests/unique_resource_2.test.cpp +++ b/tests/unique_resource_2.test.cpp @@ -4,6 +4,7 @@ #include #include +#include namespace { @@ -199,7 +200,7 @@ TEST_CASE("unique_resource supports deduction guide", "[unique_resource]") { beman::scope::unique_resource r(123, CountingDeleter{&c}); - static_assert(std::is_same_v >); + static_assert(std::is_same_v>); r.reset(); REQUIRE(c.value == 1); @@ -253,3 +254,24 @@ TEST_CASE("unique_resource operator* returns reference to resource", "[unique_re // Modifying through cref would fail to compile (correct) } + +struct Foo { + int value = 0; +}; + +TEST_CASE("unique_resource operator-> works", "[unique_resource]") { + bool deleted = false; + Foo* raw = new Foo{42}; + + // Use std::function for the deleter + beman::scope::unique_resource> r(raw, [&](Foo* p) { + deleted = true; + delete p; + }); + + REQUIRE(r->value == 42); + r->value = 100; + REQUIRE(r->value == 100); + + REQUIRE_FALSE(deleted); // deleter not called yet +} From 1a86a992dfa6dbe3e8ca246edb5dd21474e5a454 Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Thu, 15 Jan 2026 22:04:00 +0100 Subject: [PATCH 11/18] Try to BUILD_SHARED_LIBS on windows add get_version() to CXX_MODULE to export some code; --- CMakePresets.json | 1 + include/beman/scope/beman.scope.cppm | 3 +++ 2 files changed, 4 insertions(+) diff --git a/CMakePresets.json b/CMakePresets.json index 91bb5e6..54e21c1 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -116,6 +116,7 @@ ], "cacheVariables": { "CMAKE_CXX_STANDARD": "23", + "BUILD_SHARED_LIBS": true, "CMAKE_TOOLCHAIN_FILE": "infra/cmake/msvc-toolchain.cmake" } } diff --git a/include/beman/scope/beman.scope.cppm b/include/beman/scope/beman.scope.cppm index 59ce381..a2de359 100644 --- a/include/beman/scope/beman.scope.cppm +++ b/include/beman/scope/beman.scope.cppm @@ -8,6 +8,9 @@ module; export module beman.scope; export namespace beman::scope { + +constexpr int get_version() { return 0; } + using ::beman::scope::scope_exit; using ::beman::scope::scope_fail; using ::beman::scope::scope_success; From 8c636279a9aec608ea0938b8faafc02389864c8f Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Thu, 15 Jan 2026 23:38:26 +0100 Subject: [PATCH 12/18] Use GenerateExportHeader cmake module --- CMakeLists.txt | 23 ++++++++++++++++++++--- include/beman/scope/beman.scope.cppm | 3 --- include/beman/scope/scope_impl.hpp | 10 ++++++---- makefile | 2 +- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ce92578..c4d14b8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,6 +23,8 @@ else() endif() set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +set(CMAKE_CXX_VISIBILITY_PRESET hidden) +set(CMAKE_VISIBILITY_INLINES_HIDDEN TRUE) # [CMAKE.SKIP_TESTS] option( @@ -53,25 +55,40 @@ message( if(CMAKE_CXX_SCAN_FOR_MODULES) add_library(beman.scope) +else() + add_library(beman.scope INTERFACE) +endif() + +include(GenerateExportHeader) + +generate_export_header( + beman.scope + BASE_NAME beman.scope + EXPORT_FILE_NAME beman/scope/modules_export.hpp +) + +if(CMAKE_CXX_SCAN_FOR_MODULES) target_sources( beman.scope PUBLIC FILE_SET HEADERS - BASE_DIRS include + BASE_DIRS include ${CMAKE_CURRENT_BINARY_DIR} FILES include/beman/scope/scope.hpp + ${CMAKE_CURRENT_BINARY_DIR}/beman/scope/modules_export.hpp + PUBLIC FILE_SET CXX_MODULES BASE_DIRS include FILES include/beman/scope/beman.scope.cppm ) else() - add_library(beman.scope INTERFACE) target_sources( beman.scope INTERFACE FILE_SET HEADERS - BASE_DIRS include + BASE_DIRS include ${CMAKE_CURRENT_BINARY_DIR} FILES include/beman/scope/scope.hpp + ${CMAKE_CURRENT_BINARY_DIR}/beman/scope/modules_export.hpp ) endif() diff --git a/include/beman/scope/beman.scope.cppm b/include/beman/scope/beman.scope.cppm index a2de359..59ce381 100644 --- a/include/beman/scope/beman.scope.cppm +++ b/include/beman/scope/beman.scope.cppm @@ -8,9 +8,6 @@ module; export module beman.scope; export namespace beman::scope { - -constexpr int get_version() { return 0; } - using ::beman::scope::scope_exit; using ::beman::scope::scope_fail; using ::beman::scope::scope_success; diff --git a/include/beman/scope/scope_impl.hpp b/include/beman/scope/scope_impl.hpp index 31f0c10..fbb691d 100644 --- a/include/beman/scope/scope_impl.hpp +++ b/include/beman/scope/scope_impl.hpp @@ -37,6 +37,8 @@ // clang-format on #ifdef BEMAN_SCOPE_USE_FALLBACK +#include "beman/scope/modules_export.hpp" + #include #include #include @@ -46,7 +48,7 @@ namespace beman::scope { // TODO(CK): make a std::experimental::scope_exit::scope_exit conform // implementation template -class [[nodiscard]] scope_exit { +class [[nodiscard]] BEMAN_SCOPE_EXPORT scope_exit { F f; bool active = true; @@ -92,7 +94,7 @@ auto make_scope_exit(F f) -> scope_exit { // TODO(CK): make a std::experimental::scope_fail::scope_fail conform // implementation template -class [[nodiscard]] scope_fail { +class [[nodiscard]] BEMAN_SCOPE_EXPORT scope_fail { F f; bool active = true; int exception_count{}; @@ -152,7 +154,7 @@ constexpr auto make_scope_fail(F&& f) -> scope_fail> { // TODO(CK): make a std::experimental::scope_success::scope_success conform // implementation template -class [[nodiscard]] scope_success { +class [[nodiscard]] BEMAN_SCOPE_EXPORT scope_success { F f; bool active = true; int exception_count{}; @@ -210,7 +212,7 @@ constexpr auto make_scope_success(F&& f) -> scope_success> { } template -class [[nodiscard]] unique_resource { +class [[nodiscard]] BEMAN_SCOPE_EXPORT unique_resource { Resource resource; Deleter deleter; bool active = true; diff --git a/makefile b/makefile index 81041fd..3ff2d1a 100644 --- a/makefile +++ b/makefile @@ -32,7 +32,7 @@ else ifeq (${hostSystemName},Linux) export CXX=clang++-20 endif -.PHONY: all install coverage clean distclean +.PHONY: all install coverage gclean distclean all: build/compile_commands.json ln -sf $< . From f51bfa4b0a85363a3461bebafff2d1abce6fc8d6 Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Sat, 17 Jan 2026 00:07:13 +0100 Subject: [PATCH 13/18] Quickfix for missing export header This is needed if only an interface library is build --- CMakeLists.txt | 18 +++++++++--------- include/beman/scope/scope_impl.hpp | 8 ++++++-- makefile | 4 ++-- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c4d14b8..4dc32b7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,18 +55,18 @@ message( if(CMAKE_CXX_SCAN_FOR_MODULES) add_library(beman.scope) + + include(GenerateExportHeader) + + generate_export_header( + beman.scope + BASE_NAME beman.scope + EXPORT_FILE_NAME beman/scope/modules_export.hpp + ) else() add_library(beman.scope INTERFACE) endif() -include(GenerateExportHeader) - -generate_export_header( - beman.scope - BASE_NAME beman.scope - EXPORT_FILE_NAME beman/scope/modules_export.hpp -) - if(CMAKE_CXX_SCAN_FOR_MODULES) target_sources( beman.scope @@ -88,7 +88,7 @@ else() FILE_SET HEADERS BASE_DIRS include ${CMAKE_CURRENT_BINARY_DIR} FILES include/beman/scope/scope.hpp - ${CMAKE_CURRENT_BINARY_DIR}/beman/scope/modules_export.hpp + # NO! ${CMAKE_CURRENT_BINARY_DIR}/beman/scope/modules_export.hpp ) endif() diff --git a/include/beman/scope/scope_impl.hpp b/include/beman/scope/scope_impl.hpp index fbb691d..b05159c 100644 --- a/include/beman/scope/scope_impl.hpp +++ b/include/beman/scope/scope_impl.hpp @@ -34,10 +34,14 @@ # error "Standard present but __cpp_lib_scope_exit not defined" # endif #endif -// clang-format on #ifdef BEMAN_SCOPE_USE_FALLBACK -#include "beman/scope/modules_export.hpp" +# if __has_include("beman/scope/modules_export.hpp") +# include "beman/scope/modules_export.hpp" +# else +# define BEMAN_SCOPE_EXPORT +# endif +// clang-format on #include #include diff --git a/makefile b/makefile index 3ff2d1a..bf4e84e 100644 --- a/makefile +++ b/makefile @@ -50,8 +50,8 @@ build/compile_commands.json: CMakeLists.txt makefile install: build/cmake_install.cmake cmake --install build -distclean: clean - rm -rf build +distclean: # XXX clean + rm -rf build compile_commands.json find . -name '*~' -delete gclean: clean From c9197e604851cc852543dba10ee562d2ce4c1a07 Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Sat, 17 Jan 2026 16:23:47 +0100 Subject: [PATCH 14/18] Use ctest --build-and-test to check the installed version --- CMakeLists.txt | 49 +++++++++++++++------------- CMakePresets.json | 3 +- examples/CMakeLists.txt | 20 +++++++++++- examples/scope-module.cpp | 10 +++--- include/beman/scope/beman.scope.cppm | 2 +- include/beman/scope/scope.hpp | 2 +- include/beman/scope/scope_impl.hpp | 7 ++-- tests/CMakeLists.txt | 28 +++++++++++++++- tests/module.test.cpp | 18 +++++----- tests/unique_resource.test.cpp | 2 +- 10 files changed, 94 insertions(+), 47 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4dc32b7..d0a72d5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,12 +47,14 @@ option( ) message( - "Compiler is: ${CMAKE_CXX_COMPILER_ID} version: ${CMAKE_CXX_COMPILER_VERSION}" + "Compiler is: ${CMAKE_CXX_COMPILER_ID} version: ${CMAKE_CXX_COMPILER_VERSION}" ) message( - "cmake is: ${CMAKE_VERSION} modules scan : ${CMAKE_CXX_SCAN_FOR_MODULES}" + "cmake is: ${CMAKE_VERSION} modules scan: ${CMAKE_CXX_SCAN_FOR_MODULES}" ) +# gersemi: on + if(CMAKE_CXX_SCAN_FOR_MODULES) add_library(beman.scope) @@ -72,51 +74,51 @@ if(CMAKE_CXX_SCAN_FOR_MODULES) beman.scope PUBLIC FILE_SET HEADERS - BASE_DIRS include ${CMAKE_CURRENT_BINARY_DIR} - FILES include/beman/scope/scope.hpp - ${CMAKE_CURRENT_BINARY_DIR}/beman/scope/modules_export.hpp - + BASE_DIRS include ${CMAKE_CURRENT_BINARY_DIR} + FILES + include/beman/scope/scope.hpp + include/beman/scope/scope_impl.hpp + ${CMAKE_CURRENT_BINARY_DIR}/beman/scope/modules_export.hpp PUBLIC FILE_SET CXX_MODULES - BASE_DIRS include - FILES include/beman/scope/beman.scope.cppm + BASE_DIRS include + FILES include/beman/scope/beman.scope.cppm ) else() target_sources( beman.scope INTERFACE FILE_SET HEADERS - BASE_DIRS include ${CMAKE_CURRENT_BINARY_DIR} - FILES include/beman/scope/scope.hpp - # NO! ${CMAKE_CURRENT_BINARY_DIR}/beman/scope/modules_export.hpp + BASE_DIRS include ${CMAKE_CURRENT_BINARY_DIR} + FILES include/beman/scope/scope.hpp + # NO! ${CMAKE_CURRENT_BINARY_DIR}/beman/scope/modules_export.hpp ) endif() +target_compile_features(beman.scope PUBLIC cxx_std_20) add_library(beman::scope ALIAS beman.scope) set_target_properties( beman.scope - PROPERTIES - VERIFY_INTERFACE_HEADER_SETS ON - EXPORT_NAME scope + PROPERTIES VERIFY_INTERFACE_HEADER_SETS ON EXPORT_NAME scope ) include(GNUInstallDirs) +set(package_install_dir ${CMAKE_INSTALL_LIBDIR}/cmake/beman.scope) + +# TBD: always? CK install( TARGETS beman.scope COMPONENT beman.scope EXPORT beman.scope-targets - - FILE_SET CXX_MODULES - DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} + FILE_SET CXX_MODULES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} CXX_MODULES_BMI - DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/beman.scope/bmi-${CMAKE_CXX_COMPILER_ID}_$ + DESTINATION + ${package_install_dir}/bmi-${CMAKE_CXX_COMPILER_ID}_$ FILE_SET HEADERS ) -# gersemi: on - if(BEMAN_SCOPE_INSTALL_CONFIG_FILE_PACKAGE) include(CMakePackageConfigHelpers) @@ -129,13 +131,13 @@ if(BEMAN_SCOPE_INSTALL_CONFIG_FILE_PACKAGE) FILES cmake/beman.scope-config.cmake ${CMAKE_CURRENT_BINARY_DIR}/beman.scope-config-version.cmake - DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/beman.scope + DESTINATION ${package_install_dir} COMPONENT beman.scope ) install( EXPORT beman.scope-targets - DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/beman.scope + DESTINATION ${package_install_dir} NAMESPACE beman:: CXX_MODULES_DIRECTORY cxx-modules @@ -143,8 +145,9 @@ if(BEMAN_SCOPE_INSTALL_CONFIG_FILE_PACKAGE) ) endif() +enable_testing() + if(BEMAN_SCOPE_BUILD_TESTS) - enable_testing() add_subdirectory(tests) endif() diff --git a/CMakePresets.json b/CMakePresets.json index 54e21c1..659825c 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -104,6 +104,7 @@ ], "cacheVariables": { "CMAKE_CXX_STANDARD": "23", + "BUILD_SHARED_LIBS": false, "CMAKE_TOOLCHAIN_FILE": "infra/cmake/msvc-toolchain.cmake" } }, @@ -116,7 +117,7 @@ ], "cacheVariables": { "CMAKE_CXX_STANDARD": "23", - "BUILD_SHARED_LIBS": true, + "BUILD_SHARED_LIBS": false, "CMAKE_TOOLCHAIN_FILE": "infra/cmake/msvc-toolchain.cmake" } } diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 0f1d6d3..60bcdea 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,9 +1,27 @@ # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +cmake_minimum_required(VERSION 3.28...4.2) + +project(beman.scope.example LANGUAGES CXX) + +if(PROJECT_IS_TOP_LEVEL) + if(NOT DEFINED CMAKE_CXX_STANDARD) + set(CMAKE_CXX_STANDARD 23) + set(CMAKE_CXX_EXTENSIONS YES) + set(CMAKE_CXX_STANDARD_REQUIRED YES) + endif() + set(CMAKE_CXX_SCAN_FOR_MODULES OFF) + + find_package(beman.scope REQUIRED) + + enable_testing() +endif() + set(ALL_EXAMPLES scope_example unique_resource unique_resource-file) # module tests will only compile with gcc15 or clang20 and above -if(CMAKE_CXX_SCAN_FOR_MODULES AND CMAKE_CXX_MODULE_STD) +# NOTE: needs C++23 or newer! CK +if(CMAKE_CXX_SCAN_FOR_MODULES) list(APPEND ALL_EXAMPLES scope-module) endif() diff --git a/examples/scope-module.cpp b/examples/scope-module.cpp index 22dd6d3..01d7cc7 100644 --- a/examples/scope-module.cpp +++ b/examples/scope-module.cpp @@ -15,12 +15,12 @@ // destroy noisy // scope exit: true success: true fail: false -// #ifdef HAS_MODULE_STD +// NOTE: this needs C++23! CK +#ifdef HAS_MODULE_STD import std; -// #else -// NOTE: this needs C++23! -// #include -// #endif +#else +#include +#endif import beman.scope; diff --git a/include/beman/scope/beman.scope.cppm b/include/beman/scope/beman.scope.cppm index 59ce381..f862dae 100644 --- a/include/beman/scope/beman.scope.cppm +++ b/include/beman/scope/beman.scope.cppm @@ -3,7 +3,7 @@ // g++-15 -std=c++26 -O2 -fmodules -fmodule-only -c ${scopetop}/include/beman/scope/beman.scope.cppm module; -#include "scope.hpp" +#include "beman/scope/scope.hpp" export module beman.scope; diff --git a/include/beman/scope/scope.hpp b/include/beman/scope/scope.hpp index 9b45233..5631e1b 100644 --- a/include/beman/scope/scope.hpp +++ b/include/beman/scope/scope.hpp @@ -443,7 +443,7 @@ using scope_fail = scope_guard; using deleter_type = std::decay_t; + unique_resource ur(resource_type{}, std::forward(d)); if (r == invalid) { - // Disengaged resource - unique_resource ur(resource_type{}, std::forward(d)); ur.release(); // disengage immediately - return ur; } - - return unique_resource(std::forward(r), std::forward(d)); + return ur; } } // namespace beman::scope diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 6acb735..c30035b 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -7,7 +7,7 @@ FetchContent_Declare( GIT_REPOSITORY https://github.com/catchorg/Catch2.git GIT_TAG v3.11.0 EXCLUDE_FROM_ALL - # FIND_PACKAGE_ARGS 3.11 + # NO! FIND_PACKAGE_ARGS 3.11 ) FetchContent_MakeAvailable(Catch2) @@ -37,3 +37,29 @@ foreach(testname ${ALL_TESTNAMES}) ) catch_discover_tests(test.${testname}) endforeach() + +if(BEMAN_SCOPE_INSTALL_CONFIG_FILE_PACKAGE) + # test if the targets are usable from the install directory + add_test( + NAME install-to-stagedir + COMMAND + ${CMAKE_COMMAND} --install ${CMAKE_BINARY_DIR} --prefix + ${CMAKE_BINARY_DIR}/stagedir --config $ + ) + add_test( + NAME find-package-test + COMMAND + ${CMAKE_CTEST_COMMAND} # --verbose + --output-on-failure -C $ --build-and-test + "${CMAKE_SOURCE_DIR}/examples" + "${CMAKE_CURRENT_BINARY_DIR}/find-package-test" --build-generator + ${CMAKE_GENERATOR} --build-makeprogram ${CMAKE_MAKE_PROGRAM} + --build-options "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}" + "-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}" + "-DCMAKE_CXX_EXTENSIONS=${CMAKE_CXX_EXTENSIONS}" + "-DCMAKE_CXX_MODULE_STD=${CMAKE_CXX_MODULE_STD}" + "-DCMAKE_CXX_SCAN_FOR_MODULES=${CMAKE_CXX_SCAN_FOR_MODULES}" + "-DCMAKE_BUILD_TYPE=$" + "-DCMAKE_PREFIX_PATH=${CMAKE_BINARY_DIR}/stagedir" + ) +endif() diff --git a/tests/module.test.cpp b/tests/module.test.cpp index a4b22fc..ce898ea 100644 --- a/tests/module.test.cpp +++ b/tests/module.test.cpp @@ -11,27 +11,29 @@ struct DummyResource { DummyResource(bool& flag) : cleaned(flag) { cleaned = false; } - bool is_clean() const { return cleaned; } + [[nodiscard]] bool is_clean() const { return cleaned; } }; TEST_CASE("module-test", "[scope_module_test]") { - bool exit_ran, success_ran, fail_ran = false; - bool cleaned = true; + bool exit_ran{}; + bool success_ran{}; + bool fail_ran{}; + bool cleaned{true}; { // clang-format off beman::scope::scope_exit _se([&exit_ran] { exit_ran = true; }); beman::scope::scope_success _ss([&success_ran] { success_ran = true; }); beman::scope::scope_fail _sf([&fail_ran] { fail_ran = true; }); - auto resource_ptr = beman::scope::unique_resource(new DummyResource(cleaned), - [](DummyResource* ptr) { ptr->cleaned =true; delete ptr; }); - REQUIRE(cleaned == false); - REQUIRE(resource_ptr->is_clean() == false); + auto resource_ptr = beman::scope::unique_resource(new DummyResource(cleaned), + [](DummyResource* ptr) { ptr->cleaned = true; delete ptr; }); // clang-format on + + REQUIRE(cleaned == false); + REQUIRE(resource_ptr->is_clean() == false); } // Normal scope exit REQUIRE(exit_ran == true); REQUIRE(success_ran == true); REQUIRE(fail_ran == false); REQUIRE(cleaned == true); - } diff --git a/tests/unique_resource.test.cpp b/tests/unique_resource.test.cpp index 48f611d..9edd61e 100644 --- a/tests/unique_resource.test.cpp +++ b/tests/unique_resource.test.cpp @@ -68,7 +68,7 @@ TEST_CASE("unique_resource does not clean up after release", "[unique_resource]" [](DummyResource r) { *(r.cleanedUp) = true; } ); - res.release(); //no cleanup run + res.release(); // no cleanup run } REQUIRE(cleaned == false); From 7af734d63cd96cf64ecbdef83f49f10a905ad108 Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Sat, 17 Jan 2026 19:51:45 +0100 Subject: [PATCH 15/18] The final cut Prevent warning: placeholder variables are a C++2c extension --- CMakeLists.txt | 6 ++++-- CMakePresets.json | 14 ++++++-------- examples/scope-module.cpp | 11 +++++++---- include/beman/scope/beman.scope.cppm | 2 +- include/beman/scope/scope.hpp | 2 +- include/beman/scope/scope_impl.hpp | 8 ++++---- 6 files changed, 23 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d0a72d5..ffb5c17 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -84,18 +84,20 @@ if(CMAKE_CXX_SCAN_FOR_MODULES) BASE_DIRS include FILES include/beman/scope/beman.scope.cppm ) + target_compile_features(beman.scope PUBLIC cxx_std_20) else() target_sources( beman.scope INTERFACE FILE_SET HEADERS BASE_DIRS include ${CMAKE_CURRENT_BINARY_DIR} - FILES include/beman/scope/scope.hpp + FILES + include/beman/scope/scope.hpp + include/beman/scope/scope_impl.hpp # NO! ${CMAKE_CURRENT_BINARY_DIR}/beman/scope/modules_export.hpp ) endif() -target_compile_features(beman.scope PUBLIC cxx_std_20) add_library(beman::scope ALIAS beman.scope) set_target_properties( diff --git a/CMakePresets.json b/CMakePresets.json index 659825c..b48e93b 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -7,9 +7,11 @@ "generator": "Ninja", "binaryDir": "${sourceDir}/build/${presetName}", "cacheVariables": { - "CMAKE_CXX_STANDARD": "20", - "CMAKE_EXPORT_COMPILE_COMMANDS": "ON", - "CMAKE_PROJECT_TOP_LEVEL_INCLUDES": "./infra/cmake/use-fetch-content.cmake" + "CMAKE_CXX_EXTENSIONS": true, + "CMAKE_CXX_STANDARD": "23", + "CMAKE_CXX_STANDARD_REQUIRED": true, + "CMAKE_EXPORT_COMPILE_COMMANDS": true, + "CMAKE_PROJECT_TOP_LEVEL_INCLUDES": "infra/cmake/use-fetch-content.cmake" } }, { @@ -68,7 +70,7 @@ "_release-base" ], "cacheVariables": { - "CMAKE_TOOLCHAIN_FILE": "infra/cmake/llvm-toolchain.cmake" + "CMAKE_TOOLCHAIN_FILE": "infra/cmake/llvm-libc++-toolchain.cmake" } }, { @@ -79,7 +81,6 @@ "_debug-base" ], "cacheVariables": { - "CMAKE_CXX_STANDARD": "23", "CMAKE_TOOLCHAIN_FILE": "infra/cmake/appleclang-toolchain.cmake" } }, @@ -91,7 +92,6 @@ "_release-base" ], "cacheVariables": { - "CMAKE_CXX_STANDARD": "23", "CMAKE_TOOLCHAIN_FILE": "infra/cmake/appleclang-toolchain.cmake" } }, @@ -103,7 +103,6 @@ "_debug-base" ], "cacheVariables": { - "CMAKE_CXX_STANDARD": "23", "BUILD_SHARED_LIBS": false, "CMAKE_TOOLCHAIN_FILE": "infra/cmake/msvc-toolchain.cmake" } @@ -116,7 +115,6 @@ "_release-base" ], "cacheVariables": { - "CMAKE_CXX_STANDARD": "23", "BUILD_SHARED_LIBS": false, "CMAKE_TOOLCHAIN_FILE": "infra/cmake/msvc-toolchain.cmake" } diff --git a/examples/scope-module.cpp b/examples/scope-module.cpp index 01d7cc7..da0427d 100644 --- a/examples/scope-module.cpp +++ b/examples/scope-module.cpp @@ -32,12 +32,14 @@ struct noisy_resource { int main() { - bool exit_ran, success_ran, fail_ran = false; + bool exit_ran{}; + bool success_ran{}; + bool fail_ran{}; { std::print("--> scope start\n"); - beman::scope::scope_exit _([&exit_ran] { exit_ran = true; }); - beman::scope::scope_success _([&success_ran] { success_ran = true; }); - beman::scope::scope_fail _([&fail_ran] { fail_ran = true; }); + beman::scope::scope_exit _se([&exit_ran] { exit_ran = true; }); + beman::scope::scope_success _ss([&success_ran] { success_ran = true; }); + beman::scope::scope_fail _sf([&fail_ran] { fail_ran = true; }); auto resource_ptr = beman::scope::unique_resource(new noisy_resource(), // Cleanup function [](noisy_resource* ptr) { delete ptr; }); @@ -46,3 +48,4 @@ int main() { std::print("scope exit: {} success: {} fail: {} \n", exit_ran, success_ran, fail_ran); } +// clang-format on diff --git a/include/beman/scope/beman.scope.cppm b/include/beman/scope/beman.scope.cppm index f862dae..e9bfaec 100644 --- a/include/beman/scope/beman.scope.cppm +++ b/include/beman/scope/beman.scope.cppm @@ -3,7 +3,7 @@ // g++-15 -std=c++26 -O2 -fmodules -fmodule-only -c ${scopetop}/include/beman/scope/beman.scope.cppm module; -#include "beman/scope/scope.hpp" +#include export module beman.scope; diff --git a/include/beman/scope/scope.hpp b/include/beman/scope/scope.hpp index 5631e1b..0222c5c 100644 --- a/include/beman/scope/scope.hpp +++ b/include/beman/scope/scope.hpp @@ -443,7 +443,7 @@ using scope_fail = scope_guard #endif // BEMAN_SCOPE_USE_STD_EXPERIMENTAL diff --git a/include/beman/scope/scope_impl.hpp b/include/beman/scope/scope_impl.hpp index 45e607c..161d397 100644 --- a/include/beman/scope/scope_impl.hpp +++ b/include/beman/scope/scope_impl.hpp @@ -319,10 +319,10 @@ constexpr auto make_unique_resource_checked(R&& r, const Invalid& invalid, D&& d #elifdef BEMAN_SCOPE_USE_STD_EXPERIMENTAL namespace beman::scope { -using std::experimental::scope_exit; -using std::experimental::scope_fail; -using std::experimental::scope_success; -using std::experimental::unique_resource; +using ::std::experimental::scope_exit; +using ::std::experimental::scope_fail; +using ::std::experimental::scope_success; +using ::std::experimental::unique_resource; } // namespace beman::scope // #endif // BEMAN_SCOPE_USE_FALLBACK From 3c99179bef3095d01c11b945fdce87fbc459a86b Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Sat, 17 Jan 2026 20:32:30 +0100 Subject: [PATCH 16/18] Prevent use of std::print() for now --- examples/scope-module.cpp | 37 +++++++++++++++++++++++++------------ tests/module.test.cpp | 6 +++++- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/examples/scope-module.cpp b/examples/scope-module.cpp index da0427d..2bb67a8 100644 --- a/examples/scope-module.cpp +++ b/examples/scope-module.cpp @@ -15,37 +15,50 @@ // destroy noisy // scope exit: true success: true fail: false +#include + // NOTE: this needs C++23! CK #ifdef HAS_MODULE_STD import std; -#else -#include #endif +// for g++-15 the order is important -- import after #includes import beman.scope; -// clang-format off -struct noisy_resource { - noisy_resource() { std::print( "construct noisy\n" ); } - ~noisy_resource() { std::print( "destroy noisy\n" ); } +namespace { + +struct DummyResource { + bool& cleaned; + + DummyResource(bool& flag) : cleaned(flag) { cleaned = false; } + + [[nodiscard]] bool is_clean() const { return cleaned; } }; +} // namespace + int main() { bool exit_ran{}; bool success_ran{}; bool fail_ran{}; + bool cleaned{true}; { - std::print("--> scope start\n"); + // clang-format off beman::scope::scope_exit _se([&exit_ran] { exit_ran = true; }); beman::scope::scope_success _ss([&success_ran] { success_ran = true; }); beman::scope::scope_fail _sf([&fail_ran] { fail_ran = true; }); - auto resource_ptr = beman::scope::unique_resource(new noisy_resource(), - // Cleanup function - [](noisy_resource* ptr) { delete ptr; }); - std::print("--> scope end\n"); + auto resource_ptr = beman::scope::unique_resource(new DummyResource(cleaned), + [](DummyResource* ptr) { ptr->cleaned = true; delete ptr; }); + // clang-format on + + assert(cleaned == false); + assert(resource_ptr->is_clean() == false); } // Normal scope exit - std::print("scope exit: {} success: {} fail: {} \n", exit_ran, success_ran, fail_ran); + assert(exit_ran == true); + assert(success_ran == true); + assert(fail_ran == false); + assert(cleaned == true); } // clang-format on diff --git a/tests/module.test.cpp b/tests/module.test.cpp index ce898ea..d5f7ec6 100644 --- a/tests/module.test.cpp +++ b/tests/module.test.cpp @@ -3,9 +3,11 @@ #define CATCH_CONFIG_MAIN #include -// for g++-15 the order is important -- import after includes +// for g++-15 the order is important -- import after #includes import beman.scope; +namespace { + struct DummyResource { bool& cleaned; @@ -14,6 +16,8 @@ struct DummyResource { [[nodiscard]] bool is_clean() const { return cleaned; } }; +} // namespace + TEST_CASE("module-test", "[scope_module_test]") { bool exit_ran{}; bool success_ran{}; From 9b632076ce670935935a7824c098f61cb430cb49 Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Tue, 27 Jan 2026 11:32:06 +0100 Subject: [PATCH 17/18] Add infra submodule and use it --- .github/workflows/ci_tests.yml | 53 +++++++++++++------ .github/workflows/pre-commit-check.yml | 13 +++++ .github/workflows/pre-commit-update.yml | 15 ++++++ .gitignore | 8 ++- .pre-commit-config.yaml | 4 +- CMakeLists.txt | 4 +- CMakePresets.json | 30 ++++++++++- include/beman/scope/beman.scope.cppm | 2 +- infra/.beman_submodule | 4 ++ infra/.pre-commit-config.yaml | 3 ++ .../cmake/beman-install-library-config.cmake | 6 +-- infra/cmake/use-fetch-content.cmake | 5 +- makefile | 6 ++- tests/CMakeLists.txt | 8 ++- 14 files changed, 130 insertions(+), 31 deletions(-) create mode 100644 .github/workflows/pre-commit-check.yml create mode 100644 .github/workflows/pre-commit-update.yml diff --git a/.github/workflows/ci_tests.yml b/.github/workflows/ci_tests.yml index 9a463d5..f2475d5 100644 --- a/.github/workflows/ci_tests.yml +++ b/.github/workflows/ci_tests.yml @@ -10,14 +10,13 @@ on: workflow_dispatch: schedule: - cron: '30 15 * * *' - - cron: "0 0 * * 0" jobs: beman-submodule-check: - uses: ./.github/workflows/reusable-beman-submodule-check.yml + uses: bemanproject/infra-workflows/.github/workflows/reusable-beman-submodule-check.yml@1.2.1 preset-test: - uses: ./.github/workflows/reusable-beman-preset-test.yml + uses: bemanproject/infra-workflows/.github/workflows/reusable-beman-preset-test.yml@1.2.1 with: matrix_config: > [ @@ -25,10 +24,14 @@ jobs: {"preset": "gcc-release", "image": "ghcr.io/bemanproject/infra-containers-gcc:latest"}, {"preset": "llvm-debug", "image": "ghcr.io/bemanproject/infra-containers-clang:latest"}, {"preset": "llvm-release", "image": "ghcr.io/bemanproject/infra-containers-clang:latest"}, + {"preset": "appleclang-debug", "runner": "macos-latest"}, + {"preset": "appleclang-release", "runner": "macos-latest"}, + {"preset": "msvc-debug", "runner": "windows-latest"}, + {"preset": "msvc-release", "runner": "windows-latest"} ] build-and-test: - uses: ./.github/workflows/reusable-beman-build-and-test.yml + uses: bemanproject/infra-workflows/.github/workflows/reusable-beman-build-and-test.yml@1.2.1 with: matrix_config: > { @@ -51,7 +54,7 @@ jobs: } ] }, - { "versions": ["14", "13"], + { "versions": ["14"], "tests": [ { "cxxversions": ["c++26", "c++23", "c++20"], "tests": [{ "stdlibs": ["libstdc++"], "tests": ["Release.Default"]}] @@ -60,11 +63,11 @@ jobs: } ], "clang": [ - { "versions": ["20"], + { "versions": ["21"], "tests": [ {"cxxversions": ["c++26"], "tests": [ - { "stdlibs": ["libstdc++"], + { "stdlibs": ["libc++"], "tests": [ "Debug.Default", "Release.Default", "Release.TSan", "Release.MaxSan", "Debug.Werror", "Debug.Dynamic" @@ -74,16 +77,38 @@ jobs: }, { "cxxversions": ["c++23", "c++20"], "tests": [ - {"stdlibs": ["libstdc++"], "tests": ["Release.Default"]} + {"stdlibs": ["libc++"], "tests": ["Release.Default"]} ] } ] }, - { "versions": ["19"], + { "versions": ["20", "19"], "tests": [ { "cxxversions": ["c++26", "c++23", "c++20"], "tests": [ - {"stdlibs": ["libstdc++"], "tests": ["Release.Default"]} + {"stdlibs": ["libc++"], "tests": ["Release.Default"]} + ] + } + ] + } + ], + "appleclang": [ + { "versions": ["latest"], + "tests": [ + { "cxxversions": ["c++23", "c++20"], + "tests": [{ "stdlibs": ["libc++"], "tests": ["Release.Default"]}] + } + ] + } + ], + "msvc": [ + { "versions": ["latest"], + "tests": [ + { "cxxversions": ["c++23"], + "tests": [ + { "stdlibs": ["stl"], + "tests": ["Debug.Default", "Release.Default", "Release.MaxSan"] + } ] } ] @@ -93,9 +118,5 @@ jobs: create-issue-when-fault: needs: [preset-test, build-and-test] - if: failure() && github.event.schedule == '30 15 * * *' - uses: ./.github/workflows/reusable-beman-create-issue-when-fault.yml - - auto-update-pre-commit: - if: github.event.schedule == '00 16 * * 0' - uses: ./.github/workflows/reusable-beman-update-pre-commit.yml + if: failure() && github.event_name == 'schedule' + uses: bemanproject/infra-workflows/.github/workflows/reusable-beman-create-issue-when-fault.yml@1.2.1 diff --git a/.github/workflows/pre-commit-check.yml b/.github/workflows/pre-commit-check.yml new file mode 100644 index 0000000..5749343 --- /dev/null +++ b/.github/workflows/pre-commit-check.yml @@ -0,0 +1,13 @@ +name: Lint Check (pre-commit) + +on: + # We have to use pull_request_target here as pull_request does not grant + # enough permission for reviewdog + pull_request_target: + push: + branches: + - main + +jobs: + pre-commit: + uses: bemanproject/infra-workflows/.github/workflows/reusable-beman-pre-commit.yml@1.2.1 diff --git a/.github/workflows/pre-commit-update.yml b/.github/workflows/pre-commit-update.yml new file mode 100644 index 0000000..9261dbf --- /dev/null +++ b/.github/workflows/pre-commit-update.yml @@ -0,0 +1,15 @@ +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +name: Weekly pre-commit autoupdate + +on: + workflow_dispatch: + schedule: + - cron: "0 16 * * 0" + +jobs: + auto-update-pre-commit: + uses: bemanproject/infra-workflows/.github/workflows/reusable-beman-update-pre-commit.yml@1.2.1 + secrets: + APP_ID: ${{ secrets.AUTO_PR_BOT_APP_ID }} + PRIVATE_KEY: ${{ secrets.AUTO_PR_BOT_PRIVATE_KEY }} diff --git a/.gitignore b/.gitignore index baadefc..0e4b215 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,11 @@ -.cache +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + /compile_commands.json +/CMakeUserPresets.json /build +**/_deps/ +**/CMakeFiles/ +/cmake/presets # ignore emacs temp files *~ @@ -8,3 +13,4 @@ # ignore vscode settings .vscode +.cache diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a4daacd..cecf800 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -14,14 +14,14 @@ repos: # This brings in a portable version of clang-format. # See also: https://github.com/ssciwr/clang-format-wheel - repo: https://github.com/pre-commit/mirrors-clang-format - rev: v21.1.6 + rev: v21.1.8 hooks: - id: clang-format types_or: [c++, c, json] # CMake linting and formatting - repo: https://github.com/BlankSpruce/gersemi - rev: 0.23.1 + rev: 0.25.2 hooks: - id: gersemi name: CMake linting diff --git a/CMakeLists.txt b/CMakeLists.txt index ffb5c17..a434572 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,9 +12,9 @@ project( # gersemi: off # Modules opt in only on compilers that support it: msvc, g++-15 and clang-20+ -if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 20) +if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 20) set(CMAKE_CXX_SCAN_FOR_MODULES 1) -elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 15) +elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 15) set(CMAKE_CXX_SCAN_FOR_MODULES 1) elseif(MSVC) set(CMAKE_CXX_SCAN_FOR_MODULES 1) diff --git a/CMakePresets.json b/CMakePresets.json index b48e93b..194a873 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -7,16 +7,30 @@ "generator": "Ninja", "binaryDir": "${sourceDir}/build/${presetName}", "cacheVariables": { + "BEMAN_USE_STD_MODULE": true, "CMAKE_CXX_EXTENSIONS": true, + "CMAKE_CXX_SCAN_FOR_MODULES": true, "CMAKE_CXX_STANDARD": "23", "CMAKE_CXX_STANDARD_REQUIRED": true, "CMAKE_EXPORT_COMPILE_COMMANDS": true, + "CMAKE_SKIP_TEST_ALL_DEPENDENCY": false, "CMAKE_PROJECT_TOP_LEVEL_INCLUDES": "infra/cmake/use-fetch-content.cmake" } }, { "name": "_debug-base", "hidden": true, + "warnings": { + "dev": true, + "deprecated": true, + "uninitialized": true, + "unusedCli": true, + "systemVars": false + }, + "errors": { + "dev": false, + "deprecated": false + }, "cacheVariables": { "CMAKE_BUILD_TYPE": "Debug", "BEMAN_BUILDSYS_SANITIZER": "MaxSan" @@ -59,7 +73,12 @@ "_debug-base" ], "cacheVariables": { - "CMAKE_TOOLCHAIN_FILE": "infra/cmake/llvm-toolchain.cmake" + "BEMAN_USE_STD_MODULE": false, + "CMAKE_TOOLCHAIN_FILE": "infra/cmake/llvm-libc++-toolchain.cmake" + }, + "environment": { + "CXX": "clang++", + "CMAKE_CXX_FLAGS": "-stdlib=libc++" } }, { @@ -70,7 +89,12 @@ "_release-base" ], "cacheVariables": { + "BEMAN_USE_STD_MODULE": false, "CMAKE_TOOLCHAIN_FILE": "infra/cmake/llvm-libc++-toolchain.cmake" + }, + "environment": { + "CXX": "clang++", + "CMAKE_CXX_FLAGS": "-stdlib=libc++" } }, { @@ -81,6 +105,8 @@ "_debug-base" ], "cacheVariables": { + "BEMAN_USE_STD_MODULE": false, + "CMAKE_CXX_SCAN_FOR_MODULES": false, "CMAKE_TOOLCHAIN_FILE": "infra/cmake/appleclang-toolchain.cmake" } }, @@ -92,6 +118,8 @@ "_release-base" ], "cacheVariables": { + "BEMAN_USE_STD_MODULE": false, + "CMAKE_CXX_SCAN_FOR_MODULES": false, "CMAKE_TOOLCHAIN_FILE": "infra/cmake/appleclang-toolchain.cmake" } }, diff --git a/include/beman/scope/beman.scope.cppm b/include/beman/scope/beman.scope.cppm index e9bfaec..39f6a91 100644 --- a/include/beman/scope/beman.scope.cppm +++ b/include/beman/scope/beman.scope.cppm @@ -3,7 +3,7 @@ // g++-15 -std=c++26 -O2 -fmodules -fmodule-only -c ${scopetop}/include/beman/scope/beman.scope.cppm module; -#include +#include export module beman.scope; diff --git a/infra/.beman_submodule b/infra/.beman_submodule index bfed167..2d3e3b4 100644 --- a/infra/.beman_submodule +++ b/infra/.beman_submodule @@ -1,3 +1,7 @@ [beman_submodule] remote=https://github.com/bemanproject/infra.git +<<<<<<< Updated upstream commit_hash=bb58b2a1cc894d58a55bf745be78f5d27029e245 +======= +commit_hash=b3545a45640abd1fedc01441ca3f220d9ac5a8e3 +>>>>>>> Stashed changes diff --git a/infra/.pre-commit-config.yaml b/infra/.pre-commit-config.yaml index e806e59..db8f459 100644 --- a/infra/.pre-commit-config.yaml +++ b/infra/.pre-commit-config.yaml @@ -19,6 +19,7 @@ repos: - id: gersemi name: CMake linting exclude: ^.*/tests/.*/data/ # Exclude test data directories +<<<<<<< Updated upstream # Python linting and formatting # config file: ruff.toml (not currently present but add if needed) @@ -30,3 +31,5 @@ repos: files: ^tools/beman-tidy/ - id: ruff-format files: ^tools/beman-tidy/ +======= +>>>>>>> Stashed changes diff --git a/infra/cmake/beman-install-library-config.cmake b/infra/cmake/beman-install-library-config.cmake index e7fd0ad..c40959d 100644 --- a/infra/cmake/beman-install-library-config.cmake +++ b/infra/cmake/beman-install-library-config.cmake @@ -84,8 +84,8 @@ function(beman_install_library name) option( ${project_prefix}_INSTALL_CONFIG_FILE_PACKAGE - "Enable building examples. Default: ${PROJECT_IS_TOP_LEVEL}. Values: { ON, OFF }." - ${PROJECT_IS_TOP_LEVEL} + "Enable creating and installing a CMake config-file package. Default: ON. Values: { ON, OFF }." + ON ) # By default, install the config package @@ -121,7 +121,7 @@ function(beman_install_library name) find_file( config_file_template NAMES "${package_name}-config.cmake.in" - PATHS "${CMAKE_CURRENT_SOURCE_DIR}" + PATHS "${PROJECT_SOURCE_DIR}/cmake" NO_DEFAULT_PATH NO_CACHE REQUIRED diff --git a/infra/cmake/use-fetch-content.cmake b/infra/cmake/use-fetch-content.cmake index 4ed4839..d78669b 100644 --- a/infra/cmake/use-fetch-content.cmake +++ b/infra/cmake/use-fetch-content.cmake @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.24) +cmake_minimum_required(VERSION 3.30) include(FetchContent) @@ -165,9 +165,8 @@ function(BemanExemplar_provideDependency method package_name) "${BemanExemplar_name}" GIT_REPOSITORY "${BemanExemplar_repo}" GIT_TAG "${BemanExemplar_tag}" - EXCLUDE_FROM_ALL + # NO! EXCLUDE_FROM_ALL ) - set(INSTALL_GTEST OFF) # Disable GoogleTest installation FetchContent_MakeAvailable("${BemanExemplar_name}") # Important! _FOUND tells CMake that `find_package` is diff --git a/makefile b/makefile index bf4e84e..6c4875a 100644 --- a/makefile +++ b/makefile @@ -32,7 +32,7 @@ else ifeq (${hostSystemName},Linux) export CXX=clang++-20 endif -.PHONY: all install coverage gclean distclean +.PHONY: all install coverage gclean distclean format all: build/compile_commands.json ln -sf $< . @@ -63,6 +63,10 @@ build/coverage: test coverage: build/coverage gcovr --merge-mode-functions separate +format: distclean + pre-commit autoupdate + pre-commit run --all + # Anything we don't know how to build will use this rule. % :: ninja -C build $(@) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index c30035b..8e89080 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -2,12 +2,18 @@ include(FetchContent) +if(APPLE AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + set(_FIND_PACKAGE_ARGS) +else() + set(_FIND_PACKAGE_ARGS FIND_PACKAGE_ARGS 3.11) +endif() + FetchContent_Declare( Catch2 GIT_REPOSITORY https://github.com/catchorg/Catch2.git GIT_TAG v3.11.0 EXCLUDE_FROM_ALL - # NO! FIND_PACKAGE_ARGS 3.11 + ${_FIND_PACKAGE_ARGS} ) FetchContent_MakeAvailable(Catch2) From 38f5da32113380b16c2ec828a69386267ea60a4b Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Tue, 27 Jan 2026 18:55:56 +0100 Subject: [PATCH 18/18] infra/tools/beman-submodule/beman-submodule update --- infra/.beman_submodule | 4 ---- infra/.pre-commit-config.yaml | 14 -------------- infra/.pre-commit-hooks.yaml | 7 ------- infra/cmake/use-fetch-content.cmake | 5 +++-- 4 files changed, 3 insertions(+), 27 deletions(-) delete mode 100644 infra/.pre-commit-hooks.yaml diff --git a/infra/.beman_submodule b/infra/.beman_submodule index 2d3e3b4..10ea6a3 100644 --- a/infra/.beman_submodule +++ b/infra/.beman_submodule @@ -1,7 +1,3 @@ [beman_submodule] remote=https://github.com/bemanproject/infra.git -<<<<<<< Updated upstream -commit_hash=bb58b2a1cc894d58a55bf745be78f5d27029e245 -======= commit_hash=b3545a45640abd1fedc01441ca3f220d9ac5a8e3 ->>>>>>> Stashed changes diff --git a/infra/.pre-commit-config.yaml b/infra/.pre-commit-config.yaml index db8f459..bc4dd84 100644 --- a/infra/.pre-commit-config.yaml +++ b/infra/.pre-commit-config.yaml @@ -19,17 +19,3 @@ repos: - id: gersemi name: CMake linting exclude: ^.*/tests/.*/data/ # Exclude test data directories -<<<<<<< Updated upstream - - # Python linting and formatting - # config file: ruff.toml (not currently present but add if needed) - # https://docs.astral.sh/ruff/configuration/ - - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.13.2 - hooks: - - id: ruff-check - files: ^tools/beman-tidy/ - - id: ruff-format - files: ^tools/beman-tidy/ -======= ->>>>>>> Stashed changes diff --git a/infra/.pre-commit-hooks.yaml b/infra/.pre-commit-hooks.yaml deleted file mode 100644 index d327587..0000000 --- a/infra/.pre-commit-hooks.yaml +++ /dev/null @@ -1,7 +0,0 @@ -- id: beman-tidy - name: "beman-tidy: bemanification your repo" - entry: ./tools/beman-tidy/beman-tidy - language: script - pass_filenames: false - always_run: true - args: [".", "--verbose"] diff --git a/infra/cmake/use-fetch-content.cmake b/infra/cmake/use-fetch-content.cmake index d78669b..4ed4839 100644 --- a/infra/cmake/use-fetch-content.cmake +++ b/infra/cmake/use-fetch-content.cmake @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.30) +cmake_minimum_required(VERSION 3.24) include(FetchContent) @@ -165,8 +165,9 @@ function(BemanExemplar_provideDependency method package_name) "${BemanExemplar_name}" GIT_REPOSITORY "${BemanExemplar_repo}" GIT_TAG "${BemanExemplar_tag}" - # NO! EXCLUDE_FROM_ALL + EXCLUDE_FROM_ALL ) + set(INSTALL_GTEST OFF) # Disable GoogleTest installation FetchContent_MakeAvailable("${BemanExemplar_name}") # Important! _FOUND tells CMake that `find_package` is