forked from guangie88/spdlog_setup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
189 lines (153 loc) · 7.54 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
cmake_minimum_required(VERSION 3.24)
# project variables
project(spdlog_setup
VERSION 0.3.3
HOMEPAGE_URL "https://github.com/paulbuechner/spdlog_setup"
DESCRIPTION "Header-only spdlog file-based setup library for convenience in initializing spdlog. Inspired by spdlog-config for using TOML configuration, a format that is simple and easy-to-read."
LANGUAGES CXX)
set(SPDLOG_MIN_VERSION "1.0.0")
set(CPPTOML_MIN_VERSION "0.1.0")
# Needed to set the namespace for both the export targets and the alias libraries
set(cmake_package_name spdlog_setup CACHE INTERNAL "")
# ---------------------------------------------------------------------------------------
# Compiler config
# ---------------------------------------------------------------------------------------
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
endif()
# make sure __cplusplus is defined when using msvc and enable parallel build
if(MSVC)
string(APPEND CMAKE_CXX_FLAGS " /Zc:__cplusplus /MP")
endif()
# general compiler settings
if(MSVC)
set(DEBUG_FLAGS /W4)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj")
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
# using regular Clang or AppleClang
set(DEBUG_FLAGS -Wall)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++")
else()
set(DEBUG_FLAGS -Wall)
endif()
add_compile_options("$<$<CONFIG:DEBUG>:${DEBUG_FLAGS}>")
#---------------------------------------------------------------------------------------
# Set SPDLOG_SETUP_MASTER_PROJECT to ON if we are building spdlog_setup
#---------------------------------------------------------------------------------------
# Check if spdlog_setup is being used directly or via add_subdirectory, but allow overriding
if(NOT DEFINED SPDLOG_SETUP_MASTER_PROJECT)
if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
set(SPDLOG_SETUP_MASTER_PROJECT ON)
else()
set(SPDLOG_SETUP_MASTER_PROJECT OFF)
endif()
endif()
include(GNUInstallDirs)
option(SPDLOG_SETUP_INSTALL "Generate the install target" ${SPDLOG_SETUP_MASTER_PROJECT})
option(SPDLOG_SETUP_INCLUDE_UNIT_TESTS "Build with unittests." OFF)
option(SPDLOG_SETUP_CPPTOML_EXTERNAL "Use external cpptoml library instead of bundled." OFF)
# allow thread library to be used for linking
set(THREADS_PREFER_PTHREAD_FLAG ON)
set(THREADS_PTHREAD_ARG "0" CACHE STRING "Result from TRY_RUN" FORCE)
find_package(Threads REQUIRED)
# add library
add_library(${PROJECT_NAME} INTERFACE)
add_library(${cmake_package_name}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
target_include_directories(${PROJECT_NAME} INTERFACE
"$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
"$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>"
)
# Use local spdlog if it exists (development), otherwise use installed version
if(EXISTS ${CMAKE_SOURCE_DIR}/deps/spdlog/CMakeLists.txt)
add_subdirectory(deps/spdlog)
else()
find_package(spdlog ${SPDLOG_MIN_VERSION} CONFIG REQUIRED)
endif()
# ---------------------------------------------------------------------------------------
# Use cpptoml package if using external cpptoml
# ---------------------------------------------------------------------------------------
if(SPDLOG_SETUP_CPPTOML_EXTERNAL)
if(NOT TARGET cpptoml)
# Use local cpptoml if it exists (development), otherwise use installed version
if(EXISTS ${CMAKE_SOURCE_DIR}/deps/cpptoml/CMakeLists.txt)
add_subdirectory(deps/cpptoml)
else()
find_package(cpptoml ${CPPTOML_MIN_VERSION} CONFIG REQUIRED)
endif()
endif()
target_compile_definitions(${PROJECT_NAME} INTERFACE SPDLOG_SETUP_CPPTOML_EXTERNAL)
target_link_libraries(${PROJECT_NAME} INTERFACE cpptoml)
set(PKG_CONFIG_REQUIRES cpptoml) # add dependency to pkg-config
endif()
# ---------------------------------------------------------------------------------------
# Install
# ---------------------------------------------------------------------------------------
if(SPDLOG_SETUP_INSTALL)
message(STATUS "Generating install")
set(project_config_in "${CMAKE_CURRENT_SOURCE_DIR}/cmake/${PROJECT_NAME}-config.cmake.in")
set(project_config_out "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config.cmake")
set(config_targets_file "${PROJECT_NAME}-targets.cmake")
set(version_config_file "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake")
set(export_dest_dir "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
# ---------------------------------------------------------------------------------------
# Include files
# ---------------------------------------------------------------------------------------
install(DIRECTORY include/ DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" PATTERN "cpptoml" EXCLUDE)
install(
TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
if(NOT SPDLOG_SETUP_CPPTOML_EXTERNAL)
install(DIRECTORY include/${PROJECT_NAME}/cpptoml/
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}/cpptoml/")
endif()
# ---------------------------------------------------------------------------------------
# Install CMake config files
# ---------------------------------------------------------------------------------------
export(TARGETS ${PROJECT_NAME} NAMESPACE ${PROJECT_NAME}:: FILE "${CMAKE_CURRENT_BINARY_DIR}/${config_targets_file}")
install(EXPORT ${PROJECT_NAME} DESTINATION ${export_dest_dir} NAMESPACE ${PROJECT_NAME}:: FILE ${config_targets_file})
include(CMakePackageConfigHelpers)
configure_package_config_file("${project_config_in}" "${project_config_out}" INSTALL_DESTINATION ${export_dest_dir})
write_basic_package_version_file("${version_config_file}" COMPATIBILITY SameMajorVersion)
install(FILES "${project_config_out}" "${version_config_file}" DESTINATION "${export_dest_dir}")
# ---------------------------------------------------------------------------------------
# Support creation of installable packages
# ---------------------------------------------------------------------------------------
include(cmake/spdlog_setup-cpack.cmake)
endif()
# spdlog_setup_unit_test
if(SPDLOG_SETUP_INCLUDE_UNIT_TESTS)
FILE(GLOB unit_test_cpps src/unit_test/*.cpp)
add_executable(spdlog_setup_unit_test
${unit_test_cpps})
set_property(TARGET spdlog_setup_unit_test PROPERTY CXX_STANDARD 20)
enable_testing()
add_test(spdlog_setup_unit_test spdlog_setup_unit_test -s)
# Use local Catch2 if it exists (development), otherwise use installed version
if(EXISTS ${CMAKE_SOURCE_DIR}/deps/Catch2/CMakeLists.txt)
add_subdirectory(deps/Catch2)
else()
# allow usage of installed dependency
find_package(Catch2 CONFIG REQUIRED)
endif()
if(SPDLOG_SETUP_CPPTOML_EXTERNAL)
target_compile_definitions(spdlog_setup_unit_test PRIVATE SPDLOG_SETUP_CPPTOML_EXTERNAL)
target_link_libraries(spdlog_setup_unit_test PRIVATE
cpptoml)
endif()
target_link_libraries(spdlog_setup_unit_test PRIVATE
spdlog_setup
spdlog::spdlog_header_only
Catch2::Catch2WithMain
Threads::Threads)
if(SPDLOG_SETUP_INCLUDE_TEST_COVERAGE)
target_compile_options(spdlog_setup_unit_test PRIVATE
-coverage)
target_link_libraries(spdlog_setup_unit_test PRIVATE
--coverage)
endif()
endif()