-
Notifications
You must be signed in to change notification settings - Fork 265
/
Copy pathCMakeLists.txt
331 lines (248 loc) · 11.6 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
cmake_minimum_required(VERSION 3.10)
# --- include useful utility functions
# ----------------------------------------------------------
include(cmake/utilities.cmake)
# --- extract project version
# ----------------------------------------------------------
loguru_get_version_from_header() # defines LOGURU_VERSION
# --- define project and policies
# ----------------------------------------------------------
set(_namespace loguru)
project(loguru VERSION "${LOGURU_VERSION}" LANGUAGES CXX)
set(LOGURU_PACKAGE_URL "https://github.com/emilk/loguru" CACHE STRING "")
set(LOGURU_PACKAGE_VENDOR "Emil Ernerfeldt" CACHE STRING "")
set(LOGURU_PACKAGE_CONTACT "Emil Ernerfeldt <[email protected]>" CACHE STRING "")
set(LOGURU_PACKAGE_DESCRIPTION_SUMMARY "A lightweight C++ logging library" CACHE STRING "")
set(LOGURU_PACKAGE_DESCRIPTION_FILE "${PROJECT_SOURCE_DIR}/README.md" CACHE STRING "")
# --- check if toplevel or subdirectory
# ----------------------------------------------------------
# This variable is set automatically by the project() call in CMake 3.21+
string(COMPARE EQUAL "${CMAKE_SOURCE_DIR}" "${PROJECT_SOURCE_DIR}" PROJECT_IS_TOP_LEVEL)
if (PROJECT_IS_TOP_LEVEL)
message(STATUS "Configuring ${PROJECT_NAME} as top-level")
else()
message(STATUS "Configuring ${PROJECT_NAME} as sub-directory")
endif()
# --- set default build type
# ----------------------------------------------------------
# NOTE: when running as a standalone project, we only allow Release & Debug
# but as a sub-project we don't want to accidentally pollute the parent
if (PROJECT_IS_TOP_LEVEL)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose Release or Debug" FORCE)
endif()
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Release;Debug")
endif()
# --- expose cmake-specific user options
# ----------------------------------------------------------
option(LOGURU_INSTALL "Generate the install target(s)" ${PROJECT_IS_TOP_LEVEL})
option(LOGURU_BUILD_EXAMPLES "Build the project examples" ${PROJECT_IS_TOP_LEVEL})
option(LOGURU_BUILD_TESTS "Build the tests" ${PROJECT_IS_TOP_LEVEL})
if (LOGURU_INSTALL)
option(LOGURU_CPACK "Generate CPackConfig.cmake" ${PROJECT_IS_TOP_LEVEL})
endif()
# --- set global compile flags
# ----------------------------------------------------------
if (PROJECT_IS_TOP_LEVEL)
# enable ALL warnings for all subsequently defined targets
add_compile_options(
"$<$<CXX_COMPILER_ID:GNU>:-Wall;-Wextra;-Werror;-pedantic>"
"$<$<CXX_COMPILER_ID:Clang>:-Weverything;-Wno-c++98-compat;-Wno-c++98-compat-pedantic>"
"$<$<CXX_COMPILER_ID:MSVC>:/W4>"
)
endif()
# --- add loguru target
# ----------------------------------------------------------
add_library(loguru loguru.cpp) # allow BUILD_SHARED_LIBS to decide STATIC/SHARED
if (NOT PROJECT_IS_TOP_LEVEL)
add_library(${_namespace}::loguru ALIAS loguru)
endif()
# --- determine if linking 'dl' is required
# ----------------------------------------------------------
if (LOGURU_STACKTRACES AND (NOT CMAKE_DL_LIBS))
message(WARNING
"Stack traces requested but the required 'dl' library was not found. "
"LOGURU_STACKTRACES has been automatically disabled (set to 0)"
)
set(LOGURU_STACKTRACES 0)
endif()
if (LOGURU_STACKTRACES)
set(_lib_dl_linkflag "-l${CMAKE_DL_LIBS}")
else()
set(_lib_dl_linkflag) # dl dependency is not needed if STACKTRACES=0
endif()
# --- set loguru target properties
# ----------------------------------------------------------
target_include_directories(loguru
PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
)
target_compile_features(loguru PUBLIC cxx_std_11)
find_package(Threads REQUIRED) # defines IMPORTED target Threads::Threads
target_link_libraries(loguru
PUBLIC
Threads::Threads # pthreads (or equivalent)
${_lib_dl_linkflag} # dl (or equivalent)
)
set_target_properties(loguru
PROPERTIES
VERSION "${LOGURU_VERSION}"
SOVERSION "${LOGURU_VERSION_MAJOR}"
DEBUG_POSTFIX "d"
)
target_compile_definitions(loguru
# NOTE: these generator expressions are dense but the logic is quite simple!
# if any of the cache variables are not equal to the empty string, set them as a definition.
# Additionally, the "boolean" variables are coerced into a numeric representation (1 or 0)
PUBLIC
$<$<NOT:$<STREQUAL:,${LOGURU_EXPORT}>>:LOGURU_EXPORT=${LOGURU_EXPORT}>
$<$<NOT:$<STREQUAL:,${LOGURU_DEBUG_LOGGING}>>:LOGURU_DEBUG_LOGGING=$<BOOL:${LOGURU_DEBUG_LOGGING}>>
$<$<NOT:$<STREQUAL:,${LOGURU_DEBUG_CHECKS}>>:LOGURU_DEBUG_CHECKS=$<BOOL:${LOGURU_DEBUG_CHECKS}>>
$<$<NOT:$<STREQUAL:,${LOGURU_SCOPE_TEXT_SIZE}>>:LOGURU_SCOPE_TEXT_SIZE=${LOGURU_SCOPE_TEXT_SIZE}>
$<$<NOT:$<STREQUAL:,${LOGURU_REDEFINE_ASSERT}>>:LOGURU_REDEFINE_ASSERT=$<BOOL:${LOGURU_REDEFINE_ASSERT}>>
$<$<NOT:$<STREQUAL:,${LOGURU_WITH_STREAMS}>>:LOGURU_WITH_STREAMS=$<BOOL:${LOGURU_WITH_STREAMS}>>
$<$<NOT:$<STREQUAL:,${LOGURU_REPLACE_GLOG}>>:LOGURU_REPLACE_GLOG=$<BOOL:${LOGURU_REPLACE_GLOG}>>
$<$<NOT:$<STREQUAL:,${LOGURU_USE_FMTLIB}>>:LOGURU_USE_FMTLIB=$<BOOL:${LOGURU_USE_FMTLIB}>>
$<$<NOT:$<STREQUAL:,${LOGURU_FMT_HEADER_ONLY}>>:LOGURU_FMT_HEADER_ONLY=$<BOOL:${LOGURU_FMT_HEADER_ONLY}>>
$<$<NOT:$<STREQUAL:,${LOGURU_WITH_FILEABS}>>:LOGURU_WITH_FILEABS=$<BOOL:${LOGURU_WITH_FILEABS}>>
$<$<NOT:$<STREQUAL:,${LOGURU_STACKTRACES}>>:LOGURU_STACKTRACES=$<BOOL:${LOGURU_STACKTRACES}>>
$<$<NOT:$<STREQUAL:,${LOGURU_RTTI}>>:LOGURU_RTTI=$<BOOL:${LOGURU_RTTI}>>
$<$<NOT:$<STREQUAL:,${LOGURU_FILENAME_WIDTH}>>:LOGURU_FILENAME_WIDTH=${LOGURU_FILENAME_WIDTH}>
$<$<NOT:$<STREQUAL:,${LOGURU_THREADNAME_WIDTH}>>:LOGURU_THREADNAME_WIDTH=${LOGURU_THREADNAME_WIDTH}>
$<$<NOT:$<STREQUAL:,${LOGURU_SCOPE_TIME_PRECISION}>>:LOGURU_SCOPE_TIME_PRECISION=${LOGURU_SCOPE_TIME_PRECISION}>
$<$<NOT:$<STREQUAL:,${LOGURU_VERBOSE_SCOPE_ENDINGS}>>:LOGURU_VERBOSE_SCOPE_ENDINGS=$<BOOL:${LOGURU_VERBOSE_SCOPE_ENDINGS}>>
)
# --- import and link fmt (if needed)
# ----------------------------------------------------------
if (LOGURU_USE_FMTLIB)
message(STATUS "linking to fmt")
if (NOT TARGET fmt::fmt) # only search if not already found in parent scope
find_package(fmt CONFIG REQUIRED)
endif()
if (LOGURU_FMT_HEADER_ONLY)
target_link_libraries(loguru PUBLIC fmt::fmt-header-only)
else()
target_link_libraries(loguru PUBLIC fmt::fmt)
endif()
message(STATUS "linking to fmt - done")
endif()
# --- set ide-specific properties
# ----------------------------------------------------------
# make the project the default when opened in visual studio ide
set_property(DIRECTORY ${PROJECT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT ${PROJECT_NAME})
# --- setup examples
# ----------------------------------------------------------
# TODO: make the examples work with this cmake paradigm
if (LOGURU_BUILD_EXAMPLES)
message(STATUS "!!! the examples don't work with this cmake build yet")
# message(STATUS "building examples")
# add_subdirectory(glog_bench)
# add_subdirectory(glog_example)
# add_subdirectory(loguru_bench)
# add_subdirectory(loguru_example)
# message(STATUS "building examples - done")
endif()
# --- setup tests
# ----------------------------------------------------------
# TODO: make the tests work with this cmake paradigm
if (LOGURU_BUILD_TESTS)
message(STATUS "!!! the tests don't work with this cmake build yet")
# message(STATUS "building tests")
# add_subdirectory(test)
# message(STATUS "building tests - done")
endif()
# --- setup install rules
# ----------------------------------------------------------
if (LOGURU_INSTALL)
message(STATUS "generating install rules")
# -- include modules
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)
# -- expose cache variables for users to customize install location
set(LOGURU_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" CACHE STRING
"Install directory for cmake files, relative to \${CMAKE_INSTALL_PREFIX} or an absolute path")
set(LOGURU_INSTALL_LIBDIR "${CMAKE_INSTALL_LIBDIR}" CACHE STRING
"Install directory for libraries, relative to \${CMAKE_INSTALL_PREFIX} or an absolute path")
set(LOGURU_INSTALL_INCLUDEDIR "${CMAKE_INSTALL_INCLUDEDIR}" CACHE STRING
"Install directory for include files, relative to \${CMAKE_INSTALL_PREFIX} or an absolute path")
set(LOGURU_INSTALL_PKGCONFIGDIR "${CMAKE_INSTALL_LIBDIR}/pkgconfig" CACHE STRING
"Install directory for pkgconfig (.pc) files, relative to \${CMAKE_INSTALL_PREFIX} or an absolute path")
# -- set additional target properties relevant to install dir
target_include_directories(loguru
PUBLIC
$<INSTALL_INTERFACE:${LOGURU_INSTALL_INCLUDEDIR}/loguru>
)
# -- setup install config files
set(_project_config_file_in ${PROJECT_SOURCE_DIR}/cmake/${PROJECT_NAME}-config.cmake.in)
set(_project_config_file_out ${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config.cmake)
set(_version_config_file ${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake)
set(_targets_export_name ${PROJECT_NAME}-targets)
set(_pkgconfig_file_in ${PROJECT_SOURCE_DIR}/cmake/${PROJECT_NAME}.pc.in)
set(_pkgconfig_file_out ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.pc)
# -- Configure pkg-config template
set(_pkgconfig_libdir "\${exec_prefix}/${LOGURU_INSTALL_LIBDIR}")
set(_pkgconfig_includedir "\${prefix}/${LOGURU_INSTALL_INCLUDEDIR}")
# if the user chose absolute paths, strip the ${prefix} and/or ${exec_prefix}
if (IS_ABSOLUTE "${LOGURU_INSTALL_LIBDIR}")
set(_pkgconfig_libdir "${LOGURU_INSTALL_LIBDIR}")
endif()
if (IS_ABSOLUTE "${LOGURU_INSTALL_INCLUDEDIR}")
set(_pkgconfig_includedir "${LOGURU_INSTALL_INCLUDEDIR}")
endif()
configure_file(
${_pkgconfig_file_in}
${_pkgconfig_file_out}
@ONLY
)
# -- Generate the version file in the build directory
write_basic_package_version_file( # function from CMakePackageConfigHelpers
${_version_config_file}
COMPATIBILITY SameMajorVersion
)
# -- Generate the config file in the build directory
configure_package_config_file( # function from CMakePackageConfigHelpers
${_project_config_file_in}
${_project_config_file_out}
INSTALL_DESTINATION ${LOGURU_INSTALL_CMAKEDIR}
)
# -- Install the main library
install(TARGETS loguru
EXPORT ${_targets_export_name} # Add this target to the 'exports' file
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} # .dll, .exe
ARCHIVE DESTINATION ${LOGURU_INSTALL_LIBDIR} # .lib, .a
LIBRARY DESTINATION ${LOGURU_INSTALL_LIBDIR} # .so
)
# -- Install the header file
install(FILES loguru.hpp
DESTINATION ${LOGURU_INSTALL_INCLUDEDIR}/loguru
)
# -- Install version and config files
install(FILES ${_project_config_file_out} ${_version_config_file}
DESTINATION ${LOGURU_INSTALL_CMAKEDIR}
)
# -- Install pkgconfig file
install(FILES ${_pkgconfig_file_out}
DESTINATION ${LOGURU_INSTALL_PKGCONFIGDIR}
)
# -- Install target exports file
install(EXPORT ${_targets_export_name}
NAMESPACE ${_namespace}::
DESTINATION ${LOGURU_INSTALL_CMAKEDIR}
)
# -- Install .pdb file (if exists)
if (MSVC AND BUILD_SHARED_LIBS)
install(FILES $<TARGET_PDB_FILE:loguru>
CONFIGURATIONS "Debug"
DESTINATION ${LOGURU_INSTALL_LIBDIR} OPTIONAL
)
endif()
message(STATUS "generating install rules - done")
endif() # LOGURU_INSTALL
# -- Setup CPack
# ----------------------------------------------------------
if (LOGURU_INSTALL AND LOGURU_CPACK)
message(STATUS "setting up cpack")
# NOTE: this must be the very last instruction in this file
include(cmake/${PROJECT_NAME}-cpack.cmake)
message(STATUS "setting up cpack - done")
endif()