-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
251 lines (224 loc) · 7.88 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
cmake_minimum_required(VERSION 3.25)
project(amongoc VERSION 0.1.0 DESCRIPTION "An Asynchronous MongoDB Library for C")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/tools")
# Pull our deps with PMM+vcpkg
option(AMONGOC_USE_PMM "Enable PMM execution to bootstrap dependency packages automatically" ${PROJECT_IS_TOP_LEVEL})
if(AMONGOC_USE_PMM)
include(pmm)
pmm(VCPKG REVISION 2024.08.23)
endif()
find_package(asio CONFIG REQUIRED)
find_package(neo-fun CONFIG REQUIRED)
find_package(Boost CONFIG REQUIRED URL Container)
find_package(fmt CONFIG REQUIRED)
find_package(Threads REQUIRED)
# Enable testing with CTest
# Note: We respect the BUILD_TESTING option from CTest.cmake
include(CTest)
# Support modules
include(CMakePackageConfigHelpers)
# Platform bits
include(MongoSettings)
include(MongoPlatform)
# Developer aides
include(Sanitizers)
include(Ccache)
include(LLDLinker)
include(MongoWarnings)
if(PROJECT_IS_TOP_LEVEL)
# Set compile warnings to be errors
if(MONGODB_DEVELOPER)
set(CMAKE_COMPILE_WARNING_AS_ERROR TRUE)
endif()
mongo_add_warning_options(
gnu-like:-Wall
# False-positive in GCC with coroutines and a custom allocator:
gnu-like:lang-cxx:-Wno-mismatched-new-delete
gnu-like:-Wextra
# gnu-like:-Wconversion
gnu-like:-Wsign-conversion
gnu-like:-Wsign-compare
)
endif()
# Tweak some options if we are added as a subdirectory project
if(NOT PROJECT_IS_TOP_LEVEL)
# We aren't the top-level project. Don't define test cases
set(BUILD_TESTING FALSE)
endif()
# When compiling C++ with GCC, set a deeper diagnostics depth
add_compile_options($<$<AND:$<CXX_COMPILER_ID:GNU>,$<COMPILE_LANGUAGE:CXX>>:-fconcepts-diagnostics-depth=4>)
# If we generate any DLLs, use a .dll.lib suffix for the importlibs, to distinguish
# them from actual static libraries
set(CMAKE_IMPORT_LIBRARY_SUFFIX .dll.lib)
# Debug binaries will have a `-dbg` suffix to allow multi-conf installation
set(CMAKE_DEBUG_POSTFIX -dbg)
# Collect compiled source files
file(
GLOB_RECURSE sources
CONFIGURE_DEPENDS
src/*.cpp
src/*.c
)
# Public headers files live in include/
file(
GLOB_RECURSE pub_headers
CONFIGURE_DEPENDS
include/*.h
)
# Private header files live in src/
file(
GLOB_RECURSE src_headers
CONFIGURE_DEPENDS
src/*.hpp
src/*.h
)
# Test sources files have `.test` in their stem
file(
GLOB_RECURSE test_sources
CONFIGURE_DEPENDS
src/*.test.c
src/*.test.cpp
)
# Remove the test sources from the list of library sources:
list(REMOVE_ITEM sources ${test_sources})
# Create the library from our gathered files
add_library(amongoc STATIC ${sources} ${src_headers} ${pub_headers})
add_library(amongoc::amongoc ALIAS amongoc)
# Attach headers. This also adds the include directories to the target
target_sources(amongoc
PUBLIC
FILE_SET HEADERS
BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/include/
FILES ${pub_headers}
PRIVATE
FILE_SET priv_headers
TYPE HEADERS
BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/src/
FILES ${src_headers}
)
# Enable C++23 features on the target
# We currently only use C++23 for `auto(). Future revisions
# can remove these requirements for greater portability.
target_compile_features(amongoc PUBLIC cxx_std_23)
# Link deps and platform libs.
target_link_libraries(amongoc PUBLIC
neo::fun
asio::asio
fmt::fmt
amongoc::detail::c_platform
Boost::url
Boost::container
Threads::Threads
fmt::fmt
)
add_executable(bson-use-test tests/use-bson.test.c)
target_link_libraries(bson-use-test PRIVATE amongoc::amongoc)
if(BUILD_TESTING)
# Add Catch integrations
find_package(Catch2 CONFIG REQUIRED)
include(Catch)
# Add tests
add_executable(amongoc-test ${test_sources})
target_link_libraries(amongoc-test PRIVATE amongoc Catch2::Catch2)
target_include_directories(amongoc-test PRIVATE src)
catch_discover_tests(amongoc-test DISCOVERY_MODE PRE_TEST)
# Generate URI tests
include(GenURITests)
target_sources(amongoc-test PRIVATE ${URITest_SOURCES})
# Don't add the testproject tests if we are already building as a test project
if(NOT DEFINED HOST_PROJECT_CMAKE_SOURCE_DIR)
include(TestProject)
# Pass through some variables that point to our dependency packages. The
# tests will use these same packages when importing amongoc.
# TODO: Add test cases that don't share dependencies with the parent build
set(passthru_args
PASSTHRU_VARS neo-fun_DIR asio_DIR fmt_DIR
# Boost has a lot of variables that need to be forwarded:
PASSTHRU_VARS_REGEX "[bB]oost.*(_DIR|_PATH)"
)
add_test_cmake_project(
"CMake/Consume/Import with find_package" ./tests/consume/simple
INSTALL_PARENT
SETTINGS
TEST_IMPORT_MODE=find_package
# Debug issues with find_package():
CONFIGURE_ARGS --debug-find
${passthru_args}
)
add_test_cmake_project(
"CMake/Consume/Import with FetchContent" ./tests/consume/simple
SETTINGS
TEST_IMPORT_MODE=FetchContent
${passthru_args}
)
add_test_cmake_project(
"CMake/Consume/Import with FetchContent (Git)" ./tests/consume/simple
SETTINGS
TEST_IMPORT_MODE=FetchContent-Git
${passthru_args}
)
add_test_cmake_project(
"CMake/Consume/Import with add_subdirectory" ./tests/consume/simple
SETTINGS
TEST_IMPORT_MODE=add_subdirectory
${passthru_args}
)
add_test_cmake_project(
"CMake/Consume/Import with add_subdirectory - EXCLUDE_FROM_ALL" ./tests/consume/simple
SETTINGS
TEST_IMPORT_MODE=add_subdirectory
TEST_ADD_SUBDIR_EXCLUDE_FROM_ALL=EXCLUDE_FROM_ALL
${passthru_args}
)
endif()
endif()
# Compile and link all documentation examples
file(GLOB_RECURSE examples CONFIGURE_DEPENDS docs/*.example.c docs/*.example.cpp)
foreach(filepath IN LISTS examples)
cmake_path(GET filepath STEM name)
message(STATUS "Example: ${name}")
set(exe "${name}.example")
add_executable("${exe}" ${filepath})
target_link_libraries(${exe} amongoc::amongoc)
endforeach()
if(PROJECT_IS_TOP_LEVEL OR AMONGOC_GENERATE_INSTALL_RULES)
# We base our install paths on the GNU style
include(GNUInstallDirs)
set(AMONGOC_INSTALL_CMAKEDIR
${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}-${PROJECT_VERSION}
CACHE STRING "Installation directory where CMake package files will be written"
)
# Generate the package-config files. Use camelCaseFileNames to enforce case sensitive find_package()
configure_package_config_file(
etc/amongocConfig.cmake.in
amongocConfig.cmake
INSTALL_DESTINATION "${AMONGOC_INSTALL_CMAKEDIR}"
)
write_basic_package_version_file(
amongocConfig-version.cmake
COMPATIBILITY SameMajorVersion
)
install(
TARGETS amongoc
EXPORT amongoc-targets
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
FILE_SET HEADERS DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
)
install(
FILES
${CMAKE_CURRENT_BINARY_DIR}/amongocConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/amongocConfig-version.cmake
DESTINATION "${AMONGOC_INSTALL_CMAKEDIR}"
)
install(
EXPORT amongoc-targets
DESTINATION "${AMONGOC_INSTALL_CMAKEDIR}"
NAMESPACE amongoc::
)
string(TOLOWER "${CMAKE_SYSTEM_NAME}" CPACK_SYSTEM_NAME)
string(APPEND CPACK_SYSTEM_NAME "-${CMAKE_SYSTEM_PROCESSOR}")
include(CPack)
endif()