-
Notifications
You must be signed in to change notification settings - Fork 25
/
CMakeLists.txt
221 lines (183 loc) · 6.39 KB
/
CMakeLists.txt
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
cmake_minimum_required(VERSION 3.16)
project(cactus_rt)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
#################
# Setup options #
#################
# Used for building cactus-rt when all dependencies are vendored
option(CACTUS_RT_ENABLE_FETCH_DEPENDENCIES "Fetch dependencies during build" ON)
# Used to disable tracing in some builds where the overhead of tracing is unwanted.
option(CACTUS_RT_ENABLE_TRACING "Enable runtime tracing support" ON)
# Below are internal options
option(ENABLE_CLANG_TIDY "Run clang-tidy" OFF)
option(ENABLE_EXAMPLES "Build example programs" ON)
option(ENABLE_ROS2 "Enables ROS2 support" OFF)
option(BUILD_DOCS "Build documentations" OFF)
# https://stackoverflow.com/questions/5395309/how-do-i-force-cmake-to-include-pthread-option-during-compilation
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED)
#########################
# External dependencies #
#########################
if(CACTUS_RT_ENABLE_FETCH_DEPENDENCIES)
Include(FetchContent)
FetchContent_Declare(
quill
GIT_REPOSITORY https://github.com/odygrd/quill.git
GIT_TAG 9a270d5d6f57a3ac19451292e3a9f370fcd744b1
# GIT_TAG v3.3.2
)
FetchContent_MakeAvailable(quill)
# This is needed to make sure that when building the tests, catch2's headers
# are treated as system headers, as otherwise clang-tidy will run on them. This
# is different from the above because clang-tidy will run on the headers which
# is included from the tests, where as the above is for compiling catch2.
#
# After cmake 3.25, this shouldn't be needed anymore: https://gitlab.kitware.com/cmake/cmake/-/issues/18040
get_target_property(QUILL_INC quill INTERFACE_INCLUDE_DIRECTORIES)
set_target_properties(quill PROPERTIES INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${QUILL_INC}")
FetchContent_Declare(
readerwriterqueue
GIT_REPOSITORY https://github.com/cameron314/readerwriterqueue.git
GIT_TAG v1.0.6
)
FetchContent_MakeAvailable(readerwriterqueue)
get_target_property(READERWRITERQUEUE_INC readerwriterqueue INTERFACE_INCLUDE_DIRECTORIES)
set_target_properties(readerwriterqueue PROPERTIES INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${READERWRITERQUEUE_INC}")
endif()
##########################################################
# Helper function to setup all cactus-rt related targets #
##########################################################
function(setup_cactus_rt_target_options target_name)
# https://github.com/cpp-best-practices/cppbestpractices/blob/b1629eb/02-Use_the_Tools_Available.md#gcc--clang
target_compile_options(${target_name}
PRIVATE
-Wall
-Wextra
-Wshadow
-Wnon-virtual-dtor
-pedantic
-Wold-style-cast
-Wcast-align
-Wunused
-Woverloaded-virtual
-Wpedantic
-Wconversion
-Wsign-conversion
-Wmisleading-indentation
-Wduplicated-cond
-Wduplicated-branches
-Wlogical-op
-Wnull-dereference
-Wuseless-cast
-Wdouble-promotion
-Wformat=2
-Wimplicit-fallthrough
)
target_compile_features(${target_name}
PRIVATE
cxx_std_17
)
if(${CMAKE_PROJECT_NAME} STREQUAL ${PROJECT_NAME})
if (ENABLE_CLANG_TIDY)
# Need the extra args because g++ on 22.04 is by default C++17 which
# means cmake won't generate the -std=gnu++17 flag, which causes
# clang-tidy-14 (also 22.04 default) to fail.
# Probably this issue: https://gitlab.kitware.com/cmake/cmake/-/issues/24238#note_1287978
set_target_properties(${target_name} PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY};--extra-arg-before=-std=c++17")
endif()
endif()
endfunction()
#####################
# Cactus RT library #
#####################
if (CACTUS_RT_ENABLE_TRACING)
add_subdirectory(protos)
endif()
add_library(cactus_rt
STATIC
src/cactus_rt/app.cc
src/cactus_rt/thread.cc
src/cactus_rt/cyclic_thread.cc
src/cactus_rt/signal_handler.cc
src/cactus_rt/experimental/lockless/atomic_bitset.cc
)
target_include_directories(cactus_rt
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
)
target_link_libraries(cactus_rt
PUBLIC
quill::quill
readerwriterqueue
PRIVATE
Threads::Threads
)
# Use a bounded queue
target_compile_definitions(cactus_rt PUBLIC QUILL_USE_BOUNDED_QUEUE)
if (CACTUS_RT_ENABLE_TRACING)
target_sources(cactus_rt
PRIVATE
src/cactus_rt/tracing/sink.cc
src/cactus_rt/tracing/thread_tracer.cc
src/cactus_rt/tracing/trace_aggregator.cc
src/cactus_rt/tracing/tracing_enabled.cc
src/cactus_rt/tracing/utils/string_interner.cc
)
target_link_libraries(cactus_rt
PUBLIC
cactus_tracing_embedded_perfetto_protos
)
target_compile_definitions(cactus_rt
PUBLIC
CACTUS_RT_TRACING_ENABLED=1
)
endif()
if(${CMAKE_PROJECT_NAME} STREQUAL ${PROJECT_NAME})
if (ENABLE_CLANG_TIDY)
find_program(CLANG_TIDY clang-tidy clang-tidy-18 clang-tidy-17 clang-tidy-16 clang-tidy-15 clang-tidy-14)
else()
message(STATUS "Not running clang-tidy. Use ENABLE_CLANG_TIDY=ON to run clang-tidy.")
endif()
endif()
# ROS 2 build support
if (ENABLE_ROS2)
include(cmake/ros2.cmake)
endif()
setup_cactus_rt_target_options(cactus_rt)
# Build tests, examples, docs, only if this project is not embedded in another
# project.
if(${CMAKE_PROJECT_NAME} STREQUAL ${PROJECT_NAME})
include(CTest)
if (BUILD_TESTING)
add_subdirectory(tests)
endif()
if (ENABLE_EXAMPLES)
message(STATUS "Building example programs. Turn it off via ENABLE_EXAMPLES=OFF")
add_subdirectory(examples/lockless_examples)
add_subdirectory(examples/logging_example)
add_subdirectory(examples/message_passing_example)
add_subdirectory(examples/mutex_example)
add_subdirectory(examples/simple_deadline_example)
add_subdirectory(examples/simple_example)
add_subdirectory(examples/random_example)
if (CACTUS_RT_ENABLE_TRACING)
add_subdirectory(examples/tracing_protos_example)
add_subdirectory(examples/tracing_example)
add_subdirectory(examples/tracing_example_no_rt)
endif()
endif()
if (BUILD_DOCS)
message(STATUS "Building documentations. Turn it off via BUILD_DOCS=OFF")
find_package(Doxygen REQUIRED)
set(DOXYGEN_FILE ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in ${DOXYGEN_FILE} @ONLY)
add_custom_target(
docs ALL
COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_FILE}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
VERBATIM
)
endif()
endif()