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

Test installed version too #5

Open
wants to merge 2 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
2 changes: 1 addition & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
@@ -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'
38 changes: 28 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:include>)
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)

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(
Expand All @@ -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}
)
10 changes: 10 additions & 0 deletions Config.cmake.in
Original file line number Diff line number Diff line change
@@ -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}/@[email protected]")
38 changes: 19 additions & 19 deletions example.cpp
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
#include <chrono>
#include <iostream>
#include <thread>
#include <vector>
#include <chrono>

#include "ThreadPool.hpp"

int main()
{
int main() {
constexpr int USED_TASKS{ 8 };

ThreadPool pool(4);
std::vector< std::future<int> > results;
ThreadPool pool(USED_TASKS / 2);
std::vector<std::future<int>> 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;
}
9 changes: 9 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)