-
Notifications
You must be signed in to change notification settings - Fork 1
/
CMakeLists.txt
396 lines (320 loc) · 11.3 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
# Copyright (c) 2014-2024 Zuru Tech HK Limited, All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
cmake_minimum_required(VERSION 3.26.0)
project(dicey VERSION 0.3.9 LANGUAGES C)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(JoinPaths)
if (NOT CMAKE_C_BYTE_ORDER STREQUAL "LITTLE_ENDIAN")
message(FATAL_ERROR "Big endian is not supported")
endif()
set(DICEY_ROOT ${CMAKE_CURRENT_LIST_DIR})
include(GenerateExportHeader)
option(BUILD_DOC "Build documentation" OFF)
option(BUILD_SAMPLES "Build samples" ON)
if (CMAKE_CROSSCOMPILING AND NOT CMAKE_SYSROOT)
set(RAW_CROSS ON)
message(STATUS "Cross compiling without a sysroot - switching to internal libraries")
endif()
set(CMAKE_C_STANDARD 17)
set(CMAKE_C_STANDARD_REQUIRED ON)
option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
option(BUILD_LIBXML2 "Always build libxml2 from source" OFF)
option(BUILD_UV "Always build libuv from source" OFF)
option(GENERATE_EXPORTS "Generate exports for PkgConfig and CMake (for systemwide installation)" OFF)
option(USE_VENDORED_LIBS "Use vendored libraries (implies BUILD_LIBXML2 and BUILD_UV)" OFF)
if (USE_VENDORED_LIBS)
set(BUILD_LIBXML2 ON)
set(BUILD_UV ON)
endif()
add_library(${PROJECT_NAME})
if (MSVC)
target_compile_options(${PROJECT_NAME} PRIVATE /W4 /WX)
include(CheckCSourceCompiles)
set(C11_ATOMICS_TEST "int main(void) { _Atomic int x = 2; x += 1; return x; }")
check_c_source_compiles("${C11_ATOMICS_TEST}" DICEY_MSVC_HAS_C11_ATOMICS)
if (NOT DICEY_MSVC_HAS_C11_ATOMICS)
set(CMAKE_REQUIRED_FLAGS /experimental:c11atomics)
check_c_source_compiles("${C11_ATOMICS_TEST}" DICEY_MSVC_HAS_C11_EXPERIMENTAL_ATOMICS)
if (DICEY_MSVC_HAS_C11_EXPERIMENTAL_ATOMICS)
target_compile_options(${PROJECT_NAME} PRIVATE /experimental:c11atomics)
else()
message(FATAL_ERROR "MSVC is too old - no C11 atomics support")
endif()
endif()
else()
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra -pedantic -Werror)
endif()
if (NOT BUILD_UV)
if (DEFINED UV_INCLUDE_DIRS AND DEFINED UV_LIBS)
target_include_directories(${PROJECT_NAME} PUBLIC ${UV_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} PUBLIC ${UV_LIBS})
set(UV_FOUND TRUE)
elseif (NOT DEFINED RAW_CROSS)
find_package(UV QUIET)
if (UV_FOUND)
target_link_libraries(${PROJECT_NAME} PUBLIC uv)
endif()
endif()
endif()
if (NOT UV_FOUND)
include(FetchContent)
# I have no tricks left in my bag, so I'll do this the stupid way
set(LIBUV_BUILD_SHARED ${BUILD_SHARED_LIBS} CACHE BOOL "Build libuv as a shared library" FORCE)
set(LIBUV_BUILD_TESTS OFF CACHE BOOL "Do not build libuv tests" FORCE)
FetchContent_Declare(
libuv
GIT_REPOSITORY https://github.com/libuv/libuv.git
GIT_TAG v1.48.0
)
FetchContent_MakeAvailable(libuv)
if (LIBUV_BUILD_SHARED)
target_link_libraries(${PROJECT_NAME} PUBLIC uv)
else()
target_link_libraries(${PROJECT_NAME} PUBLIC uv_a)
endif()
endif()
if (NOT BUILD_LIBXML2)
find_package(LibXml2 QUIET)
if (LibXml2_FOUND)
target_link_libraries(${PROJECT_NAME} PUBLIC LibXml2::LibXml2)
endif()
endif()
if (NOT LibXml2_FOUND)
include(FetchContent)
set(LIBXML2_WITH_ICONV OFF)
set(LIBXML2_WITH_ICU OFF)
set(LIBXML2_WITH_LZMA OFF)
set(LIBXML2_WITH_PYTHON OFF)
set(LIBXML2_WITH_SCHEMAS ON)
set(LIBXML2_WITH_TESTS OFF)
set(LIBXML2_WITH_THREADS ON)
set(LIBXML2_WITH_ZLIB OFF)
FetchContent_Declare(
libxml2
GIT_REPOSITORY https://gitlab.gnome.org/GNOME/libxml2.git
GIT_TAG v2.13.1
)
FetchContent_MakeAvailable(libxml2)
target_link_libraries(${PROJECT_NAME} PUBLIC LibXml2::LibXml2)
endif()
if (NOT CMAKE_BUILD_TYPE STREQUAL "Release")
if (WIN32)
target_link_libraries(${PROJECT_NAME} PUBLIC dbghelp)
endif()
if (DEFINED BSD)
target_link_libraries(${PROJECT_NAME} PUBLIC execinfo)
endif()
endif()
generate_export_header(${PROJECT_NAME})
set(PUBLIC_HEADERS
"include/dicey/dicey.h"
# internal but public
# core
"include/dicey/core/builders.h"
"include/dicey/core/data-info.h"
"include/dicey/core/errors.h"
"include/dicey/core/hashset.h"
"include/dicey/core/hashtable.h"
"include/dicey/core/packet.h"
"include/dicey/core/type.h"
"include/dicey/core/typedescr.h"
"include/dicey/core/value.h"
"include/dicey/core/version.h"
"include/dicey/core/views.h"
# ipc
"include/dicey/ipc/address.h"
"include/dicey/ipc/builtins.h"
"include/dicey/ipc/client.h"
"include/dicey/ipc/registry.h"
"include/dicey/ipc/server.h"
"include/dicey/ipc/traits.h"
# ipc/builtins
"include/dicey/ipc/builtins/introspection.h"
"include/dicey/ipc/builtins/server.h"
)
set(CORE_SOURCES
# common
src/errors.c
src/version.c
# wirefmt
src/wirefmt/builders.c
src/wirefmt/packet-args.c
src/wirefmt/packet-args.h
src/wirefmt/packet.c
src/wirefmt/uuid.c
src/wirefmt/value-validate.c
src/wirefmt/value.c
# wirefmt/dtf
src/wirefmt/dtf/dtf.h
src/wirefmt/dtf/payload.c
src/wirefmt/dtf/payload.h
src/wirefmt/dtf/to.h
src/wirefmt/typedescr.c
src/wirefmt/dtf/value-probe.c
src/wirefmt/dtf/value-write.c
src/wirefmt/dtf/value.h
src/wirefmt/dtf/writer.c
src/wirefmt/dtf/writer.h
# sup
src/sup/assume.h
src/sup/hashset.c
src/sup/hashtable.c
src/sup/trace.c
src/sup/trace.h
src/sup/unsafe.c
src/sup/unsafe.h
src/sup/util.c
src/sup/util.h
src/sup/view-ops.c
src/sup/view-ops.h
# ipc
src/ipc/address.c
src/ipc/chunk.c
src/ipc/chunk.h
src/ipc/elemdescr.c
src/ipc/elemdescr.h
src/ipc/queue.c
src/ipc/queue.h
src/ipc/uvtools.c
src/ipc/uvtools.h
# ipc/client
src/ipc/client/client.c
src/ipc/client/waiting-list.c
src/ipc/client/waiting-list.h
# ipc/server
src/ipc/server/client-data.c
src/ipc/server/client-data.h
src/ipc/server/pending-reqs.c
src/ipc/server/pending-reqs.h
src/ipc/server/registry.c
src/ipc/server/registry-internal.h
src/ipc/server/server.c
src/ipc/server/shared-packet.c
src/ipc/server/shared-packet.h
src/ipc/server/traits.c
# ipc/server/builtins
src/ipc/server/builtins/builtins.c
src/ipc/server/builtins/builtins.h
# ipc/server/builtins/introspection
src/ipc/server/builtins/introspection/commons.c
src/ipc/server/builtins/introspection/definitions.c
src/ipc/server/builtins/introspection/introspection.h
src/ipc/server/builtins/introspection/introspection-internal.h
src/ipc/server/builtins/introspection/registry.c
src/ipc/server/builtins/introspection/traits.c
src/ipc/server/builtins/introspection/xmlgen.c
# ipc/server/builtins/server
src/ipc/server/builtins/server/server.c
src/ipc/server/builtins/server/server.h
# ipc/tasks
src/ipc/tasks/io.c
src/ipc/tasks/io.h
src/ipc/tasks/list.c
src/ipc/tasks/list.h
src/ipc/tasks/loop.c
src/ipc/tasks/loop.h
)
if (WIN32)
list(APPEND CORE_SOURCES src/sup/asprintf.c)
endif()
target_sources(
${PROJECT_NAME}
PRIVATE ${CORE_SOURCES}
PUBLIC
FILE_SET headers
TYPE HEADERS
BASE_DIRS include
FILES ${PUBLIC_HEADERS}
)
target_include_directories(${PROJECT_NAME}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
set_target_properties(${PROJECT_NAME} PROPERTIES POSITION_INDEPENDENT_CODE ${BUILD_SHARED_LIBS})
if(UNIX)
include(GNUInstallDirs)
else()
if (WIN32)
set(${CMAKE_INSTALL_LIBDIR} "lib")
set(${CMAKE_INSTALL_DATADIR} "share")
set(${CMAKE_INSTALL_INCLUDEDIR} "include")
set(${CMAKE_INSTALL_BINDIR} "bin")
message(STATUS "Setting installation destination on Windows to: ${CMAKE_INSTALL_PREFIX}")
else()
message(FATAL_ERROR "System not UNIX nor WIN32 - not implemented yet")
endif()
endif()
set(targets_export_name dicey-targets)
install(TARGETS dicey EXPORT ${targets_export_name} FILE_SET headers)
install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}_export.h" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
# this probably only works on Linux/BSD with systemwide libraries - which is what's useful for 99% of the time
if (GENERATE_EXPORTS)
include(CMakePackageConfigHelpers)
set(CMAKE_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/dicey CACHE STRING "Installation directory for cmake files")
set(version_config ${PROJECT_BINARY_DIR}/dicey-config-version.cmake)
set(project_config ${PROJECT_BINARY_DIR}/dicey-config.cmake)
set(pkgconfig ${PROJECT_BINARY_DIR}/dicey.pc)
set(DICEY_VERSION ${CMAKE_PROJECT_VERSION})
set(PKGCONFIG_DIR ${CMAKE_INSTALL_LIBDIR}/pkgconfig CACHE STRING "Installation directory for pkgconfig (.pc) files")
# Generate the version, config and target files into the build directory.
write_basic_package_version_file(
${version_config}
VERSION ${DICEY_VERSION}
COMPATIBILITY AnyNewerVersion
)
set(DICEY_LIB_NAME ${PROJECT_NAME})
join_paths(libdir_for_pc_file "\${exec_prefix}" "${CMAKE_INSTALL_LIBDIR}")
join_paths(includedir_for_pc_file "\${prefix}" "${CMAKE_INSTALL_INCLUDEDIR}")
configure_file(
"${PROJECT_SOURCE_DIR}/cmake/dicey.pc.in"
"${pkgconfig}"
@ONLY
)
configure_package_config_file(
${PROJECT_SOURCE_DIR}/cmake/dicey-config.cmake.in
${project_config}
INSTALL_DESTINATION ${CMAKE_DIR}
)
export(TARGETS dicey NAMESPACE dicey:: FILE ${PROJECT_BINARY_DIR}/${targets_export_name}.cmake)
set(find_uv "${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindUV.cmake")
# Install version, config and target files.
install(
FILES ${project_config} ${version_config} ${find_uv}
DESTINATION ${CMAKE_DIR}
)
install(EXPORT ${targets_export_name} DESTINATION ${CMAKE_DIR} NAMESPACE dicey::)
install(FILES "${pkgconfig}" DESTINATION "${PKGCONFIG_DIR}")
endif()
if (BUILD_SAMPLES)
add_subdirectory(samples)
endif()
if (BUILD_DOC)
find_package(Doxygen)
if (NOT DOXYGEN_FOUND)
message(FATAL_ERROR "Doxygen is needed to build the documentation.")
endif()
set(DOXYGEN_IN ${CMAKE_CURRENT_SOURCE_DIR}/docs/Doxyfile.in)
set(DOXYGEN_OUT ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)
configure_file(${DOXYGEN_IN} ${DOXYGEN_OUT} @ONLY)
add_custom_target(doxygen
COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_OUT}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Generating API documentation with Doxygen"
ERBATIM
)
endif()