From 4f1a402240a2536653b8ca9fef8ea8f437142d28 Mon Sep 17 00:00:00 2001 From: afiqzudinhadi Date: Mon, 22 Jun 2026 23:02:16 +0800 Subject: [PATCH] Fix: suppress Javelin test compilation via template dead-code elimination --- CMakeLists.txt | 4 +++ include/zmk_javelin_steno/suppress_tests.h | 37 ++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 include/zmk_javelin_steno/suppress_tests.h diff --git a/CMakeLists.txt b/CMakeLists.txt index fe90869..6f565ff 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -220,6 +220,10 @@ if(CONFIG_ZMK_JAVELIN_STENO) JAVELIN_BOARD_CONFIG= M_PI=3.14159265358979323846 ) + # Suppress test compilation: Javelin's TEST_BEGIN/TEST_END macros + # compile test bodies even without RUN_TESTS. Some tests reference + # RUN_TESTS-only APIs. Override macros via include to dead-code them. + zephyr_library_compile_options(-include ${CMAKE_CURRENT_LIST_DIR}/include/zmk_javelin_steno/suppress_tests.h) target_sources(app PRIVATE ${ZMK_BEHAVIOR_SRC} diff --git a/include/zmk_javelin_steno/suppress_tests.h b/include/zmk_javelin_steno/suppress_tests.h new file mode 100644 index 0000000..93d5457 --- /dev/null +++ b/include/zmk_javelin_steno/suppress_tests.h @@ -0,0 +1,37 @@ +// Force-included before all Javelin sources to suppress test compilation. +// Redefine TEST macros to produce no function body, avoiding compilation +// of test code that references RUN_TESTS-only APIs. + +#ifndef JAVELIN_SUPPRESS_TESTS_H +#define JAVELIN_SUPPRESS_TESTS_H + +// Override the test macros from unit_test.h. +// Original (without RUN_TESTS): +// TEST_BEGIN → namespace UnitTestNNN { static void Test() { +// TEST_END → } } +// +// Our override makes Test() an empty function, then opens a dead namespace +// that swallows the original test body code until TEST_END closes it. +// The swallowed code goes into a struct with an unused template, so it +// never gets instantiated or compiled. + +#undef TEST_BEGIN__ +#undef TEST_BEGIN_ +#undef TEST_BEGIN +#undef TEST_END + +#define TEST_BEGIN__(text, file, line) \ + namespace UnitTest##line { \ + static inline void Test() {} \ + template struct Unused { \ + static void Swallowed() { + +#define TEST_END \ + } \ + }; \ + } + +#define TEST_BEGIN_(desc, file, line) TEST_BEGIN__(desc, file, line) +#define TEST_BEGIN(desc) TEST_BEGIN_(desc, __FILE__, __LINE__) + +#endif