diff options
| -rw-r--r-- | CMakeLists.txt | 9 | ||||
| -rw-r--r-- | HOWTO.md | 10 | ||||
| -rw-r--r-- | PROJECT_CONTEXT.md | 8 | ||||
| -rw-r--r-- | src/gpu/gpu.cc | 34 | ||||
| -rw-r--r-- | src/main.cc | 7 |
5 files changed, 63 insertions, 5 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index fc4ea81..1238a8a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,12 @@ cmake_minimum_required(VERSION 3.16) project(demo64k LANGUAGES C CXX) option(DEMO_SIZE_OPT "Enable size optimization flags" OFF) +option(DEMO_STRIP_ALL "Strip all unnecessary code for final build" OFF) + +if (DEMO_STRIP_ALL) + add_definitions(-DSTRIP_ALL) + set(DEMO_SIZE_OPT ON) +endif() set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) @@ -53,6 +59,9 @@ if (DEMO_SIZE_OPT) if (MSVC) target_compile_options(demo64k PRIVATE /Os /GS-) target_link_options(demo64k PRIVATE /OPT:REF /OPT:ICF /INCREMENTAL:NO) + elseif (APPLE) + target_compile_options(demo64k PRIVATE -Os) + target_link_options(demo64k PRIVATE -Wl,-dead_strip) else() target_compile_options(demo64k PRIVATE -Os -ffunction-sections -fdata-sections) target_link_options(demo64k PRIVATE -Wl,--gc-sections -s) @@ -30,6 +30,16 @@ cmake -S . -B build -DDEMO_SIZE_OPT=ON cmake --build build ``` +### Final / Strip Build + +To produce the smallest possible binary (stripping all unnecessary code like command-line parsing and debug info), use the `DEMO_STRIP_ALL` option: + +```bash +cmake -S . -B build -DDEMO_STRIP_ALL=ON +cmake --build build +``` +In this mode, the demo will always start in fullscreen. + ## Testing **Commit Policy**: Always run tests before committing. Refer to `CONTRIBUTING.md` for details. diff --git a/PROJECT_CONTEXT.md b/PROJECT_CONTEXT.md index 39f050c..7234d7e 100644 --- a/PROJECT_CONTEXT.md +++ b/PROJECT_CONTEXT.md @@ -24,16 +24,16 @@ Style: - No engine abstractions Incoming tasks in no particular order: -- 1. add a fullscreen mode (as command line option) -- 2. parse the keyboard key. Exit the demo when 'esc' is pressed. Toggle full-screen when 'f' is pressed. +- [x] 1. add a fullscreen mode (as command line option) +- [x] 2. parse the keyboard key. Exit the demo when 'esc' is pressed. Toggle full-screen when 'f' is pressed. - 3. add binary crunchers for all platforms - 4. add cross-compilation for PC+linux (x86_64) and PC+Windows (.exe binary) - 5. implement a spectrogram editor for representing .spec with elementary shapes (bezier curves, lines, random noise, rectangles...) as a mean of compression - 6. add a scripting tool to edit the demo (compiled into the binary at the end) -- 7. compile wgpu-native in optimized mode (not unoptimized) -- 8. add a #define STRIP_ALL to remove all unnecessary code for the final build +- [x] 7. compile wgpu-native in optimized mode (not unoptimized) +- [x] 8. add a #define STRIP_ALL to remove all unnecessary code for the final build (for instance, command-line args parsing, or unnecessary options, constant parameters to function calls, etc.) 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 <algorithm> #include <cassert> #include <cstring> -#include <iostream> #include <vector> +#ifndef STRIP_ALL +#include <iostream> +#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()); |
