-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
77 lines (64 loc) · 2.74 KB
/
CMakeLists.txt
File metadata and controls
77 lines (64 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 指定CMake最低版本要求
cmake_minimum_required(VERSION 3.20)
project(ThreadPool LANGUAGES C CXX VERSION 0.9.2)
include(GNUInstallDirs)
# ---------------------------------------------------------------------------------------
# Set default build to Release for single-config generators (Makefile/Ninja)
# ---------------------------------------------------------------------------------------
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
if((DEFINED PROJECT_IS_TOP_LEVEL AND PROJECT_IS_TOP_LEVEL)
OR (NOT DEFINED PROJECT_IS_TOP_LEVEL AND CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR))
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose Release or Debug" FORCE)
endif()
endif()
# 仅头文件库 (Header-Only) INTERFACE 类型
# INTERFACE 目标不会生成 .a 或 .so 文件, 但可以被 target_link_libraries 依赖.
add_library(thread_pool INTERFACE)
add_library(thread_pool::thread_pool ALIAS thread_pool)
target_include_directories(thread_pool
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
target_compile_features(thread_pool INTERFACE cxx_std_11)
# POSIX 系统下使用 pthread 库
find_package(Threads REQUIRED)
target_link_libraries(thread_pool INTERFACE Threads::Threads)
# ----------------------------------------------------------
# Detect master (top-level) project
# ----------------------------------------------------------
if(NOT DEFINED THREAD_POOL_MASTER_PROJECT)
if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
set(THREAD_POOL_MASTER_PROJECT ON)
else()
set(THREAD_POOL_MASTER_PROJECT OFF)
endif()
endif()
# Compiler-specific warning/options
set(gcc_like_cxx "$<COMPILE_LANG_AND_ID:CXX,ARMClang,AppleClang,Clang,GNU,LCC>")
set(msvc_cxx "$<COMPILE_LANG_AND_ID:CXX,MSVC>")
if(THREAD_POOL_MASTER_PROJECT)
message(STATUS "[thread_pool] Master project: ${THREAD_POOL_MASTER_PROJECT}")
# Generate compile_commands.json for clangd/clang-tidy
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# Enable PIC for all targets (safest for static libs)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
target_compile_options(thread_pool INTERFACE
"$<${gcc_like_cxx}:-Wall;-Wextra;-Wshadow;-Wformat=2;-Wunused>"
"$<${msvc_cxx}:/W3;/utf-8>"
)
endif()
option(THREAD_POOL_BUILD_EXAMPLES "Build examples" ${THREAD_POOL_MASTER_PROJECT})
option(THREAD_POOL_BUILD_TESTS "Build tests" ${THREAD_POOL_MASTER_PROJECT})
if(THREAD_POOL_BUILD_EXAMPLES)
message(STATUS "[thread_pool] Building examples...")
add_subdirectory(examples)
endif()
if(THREAD_POOL_BUILD_TESTS)
message(STATUS "[thread_pool] Building tests...")
include(CTest) # enable add_test
if(NOT TARGET Catch2::Catch2)
add_subdirectory(external)
endif()
add_subdirectory(tests)
endif()