From 1f559d5c061c8cfea286810389358ecb1af25c99 Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Mon, 1 Dec 2025 18:11:46 +0100 Subject: [PATCH 1/8] Feat: modernize CMake files --- CMakeLists.txt | 56 +++++++++++++++++++++++++++++---------- include/boost/any/fwd.hpp | 3 +++ modules/boost_any.cppm | 4 ++- 3 files changed, 48 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 014cdad..57d3333 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,39 +2,67 @@ # Distributed under the Boost Software License, Version 1.0. # See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt -cmake_minimum_required( VERSION 3.5...3.31 ) -project( boost_any VERSION "${BOOST_SUPERPROJECT_VERSION}" LANGUAGES CXX ) +cmake_minimum_required(VERSION 3.21...4.2) +project(boost_any VERSION "${BOOST_SUPERPROJECT_VERSION}" LANGUAGES CXX) -if (BOOST_USE_MODULES) +if(BOOST_USE_MODULES) add_library(boost_any) - target_sources(boost_any PUBLIC - FILE_SET modules_public TYPE CXX_MODULES FILES - ${CMAKE_CURRENT_LIST_DIR}/modules/boost_any.cppm + target_sources( + boost_any + PUBLIC + FILE_SET modules_public + TYPE CXX_MODULES + FILES ${CMAKE_CURRENT_LIST_DIR}/modules/boost_any.cppm ) target_compile_features(boost_any PUBLIC cxx_std_20) target_compile_definitions(boost_any PUBLIC BOOST_USE_MODULES) - if (CMAKE_CXX_COMPILER_IMPORT_STD) + if(${CMAKE_CXX_STANDARD} IN_LIST CMAKE_CXX_COMPILER_IMPORT_STD) + set(CMAKE_CXX_MODULE_STD ON) target_compile_definitions(boost_any PRIVATE BOOST_ANY_USE_STD_MODULE) message(STATUS "Using `import std;`") else() - message(STATUS "`import std;` is not available") + message(WARNING "`import std;` is not available") endif() set(__scope PUBLIC) + + # TODO(CK): + # add_executable(module_sample modules/usage_sample.cpp) + # (module_sample PRIVATE boost_any) else() + set(CMAKE_VERIFY_INTERFACE_HEADER_SETS ON) add_library(boost_any INTERFACE) set(__scope INTERFACE) endif() -target_include_directories(boost_any ${__scope} include) -target_link_libraries( boost_any +if(CMAKE_SKIP_INSTALL_RULES OR PROJECT_IS_TOP_LEVEL) + target_sources( + boost_any + ${__scope} + FILE_SET headers_public + TYPE HEADERS + BASE_DIRS include + FILES + include/boost/any/bad_any_cast.hpp + include/boost/any/basic_any.hpp + include/boost/any/fwd.hpp + include/boost/any/unique_any.hpp + include/boost/any/detail/config.hpp + include/boost/any/detail/placeholder.hpp + ) +else() + target_include_directories(boost_any ${__scope} include) +endif() + +target_link_libraries( + boost_any ${__scope} - Boost::config - Boost::throw_exception - Boost::type_index + Boost::config + Boost::throw_exception + Boost::type_index ) -add_library( Boost::any ALIAS boost_any ) +add_library(Boost::any ALIAS boost_any) if(BUILD_TESTING) add_subdirectory(test) diff --git a/include/boost/any/fwd.hpp b/include/boost/any/fwd.hpp index 0263a61..d41a735 100644 --- a/include/boost/any/fwd.hpp +++ b/include/boost/any/fwd.hpp @@ -17,6 +17,9 @@ #ifdef BOOST_HAS_PRAGMA_ONCE # pragma once #endif + +#include + #endif // #ifndef BOOST_ANY_INTERFACE_UNIT /// \file boost/any/fwd.hpp diff --git a/modules/boost_any.cppm b/modules/boost_any.cppm index 9eedd5b..da1f2c9 100644 --- a/modules/boost_any.cppm +++ b/modules/boost_any.cppm @@ -11,7 +11,9 @@ module; #include #include #include -#include + +// FIXME(CK): #include +import boost.type_index; #ifdef BOOST_ANY_USE_STD_MODULE import std; From b6644ff43deb92525c9969c85b7a964d0171873e Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Mon, 1 Dec 2025 21:49:44 +0100 Subject: [PATCH 2/8] Add any_module_example to build --- CMakeLists.txt | 11 +++++------ modules/usage_sample.cpp | 40 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 57d3333..4bfd66c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,9 +26,8 @@ if(BOOST_USE_MODULES) endif() set(__scope PUBLIC) - # TODO(CK): - # add_executable(module_sample modules/usage_sample.cpp) - # (module_sample PRIVATE boost_any) + add_executable(module_sample modules/usage_sample.cpp) + target_link_libraries(module_sample PRIVATE boost_any) else() set(CMAKE_VERIFY_INTERFACE_HEADER_SETS ON) add_library(boost_any INTERFACE) @@ -57,9 +56,9 @@ endif() target_link_libraries( boost_any ${__scope} - Boost::config - Boost::throw_exception - Boost::type_index + Boost::config + Boost::throw_exception + Boost::type_index ) add_library(Boost::any ALIAS boost_any) diff --git a/modules/usage_sample.cpp b/modules/usage_sample.cpp index ba72469..e19cde6 100644 --- a/modules/usage_sample.cpp +++ b/modules/usage_sample.cpp @@ -7,10 +7,46 @@ // clang++ -std=c++20 -fmodule-file=type_index.pcm type_index.pcm usage_sample.cpp //[any_module_example + +#ifdef BOOST_ANY_USE_STD_MODULE +import std; +#else +# include +# include +# include +#endif + import boost.any; -int main() { - boost::any a = 42; +namespace { + + template auto any_to_string(const boost::any& a) -> std::string { + std::ostringstream oss; + + auto try_cast = [&](auto* dummy) -> bool { + using T = std::decay_t; + if (a.type() == typeid(T)) { + oss << boost::any_cast(a); + return true; + } + return false; + }; + + // Expand over Ts... + bool const success = (try_cast((Ts*)nullptr) || ...); + + if (!success) { + oss << ""; + } + return oss.str(); + } + +} // namespace + +// Usage: +auto main() -> int { + boost::any const a = 42; + std::println(stdout, "{}", any_to_string(a)); } //] From 2d30a8f953e013b2b09910f9e0d2b93380252762 Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Thu, 4 Dec 2025 06:49:21 +0100 Subject: [PATCH 3/8] Respect review comments --- CMakeLists.txt | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4bfd66c..17301fd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,8 +3,13 @@ # See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt cmake_minimum_required(VERSION 3.21...4.2) + project(boost_any VERSION "${BOOST_SUPERPROJECT_VERSION}" LANGUAGES CXX) +if(PROJECT_IS_TOP_LEVEL) + find_package(Boost 1.90.0 CONFIG) +endif() + if(BOOST_USE_MODULES) add_library(boost_any) target_sources( @@ -26,15 +31,18 @@ if(BOOST_USE_MODULES) endif() set(__scope PUBLIC) - add_executable(module_sample modules/usage_sample.cpp) - target_link_libraries(module_sample PRIVATE boost_any) + # TODO(CK): unresolved with shared libs? + # add_executable(module_sample modules/usage_sample.cpp) + # target_link_libraries(module_sample PRIVATE boost_any) else() - set(CMAKE_VERIFY_INTERFACE_HEADER_SETS ON) + set(CMAKE_VERIFY_INTERFACE_HEADER_SETS ${PROJECT_IS_TOP_LEVEL}) add_library(boost_any INTERFACE) set(__scope INTERFACE) endif() -if(CMAKE_SKIP_INSTALL_RULES OR PROJECT_IS_TOP_LEVEL) +if(CMAKE_VERSION VERSION_LESS 3.23) + target_include_directories(boost_any ${__scope} include) +else() target_sources( boost_any ${__scope} @@ -49,17 +57,24 @@ if(CMAKE_SKIP_INSTALL_RULES OR PROJECT_IS_TOP_LEVEL) include/boost/any/detail/config.hpp include/boost/any/detail/placeholder.hpp ) -else() - target_include_directories(boost_any ${__scope} include) + target_compile_features(boost_any ${__scope} cxx_std_17) endif() -target_link_libraries( - boost_any - ${__scope} - Boost::config - Boost::throw_exception - Boost::type_index -) +if(PROJECT_IS_TOP_LEVEL) + target_link_libraries( + boost_any + ${__scope} + Boost::headers + ) +else() + target_link_libraries( + boost_any + ${__scope} + Boost::config + Boost::throw_exception + Boost::type_index + ) +endif() add_library(Boost::any ALIAS boost_any) From 6b0c4457a43eafd526f162b1a6e0655387557443 Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Thu, 4 Dec 2025 08:54:03 +0100 Subject: [PATCH 4/8] Feat: Use FILE_SET HEADERS if possible --- CMakeLists.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 17301fd..3059b44 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,7 +57,9 @@ else() include/boost/any/detail/config.hpp include/boost/any/detail/placeholder.hpp ) - target_compile_features(boost_any ${__scope} cxx_std_17) + if(NOT BOOST_USE_MODULES) + target_compile_features(boost_any ${__scope} cxx_std_17) + endif() endif() if(PROJECT_IS_TOP_LEVEL) From 2a0331fc719bb730c699b58ec86b2a8b8563e794 Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Mon, 8 Dec 2025 08:28:16 +0100 Subject: [PATCH 5/8] Cleanup cmake code --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3059b44..e4c5fae 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,7 +17,7 @@ if(BOOST_USE_MODULES) PUBLIC FILE_SET modules_public TYPE CXX_MODULES - FILES ${CMAKE_CURRENT_LIST_DIR}/modules/boost_any.cppm + FILES modules/boost_any.cppm ) target_compile_features(boost_any PUBLIC cxx_std_20) From 810868657bdab5cdf513a29b94d0df9a003249a2 Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Fri, 26 Dec 2025 19:40:41 +0100 Subject: [PATCH 6/8] Set CMAKE_CXX_STANDARD to 20 if not defined --- CMakeLists.txt | 3 +++ modules/boost_any.cppm | 1 - 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e4c5fae..74fa472 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,9 @@ if(PROJECT_IS_TOP_LEVEL) endif() if(BOOST_USE_MODULES) + if(NOT DEFINED CMAKE_CXX_STANDARD) + set(CMAKE_CXX_STANDARD 20) + endif() add_library(boost_any) target_sources( boost_any diff --git a/modules/boost_any.cppm b/modules/boost_any.cppm index da1f2c9..652343e 100644 --- a/modules/boost_any.cppm +++ b/modules/boost_any.cppm @@ -12,7 +12,6 @@ module; #include #include -// FIXME(CK): #include import boost.type_index; #ifdef BOOST_ANY_USE_STD_MODULE From 58d8f7d47c997d810412780fea06e6963d58a793 Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Sat, 27 Dec 2025 20:18:23 +0100 Subject: [PATCH 7/8] Add missing dependency lib --- CMakeLists.txt | 4 ---- modules/usage_sample.cpp | 6 +++++- test/cmake_subdir_test/CMakeLists.txt | 10 +++++----- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 74fa472..2a26570 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,10 +33,6 @@ if(BOOST_USE_MODULES) message(WARNING "`import std;` is not available") endif() set(__scope PUBLIC) - - # TODO(CK): unresolved with shared libs? - # add_executable(module_sample modules/usage_sample.cpp) - # target_link_libraries(module_sample PRIVATE boost_any) else() set(CMAKE_VERIFY_INTERFACE_HEADER_SETS ${PROJECT_IS_TOP_LEVEL}) add_library(boost_any INTERFACE) diff --git a/modules/usage_sample.cpp b/modules/usage_sample.cpp index e19cde6..6c88273 100644 --- a/modules/usage_sample.cpp +++ b/modules/usage_sample.cpp @@ -11,9 +11,9 @@ #ifdef BOOST_ANY_USE_STD_MODULE import std; #else +# include # include # include -# include #endif import boost.any; @@ -46,7 +46,11 @@ namespace { // Usage: auto main() -> int { boost::any const a = 42; +#ifdef BOOST_ANY_USE_STD_MODULE std::println(stdout, "{}", any_to_string(a)); +#else + std::cout << any_to_string(a) << '\n'; +#endif } //] diff --git a/test/cmake_subdir_test/CMakeLists.txt b/test/cmake_subdir_test/CMakeLists.txt index a934b81..4ddb910 100644 --- a/test/cmake_subdir_test/CMakeLists.txt +++ b/test/cmake_subdir_test/CMakeLists.txt @@ -2,7 +2,7 @@ # 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 -cmake_minimum_required(VERSION 3.5...4.0) +cmake_minimum_required(VERSION 3.21...4.2) project(any_subdir_test LANGUAGES CXX) @@ -24,8 +24,8 @@ add_subdirectory(../../ boostorg/any) enable_testing() -if (BOOST_USE_MODULES) - add_executable(boost_any_module_usage ../modules/usage_sample.cpp) +if(BOOST_USE_MODULES) + add_executable(boost_any_module_usage ../../modules/usage_sample.cpp) target_link_libraries(boost_any_module_usage PRIVATE Boost::any) add_test(NAME boost_any_module_usage COMMAND boost_any_module_usage) endif() @@ -47,10 +47,10 @@ list(APPEND RUN_TESTS_SOURCES # any_test.cpp # Ambiguous with modules, because all the anys now available ) -foreach (testsourcefile ${RUN_TESTS_SOURCES}) +foreach(testsourcefile ${RUN_TESTS_SOURCES}) get_filename_component(testname ${testsourcefile} NAME_WLE) add_executable(${PROJECT_NAME}_${testname} ../${testsourcefile}) - target_link_libraries(${PROJECT_NAME}_${testname} Boost::any) + target_link_libraries(${PROJECT_NAME}_${testname} Boost::any Boost::core) add_test(NAME ${PROJECT_NAME}_${testname} COMMAND ${PROJECT_NAME}_${testname}) endforeach() From bba76dee34908c6d2be378a790d1e4800903ce4a Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Sat, 27 Dec 2025 20:29:07 +0100 Subject: [PATCH 8/8] Fix typos --- .github/workflows/ci.yml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 12efe5e..3d6b80e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -85,7 +85,7 @@ jobs: cd .. rm -rf __build - - name: Run modules tests wihtout 'import std;' + - name: Run modules tests without 'import std;' if: ${{matrix.toolset == 'clang-19'}} run: | cd ../boost-root/libs/any @@ -218,7 +218,7 @@ jobs: cd .. rm -rf build_module - - name: Run modules tests wihtout 'import std;' + - name: Run modules tests without 'import std;' if: ${{matrix.toolset == 'msvc-14.3'}} shell: cmd run: | @@ -243,8 +243,8 @@ jobs: needs: posix runs-on: ubuntu-latest steps: - - name: Coveralls Finished - uses: coverallsapp/github-action@master - with: - github-token: ${{ secrets.github_token }} - parallel-finished: true + - name: Coveralls Finished + uses: coverallsapp/github-action@master + with: + github-token: ${{ secrets.github_token }} + parallel-finished: true