From de047e1250e973953242234e1f6145e840a2626a Mon Sep 17 00:00:00 2001 From: Nigel Stewart Date: Mon, 8 Sep 2025 18:55:19 +1000 Subject: [PATCH 1/2] cmake: Add standalone build support for building and running unit tests --- CMakeLists.txt | 34 +++++++++++++++++++++++++++++++--- test/CMakeLists.txt | 5 +++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c720226..85908b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,11 +1,31 @@ # Generated by `boostdep --cmake sort` # Copyright 2020 Peter Dimov +# Copyright 2025 Nigel Stewart # Distributed under the Boost Software License, Version 1.0. # https://www.boost.org/LICENSE_1_0.txt -cmake_minimum_required(VERSION 3.5...3.16) +message(STATUS "Using cmake version ${CMAKE_VERSION}") -project(boost_sort VERSION "${BOOST_SUPERPROJECT_VERSION}" LANGUAGES CXX) +cmake_minimum_required(VERSION 3.10...3.16) + +if(BOOST_SUPERPROJECT_VERSION) + project(boost_sort VERSION "${BOOST_SUPERPROJECT_VERSION}" LANGUAGES CXX) +else() + project(boost_sort LANGUAGES CXX) +endif() + +# Test coverage in stand-alone mode requires boost dependencies + +if(NOT BOOST_SUPERPROJECT_VERSION) + if(BUILD_TESTING) + set(Boost_DEBUG ON) + find_package(Boost 1.85 REQUIRED COMPONENTS core range included_test_exec_monitor) + if(NOT Boost_FOUND) + message(STATUS "Boost 1.85 (or later) is required") + return() + endif() + endif() +endif() add_library(boost_sort INTERFACE) add_library(Boost::sort ALIAS boost_sort) @@ -21,8 +41,16 @@ target_link_libraries(boost_sort Boost::type_traits ) +# Testing + if(BUILD_TESTING AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/test/CMakeLists.txt") - add_subdirectory(test) + if(NOT BOOST_SUPERPROJECT_VERSION) + include(CTest) + enable_testing() + add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure -C $) + add_dependencies(check tests) + endif() + add_subdirectory(test) endif() diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index ad6895a..c4ce331 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -26,6 +26,11 @@ function(boost_sort_add_test name source) set(pname "${PREFIX}${name}") add_executable(${pname} ${source}) target_link_libraries(${pname} ${LINK_LIBRARIES}) + + # Follow the Boost convention: don't build test targets by default, + # and only when explicitly requested by building target tests + set_target_properties(${pname} PROPERTIES EXCLUDE_FROM_ALL ON) + add_test(NAME ${pname} COMMAND ${pname}) add_dependencies(tests ${pname}) endfunction() From 08ee0e89f3f791fd25e2cc01b271d1e4fb21d17e Mon Sep 17 00:00:00 2001 From: Nigel Stewart Date: Tue, 9 Sep 2025 07:36:57 +1000 Subject: [PATCH 2/2] cmake: Standalone build support refinement, review feedback --- CMakeLists.txt | 18 ++++++------------ test/CMakeLists.txt | 5 ----- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 85908b2..dbc4f15 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,16 +15,9 @@ else() endif() # Test coverage in stand-alone mode requires boost dependencies - -if(NOT BOOST_SUPERPROJECT_VERSION) - if(BUILD_TESTING) - set(Boost_DEBUG ON) - find_package(Boost 1.85 REQUIRED COMPONENTS core range included_test_exec_monitor) - if(NOT Boost_FOUND) - message(STATUS "Boost 1.85 (or later) is required") - return() - endif() - endif() +if(BUILD_TESTING AND NOT BOOST_SUPERPROJECT_VERSION) + set(Boost_DEBUG ON) + find_package(Boost 1.85 REQUIRED COMPONENTS core range included_test_exec_monitor) endif() add_library(boost_sort INTERFACE) @@ -42,7 +35,6 @@ target_link_libraries(boost_sort ) # Testing - if(BUILD_TESTING AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/test/CMakeLists.txt") if(NOT BOOST_SUPERPROJECT_VERSION) @@ -52,5 +44,7 @@ if(BUILD_TESTING AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/test/CMakeLists.txt") add_dependencies(check tests) endif() - 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() diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index c4ce331..ad6895a 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -26,11 +26,6 @@ function(boost_sort_add_test name source) set(pname "${PREFIX}${name}") add_executable(${pname} ${source}) target_link_libraries(${pname} ${LINK_LIBRARIES}) - - # Follow the Boost convention: don't build test targets by default, - # and only when explicitly requested by building target tests - set_target_properties(${pname} PROPERTIES EXCLUDE_FROM_ALL ON) - add_test(NAME ${pname} COMMAND ${pname}) add_dependencies(tests ${pname}) endfunction()