-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
285 lines (237 loc) · 11.7 KB
/
CMakeLists.txt
File metadata and controls
285 lines (237 loc) · 11.7 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# -----------------------------------------------------------------------------
# @brief : Root cmake file.
# @author : Enrico Fraccaroli
# -----------------------------------------------------------------------------
# Set the minimum CMake version, the project name and default build type.
cmake_minimum_required(VERSION 3.1...3.18)
# Set the project name.
project(bvlib CXX)
# Set the default build type to Debug.
if(NOT CMAKE_BUILD_TYPE)
message(STATUS "Setting build type to 'Debug' as none was specified.")
set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build." FORCE)
endif()
# -----------------------------------------------------------------------------
# ENABLE FETCH CONTENT
# -----------------------------------------------------------------------------
# We need this in order to import external projects.
include(FetchContent)
# Hide fetchcontent variables.
mark_as_advanced(FORCE
FETCHCONTENT_QUIET
FETCHCONTENT_BASE_DIR
FETCHCONTENT_FULLY_DISCONNECTED
FETCHCONTENT_UPDATES_DISCONNECTED
)
# -----------------------------------------------------------------------------
# OPTIONS
# -----------------------------------------------------------------------------
option(STRICT_WARNINGS "Enable strict compiler warnings" ON)
option(WARNINGS_AS_ERRORS "Treat all warnings as errors" OFF)
option(BUILD_EXAMPLES "Build examples" ON)
option(BUILD_TESTS "Build tests" ON)
# -----------------------------------------------------------------------------
# DEPENDENCIES
# -----------------------------------------------------------------------------
# We want doxygen for the documentation.
find_package(Doxygen)
find_program(CLANG_TIDY_EXE NAMES clang-tidy)
# -----------------------------------------------------------------------------
# LIBRARY
# -----------------------------------------------------------------------------
# Add the C++ Library.
add_library(${PROJECT_NAME} INTERFACE)
add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
# Inlcude header directories and set the library.
target_include_directories(${PROJECT_NAME} INTERFACE ${PROJECT_SOURCE_DIR}/include)
# Enalbe C++17.
target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_17)
# -----------------------------------------------------------------------------
# Set the compilation flags.
# -----------------------------------------------------------------------------
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
# Disable warnings for MSVC-specific "safe" functions like strcpy_s, etc.,
# which are not portable and may clutter warning logs.
target_compile_definitions(${PROJECT_NAME} INTERFACE _CRT_SECURE_NO_WARNINGS)
# Disable warning C4702: unreachable code.
add_compile_options(/wd4702)
if(WARNINGS_AS_ERRORS)
# Treat all warnings as errors to enforce stricter code quality.
target_compile_options(${PROJECT_NAME} INTERFACE /WX)
endif()
if(STRICT_WARNINGS)
# Enable external header management to suppress warnings in system and
# external headers, making it easier to focus on project-specific issues.
target_compile_options(${PROJECT_NAME} INTERFACE /experimental:external)
target_compile_options(${PROJECT_NAME} INTERFACE /external:I ${CMAKE_BINARY_DIR})
target_compile_options(${PROJECT_NAME} INTERFACE /external:anglebrackets)
target_compile_options(${PROJECT_NAME} INTERFACE /external:W0)
# Use a high warning level to catch as many potential issues as possible.
target_compile_options(${PROJECT_NAME} INTERFACE /W4)
# Enforce standards-compliant behavior to avoid relying on MSVC-specific extensions.
target_compile_options(${PROJECT_NAME} INTERFACE /permissive-)
endif()
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
if(WARNINGS_AS_ERRORS)
# Treat all warnings as errors to enforce stricter code quality.
target_compile_options(${PROJECT_NAME} INTERFACE -Werror)
endif()
if(STRICT_WARNINGS)
# Enable a broad set of warnings to catch common and subtle issues:
target_compile_options(${PROJECT_NAME} INTERFACE
-Wall # Enable most general-purpose warnings.
-Wextra # Enable extra warnings not included in -Wall.
-Wconversion # Warn about implicit type conversions that may lose data.
-pedantic # Enforce strict compliance with the C++ standard.
-Wshadow # Warn about variable shadowing, which can cause subtle bugs.
-Wnon-virtual-dtor # Warn when a class with virtual functions lacks a virtual destructor.
-Wnull-dereference # Warn about potential null pointer dereferences.
-Wformat=2 # Enable strict checks for printf/scanf format strings.
-Woverloaded-virtual # Warn when a derived class function hides a base class virtual function.
-Wfloat-equal # Warn about direct comparisons of floating-point values, which can be imprecise.
)
endif()
endif()
# -----------------------------------------------------------------------------
# EXAMPLES
# -----------------------------------------------------------------------------
if(BUILD_EXAMPLES)
# Add the example.
add_executable(${PROJECT_NAME}_example examples/example.cpp)
target_link_libraries(${PROJECT_NAME}_example PUBLIC ${PROJECT_NAME})
endif()
# -----------------------------------------------------------------------------
# TESTS
# -----------------------------------------------------------------------------
if(BUILD_TESTS)
# CMake has support for adding tests to a project.
enable_testing()
add_executable(${PROJECT_NAME}_test_core ${PROJECT_SOURCE_DIR}/tests/test_core.cpp)
target_link_libraries(${PROJECT_NAME}_test_core ${PROJECT_NAME})
add_test(${PROJECT_NAME}_test_core_run ${PROJECT_NAME}_test_core)
add_executable(${PROJECT_NAME}_test_support ${PROJECT_SOURCE_DIR}/tests/test_support.cpp)
target_link_libraries(${PROJECT_NAME}_test_support ${PROJECT_NAME})
add_test(${PROJECT_NAME}_test_support_run ${PROJECT_NAME}_test_support)
add_executable(${PROJECT_NAME}_test_shift ${PROJECT_SOURCE_DIR}/tests/test_shift.cpp)
target_link_libraries(${PROJECT_NAME}_test_shift ${PROJECT_NAME})
add_test(${PROJECT_NAME}_test_shift_run ${PROJECT_NAME}_test_shift)
add_executable(${PROJECT_NAME}_test_comparison tests/test_comparison.cpp)
target_link_libraries(${PROJECT_NAME}_test_comparison ${PROJECT_NAME})
add_test(${PROJECT_NAME}_test_comparison_run ${PROJECT_NAME}_test_comparison)
add_executable(${PROJECT_NAME}_test_math ${PROJECT_SOURCE_DIR}/tests/test_math.cpp)
target_link_libraries(${PROJECT_NAME}_test_math ${PROJECT_NAME})
add_test(${PROJECT_NAME}_test_math_run ${PROJECT_NAME}_test_math)
add_executable(${PROJECT_NAME}_test_bitwise ${PROJECT_SOURCE_DIR}/tests/test_bitwise.cpp)
target_link_libraries(${PROJECT_NAME}_test_bitwise ${PROJECT_NAME})
add_test(${PROJECT_NAME}_test_bitwise_run ${PROJECT_NAME}_test_bitwise)
endif()
# -----------------------------------------------------------------------------
# CODE ANALYSIS
# -----------------------------------------------------------------------------
if(CLANG_TIDY_EXE)
file(GLOB_RECURSE PROJECT_HEADERS_AND_SOURCES
"${PROJECT_SOURCE_DIR}/include/**/*.hpp"
"${PROJECT_SOURCE_DIR}/include/**/*.hh"
"${PROJECT_SOURCE_DIR}/include/**/*.h"
"${PROJECT_SOURCE_DIR}/include/*.hpp"
"${PROJECT_SOURCE_DIR}/include/*.hh"
"${PROJECT_SOURCE_DIR}/include/*.h"
"${PROJECT_SOURCE_DIR}/src/**/*.cpp"
"${PROJECT_SOURCE_DIR}/src/**/*.cc"
"${PROJECT_SOURCE_DIR}/src/**/*.c"
"${PROJECT_SOURCE_DIR}/src/*.cpp"
"${PROJECT_SOURCE_DIR}/src/*.cc"
"${PROJECT_SOURCE_DIR}/src/*.c"
)
add_custom_target(
${PROJECT_NAME}_clang_tidy
COMMAND ${CLANG_TIDY_EXE}
--p=${CMAKE_BINARY_DIR}
${PROJECT_HEADERS_AND_SOURCES}
COMMENT "Running clang-tidy"
VERBATIM
)
add_custom_target(
${PROJECT_NAME}_clang_tidy_fix
COMMAND ${CLANG_TIDY_EXE}
--fix --fix-errors
--p=${CMAKE_BINARY_DIR}
${PROJECT_HEADERS_AND_SOURCES}
COMMENT "Running clang-tidy-fix"
VERBATIM
)
endif()
# -----------------------------------------------------------------------------
# DOCUMENTATION
# -----------------------------------------------------------------------------
if(DOXYGEN_FOUND)
# FetchContent: Doxygen Awesome CSS
FetchContent_Declare(doxygenawesome
GIT_REPOSITORY https://github.com/jothepro/doxygen-awesome-css
GIT_TAG main
)
FetchContent_MakeAvailable(doxygenawesome)
# Hide FetchContent variables to avoid clutter in ccmake.
mark_as_advanced(FORCE
FETCHCONTENT_UPDATES_DISCONNECTED_DOXYGENAWESOME
FETCHCONTENT_SOURCE_DIR_DOXYGENAWESOME
)
# Read the file with the version.
file(READ ${PROJECT_SOURCE_DIR}/include/bvlib/bitvector.hpp version_file)
# Extract the version.
string(REGEX MATCH "BVLIB_MAJOR_VERSION ([0-9]*)" _ ${version_file})
set(PROJECT_MAJOR_VERSION ${CMAKE_MATCH_1})
string(REGEX MATCH "BVLIB_MINOR_VERSION ([0-9]*)" _ ${version_file})
set(PROJECT_MINOR_VERSION ${CMAKE_MATCH_1})
string(REGEX MATCH "BVLIB_MICRO_VERSION ([0-9]*)" _ ${version_file})
set(PROJECT_MICRO_VERSION ${CMAKE_MATCH_1})
# Customization: Doxygen Configuration
set(DOXYGEN_WARN_FORMAT "$file:$line:1: $text")
set(DOXYGEN_PROJECT_NAME BVLib)
set(DOXYGEN_PROJECT_BRIEF "A simple bitvector library for C++")
set(DOXYGEN_PROJECT_NUMBER "${PROJECT_MAJOR_VERSION}.${PROJECT_MINOR_VERSION}.${PROJECT_MICRO_VERSION}")
set(DOXYGEN_USE_MDFILE_AS_MAINPAGE ${PROJECT_SOURCE_DIR}/README.md)
set(DOXYGEN_MARKDOWN_SUPPORT YES)
set(DOXYGEN_AUTOLINK_REFERENCES YES)
set(DOXYGEN_SHOW_INCLUDE_FILES NO)
set(DOXYGEN_GENERATE_TREEVIEW YES)
set(DOXYGEN_GENERATE_LATEX NO)
set(DOXYGEN_GENERATE_MAN NO)
# Styling and UX enhancements using Doxygen Awesome
set(DOXYGEN_HTML_HEADER ${doxygenawesome_SOURCE_DIR}/doxygen-custom/header.html)
set(DOXYGEN_HTML_EXTRA_STYLESHEET ${doxygenawesome_SOURCE_DIR}/doxygen-awesome.css)
set(DOXYGEN_HTML_EXTRA_FILES
${doxygenawesome_SOURCE_DIR}/doxygen-awesome-fragment-copy-button.js
${doxygenawesome_SOURCE_DIR}/doxygen-awesome-paragraph-link.js
${doxygenawesome_SOURCE_DIR}/doxygen-awesome-darkmode-toggle.js
)
# Set stricter warnings for better documentation quality
set(DOXYGEN_WARN_IF_UNDOCUMENTED YES)
set(DOXYGEN_WARN_IF_DOC_ERROR YES)
set(DOXYGEN_WARN_NO_PARAMDOC YES)
set(DOXYGEN_WARN_AS_ERROR YES) # Treat warnings as errors for CI
# Exclude certain files or directories from documentation (if needed)
set(DOXYGEN_EXCLUDE_PATTERNS "${PROJECT_SOURCE_DIR}/tests/*" "${PROJECT_SOURCE_DIR}/examples/*")
# Add Doxygen documentation target.
file(GLOB_RECURSE PROJECT_HEADERS_AND_SOURCES
"${PROJECT_SOURCE_DIR}/include/**/*.hpp"
"${PROJECT_SOURCE_DIR}/include/**/*.hh"
"${PROJECT_SOURCE_DIR}/include/**/*.h"
"${PROJECT_SOURCE_DIR}/include/*.hpp"
"${PROJECT_SOURCE_DIR}/include/*.hh"
"${PROJECT_SOURCE_DIR}/include/*.h"
"${PROJECT_SOURCE_DIR}/src/**/*.cpp"
"${PROJECT_SOURCE_DIR}/src/**/*.cc"
"${PROJECT_SOURCE_DIR}/src/**/*.c"
"${PROJECT_SOURCE_DIR}/src/*.cpp"
"${PROJECT_SOURCE_DIR}/src/*.cc"
"${PROJECT_SOURCE_DIR}/src/*.c"
)
doxygen_add_docs(
${PROJECT_NAME}_documentation
${PROJECT_SOURCE_DIR}/README.md
${PROJECT_SOURCE_DIR}/LICENSE.md
${PROJECT_HEADERS_AND_SOURCES}
COMMENT "Generating Doxygen documentation"
)
endif()