generated from filipdutescu/modern-cpp-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CMakeLists.txt
399 lines (332 loc) · 9.56 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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
cmake_minimum_required(VERSION 3.21)
#
# Project details
#
project(
"ANA"
VERSION 0.1.0
DESCRIPTION "ayanami-nemesis-analyzer"
LANGUAGES CXX
)
# TODO: make use local libraries is optional
set(BUILD_SHARED_LIBS on)
set(CMAKE_CXX_STANDARD 20)
set(${PROJECT_NAME}_ENABLE_UNIT_TESTING ON)
set(${PROJECT_NAME}_BUILD_EXECUTABLE ON)
set(EnablesClangPlugins on)
add_subdirectory(fmt)
add_subdirectory(googletest)
#
# Deal with Project Options
if (EnablesClangPlugins)
set(LLVM_DIR /Volumes/CODE/llvm-project/build/lib/cmake/llvm) # you can reload the llvm dir
set(Clang_DIR /Volumes/CODE/llvm-project/build/lib/cmake/clang)# I use my own version of llvm
add_subdirectory(ClangPlugins)
add_subdirectory(TestANA)
endif ()
#
#
# Set project options
#
include(cmake/StandardSettings.cmake)
include(cmake/StaticAnalyzers.cmake)
include(cmake/Utils.cmake)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Debug")
endif()
message(STATUS "Started CMake for ${PROJECT_NAME} v${PROJECT_VERSION}...\n")
if (UNIX)
add_compile_options("$<$<CONFIG:DEBUG>:-D_DEBUG>") #this will allow to use same _DEBUG macro available in both Linux as well as Windows - MSCV environment. Easy to put Debug specific code.
endif (UNIX)
#
# Setup alternative names
#
if(${PROJECT_NAME}_USE_ALT_NAMES)
string(TOLOWER ${PROJECT_NAME} PROJECT_NAME_LOWERCASE)
string(TOUPPER ${PROJECT_NAME} PROJECT_NAME_UPPERCASE)
else()
set(PROJECT_NAME_LOWERCASE ${PROJECT_NAME})
set(PROJECT_NAME_UPPERCASE ${PROJECT_NAME})
endif()
#
# Prevent building in the source directory
#
if(PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR)
message(FATAL_ERROR "In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there.\n")
endif()
#
# Enable package managers
#
include(cmake/Conan.cmake)
include(cmake/Vcpkg.cmake)
#
# Create library, setup header and source files
#
# Find all headers and implementation files
include(cmake/SourcesAndHeaders.cmake)
if(${PROJECT_NAME}_BUILD_EXECUTABLE)
add_executable(${PROJECT_NAME} ${exe_sources})
if(${PROJECT_NAME}_VERBOSE_OUTPUT)
verbose_message("Found the following sources:")
foreach(source IN LISTS exe_sources)
verbose_message("* ${source}")
endforeach()
endif()
if(${PROJECT_NAME}_ENABLE_UNIT_TESTING)
add_library(${PROJECT_NAME}_LIB STATIC ${headers} ${sources})
if(${PROJECT_NAME}_VERBOSE_OUTPUT)
verbose_message("Found the following headers:")
foreach(header IN LISTS headers)
verbose_message("* ${header}")
endforeach()
endif()
endif()
elseif(${PROJECT_NAME}_BUILD_HEADERS_ONLY)
add_library(${PROJECT_NAME} INTERFACE)
if(${PROJECT_NAME}_VERBOSE_OUTPUT)
verbose_message("Found the following headers:")
foreach(header IN LIST headers)
verbose_message("* ${header}")
endforeach()
endif()
else()
add_library(
${PROJECT_NAME}
${headers}
${sources}
)
if(${PROJECT_NAME}_VERBOSE_OUTPUT)
verbose_message("Found the following sources:")
foreach(source IN LISTS sources)
verbose_message("* ${source}")
endforeach()
verbose_message("Found the following headers:")
foreach(header IN LISTS headers)
verbose_message("* ${header}")
endforeach()
endif()
endif()
set_target_properties(
${PROJECT_NAME}
PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/${CMAKE_BUILD_TYPE}"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/${CMAKE_BUILD_TYPE}"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/${CMAKE_BUILD_TYPE}"
)
if(${PROJECT_NAME}_BUILD_EXECUTABLE AND ${PROJECT_NAME}_ENABLE_UNIT_TESTING)
set_target_properties(
${PROJECT_NAME}_LIB
PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/${CMAKE_BUILD_TYPE}"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/${CMAKE_BUILD_TYPE}"
OUTPUT_NAME ${PROJECT_NAME}
)
endif()
message(STATUS "Added all header and implementation files.\n")
#
# Set the project standard and warnings
#
if(${PROJECT_NAME}_BUILD_HEADERS_ONLY)
target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_17)
else()
target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_17)
if(${PROJECT_NAME}_BUILD_EXECUTABLE AND ${PROJECT_NAME}_ENABLE_UNIT_TESTING)
target_compile_features(${PROJECT_NAME}_LIB PUBLIC cxx_std_17)
endif()
endif()
include(cmake/CompilerWarnings.cmake)
set_project_warnings(${PROJECT_NAME})
verbose_message("Applied compiler warnings. Using standard ${CMAKE_CXX_STANDARD}.\n")
#
# Enable Doxygen
#
include(cmake/Doxygen.cmake)
#
# Model project dependencies
#
find_package(fmt CONFIG REQUIRED)
target_link_libraries(
${PROJECT_NAME}
PRIVATE
fmt::fmt
)
# TODO: make llvm path is a option
# Identify and link with the specific "packages" the project uses
#find_package(package_name package_version REQUIRED package_type [other_options])
#target_link_libraries(
# ${PROJECT_NAME}
# PUBLIC
# dependency1 ...
# PRIVATE
# dependency2 ...
# ${PROJECT_NAME}_PROJECT_OPTIONS
# ${PROJECT_NAME}_PROJECT_WARNINGS
#)
#if(${PROJECT_NAME}_BUILD_EXECUTABLE AND ${PROJECT_NAME}_ENABLE_UNIT_TESTING)
# target_link_libraries(
# ${PROJECT_NAME}_LIB
# PUBLIC
# dependency1 ...
# )
#endif()
# For Windows, it is necessary to link with the MultiThreaded library.
# Depending on how the rest of the project's dependencies are linked, it might be necessary
# to change the line to statically link with the library.
#
# This is done as follows:
#
# set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
#
# On Linux and Mac this variable is ignored. If any issues rise from it, try commenting it out
# and letting CMake decide how to link with it.
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
verbose_message("Successfully added all dependencies and linked against them.")
#
# Set the build/user include directories
#
# Allow usage of header files in the `src` directory, but only for utilities
if(${PROJECT_NAME}_BUILD_HEADERS_ONLY)
target_include_directories(
${PROJECT_NAME}
INTERFACE
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)
else()
target_include_directories(
${PROJECT_NAME}
PUBLIC
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src
)
if(${PROJECT_NAME}_BUILD_EXECUTABLE AND ${PROJECT_NAME}_ENABLE_UNIT_TESTING)
target_include_directories(
${PROJECT_NAME}_LIB
PUBLIC
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src
)
endif()
endif()
message(STATUS "Finished setting up include directories.")
#
# Provide alias to library for
#
if(${PROJECT_NAME}_BUILD_EXECUTABLE)
add_executable(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
else()
add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
endif()
verbose_message("Project is now aliased as ${PROJECT_NAME}::${PROJECT_NAME}.\n")
#
# Format the project using the `clang-format` target (i.e: cmake --build build --target clang-format)
#
add_clang_format_target()
#
# Install library for easy downstream inclusion
#
include(GNUInstallDirs)
install(
TARGETS
${PROJECT_NAME}
EXPORT
${PROJECT_NAME}Targets
LIBRARY DESTINATION
${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION
${CMAKE_INSTALL_BINDIR}
ARCHIVE DESTINATION
${CMAKE_INSTALL_LIBDIR}
INCLUDES DESTINATION
include
PUBLIC_HEADER DESTINATION
include
)
install(
EXPORT
${PROJECT_NAME}Targets
FILE
${PROJECT_NAME}Targets.cmake
NAMESPACE
${PROJECT_NAME}::
DESTINATION
${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
)
#
# Add version header
#
configure_file(
${CMAKE_CURRENT_LIST_DIR}/cmake/version.hpp.in
include/${PROJECT_NAME_LOWERCASE}/version.hpp
@ONLY
)
install(
FILES
${CMAKE_CURRENT_BINARY_DIR}/include/${PROJECT_NAME_LOWERCASE}/version.hpp
DESTINATION
include/${PROJECT_NAME_LOWERCASE}
)
#
# Install the `include` directory
#
install(
DIRECTORY
include/${PROJECT_NAME_LOWERCASE}
DESTINATION
include
)
verbose_message("Install targets succesfully build. Install with `cmake --build <build_directory> --target install --config <build_config>`.")
#
# Quick `ConfigVersion.cmake` creation
#
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
${PROJECT_NAME}ConfigVersion.cmake
VERSION
${PROJECT_VERSION}
COMPATIBILITY
SameMajorVersion
)
configure_package_config_file(
${CMAKE_CURRENT_LIST_DIR}/cmake/${PROJECT_NAME}Config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
INSTALL_DESTINATION
${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
)
install(
FILES
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
DESTINATION
${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
)
#
# Generate export header if specified
#
if(${PROJECT_NAME}_GENERATE_EXPORT_HEADER)
include(GenerateExportHeader)
generate_export_header(${PROJECT_NAME})
install(
FILES
${PROJECT_BINARY_DIR}/${PROJECT_NAME_LOWERCASE}_export.h
DESTINATION
include
)
message(STATUS "Generated the export header `${PROJECT_NAME_LOWERCASE}_export.h` and installed it.")
endif()
message(STATUS "Finished building requirements for installing the package.\n")
#
# Unit testing setup
#
# TODO: use customsized gtest
set(GTEST_DIR "build/googletest/googletest/generated/")
set(GTEST_LIBRARY "build/lib/libgtest.a")
set(GTEST_INCLUDE_DIR "googletest/googletest/include")
set(GTEST_MAIN_LIBRARY "build/lib/libgtest_main.a")
if(${PROJECT_NAME}_ENABLE_UNIT_TESTING)
add_subdirectory(test)
endif()