Repository navigation
Expand file tree
/
Copy pathCoreCppModules.cmake
More file actions
321 lines (300 loc) · 18.4 KB
/
Copy pathCoreCppModules.cmake
File metadata and controls
321 lines (300 loc) · 18.4 KB
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
# SPDX-License-Identifier: Apache-2.0
#
# The module table: every directory under src/core that builds a library, in
# dependency order (Part I §1).
#
# core_cpp_module(NAME <name> [DIR <directory under src/core>] KIND STATIC|INTERFACE
# [DEPS <module>...] PLATFORMS any|native|wasm-subset [wasi] [WHEN <option>])
#
# DIR defaults to NAME. DEPS lists the modules this one may link, and every one of
# them must appear in an earlier row. WHEN names the option that has to be ON for
# the module to build. PLATFORMS says what of the module an Emscripten build has
# (Part I §1):
#
# any all of it: SOURCES, plus SOURCES_EMSCRIPTEN;
# wasm-subset only its SOURCES_EMSCRIPTEN, which lists the subset;
# native none of it: the module is skipped.
#
# and a trailing `wasi` says the row builds under WASI (wasm32-wasip1) too, from its SOURCES,
# SOURCES_WASI and SOURCES_PORTABLE. WASI builds no row without it: the mark is given to a row once
# it is known to compile, link and run there (core-cpp.tui-core-wasi), not assumed of every `any`.
# Only `any` takes it.
#
# core_cpp_add_module() and core_cpp_add_test() apply it (cmake/CoreCppTargets.cmake).
#
# core_cpp_module_target(NAME <target> MODULE <module> KIND STATIC|INTERFACE|OBJECT
# [DEPS <target or module>...]
# PLATFORMS any|native|wasm-subset [wasi] [WHEN <option>])
#
# A module builds the target named after it, and may build more in the same directory
# and namespace (core::net_types and core::net_tls beside core::net). Such a target
# follows its module's row unless it has a row of its own, which it needs where that
# row does not describe it: a part that builds under Emscripten while the rest of the
# module does not, a part that needs an option the module does not, or a part that
# links less than the module does. Its PLATFORMS and WHEN then apply to it alone, and
# its KIND is checked like the module's.
#
# A row's DEPS are all that its target may link, and a row without DEPS links no
# core-cpp target at all. Each entry is another target of the same module, by its
# name (the module's own, or one with an earlier row), or a module the module's row
# lists in DEPS, whose targets it may then link; anything else is refused here. A
# target that follows its module's row links what that row lists and the module's
# other targets.
#
# core_cpp_add_modules() walks the rows in order and enters each enabled module's
# directory. That directory declares its targets with core_cpp_add_module(), which
# holds them to the row: the KIND of the target named after the module or with a row
# of its own, the DEPS every core::<x> it links must come from, and whether the
# target builds here at all.
include_guard(GLOBAL)
## @brief Refuses a row whose WHEN @p when names a variable nothing has declared, for @p where.
##
## A misspelled WHEN does not fail: it disappears. `core_cpp_row_builds()` evaluates
## `if(when AND NOT ${when})`, so `WHEN CORE_CPP_WITH_TSL` expands to `NOT` an undefined variable,
## which is true, and the row stops building. The target goes, every test `core_cpp_add_test()`
## registers against it goes with it, and nothing says so: it configures clean, it builds clean,
## and a module is simply not there. A guard whose misspelling passes is worse than one that is
## wrong out loud, because it is indistinguishable from a working one and nobody looks at it again.
##
## The test is DEFINED rather than "is an `option()`", and that is a decision rather than laziness.
## `CORE_CPP_USE_THREADS` is a plain `set()` in CoreCppDependencies.cmake and is already used as a
## WHEN there, so an option()-only rule would refuse a condition this build uses today -- a rule
## born needing a workaround, which is a rule people route around and then stop reading. What is
## being closed is a typo, and a typo is undefined by construction: DEFINED refuses exactly that
## and nothing else. A row may legitimately be OFF; it may not be conditional on nothing.
##
## Declaration time is the right moment because CoreCppOptions.cmake and CoreCppDependencies.cmake
## are both included before this file, so every name a row may reasonably use already exists, and
## the error can point at the row rather than at the first target that consulted it.
function(core_cpp_check_when where when)
if(when AND NOT DEFINED ${when})
message(FATAL_ERROR
"${where}: WHEN names '${when}', which no option, cache entry or variable declares. "
"A row whose WHEN is undefined does not fail -- it silently stops building, taking "
"its target and every test registered against it with it. Declare the option, or fix "
"the spelling.")
endif()
endfunction()
## @brief Refuses a PLATFORMS @p platforms that is not one of any, native and wasm-subset, optionally
## followed by wasi, for @p where. wasi goes only with any: what WASI compiles of a row is SOURCES,
## SOURCES_WASI and SOURCES_PORTABLE, all of it, so a row with a subset or none has nothing to give it.
function(core_cpp_check_platforms where platforms)
if(NOT platforms MATCHES "^(any|native|wasm-subset)(;wasi)?$" OR platforms MATCHES "^(native|wasm-subset);wasi$")
message(FATAL_ERROR
"${where}: PLATFORMS must be any, native or wasm-subset, the first optionally followed by wasi, "
"not '${platforms}'.")
endif()
endfunction()
function(core_cpp_module)
cmake_parse_arguments(PARSE_ARGV 0 arg "" "NAME;DIR;KIND;WHEN" "DEPS;PLATFORMS")
if(arg_UNPARSED_ARGUMENTS)
message(FATAL_ERROR "core_cpp_module(${arg_NAME}): unexpected arguments: ${arg_UNPARSED_ARGUMENTS}")
endif()
if(NOT arg_NAME)
message(FATAL_ERROR "core_cpp_module(): NAME is required.")
endif()
if(NOT arg_KIND MATCHES "^(STATIC|INTERFACE)$")
message(FATAL_ERROR "core_cpp_module(${arg_NAME}): KIND must be STATIC or INTERFACE, not '${arg_KIND}'.")
endif()
core_cpp_check_when("core_cpp_module(${arg_NAME})" "${arg_WHEN}")
core_cpp_check_platforms("core_cpp_module(${arg_NAME})" "${arg_PLATFORMS}")
if(arg_NAME IN_LIST CORE_CPP_MODULES)
message(FATAL_ERROR "core_cpp_module(${arg_NAME}): declared twice.")
endif()
foreach(dep IN LISTS arg_DEPS)
if(NOT dep IN_LIST CORE_CPP_MODULES)
message(FATAL_ERROR
"core_cpp_module(${arg_NAME}): DEPS names '${dep}', which is not declared in an earlier row.")
endif()
endforeach()
if(NOT arg_DIR)
set(arg_DIR "${arg_NAME}")
endif()
set(CORE_CPP_MODULES ${CORE_CPP_MODULES} ${arg_NAME} PARENT_SCOPE)
foreach(field IN ITEMS DIR KIND DEPS PLATFORMS WHEN)
set(CORE_CPP_MODULE_${arg_NAME}_${field} "${arg_${field}}" PARENT_SCOPE)
endforeach()
set(CORE_CPP_MODULE_${arg_NAME}_TARGETS "" PARENT_SCOPE)
endfunction()
function(core_cpp_module_target)
cmake_parse_arguments(PARSE_ARGV 0 arg "" "NAME;MODULE;KIND;WHEN" "DEPS;PLATFORMS")
if(arg_UNPARSED_ARGUMENTS)
message(FATAL_ERROR "core_cpp_module_target(${arg_NAME}): unexpected arguments: ${arg_UNPARSED_ARGUMENTS}")
endif()
if(NOT arg_NAME OR NOT arg_MODULE)
message(FATAL_ERROR "core_cpp_module_target(): NAME and MODULE are required.")
endif()
if(NOT arg_MODULE IN_LIST CORE_CPP_MODULES)
message(FATAL_ERROR
"core_cpp_module_target(${arg_NAME}): MODULE names '${arg_MODULE}', which is not declared in an earlier row.")
endif()
if(arg_NAME IN_LIST CORE_CPP_MODULES OR DEFINED CORE_CPP_TARGET_${arg_NAME}_MODULE)
message(FATAL_ERROR "core_cpp_module_target(${arg_NAME}): declared twice.")
endif()
core_cpp_check_when("core_cpp_module_target(${arg_NAME})" "${arg_WHEN}")
if(NOT arg_KIND MATCHES "^(STATIC|INTERFACE|OBJECT)$")
message(FATAL_ERROR
"core_cpp_module_target(${arg_NAME}): KIND must be STATIC, INTERFACE or OBJECT, not '${arg_KIND}'.")
endif()
core_cpp_check_platforms("core_cpp_module_target(${arg_NAME})" "${arg_PLATFORMS}")
foreach(dep IN LISTS arg_DEPS)
if(dep STREQUAL arg_MODULE OR CORE_CPP_TARGET_${dep}_MODULE STREQUAL arg_MODULE
OR dep IN_LIST CORE_CPP_MODULE_${arg_MODULE}_DEPS)
continue()
endif()
message(FATAL_ERROR
"core_cpp_module_target(${arg_NAME}): DEPS names '${dep}', which is neither a target of module "
"'${arg_MODULE}' declared in an earlier row nor a module the '${arg_MODULE}' row lists in DEPS "
"(it lists: '${CORE_CPP_MODULE_${arg_MODULE}_DEPS}').")
endforeach()
set(CORE_CPP_MODULE_${arg_MODULE}_TARGETS ${CORE_CPP_MODULE_${arg_MODULE}_TARGETS} ${arg_NAME} PARENT_SCOPE)
foreach(field IN ITEMS MODULE KIND DEPS PLATFORMS WHEN)
set(CORE_CPP_TARGET_${arg_NAME}_${field} "${arg_${field}}" PARENT_SCOPE)
endforeach()
endfunction()
## @brief Enters the directory of every module the configuration enables, in table order.
##
## A module whose own row does not build here -- its WHEN option is OFF, or it is native-only
## in an Emscripten build -- is skipped, unless a target of it has a row of its own that does
## build, in which case the directory is entered and only those targets are created. That is
## what a row of its own promises: its PLATFORMS and WHEN apply to it alone, so the module's
## row switching off must not take the target with it (core::tui_output, whose module is off
## with CORE_CPP_WITH_TUI while the leaf is on with CORE_CPP_WITH_TUI_OUTPUT).
##
## An enabled module whose DEPS include a skipped one stops the configure. For a module entered
## for some of its targets only, the DEPS checked are those targets' rows', not the module's.
function(core_cpp_add_modules)
set(enabled "")
foreach(module IN LISTS CORE_CPP_MODULES)
set(when "${CORE_CPP_MODULE_${module}_WHEN}")
core_cpp_row_builds("${CORE_CPP_MODULE_${module}_PLATFORMS}" "${when}" moduleBuilds)
set(only "")
set(deps ${CORE_CPP_MODULE_${module}_DEPS})
if(NOT moduleBuilds)
if(when AND NOT ${when})
set(reason "${when}=OFF")
elseif(CORE_CPP_PLATFORM_WASI)
set(reason "its row is not marked wasi, and this is WASI")
else()
set(reason "native only, and this is Emscripten")
endif()
set(deps "")
foreach(target IN LISTS CORE_CPP_MODULE_${module}_TARGETS)
core_cpp_row_builds("${CORE_CPP_TARGET_${target}_PLATFORMS}" "${CORE_CPP_TARGET_${target}_WHEN}" builds)
if(NOT builds)
continue()
endif()
list(APPEND only core::${target})
# A row's DEPS name modules and targets of its own module; only the modules are
# entered by this function, so only they can be missing.
foreach(dep IN LISTS CORE_CPP_TARGET_${target}_DEPS)
if(dep IN_LIST CORE_CPP_MODULES AND NOT dep STREQUAL module)
list(APPEND deps ${dep})
endif()
endforeach()
endforeach()
if(NOT only)
message(STATUS "[core-cpp] module ${module}: off (${reason})")
continue()
endif()
endif()
foreach(dep IN LISTS deps)
if(NOT dep IN_LIST enabled)
message(FATAL_ERROR
"[core-cpp] module ${module} is enabled but depends on module ${dep}, which is not. "
"Enable ${dep} (see its row in cmake/CoreCppModules.cmake) or turn ${module} off.")
endif()
endforeach()
# A vendored copy may leave a module's directory out (docs/vendoring.md). Without this, the
# add_subdirectory() below fails naming a path, and nothing says which option would have
# switched the module off -- the one thing the consumer has to know.
if(NOT EXISTS "${CORE_CPP_SOURCE_DIR}/src/core/${CORE_CPP_MODULE_${module}_DIR}/CMakeLists.txt")
if(when)
set(remedy "Set ${when}=OFF before adding core-cpp, or copy the module in.")
else()
set(remedy "No option switches it off, so the copy has to carry it.")
endif()
message(FATAL_ERROR
"[core-cpp] module ${module} is enabled but its directory src/core/${CORE_CPP_MODULE_${module}_DIR} "
"is missing from ${CORE_CPP_SOURCE_DIR}. ${remedy}")
endif()
set(CORE_CPP_CURRENT_MODULE "${module}")
add_subdirectory("${CORE_CPP_SOURCE_DIR}/src/core/${CORE_CPP_MODULE_${module}_DIR}"
"${CORE_CPP_BINARY_DIR}/src/core/${CORE_CPP_MODULE_${module}_DIR}")
list(APPEND enabled ${module})
if(only)
list(JOIN only ", " only)
message(STATUS "[core-cpp] module ${module}: ${only} only (the rest is off: ${reason})")
else()
message(STATUS "[core-cpp] module ${module}: on")
endif()
endforeach()
endfunction()
# --- the table -----------------------------------------------------------------
# The headers directly in src/core/ are core::base, namespace core.
core_cpp_module(NAME base DIR . KIND STATIC PLATFORMS any wasi)
core_cpp_module(NAME log KIND STATIC DEPS base PLATFORMS any)
core_cpp_module(NAME cli KIND STATIC DEPS base log PLATFORMS any)
# A tolerant field codec: varint streams and the structs read and written over them.
core_cpp_module(NAME codec KIND STATIC DEPS base PLATFORMS any)
# No WHEN: core::testing needs no test framework and consumers use it with CORE_CPP_TESTING off.
# Only core::testing_main needs Catch2, and CORE_CPP_CATCH2_MAIN gates that one target; it links
# log to apply the LOG filter.
core_cpp_module(NAME testing KIND STATIC DEPS base log PLATFORMS any)
# Header-only but for the thread-local state its headers share (src/core/async/CMakeLists.txt), and
# needing nothing but the standard library.
core_cpp_module(NAME async KIND STATIC PLATFORMS any)
# Under single-threaded Emscripten only Types, PlatformError, Clock, StringUtils, PathUtils,
# GlobMatch, FileUri and the POSIX providers build: see its SOURCES_EMSCRIPTEN.
core_cpp_module(NAME platform KIND STATIC DEPS base log PLATFORMS wasm-subset)
# The event loop, the backends, the sockets and the HTTP server. PLATFORMS wasm-subset: under
# single-threaded Emscripten only its SOURCES_EMSCRIPTEN builds -- the IoBackend contract and the
# host-driven backend, which is pumped by the browser rather than blocking a thread it does not own
# (Task B3). The loop, its timers and the sockets join in Tasks B4 and B5. Its error vocabulary,
# core::net_types, is header-only, links nothing and builds everywhere; core::net_tls is the part
# that needs OpenSSL, on top of core::net, and stays native.
core_cpp_module(NAME net KIND STATIC DEPS async platform PLATFORMS wasm-subset)
core_cpp_module_target(NAME net_types MODULE net KIND INTERFACE PLATFORMS any)
core_cpp_module_target(NAME net_tls MODULE net KIND STATIC DEPS net PLATFORMS native WHEN CORE_CPP_WITH_TLS)
# The descriptor-relative file system: every operation an async::Task, every blocking call offloaded to
# an injected executor (async::offload), and no path resolved outside the directory it is relative to.
# Its own module rather than more of platform, which has no async edge. Native only: Emscripten and
# WASI have no file descriptors to hold.
core_cpp_module(NAME fs KIND STATIC DEPS base log async platform PLATFORMS native WHEN CORE_CPP_WITH_FS)
# HTTP/1.1 (RFC 9110/9112): the message vocabulary, the framing rules, the head parser and the
# chunked codec, a pure layer no socket enters. async and net are its edges for what runs over
# core::net's sockets; the error payload already names net::TlsFailure. Native only, like those
# sockets. Its own module rather than more of net, whose serve()/HttpServer.hpp it leaves untouched.
core_cpp_module(NAME http KIND STATIC DEPS base async net PLATFORMS native WHEN CORE_CPP_WITH_HTTP)
# endo's terminal UI. Native only: there is no terminal under Emscripten, where CORE_CPP_WITH_TUI is
# forced off. core::tui_output is the part that composes and writes bytes, and it has a row of its
# own because it links less than the module does: base alone, no libunicode and no coroutines, so a
# program that only prints styled text (Lightweight's dbtool) takes nothing else with it.
#
# net is here because Task B12 composed the TUI runtime on core::net::EventLoop: TuiRuntime.hpp
# names the loop, its awaitables and its handle kinds in its own signatures, so the dependency is
# public rather than an implementation detail. core::tui_output still links base alone, which is
# the whole reason it has a row of its own -- dbtool takes none of this.
core_cpp_module(NAME tui KIND STATIC DEPS base platform async net PLATFORMS native
WHEN CORE_CPP_WITH_TUI)
#
# tui_output has an option of its own for the same reason it has a row of its own. Gated on
# CORE_CPP_WITH_TUI it existed only in the configuration that also fetched libunicode (and, through
# libunicode's configure, UCD.zip), so the one consumer it was split off for could not have it
# without everything it was split off from. With CORE_CPP_WITH_TUI off and CORE_CPP_WITH_TUI_OUTPUT
# on, core_cpp_add_modules() enters this directory for the leaf alone. PLATFORMS any: on a platform
# that is neither POSIX nor Windows its OS-bound members come from portable/, which needs only the C++
# standard library. Its option stays forced off under Emscripten, as the whole TUI's is; WASI builds
# it (`wasi`), with portable/ for its OS-bound members (docs/design/tui-headless.md).
core_cpp_module_target(NAME tui_output MODULE tui KIND STATIC DEPS base
PLATFORMS any wasi WHEN CORE_CPP_WITH_TUI_OUTPUT)
#
# tui_core is the headless half: the parser, cells and buffers, components, widgets and the frame
# driver, which draws on a ScreenSurface rather than on a Terminal (docs/design/tui-headless.md).
# It links base, the leaf and libunicode, and nothing of platform, async or net, so a program with
# no terminal under it -- a WebAssembly guest -- can render. core::tui links it and keeps the
# terminal, the runtime and the file-system image loaders. PLATFORMS any describes its code; the
# option is still forced off under Emscripten, with the rest of the TUI's. `wasi`: a wasm32-wasip1
# guest builds it, which is what it exists for, and core-cpp.tui-core-wasi links and runs one.
core_cpp_module_target(NAME tui_core MODULE tui KIND STATIC DEPS base tui_output
PLATFORMS any wasi WHEN CORE_CPP_WITH_TUI_CORE)