Repository navigation
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
273 lines (246 loc) · 10.6 KB
/
Copy pathCMakeLists.txt
File metadata and controls
273 lines (246 loc) · 10.6 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
# SPDX-License-Identifier: MPL-2.0
# DeMoD UI Framework — portable CMake build (ADDITIVE; the Unix Makefile is
# unchanged and remains the canonical Linux/Nix/AppImage build).
#
# This CMake path exists to build the framework as a cross-platform *remote GUI
# client* (Linux / macOS / Windows) that drives a Linux engine over DCF/UDP. The
# Linux-only local backend (AF_UNIX control socket, /dev/shm param+meter bus,
# ALSA rawmidi) and the POSIX-only serial encoder are compiled everywhere but
# gate to harmless stubs off their supported platform (see src/ipc/*, src/input/*),
# so the Lua side transparently falls back to the remote (dm.dcf) transport.
#
# Copyright (c) 2026 DeMoD LLC. Licensed under the Mozilla Public License, v. 2.0.
cmake_minimum_required(VERSION 3.16)
project(demod-ui C)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
# ── Emscripten / WebAssembly build ──────────────────────────────────────────
# Additive branch: builds the *real* C/SDL2/Lua UI to WASM so it runs on a
# <canvas> in a browser. Entirely gated on EMSCRIPTEN (set by emcmake), so the
# native find_package(SDL2/Lua) path below is byte-unchanged off Emscripten.
# emcmake cmake -S . -B build-wasm && cmake --build build-wasm
# Produces build-wasm/demod-ui.{js,wasm,data}. No DCF (phase B), no pthreads.
if(EMSCRIPTEN)
include(FetchContent)
# Lua 5.4.7 from source — Emscripten ships no Lua. Build the library .c files
# (excluding the lua.c / luac.c CLI drivers) into a static lib.
FetchContent_Declare(lua_upstream
URL https://www.lua.org/ftp/lua-5.4.7.tar.gz
URL_HASH SHA256=9fbf5e28ef86c69858f6d3d34eccc32e911c1a28b4120ff3e84aaa70cfbf1e30
)
FetchContent_MakeAvailable(lua_upstream)
set(LUA_SRC_DIR ${lua_upstream_SOURCE_DIR}/src)
set(LUA_LIB_C
lapi lcode lctype ldebug ldo ldump lfunc lgc llex lmem lobject lopcodes
lparser lstate lstring ltable ltm lundump lvm lzio
lauxlib lbaselib lcorolib ldblib liolib lmathlib loadlib loslib lstrlib
ltablib lutf8lib linit)
set(LUA_SOURCES "")
foreach(u ${LUA_LIB_C})
list(APPEND LUA_SOURCES ${LUA_SRC_DIR}/${u}.c)
endforeach()
add_library(demod_lua STATIC ${LUA_SOURCES})
target_include_directories(demod_lua PUBLIC ${LUA_SRC_DIR})
# demod sources: CORE + the (stubbed-off-platform) IPC/input + crypto +
# streamdb + dm_dcf.c (Phase B: its __EMSCRIPTEN__ branch is an async binary
# WebSocket to dcf-ws-bridge). streamdb runs its inline-flush no-thread path
# (also selected by the __EMSCRIPTEN__ guard in streamdb.c).
set(WASM_SOURCES
src/core/framebuffer.c
src/core/font.c
src/widgets/widgets.c
src/widgets/dsp_widgets.c
src/lua/lua_bindings.c
src/app/app.c
src/main.c
src/dsl/system_dsl.c
src/dsl/control_dsl.c
src/crypto/monocypher.c
src/crypto/monocypher-ed25519.c
src/db/streamdb.c
src/input/gamepad.c
src/ipc/demod_control.c
src/ipc/demod_params.c
src/ipc/demod_rt_meters.c
src/ipc/dm_dcf.c
src/input/midi_input.c
src/input/serial_encoder.c
)
add_executable(demod-ui ${WASM_SOURCES})
target_include_directories(demod-ui PRIVATE
${CMAKE_SOURCE_DIR}/include
${CMAKE_SOURCE_DIR}/src
${CMAKE_SOURCE_DIR}/src/crypto
${CMAKE_SOURCE_DIR}/src/db
${CMAKE_SOURCE_DIR}/third_party # hydramesh header-only codec for dm_dcf.c
${LUA_SRC_DIR}
)
# DEMOD_DCF registers dm.dcf; the WS transport pulls in emscripten/websocket.h.
target_compile_definitions(demod-ui PRIVATE STREAMDB_NO_THREADS DEMOD_DCF)
# -sUSE_SDL=2 in CFLAGS so <SDL2/SDL.h> resolves from the emscripten port.
target_compile_options(demod-ui PRIVATE "SHELL:-sUSE_SDL=2")
target_link_libraries(demod-ui PRIVATE demod_lua)
# The dsp_studio example is preloaded into MEMFS as main.lua (main.c's default
# entry when no argv is given, which is the case in the browser).
target_link_options(demod-ui PRIVATE
"SHELL:-sUSE_SDL=2"
"SHELL:-sALLOW_MEMORY_GROWTH=1"
"SHELL:-sEXIT_RUNTIME=0"
"SHELL:-sEXPORTED_RUNTIME_METHODS=['callMain','FS']"
"SHELL:-lwebsocket.js" # emscripten WebSocket API for dm_dcf.c's WS transport
"SHELL:--preload-file ${CMAKE_SOURCE_DIR}/examples/dsp_studio.lua@main.lua"
)
# Emit demod-ui.js (+ .wasm + .data), not an .html shell (web/index.html hosts).
set_target_properties(demod-ui PROPERTIES OUTPUT_NAME demod-ui SUFFIX ".js")
return() # Skip the native find_package path entirely on Emscripten.
endif()
# ── Options ────────────────────────────────────────────────────────────────
# DEMOD_CLIENT: build as a remote GUI client. Defaults ON off-Linux (where there
# is no local engine to talk to) and OFF on Linux (the device/desktop can host
# the local backend). It forces the DCF transport on so the client can reach a
# remote engine.
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
option(DEMOD_CLIENT "Build as a remote GUI client (DCF transport)" OFF)
else()
option(DEMOD_CLIENT "Build as a remote GUI client (DCF transport)" ON)
endif()
# DEMOD_DCF: compile the dm.dcf UDP transport binding. Independently toggleable.
option(DEMOD_DCF "Build the DCF/UDP remote transport (dm.dcf)" ON)
if(DEMOD_CLIENT)
set(DEMOD_DCF ON CACHE BOOL "Build the DCF/UDP remote transport (dm.dcf)" FORCE)
endif()
# DEMOD_AR: compile the dm.ar AR passthrough HUD binding. Off by default.
option(DEMOD_AR "Build the AR passthrough HUD (dm.ar)" OFF)
# DEMOD_XR: compile the OpenXR present-sink scaffold (UNTESTED). Off by default.
option(DEMOD_XR "Build the OpenXR present sink (src/ar/xr_sink.c, UNTESTED)" OFF)
# ── Dependencies: SDL2 + Lua 5.4 ────────────────────────────────────────────
find_package(SDL2 QUIET)
find_package(Lua QUIET) # provides LUA_INCLUDE_DIR / LUA_LIBRARIES
# pkg-config fallback (Linux/macOS distros where the CMake configs are absent).
find_package(PkgConfig QUIET)
if(NOT SDL2_FOUND AND PkgConfig_FOUND)
pkg_check_modules(SDL2PC QUIET sdl2)
endif()
if(NOT LUA_FOUND AND PkgConfig_FOUND)
pkg_check_modules(LUAPC QUIET lua5.4)
if(NOT LUAPC_FOUND)
pkg_check_modules(LUAPC QUIET lua)
endif()
endif()
# Normalize into DEMOD_SDL2_* / DEMOD_LUA_* used below.
if(SDL2_FOUND)
set(DEMOD_SDL2_INCLUDE ${SDL2_INCLUDE_DIRS})
if(TARGET SDL2::SDL2)
set(DEMOD_SDL2_LIBS SDL2::SDL2)
# On Windows SDL.h renames main() to SDL_main(), and SDL2main supplies the
# real entry point that calls it; without it MSVC's link fails with an
# unresolved `main`. vcpkg's SDL2 usage note says the same.
if(WIN32 AND TARGET SDL2::SDL2main)
list(PREPEND DEMOD_SDL2_LIBS SDL2::SDL2main)
endif()
else()
set(DEMOD_SDL2_LIBS ${SDL2_LIBRARIES})
endif()
elseif(SDL2PC_FOUND)
set(DEMOD_SDL2_INCLUDE ${SDL2PC_INCLUDE_DIRS})
set(DEMOD_SDL2_LIBS ${SDL2PC_LIBRARIES})
link_directories(${SDL2PC_LIBRARY_DIRS})
else()
message(FATAL_ERROR "SDL2 not found (tried find_package(SDL2) and pkg-config sdl2)")
endif()
if(LUA_FOUND)
set(DEMOD_LUA_INCLUDE ${LUA_INCLUDE_DIR})
set(DEMOD_LUA_LIBS ${LUA_LIBRARIES})
elseif(LUAPC_FOUND)
set(DEMOD_LUA_INCLUDE ${LUAPC_INCLUDE_DIRS})
set(DEMOD_LUA_LIBS ${LUAPC_LIBRARIES})
link_directories(${LUAPC_LIBRARY_DIRS})
else()
message(FATAL_ERROR "Lua 5.4 not found (tried find_package(Lua) and pkg-config lua5.4/lua)")
endif()
# ── Source groups ───────────────────────────────────────────────────────────
# CORE: always portable, no platform code.
set(CORE_SOURCES
src/core/framebuffer.c
src/core/font.c
src/widgets/widgets.c
src/widgets/dsp_widgets.c
src/lua/lua_bindings.c
src/app/app.c
src/main.c
src/dsl/system_dsl.c
src/dsl/control_dsl.c
src/crypto/monocypher.c
src/crypto/monocypher-ed25519.c
src/db/streamdb.c
src/input/gamepad.c # pure SDL GameController — portable
)
# LOCAL: Linux/POSIX backends. Compiled EVERYWHERE — they are #ifdef-gated to
# stubs off their platform (Task C), so lua_bindings.c's externs always resolve
# and the same binary runs anywhere (falling back to the remote transport).
set(LOCAL_SOURCES
src/ipc/demod_control.c
src/ipc/demod_params.c
src/ipc/demod_rt_meters.c
src/input/midi_input.c
src/input/serial_encoder.c
)
# DCF: the portable UDP remote transport (Winsock-shimmed via platform/compat.h).
set(DCF_SOURCES src/ipc/dm_dcf.c)
# AR: the AR passthrough compositor + frame source (dm.ar). libc only.
set(AR_SOURCES src/ar/ar_source.c src/ar/ar_composite.c)
# XR: the OpenXR present-sink scaffold. Compiles to a stub without the SDK.
set(XR_SOURCES src/ar/xr_sink.c)
set(DEMOD_SOURCES ${CORE_SOURCES} ${LOCAL_SOURCES})
if(DEMOD_DCF)
list(APPEND DEMOD_SOURCES ${DCF_SOURCES})
endif()
if(DEMOD_AR)
list(APPEND DEMOD_SOURCES ${AR_SOURCES})
endif()
if(DEMOD_XR)
list(APPEND DEMOD_SOURCES ${XR_SOURCES})
endif()
add_executable(demod-ui ${DEMOD_SOURCES})
target_include_directories(demod-ui PRIVATE
${CMAKE_SOURCE_DIR}/include
${CMAKE_SOURCE_DIR}/src
${CMAKE_SOURCE_DIR}/src/crypto
${CMAKE_SOURCE_DIR}/src/db
${CMAKE_SOURCE_DIR}/third_party # hydramesh header-only codec for dm_dcf.c
${DEMOD_SDL2_INCLUDE}
${DEMOD_LUA_INCLUDE}
)
if(DEMOD_DCF)
target_compile_definitions(demod-ui PRIVATE DEMOD_DCF)
endif()
if(DEMOD_AR)
target_compile_definitions(demod-ui PRIVATE DEMOD_AR)
endif()
if(DEMOD_XR)
target_compile_definitions(demod-ui PRIVATE DEMOD_XR)
# Live path only: point OPENXR_SDK at an SDK to link the loader (+ GL/X11).
# Without it, xr_sink.c compiles to a stub (no extra link deps).
if(DEFINED OPENXR_SDK AND EXISTS "${OPENXR_SDK}/include")
target_include_directories(demod-ui PRIVATE "${OPENXR_SDK}/include")
target_link_directories(demod-ui PRIVATE "${OPENXR_SDK}/lib")
target_link_libraries(demod-ui PRIVATE openxr_loader GL X11)
endif()
endif()
if(DEMOD_CLIENT)
target_compile_definitions(demod-ui PRIVATE DEMOD_CLIENT)
endif()
target_link_libraries(demod-ui PRIVATE ${DEMOD_SDL2_LIBS} ${DEMOD_LUA_LIBS})
if(NOT WIN32)
target_link_libraries(demod-ui PRIVATE m)
endif()
if(WIN32)
target_link_libraries(demod-ui PRIVATE ws2_32) # Winsock for dm_dcf.c
endif()
if(MSVC)
# <stdatomic.h> (demod_rt_meters.h, demod_triple_buf.h) needs MSVC's C11
# atomics switch on top of /std:c11 (CMAKE_C_STANDARD above); without it
# vcruntime_c11_stdatomic.h stops with "C atomic support is not enabled".
target_compile_options(demod-ui PRIVATE /experimental:c11atomics)
endif()
install(TARGETS demod-ui RUNTIME DESTINATION bin)