Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows using a pure modular build. #570

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ option(BOOST_UT_BUILD_EXAMPLES "Build the examples" ${PROJECT_IS_TOP_LEVEL})
option(BOOST_UT_BUILD_TESTS "Build the tests" ${PROJECT_IS_TOP_LEVEL})
option(BOOST_UT_USE_WARNINGS_AS_ERORS "Build the tests" ${PROJECT_IS_TOP_LEVEL})
option(BOOST_UT_DISABLE_MODULE "Disable ut module" OFF)
option(BOOST_UT_ENBLE_MODULE "Enable ut module" OFF)
option(BOOST_UT_DISABLE_STD_INCLUDES "Disble including Standar headers" OFF)
option(BOOST_UT_ALLOW_CPM_USE "Do not reach out across network for CPM" ON)

if(NOT CMAKE_SKIP_INSTALL_RULES)
Expand Down Expand Up @@ -80,9 +82,20 @@ if(BOOST_UT_ENABLE_SANITIZERS)
endif()

if(BOOST_UT_DISABLE_MODULE)
if(BOOST_UT_ENBLE_MODULE)
MESSAGE(FATAL_ERROR "Modules both enabled and disabled")
endif()
target_compile_definitions(ut INTERFACE BOOST_UT_DISABLE_MODULE)
endif()

if(BOOST_UT_ENBLE_MODULE)
target_compile_definitions(ut INTERFACE BOOST_UT_USE_MODULE)
endif()

if(BOOST_UT_DISABLE_STD_INCLUDES)
target_compile_definitions(ut INTERFACE BOOST_UT_DISABLE_STD_INCLUDES)
endif()

if(NOT CMAKE_SKIP_INSTALL_RULES)
# Create target Boost::ut and install target
packageProject(
Expand Down
32 changes: 26 additions & 6 deletions include/boost/ut.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//

#if not defined(BOOST_UT_USE_MODULE)
#if defined(__cpp_modules) && !defined(BOOST_UT_DISABLE_MODULE)
export module boost.ut;
export import std;
#define BOOST_UT_USE_MODULE
#endif
#endif

#if defined(BOOST_UT_USE_MODULE)
module;
#define BOOST_UT_EXPORT export
#else
#pragma once
Expand Down Expand Up @@ -53,6 +59,7 @@ export import std;
#else
#define BOOST_UT_VERSION 1'1'9

#if not defined(BOOST_UT_DISABLE_STD_INCLUDES)
#if defined(__has_builtin) and defined(__GNUC__) and (__GNUC__ < 10) and \
not defined(__clang__)
#undef __has_builtin
Expand Down Expand Up @@ -83,10 +90,6 @@ export import std;
#include <utility>
#include <variant>
#include <vector>
#if __has_include(<unistd.h>) and __has_include(<sys/wait.h>)
#include <sys/wait.h>
#include <unistd.h>
#endif
#if defined(__cpp_exceptions)
#include <exception>
#endif
Expand All @@ -97,6 +100,17 @@ export import std;
#if __has_include(<source_location>)
#include <source_location>
#endif
#endif // BOOST_UT_DISABLE_STD_INCLUDES

#if __has_include(<unistd.h>) and __has_include(<sys/wait.h>)
#include <sys/wait.h>
#include <unistd.h>
#endif

#if defined(BOOST_UT_USE_MODULE)
export module boost.ut;
import std;
#endif

struct _unique_name_for_auto_detect_prefix_and_suffix_lenght_0123456789_struct {
};
Expand Down Expand Up @@ -302,17 +316,23 @@ inline constexpr const std::string_view need_name =
"_unique_name_for_auto_detect_prefix_and_suffix_lenght_0123456789_struct";
#endif
inline constexpr const std::size_t need_length = need_name.length();
#if not defined(BOOST_UT_USE_MODULE)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note this is not great. I think to solve this properly the selection of which declarations to export and not to export needs a rework. Either exporting in a smaller scope than the entire boost::ut namespace or using using namespace::declaration at the end of the header.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would +1 the explicit symbol export, as it also causes troubles when building with MSVC, see here for an example.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy with that too ☝️

static_assert(need_length <= raw_length,
"Auto find prefix and suffix lenght broken error 1");
#endif
inline constexpr const std::size_t prefix_length =
raw_type_name.find(need_name);
#if not defined(BOOST_UT_USE_MODULE)
static_assert(prefix_length != std::string_view::npos,
"Auto find prefix and suffix lenght broken error 2");
static_assert(prefix_length <= raw_length,
"Auto find prefix and suffix lenght broken error 3");
#endif
inline constexpr const std::size_t tail_lenght = raw_length - prefix_length;
#if not defined(BOOST_UT_USE_MODULE)
static_assert(need_length <= tail_lenght,
"Auto find prefix and suffix lenght broken error 4");
#endif
inline constexpr const std::size_t suffix_length = tail_lenght - need_length;

} // namespace detail
Expand Down