From 7ed0286e8cd40367ab6ba7f100e3b30d9d1ae383 Mon Sep 17 00:00:00 2001 From: skal Date: Wed, 28 Jan 2026 01:27:02 +0100 Subject: opt: Implement build stripping and platform-specific size optimizations Adds a 'STRIP_ALL' mode to minimize binary size for the final build, and refines size optimization flags for macOS. - **STRIP_ALL Mode**: Added a 'DEMO_STRIP_ALL' CMake option that defines 'STRIP_ALL'. In this mode, command-line parsing is bypassed (forcing fullscreen), debug labels/error callbacks are removed from WebGPU, and non-essential code (like iostream) is stripped. - **macOS Optimizations**: Updated CMake to use '-dead_strip' instead of GNU '--gc-sections' on Apple platforms to resolve linker errors and improve dead code elimination. - **Documentation**: Updated HOWTO.md to document the new 'Final / Strip Build' process and FETCH_DEPS.md for optimized wgpu-native build guidance. - **Task 7 & 8**: Marks these tasks as completed in PROJECT_CONTEXT.md. --- src/gpu/gpu.cc | 34 +++++++++++++++++++++++++++++++++- src/main.cc | 7 +++++++ 2 files changed, 40 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/gpu/gpu.cc b/src/gpu/gpu.cc index a1da2db..9eb2437 100644 --- a/src/gpu/gpu.cc +++ b/src/gpu/gpu.cc @@ -8,9 +8,12 @@ #include #include #include -#include #include +#ifndef STRIP_ALL +#include +#endif + static WGPUInstance g_instance = nullptr; static WGPUAdapter g_adapter = nullptr; static WGPUDevice g_device = nullptr; @@ -23,10 +26,16 @@ static WGPUBuffer g_uniform_buffer = nullptr; static WGPUBindGroup g_bind_group = nullptr; static WGPUStringView str_view(const char *str) { +#ifndef STRIP_ALL if (!str) return {nullptr, 0}; return {str, strlen(str)}; +#else + (void)str; + return {nullptr, 0}; +#endif } +#ifndef STRIP_ALL static void handle_request_adapter(WGPURequestAdapterStatus status, WGPUAdapter adapter, WGPUStringView message, void *userdata1, void *userdata2) { @@ -36,7 +45,17 @@ static void handle_request_adapter(WGPURequestAdapterStatus status, printf("Request adapter failed: %.*s\n", (int)message.length, message.data); } } +#else +static void handle_request_adapter(WGPURequestAdapterStatus status, + WGPUAdapter adapter, WGPUStringView message, + void *userdata1, void *userdata2) { + if (status == WGPURequestAdapterStatus_Success) { + *((WGPUAdapter *)userdata1) = adapter; + } +} +#endif +#ifndef STRIP_ALL static void handle_request_device(WGPURequestDeviceStatus status, WGPUDevice device, WGPUStringView message, void *userdata1, void *userdata2) { @@ -46,12 +65,23 @@ static void handle_request_device(WGPURequestDeviceStatus status, printf("Request device failed: %.*s\n", (int)message.length, message.data); } } +#else +static void handle_request_device(WGPURequestDeviceStatus status, + WGPUDevice device, WGPUStringView message, + void *userdata1, void *userdata2) { + if (status == WGPURequestDeviceStatus_Success) { + *((WGPUDevice *)userdata1) = device; + } +} +#endif +#ifndef STRIP_ALL static void handle_device_error(WGPUDevice const *device, WGPUErrorType type, WGPUStringView message, void *userdata1, void *userdata2) { printf("WebGPU Error: %.*s\n", (int)message.length, message.data); } +#endif const char *shader_wgsl_code = R"( struct Uniforms { @@ -116,7 +146,9 @@ void gpu_init(GLFWwindow *window) { WGPUDeviceDescriptor device_desc = {}; device_desc.label = str_view("Demo Device"); +#ifndef STRIP_ALL device_desc.uncapturedErrorCallbackInfo.callback = handle_device_error; +#endif wgpuAdapterRequestDevice( g_adapter, &device_desc, diff --git a/src/main.cc b/src/main.cc index 315fa10..ef48f21 100644 --- a/src/main.cc +++ b/src/main.cc @@ -28,12 +28,19 @@ void generate_tone(float *buffer, float freq) { int main(int argc, char **argv) { bool fullscreen_enabled = false; + +#ifndef STRIP_ALL for (int i = 1; i < argc; ++i) { if (strcmp(argv[i], "--fullscreen") == 0) { fullscreen_enabled = true; break; } } +#else + (void)argc; + (void)argv; + fullscreen_enabled = true; +#endif platform_init_window(fullscreen_enabled); gpu_init(platform_get_window()); -- cgit v1.2.3