generated from Life4gal/CMakeTemplateProject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
369 lines (321 loc) · 12.9 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
cmake_minimum_required(VERSION 3.20)
set(PROJECT_NAME_PREFIX "GAL_INI_")
set(
${PROJECT_NAME_PREFIX}MAJOR_VERSION
1
)
set(
${PROJECT_NAME_PREFIX}MINOR_VERSION
5
)
set(
${PROJECT_NAME_PREFIX}PATCH_VERSION
3
)
set(${PROJECT_NAME_PREFIX}VERSION ${${PROJECT_NAME_PREFIX}MAJOR_VERSION}.${${PROJECT_NAME_PREFIX}MINOR_VERSION}.${${PROJECT_NAME_PREFIX}PATCH_VERSION})
# Determine if our project is built as a subproject (using add_subdirectory)
# or if it is the master project.
set(${PROJECT_NAME_PREFIX}MASTER_PROJECT OFF)
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
set(${PROJECT_NAME_PREFIX}MASTER_PROJECT ON)
else ()
endif (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
# fetch remote cmake utils
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake_utils/fetch_utils.cmake)
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake_utils/doc_var.cmake)
# Set the default CMAKE_BUILD_TYPE to RelWithDebInfo.
# This should be done before the project command since the latter can set
# CMAKE_BUILD_TYPE itself.
if (${PROJECT_NAME_PREFIX}MASTER_PROJECT AND NOT CMAKE_BUILD_TYPE)
cmake_language(
CALL
${PROJECT_NAME_PREFIX}doc_var
CMAKE_BUILD_TYPE
RelWithDebInfo
STRING
"Choose the type of build, options are: None(CMAKE_CXX_FLAGS or "
"CMAKE_C_FLAGS used) Debug Release RelWithDebInfo MinSizeRel."
)
endif ()
project(
ini
VERSION ${${PROJECT_NAME_PREFIX}VERSION}
DESCRIPTION "GAL INI PARSER"
HOMEPAGE_URL "https://github.com/Life4gal/ini"
LANGUAGES CXX
)
if (${${PROJECT_NAME_PREFIX}MASTER_PROJECT})
message(STATUS "Our project [${PROJECT_NAME}] is the main-project. CMAKE VERSION: ${CMAKE_VERSION}. Compiler ID: ${CMAKE_CXX_COMPILER_ID}. Platform: ${CMAKE_SYSTEM_NAME}.")
else ()
message(STATUS "Our project [${PROJECT_NAME}] is the sub-project. CMAKE VERSION: ${CMAKE_VERSION}. Compiler ID: ${CMAKE_CXX_COMPILER_ID}. Platform: ${CMAKE_SYSTEM_NAME}.")
endif (${${PROJECT_NAME_PREFIX}MASTER_PROJECT})
message(STATUS "${PROJECT_NAME} VERSION: ${${PROJECT_NAME_PREFIX}VERSION}")
message(STATUS "${PROJECT_NAME} BUILD TYPE: ${CMAKE_BUILD_TYPE}")
include(GNUInstallDirs)
include(CheckCXXCompilerFlag)
include(${PROJECT_SOURCE_DIR}/cmake_utils/CPM.cmake)
# Let CPMAddPackage first check if the required package exists locally.
# Compared to CPMFindPackage, CPMAddPackage will call cpm_export_variables after finding the local package.
set(CPM_USE_LOCAL_PACKAGES ON)
include(${PROJECT_SOURCE_DIR}/cmake_utils/cpm_install.cmake)
include(${PROJECT_SOURCE_DIR}/cmake_utils/nuget_install.cmake)
set(${PROJECT_NAME_PREFIX}3RD_PARTY_PATH ${PROJECT_SOURCE_DIR}/3rd-party)
if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
set(${PROJECT_NAME_PREFIX}PLATFORM_WINDOWS ON)
set(${PROJECT_NAME_PREFIX}PLATFORM ${PROJECT_NAME_PREFIX}PLATFORM_WINDOWS)
elseif (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
set(${PROJECT_NAME_PREFIX}PLATFORM_LINUX ON)
set(${PROJECT_NAME_PREFIX}PLATFORM ${PROJECT_NAME_PREFIX}PLATFORM_LINUX)
elseif (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set(${PROJECT_NAME_PREFIX}PLATFORM_MACOS ON)
set(${PROJECT_NAME_PREFIX}PLATFORM ${PROJECT_NAME_PREFIX}PLATFORM_MACOS)
else ()
message(FATAL_ERROR "Unsupported Platform: ${CMAKE_SYSTEM_NAME}")
endif (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
cmake_language(
CALL
${PROJECT_NAME_PREFIX}doc_var
${PROJECT_NAME_PREFIX}INSTALL_HEADERS
${CMAKE_INSTALL_INCLUDEDIR}
STRING
"Installation directory for include files, a relative path that "
"will be joined with ${CMAKE_INSTALL_PREFIX} or an absolute path."
)
option(${PROJECT_NAME_PREFIX}PEDANTIC "Enable extra warnings and expensive tests." ON)
option(${PROJECT_NAME_PREFIX}WERROR "Halt the compilation with an error on compiler warnings." ON)
option(${PROJECT_NAME_PREFIX}DOC "Generate the doc target." ${${PROJECT_NAME_PREFIX}MASTER_PROJECT}) # Do we have the documentation? :)
option(${PROJECT_NAME_PREFIX}INSTALL "Generate the install target." ${${PROJECT_NAME_PREFIX}MASTER_PROJECT})
option(${PROJECT_NAME_PREFIX}TEST "Generate the test target." ${${PROJECT_NAME_PREFIX}MASTER_PROJECT})
option(${PROJECT_NAME_PREFIX}SYSTEM_HEADERS "Expose headers with marking them as system.(This allows other libraries that use this library to ignore the warnings generated by this library.)" OFF)
option(${PROJECT_NAME_PREFIX}BUILD_SHARED "Build shared library.)" OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
#set(CMAKE_POSITION_INDEPENDENT_CODE ON)
#set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
#set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
#set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
if (NOT CMAKE_RUNTIME_OUTPUT_DIRECTORY)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin)
endif ()
set(${PROJECT_NAME_PREFIX}MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake_modules)
set(${PROJECT_NAME_PREFIX}TEMP_MODULE_PATH ${CMAKE_BINARY_DIR}/cmake_modules)
# Folders that do not exist must be created first, otherwise configure_file does not have permission to create the file.
file(MAKE_DIRECTORY ${${PROJECT_NAME_PREFIX}TEMP_MODULE_PATH})
list(APPEND CMAKE_MODULE_PATH ${${PROJECT_NAME_PREFIX}MODULE_PATH})
list(APPEND CMAKE_MODULE_PATH ${${PROJECT_NAME_PREFIX}TEMP_MODULE_PATH})
# Macros about the compiler.
set(${PROJECT_NAME_PREFIX}COMPILER_ID ${CMAKE_CXX_COMPILER_ID})
set(${PROJECT_NAME_PREFIX}COMPILER_VERSION ${CMAKE_CXX_COMPILER_VERSION})
set(${PROJECT_NAME_PREFIX}COMPILER_NAME ${${PROJECT_NAME_PREFIX}COMPILER_ID}.${${PROJECT_NAME_PREFIX}COMPILER_VERSION})
if (${PROJECT_NAME_PREFIX}MASTER_PROJECT AND NOT DEFINED CMAKE_CXX_VISIBILITY_PRESET)
cmake_language(
CALL
${PROJECT_NAME_PREFIX}doc_var
CMAKE_CXX_VISIBILITY_PRESET
hidden
STRING
"Preset for the export of private symbols"
)
set_property(
CACHE CMAKE_CXX_VISIBILITY_PRESET
PROPERTY STRINGS
hidden default
)
endif ()
if (${PROJECT_NAME_PREFIX}PLATFORM_WINDOWS)
#########################
# MSVC / CLANG-CL
########################
set(${PROJECT_NAME_PREFIX}COMPILE_FLAGS "/D_CRT_SECURE_NO_WARNINGS")
if (${PROJECT_NAME_PREFIX}PEDANTIC)
list(APPEND ${PROJECT_NAME_PREFIX}COMPILE_FLAGS "/W4")
else ()
list(APPEND ${PROJECT_NAME_PREFIX}COMPILE_FLAGS "/W3")
endif (${PROJECT_NAME_PREFIX}PEDANTIC)
if (${PROJECT_NAME_PREFIX}WERROR)
list(APPEND ${PROJECT_NAME_PREFIX}COMPILE_FLAGS "/WX")
endif ()
elseif (${PROJECT_NAME_PREFIX}PLATFORM_LINUX)
#########################
# GCC / CLANG
########################
set(${PROJECT_NAME_PREFIX}COMPILE_FLAGS "-Wall")
if (${PROJECT_NAME_PREFIX}PEDANTIC)
list(APPEND ${PROJECT_NAME_PREFIX}COMPILE_FLAGS "-Wextra" "-Wpedantic")
endif (${PROJECT_NAME_PREFIX}PEDANTIC)
if (${PROJECT_NAME_PREFIX}WERROR)
list(APPEND ${PROJECT_NAME_PREFIX}COMPILE_FLAGS "-Werror")
endif ()
elseif (${PROJECT_NAME_PREFIX}PLATFORM_MACOS)
#########################
# APPLE CLANG
########################
set(${PROJECT_NAME_PREFIX}COMPILE_FLAGS "-Wall")
if (${PROJECT_NAME_PREFIX}PEDANTIC)
list(APPEND ${PROJECT_NAME_PREFIX}COMPILE_FLAGS "-Wextra" "-Wpedantic")
endif (${PROJECT_NAME_PREFIX}PEDANTIC)
if (${PROJECT_NAME_PREFIX}WERROR)
list(APPEND ${PROJECT_NAME_PREFIX}COMPILE_FLAGS "-Werror")
endif ()
else ()
message(FATAL_ERROR "Unsupported compilers: ${CMAKE_CXX_COMPILER}")
endif (${PROJECT_NAME_PREFIX}PLATFORM_WINDOWS)
if (${PROJECT_NAME_PREFIX}SYSTEM_HEADERS)
set(${PROJECT_NAME_PREFIX}HEADER_ATTRIBUTES SYSTEM)
else ()
set(${PROJECT_NAME_PREFIX}HEADER_ATTRIBUTES)
endif (${PROJECT_NAME_PREFIX}SYSTEM_HEADERS)
# HEADER FILES
set(
${PROJECT_NAME_PREFIX}HEADER
${PROJECT_SOURCE_DIR}/include/${PROJECT_NAME}/internal/common.hpp
${PROJECT_SOURCE_DIR}/include/${PROJECT_NAME}/extractor.hpp
${PROJECT_SOURCE_DIR}/include/${PROJECT_NAME}/flusher.hpp
)
# SOURCE FILES
set(
${PROJECT_NAME_PREFIX}SOURCE
${PROJECT_SOURCE_DIR}/src/impl.cpp
)
# LIBRARY
if(${PROJECT_NAME_PREFIX}BUILD_SHARED OR BUILD_SHARED_LIBS)
add_library(
${PROJECT_NAME}
SHARED
${${PROJECT_NAME_PREFIX}HEADER}
${${PROJECT_NAME_PREFIX}SOURCE}
)
target_compile_definitions(
${PROJECT_NAME}
PUBLIC
${PROJECT_NAME_PREFIX}SHARED_LIBRARY
)
else()
add_library(
${PROJECT_NAME}
STATIC
${${PROJECT_NAME_PREFIX}HEADER}
${${PROJECT_NAME_PREFIX}SOURCE}
)
endif(${PROJECT_NAME_PREFIX}BUILD_SHARED OR BUILD_SHARED_LIBS)
# LIBRARY ALIAS
add_library(
${PROJECT_NAME}::${PROJECT_NAME}
ALIAS
${PROJECT_NAME}
)
add_library(
gal::ini
ALIAS
${PROJECT_NAME}
)
# SET FLAGS
target_compile_options(
${PROJECT_NAME}
PRIVATE
${${PROJECT_NAME_PREFIX}COMPILE_FLAGS}
)
# SET DEFINITIONS
target_compile_definitions(
${PROJECT_NAME}
PUBLIC
# clang-format off
# @formatter:off
${PROJECT_NAME_PREFIX}MAJOR_VERSION=${${PROJECT_NAME_PREFIX}MAJOR_VERSION}
${PROJECT_NAME_PREFIX}MINOR_VERSION=${${PROJECT_NAME_PREFIX}MINOR_VERSION}
${PROJECT_NAME_PREFIX}PATCH_VERSION=${${PROJECT_NAME_PREFIX}PATCH_VERSION}
${PROJECT_NAME_PREFIX}VERSION="${${PROJECT_NAME_PREFIX}VERSION}"
${PROJECT_NAME_PREFIX}COMPILER_ID="${${PROJECT_NAME_PREFIX}COMPILER_ID}"
${PROJECT_NAME_PREFIX}COMPILER_VERSION="${${PROJECT_NAME_PREFIX}COMPILER_VERSION}"
${PROJECT_NAME_PREFIX}COMPILER_NAME="${${PROJECT_NAME_PREFIX}COMPILER_NAME}"
${${PROJECT_NAME_PREFIX}PLATFORM}
# Tool macros for platform determination.
$<$<CXX_COMPILER_ID:MSVC>:${PROJECT_NAME_PREFIX}COMPILER_MSVC>
$<$<CXX_COMPILER_ID:GNU>:${PROJECT_NAME_PREFIX}COMPILER_GNU>
# clang-cl
$<$<AND:$<CXX_COMPILER_ID:Clang>,$<STREQUAL:"${CMAKE_CXX_SIMULATE_ID}","MSVC">>:${PROJECT_NAME_PREFIX}COMPILER_CLANG_CL>
# clang
$<$<AND:$<CXX_COMPILER_ID:Clang>,$<NOT:$<STREQUAL:"${CMAKE_CXX_SIMULATE_ID}","MSVC">>>:${PROJECT_NAME_PREFIX}COMPILER_CLANG>
# apple clang
$<$<CXX_COMPILER_ID:AppleClang>:${PROJECT_NAME_PREFIX}COMPILER_APPLE_CLANG>
# @formatter:on
# clang-format on
)
# SET FEATURES
target_compile_features(
${PROJECT_NAME}
PUBLIC
cxx_std_20
)
# SET INCLUDE DIRECTORY
target_include_directories(
${PROJECT_NAME}
${${PROJECT_NAME_PREFIX}HEADER_ATTRIBUTES}
PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${${PROJECT_NAME_PREFIX}INSTALL_HEADERS}/${PROJECT_NAME}-${PROJECT_VERSION}>
)
# LINK 3rd-PARTY LIBRARIES
set(${PROJECT_NAME_PREFIX}3RD_PARTY_DEPENDENCIES "")
include(${${PROJECT_NAME_PREFIX}3RD_PARTY_PATH}/lexy/lexy.cmake)
message(STATUS "=======================================")
message(STATUS "[${PROJECT_NAME}] DEPENDENCIES:")
foreach (DEPENDENCY IN LISTS ${PROJECT_NAME_PREFIX}3RD_PARTY_DEPENDENCIES)
message(STATUS "-->\t\t ${DEPENDENCY}")
endforeach (DEPENDENCY IN LISTS ${PROJECT_NAME_PREFIX}3RD_PARTY_DEPENDENCIES)
message(STATUS "=======================================")
# SET DEBUG POSTFIX ==> lib${PROJECT_NAME} -> Release lib${PROJECT}d -> Debug
set(${PROJECT_NAME_PREFIX}DEBUG_POSTFIX d CACHE STRING "Debug library postfix.")
set_target_properties(
${PROJECT_NAME}
PROPERTIES
VERSION ${${PROJECT_NAME_PREFIX}VERSION}
SOVERSION ${${PROJECT_NAME_PREFIX}MAJOR_VERSION}
PUBLIC_HEADER "${${PROJECT_NAME_PREFIX}HEADER}"
DEBUG_POSTFIX "${${PROJECT_NAME_PREFIX}DEBUG_POSTFIX}")
# INSTALL TARGETS
if (${PROJECT_NAME_PREFIX}INSTALL)
# PackageProject.cmake will be used to make our target installable
CPMAddPackage("gh:TheLartians/[email protected]")
# the location where the project's version header will be placed should match the project's regular
# header paths
string(TOLOWER ${PROJECT_NAME}/version.h VERSION_HEADER_LOCATION)
packageProject(
# the name of the target to export
NAME ${PROJECT_NAME}
# the version of the target to export
VERSION ${PROJECT_VERSION}
# (optional) install your library with a namespace (Note: do NOT add extra '::')
NAMESPACE gal
# a temporary directory to create the config files
BINARY_DIR ${PROJECT_BINARY_DIR}
# location of the target's public headers
# see target_include_directories -> BUILD_INTERFACE
INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include
# should match the target's INSTALL_INTERFACE include directory
# see target_include_directories -> INSTALL_INTERFACE
INCLUDE_DESTINATION ${${PROJECT_NAME_PREFIX}INSTALL_HEADERS}/${PROJECT_NAME}-${PROJECT_VERSION}
# (optional) create a header containing the version info
# Note: that the path to headers should be lowercase
VERSION_HEADER "${VERSION_HEADER_LOCATION}"
# (optional) define the project's version compatibility, defaults to `AnyNewerVersion`
# supported values: `AnyNewerVersion|SameMajorVersion|SameMinorVersion|ExactVersion`
COMPATIBILITY AnyNewerVersion
# semicolon separated list of the project's dependencies
# see `LINK 3rd-PARTY LIBRARIES`
DEPENDENCIES ${${PROJECT_NAME_PREFIX}3RD_PARTY_DEPENDENCIES}
# (optional) option to disable the versioning of install destinations
DISABLE_VERSION_SUFFIX YES
# (optional) option to ignore target architecture for package resolution
# defaults to YES for header only (i.e. INTERFACE) libraries
ARCH_INDEPENDENT YES
)
endif (${PROJECT_NAME_PREFIX}INSTALL)
# DOCUMENTS
if (${PROJECT_NAME_PREFIX}DOC)
# add_subdirectory(doc)
endif (${PROJECT_NAME_PREFIX}DOC)
# TESTS
if (${PROJECT_NAME_PREFIX}TEST)
enable_testing()
add_subdirectory(unit_test)
endif (${PROJECT_NAME_PREFIX}TEST)