Skip to content

Commit

Permalink
Cleaning up cmake
Browse files Browse the repository at this point in the history
- work to improve cmake presets
- enabled analyze and asan under VC++
- cleaned out resulting warnings
  • Loading branch information
sean-parent committed Dec 12, 2024
1 parent 5232aca commit e70b8e7
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 58 deletions.
11 changes: 10 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ cmake_minimum_required(VERSION 3.23)

project(stlab VERSION 1.7.1 LANGUAGES CXX)

# Set the default C++ language version
set(CMAKE_CXX_STANDARD 20 CACHE STRING "The C++ standard to use")
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# Set the default build type to Release
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build" FORCE)
endif()

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
include(CMakeDependentOption)
include(StlabUtil)
Expand All @@ -13,7 +23,6 @@ find_package(Qt5 QUIET COMPONENTS Core)
find_package(Qt6 QUIET COMPONENTS Core)
find_package(Threads)


cmake_dependent_option(stlab.coverage
"Enable binary instrumentation to collect test coverage information in the DEBUG configuration"
OFF PROJECT_IS_TOP_LEVEL OFF)
Expand Down
39 changes: 14 additions & 25 deletions CMakePresets.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"version": 3,
"version": 10,
"cmakeMinimumRequired": {
"major": 3,
"minor": 12,
"minor": 28,
"patch": 0
},
"configurePresets": [
{
"name": "hyde-build-docs",
"description": "Build documentation",
"hidden": false,
"binaryDir": "${sourceDir}/../build/hyde",
"binaryDir": "${sourceDir}/build/hyde",
"cacheVariables": {
"CMAKE_CXX_STANDARD": "20",
"BUILD_TESTING": "OFF",
Expand All @@ -30,7 +30,6 @@
"cacheVariables": {
"CMAKE_CXX_STANDARD": "20",
"CMAKE_BUILD_TYPE": "DEBUG",
"BUILD_TESTING": "ON",
"CMAKE_CXX_FLAGS": "-fsanitize=thread -fsanitize=undefined",
"CMAKE_LINKER_FLAGS": "-fsanitize=thread -fsanitize=undefined"
}
Expand All @@ -40,62 +39,53 @@
"description": "",
"hidden": false,
"generator": "Xcode",
"binaryDir": "${sourceDir}/../build/xcode-cpp20-debug",
"binaryDir": "${sourceDir}/build/xcode-cpp20-debug",
"cacheVariables": {
"CMAKE_CXX_STANDARD": "20",
"CMAKE_BUILD_TYPE": "DEBUG",
"BUILD_TESTING": "ON"
"CMAKE_BUILD_TYPE": "DEBUG"
}
},
{
"name": "ninja-cpp17-debug",
"description": "Ninja Debug Build",
"hidden": false,
"generator": "Ninja",
"binaryDir": "${sourceDir}/../build/ninja-cpp17-debug",
"binaryDir": "${sourceDir}/build/ninja-cpp17-debug",
"cacheVariables": {
"CMAKE_CXX_STANDARD": "17",
"CMAKE_BUILD_TYPE": "DEBUG",
"BUILD_TESTING": "ON",
"CMAKE_VERBOSE_MAKEFILE": "ON"
"CMAKE_BUILD_TYPE": "DEBUG"
}
},
{
"name": "ninja-cpp20-debug-address",
"description": "Ninja Debug Build",
"name": "debug",
"description": "Debug build",
"hidden": false,
"generator": "Ninja",
"binaryDir": "${sourceDir}/../build/ninja-cpp20-debug-address",
"binaryDir": "${sourceDir}/build/debug",
"cacheVariables": {
"CMAKE_CXX_STANDARD": "20",
"CMAKE_BUILD_TYPE": "DEBUG",
"BUILD_TESTING": "ON",
"CMAKE_CXX_FLAGS": "-fsanitize=address",
"CMAKE_LINKER_FLAGS": "-fsanitize=address",
"CMAKE_VERBOSE_MAKEFILE": "ON"
"STLAB_SANITIZER": "address"
}
},
{
"name": "clang-tidy-fix",
"hidden": false,
"generator": "Ninja",
"binaryDir": "${sourceDir}/../build/clang-tidy-fix",
"binaryDir": "${sourceDir}/build/clang-tidy-fix",
"cacheVariables": {
"CMAKE_CXX_STANDARD": "17",
"CMAKE_BUILD_TYPE": "DEBUG",
"BUILD_TESTING": "ON",
"CMAKE_CXX_CLANG_TIDY": "clang-tidy;--fix"
}
},
{
"name": "clang-tidy-nofix",
"hidden": false,
"generator": "Ninja",
"binaryDir": "${sourceDir}/../build/clang-tidy-nofix",
"binaryDir": "${sourceDir}/build/clang-tidy-nofix",
"cacheVariables": {
"CMAKE_CXX_STANDARD": "17",
"CMAKE_BUILD_TYPE": "DEBUG",
"BUILD_TESTING": "ON",
"CMAKE_CXX_CLANG_TIDY": "clang-tidy"
}
},
Expand All @@ -107,8 +97,7 @@
"binaryDir": "${sourceDir}/build/win-ninja-cpp20-debug-thread-undefined",
"cacheVariables": {
"CMAKE_CXX_STANDARD": "20",
"CMAKE_BUILD_TYPE": "DEBUG",
"BUILD_TESTING": "ON"
"CMAKE_BUILD_TYPE": "DEBUG"
}
}
],
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ for an introduction to this tool.

