From 1157e9caff9b6c802e1237f795c0fd27f8ac0f64 Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Sun, 29 Jan 2023 13:18:27 +0100 Subject: [PATCH 1/2] Test installed version too --- .clang-tidy | 2 +- CMakeLists.txt | 38 ++++++++++++++++++++++++++++---------- example.cpp | 38 +++++++++++++++++++------------------- 3 files changed, 48 insertions(+), 30 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index e362afd..420499c 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,3 +1,3 @@ --- -Checks: '*,-cert-err58-cpp,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-google-readability-todo,-google-runtime-references,-readability-named-parameter,-clang-diagnostic-unused-command-line-argument,-readability-implicit-bool-cast,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-fuchsia-default-arguments' +Checks: '*,-cert-err58-cpp,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-google-readability-todo,-google-runtime-references,-clang-diagnostic-unused-command-line-argument,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-fuchsia-default-arguments,-llvmlibc-*,-altera-unroll-loops,-modernize-use-trailing-return-type' WarningsAsErrors: 'hicpp-use-override,modernize-avoid-bind,llvm-namespace-comment,modernize-use-nullptr' diff --git a/CMakeLists.txt b/CMakeLists.txt index 4fb3e86..ba332a1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,34 +1,45 @@ -cmake_minimum_required(VERSION 3.12) -project(ThreadPool) +cmake_minimum_required(VERSION 3.21...3.25) +project(ThreadPool CXX) include(GNUInstallDirs) -set(CMAKE_INSTALL_CMAKEDIR "share/cmake" CACHE STRING "Install location for cmake related files") +set(CMAKE_INSTALL_CMAKEDIR "cmake" CACHE STRING "Install location for cmake related files") mark_as_advanced(CMAKE_INSTALL_CMAKEDIR) set(LIBRARY_TYPES "OBJECT;STATIC;SHARED") -set(THREADPOOL_LIBRARY_TYPE "OBJECT" CACHE STRING "Choose the type of library to build, options are: ${LIBRARY_TYPES}.") +set(THREADPOOL_LIBRARY_TYPE "STATIC" CACHE STRING "Choose the type of library to build, options are: ${LIBRARY_TYPES}.") set_property(CACHE THREADPOOL_LIBRARY_TYPE PROPERTY STRINGS ${LIBRARY_TYPES}) if (NOT THREADPOOL_LIBRARY_TYPE IN_LIST LIBRARY_TYPES) message(FATAL_ERROR "Invalid option: THREADPOOL_LIBRARY_TYPE=${THREADPOOL_LIBRARY_TYPE}. Supported options: ${LIBRARY_TYPES}.") endif() find_package(Threads REQUIRED) +set(PROJECT_DEPENDENCIES Threads) -add_library(ThreadPool ${THREADPOOL_LIBRARY_TYPE} ThreadPool.cpp) +add_library(ThreadPool ${THREADPOOL_LIBRARY_TYPE} ThreadPool.cpp ThreadPool.hpp ) target_link_libraries(ThreadPool PUBLIC Threads::Threads) target_include_directories(ThreadPool PUBLIC $ - $) + $) -target_compile_features(ThreadPool PRIVATE cxx_std_17) +target_compile_features(ThreadPool PUBLIC cxx_std_17) + +enable_testing() +add_executable(example example.cpp) +target_link_libraries(example PRIVATE ThreadPool) +add_test(example example) # Define public headers to get them automatically installed. set_target_properties(ThreadPool PROPERTIES PUBLIC_HEADER "ThreadPool.hpp") -set(THREADPOOL_EXPORT ThreadPoolConfig) +set(THREADPOOL_EXPORT ThreadPoolTargets) + +configure_file( + Config.cmake.in + ${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake @ONLY +) # Install the lib and its headers. Flag it for export. install( @@ -40,8 +51,15 @@ install( ) # Create the export file for the build tree. -export(TARGETS ThreadPool FILE "${PROJECT_BINARY_DIR}/${THREADPOOL_EXPORT}.cmake") +export(TARGETS ThreadPool FILE ${PROJECT_BINARY_DIR}/${THREADPOOL_EXPORT}.cmake) # Create the export file for the install tree. install(EXPORT ${THREADPOOL_EXPORT} - DESTINATION "${CMAKE_INSTALL_CMAKEDIR}") + DESTINATION ${CMAKE_INSTALL_CMAKEDIR} +) + +install( + FILES #TODO ${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake + ${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake + DESTINATION ${CMAKE_INSTALL_CMAKEDIR} +) diff --git a/example.cpp b/example.cpp index 67f60bd..cf51182 100644 --- a/example.cpp +++ b/example.cpp @@ -1,30 +1,30 @@ +#include #include #include #include -#include #include "ThreadPool.hpp" -int main() -{ +int main() { + constexpr int USED_TASKS{ 8 }; - ThreadPool pool(4); - std::vector< std::future > results; + ThreadPool pool(USED_TASKS / 2); + std::vector> results; - for(int i = 0; i < 8; ++i) { - results.emplace_back( - pool.enqueue([i] { - std::cout << "hello " << i << std::endl; - std::this_thread::sleep_for(std::chrono::seconds(1)); - std::cout << "world " << i << std::endl; - return i*i; - }) - ); - } + results.reserve(USED_TASKS); + for (int i = 0; i < USED_TASKS; ++i) { + results.emplace_back(pool.enqueue([i] { + std::cout << "hello " << i << std::endl; + std::this_thread::sleep_for(std::chrono::seconds(1)); + std::cout << "world " << i << std::endl; + return i * i; + })); + } - for(auto && result: results) - std::cout << result.get() << ' '; - std::cout << std::endl; + for (auto&& result : results) { + std::cout << result.get() << ' '; + } + std::cout << std::endl; - return 0; + return 0; } From af56f13743594331c608fdba39d755beeaeeebf3 Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Sun, 29 Jan 2023 13:20:00 +0100 Subject: [PATCH 2/2] Add new fils neede for test --- Config.cmake.in | 10 ++++++++++ test/CMakeLists.txt | 9 +++++++++ 2 files changed, 19 insertions(+) create mode 100644 Config.cmake.in create mode 100644 test/CMakeLists.txt diff --git a/Config.cmake.in b/Config.cmake.in new file mode 100644 index 0000000..15f626a --- /dev/null +++ b/Config.cmake.in @@ -0,0 +1,10 @@ +include(CMakeFindDependencyMacro) + +string(REGEX MATCHALL "[^;]+" SEPARATE_DEPENDENCIES "@PROJECT_DEPENDENCIES@") + +foreach(dependency ${SEPARATE_DEPENDENCIES}) + string(REPLACE " " ";" args "${dependency}") + find_dependency(${args}) +endforeach() + +include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake") diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..3d95cfc --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required(VERSION 3.21...3.25) +project(TestThreadPool CXX) + +find_package(ThreadPool CONFIG REQUIRED) + +enable_testing() +add_executable(test_install ../example.cpp) +target_link_libraries(test_install PRIVATE ThreadPool) +add_test(test_install test_install)