### Configure

Run CMake in the root directory of this project, setting `../BUILD` as your build directory. The
Run CMake in the root directory of this project, setting `./build` as your build directory. The
basis of your command will be

```
Expand All @@ -58,10 +58,10 @@ cmake -S . -B ../BUILD -DCMAKE_BUILD_TYPE=# SEE BELOW

but there are other options you may need to append in order to be successful. Among them:

- `-DCMAKE_BUILD_TYPE=`[`Release`|`Debug`] to build the given configuration (required unless you're using Visual Studio or another multi-config generator).
- `-DCMAKE_CXX_STANDARD=`[`17`|`20`|`23`] to build with compliance to the given C++ standard.
- `-DBUILD_TESTING=OFF` if you only intend to build, but not test, this library.
- `-DBoost_USE_STATIC_LIBS=TRUE` if you will be testing on Windows.
- `-DCMAKE_BUILD_TYPE=`[**`Release`**|`Debug`] to build the given configuration (required unless you're using Visual Studio or another multi-config generator).
- `-DCMAKE_CXX_STANDARD=`[`17`|**`20`**|`23`] to build with compliance to the given C++ standard.
- `-DBUILD_TESTING=`[`ON`, `OFF`] turn off if you intend to build, but not test, this library.
- `-DSTLAB_TASK_SYSTEM=`[`portable`, `libdispatch`, `emscripten`, `windows`] to select the task system to use. Default is platform dependent.

We also suggest the installation of [Ninja](https://ninja-build.org/) and its use by adding
`-GNinja` to your cmake command line… but ninja is not required.
Expand Down
9 changes: 9 additions & 0 deletions cmake/stlab/development.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@ include(stlab/development/AppleClang)
include(stlab/development/Clang)
include(stlab/development/GNU)
include(stlab/development/MSVC)

if(STLAB_SANITIZER STREQUAL "address")
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
add_compile_options(/fsanitize=address)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
add_compile_options(-fsanitize=address -fno-omit-frame-pointer)
add_link_options(-fsanitize=address)
endif()
endif()
47 changes: 34 additions & 13 deletions cmake/stlab/development/MSVC.cmake
Original file line number Diff line number Diff line change
@@ -1,17 +1,38 @@
set( stlab_MSVC_base_flags /W3 /WX /D_SILENCE_ALL_CXX17_DEPRECATION_WARNINGS /bigobj /Zc:__cplusplus)
set( stlab_MSVC_debug_flags )
set( stlab_MSVC_coverage_flags )
set( stlab_MSVC_release_flags )
set(stlab_MSVC_base_flags
/permissive- # Strict standards conformance
/W4 # Warning level 4
/WX # Treat warnings as errors
/bigobj # Increase object file section limit
# /D_SILENCE_ALL_CXX17_DEPRECATION_WARNINGS
/Zc:__cplusplus # Correct __cplusplus macro
/Zc:preprocessor # Standards-conforming preprocessor
/Zc:inline # Remove unreferenced functions
/Zc:externConstexpr # Enable external constexpr
/Zc:throwingNew # Assume operator new throws
/Zc:hiddenFriend # Conform to hidden friend lookup rules
/Zc:referenceBinding # Enforce reference binding rules
/Zc:rvalueCast # Enforce type conversion rules
/Zc:strictStrings # Disable string literal type conversion
/Zc:templateScope # Fix template parameter scope
/Zc:ternary # Enforce conditional operator rules
/volatile:iso # Use ISO-compliant volatile
)
set(stlab_MSVC_debug_flags
/analyze:external- # Static analysis but filter external headers (boost test)
)
set(stlab_MSVC_coverage_flags)
set(stlab_MSVC_release_flags)

string(CONCAT generator
"${stlab_MSVC_base_flags};"
"$<$<OR:$<CONFIG:DEBUG>,"
"$<CONFIG:RELWITHDEBINFO>>:${stlab_MSVC_debug_flags};>"
"$<$<OR:$<CONFIG:RELEASE>,"
"$<CONFIG:RELWITHDEBINFO>,"
"$<CONFIG:MINSIZEREL>>:${stlab_MSVC_release_flags};>"
"$<$<AND:$<CONFIG:DEBUG>,"
"$<BOOL:${stlab.coverage}>>:${stlab_MSVC_debug_flags};>")
"${stlab_MSVC_base_flags};"
"$<$<OR:$<CONFIG:DEBUG>,"
"$<CONFIG:RELWITHDEBINFO>>:${stlab_MSVC_debug_flags};>"
"$<$<OR:$<CONFIG:RELEASE>,"
"$<CONFIG:RELWITHDEBINFO>,"
"$<CONFIG:MINSIZEREL>>:${stlab_MSVC_release_flags};>"
"$<$<AND:$<CONFIG:DEBUG>,"
"$<BOOL:${stlab.coverage}>>:${stlab_MSVC_debug_flags};>")


target_compile_options(development INTERFACE
$<$<CXX_COMPILER_ID:MSVC>:${generator}>)
$<$<CXX_COMPILER_ID:MSVC>:${generator}>)
3 changes: 3 additions & 0 deletions include/stlab/concurrency/system_timer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ class system_timer {

auto file_time = duration_to_FILETIME(duration);

#pragma warning(push)
#pragma warning(disable : 6553) // bad annotation on SetThreadpoolTimer
SetThreadpoolTimer(timer, &file_time, 0, 0);
#pragma warning(pop)
}

private:
Expand Down
4 changes: 2 additions & 2 deletions include/stlab/concurrency/tuple_algorithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ struct void_i_impl<L, L> {
*/
template <typename T, typename Op>
auto tuple_find(const T& t, Op op) -> std::size_t {
if (std::tuple_size<T>::value == 0) return 1;
return detail::tuple_find_impl<0, std::tuple_size<T>::value, T, Op>::find(t, op);
if constexpr (std::tuple_size<T>::value == 0) return 1;
else return detail::tuple_find_impl<0, std::tuple_size<T>::value, T, Op>::find(t, op);
}

/*
Expand Down
20 changes: 19 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ include(stlab/development)
set(BUILD_SHARED_LIBS OFF)
set(Boost_USE_STATIC_LIBS ON)

# Set global sanitizer flags before any targets are created
if(STLAB_SANITIZER STREQUAL "address")
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
add_compile_options(/fsanitize=address)
add_link_options(/fsanitize=address)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
add_compile_options(-fsanitize=address -fno-omit-frame-pointer)
add_link_options(-fsanitize=address)
endif()
endif()

CPMAddPackage(
NAME Boost
VERSION 1.86.0
Expand All @@ -15,6 +26,12 @@ CPMAddPackage(
OPTIONS "BOOST_ENABLE_CMAKE ON" "BOOST_INCLUDE_LIBRARIES test" # Note the escapes! container\\\;asio
)

# Mark the boost header files as external for MSVC
if (MSVC)
get_target_property(Boost_INCLUDE_DIR Boost::unit_test_framework INTERFACE_INCLUDE_DIRECTORIES)
add_compile_options(/external:I${Boost_INCLUDE_DIR} /external:W0)
endif()

add_executable(stlab.test.channel
channel_functor_tests.cpp
channel_merge_round_robin_tests.cpp
Expand Down Expand Up @@ -59,8 +76,9 @@ add_executable(stlab.test.executor
executor_test.cpp
main.cpp)

target_compile_definitions(stlab.test.executor PRIVATE STLAB_UNIT_TEST)


target_compile_definitions(stlab.test.executor PRIVATE STLAB_UNIT_TEST)
target_link_libraries(stlab.test.executor PUBLIC stlab::testing)

add_test(
Expand Down
6 changes: 4 additions & 2 deletions test/channel_process_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ struct process_with_set_error {
};
} // namespace

bool always_true{true}; // used to avoid unused variable warning

BOOST_AUTO_TEST_CASE(int_channel_process_set_error_is_called_on_upstream_error) {
BOOST_TEST_MESSAGE("int channel process set_error is called on upstream error");

Expand All @@ -344,7 +346,7 @@ BOOST_AUTO_TEST_CASE(int_channel_process_set_error_is_called_on_upstream_error)

auto result = receive |
[](auto v) {
throw std::runtime_error{""};
if (always_true) throw std::runtime_error{""};
return v;
} |
process_with_set_error{check} | [](int) {};
Expand Down Expand Up @@ -386,7 +388,7 @@ BOOST_AUTO_TEST_CASE(int_channel_process_close_is_called_on_upstream_error) {

auto result = receive |
[](auto v) {
throw std::runtime_error{""};
if (always_true) throw std::runtime_error{""};
return v;
} |
process_with_close{check} | [](int) {};
Expand Down
2 changes: 1 addition & 1 deletion test/forest_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ BOOST_AUTO_TEST_CASE(assignment) {
}

/* self-move assignment */ {
auto f{big_test_forest()};
f = big_test_forest();
auto* pf{&f}; // We use a pointer here to get around a clang error when moving to self.
auto f_size{f.size()};
f = std::move(*pf);
Expand Down
4 changes: 3 additions & 1 deletion test/future_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,10 +555,12 @@ BOOST_AUTO_TEST_CASE(future_wait_moveonly_value_and_timeout) {
BOOST_REQUIRE_EQUAL(42, r.get_try()->member());
}

bool always_true{true}; // used to avoid unused variable warning

BOOST_AUTO_TEST_CASE(future_wait_moveonly_value_error_case_and_timeout) {
BOOST_TEST_MESSAGE("future wait with moveonly value and timeout set");
auto answer = [] {
throw test_exception("failure");
if (always_true) throw test_exception("failure");
return stlab::move_only(42);
};

Expand Down
14 changes: 7 additions & 7 deletions test/future_when_all_arguments_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ BOOST_AUTO_TEST_CASE(future_when_all_args_int_with_two_ready_element) {

BOOST_AUTO_TEST_CASE(future_when_all_args) {
auto main_thread_id = std::this_thread::get_id();
auto sut = when_all(
auto r = when_all(
make_executor<1>(), [] { return std::this_thread::get_id(); },
make_ready_future(stlab::immediate_executor));

wait_until_future_completed(copy(sut));
wait_until_future_completed(copy(r));

BOOST_REQUIRE(main_thread_id != *sut.get_try());
BOOST_REQUIRE(main_thread_id != *r.get_try());
BOOST_REQUIRE_LE(1, custom_scheduler<1>::usage_counter());
}

Expand All @@ -133,7 +133,7 @@ BOOST_AUTO_TEST_CASE(future_when_all_Arguments_with_mutable_task) {
mutable_int func1;
mutable_int func2;

auto sut = when_all(
auto r = when_all(
stlab::default_executor, [](auto f1, auto f2) { return f1() + f2(); },
async(stlab::default_executor,
[func = func1]() mutable {
Expand All @@ -145,7 +145,7 @@ BOOST_AUTO_TEST_CASE(future_when_all_Arguments_with_mutable_task) {
return func;
}));

BOOST_REQUIRE_EQUAL(4, stlab::await(std::move(sut)));
BOOST_REQUIRE_EQUAL(4, stlab::await(std::move(r)));
}
BOOST_AUTO_TEST_CASE(future_when_all_arguments_with_mutable_move_onlytask) {
BOOST_TEST_MESSAGE("future when all arguments with mutable move only task");
Expand All @@ -160,7 +160,7 @@ BOOST_AUTO_TEST_CASE(future_when_all_arguments_with_mutable_move_onlytask) {
mutable_move_only func1;
mutable_move_only func2;

auto sut = when_all(
auto r = when_all(
stlab::default_executor, [](auto f1, auto f2) { return f1().member() + f2().member(); },
async(stlab::default_executor,
[func = std::move(func1)]() mutable {
Expand All @@ -172,7 +172,7 @@ BOOST_AUTO_TEST_CASE(future_when_all_arguments_with_mutable_move_onlytask) {
return std::move(func);
}));

BOOST_REQUIRE_EQUAL(4, stlab::await(std::move(sut)));
BOOST_REQUIRE_EQUAL(4, stlab::await(std::move(r)));
}
BOOST_AUTO_TEST_SUITE_END()

Expand Down

0 comments on commit e70b8e7

Please sign in to comment.