From e93ad73a0f21563a032fd083414d6a2dceffe523 Mon Sep 17 00:00:00 2001 From: William Huynh Date: Fri, 27 Jun 2025 13:35:54 +0100 Subject: [PATCH] [libc] Fix issue with using clock() in hermetic testing Part of https://github.com/llvm/llvm-project/issues/145349. Some targets (like baremetal) don't implement clock(). However, they may later be overridden, we make it `[[gnu::weak]]`. This resolves one of the errors when building hermetic tests downstream. I plan to use the embedding API to implement clock() later, however, that is out of scope of this PR. Also, this allows for other architectures to start testing without a clock() implementation. --- libc/test/UnitTest/CMakeLists.txt | 2 +- libc/test/UnitTest/LibcTest.cpp | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/libc/test/UnitTest/CMakeLists.txt b/libc/test/UnitTest/CMakeLists.txt index c32809da577d4..4e18c3faafd57 100644 --- a/libc/test/UnitTest/CMakeLists.txt +++ b/libc/test/UnitTest/CMakeLists.txt @@ -20,7 +20,7 @@ function(add_unittest_framework_library name) ${TEST_LIB_HDRS} ) target_include_directories(${lib} PRIVATE ${LIBC_SOURCE_DIR}) - if(TARGET libc.src.time.clock) + if(TARGET libc.src.time.${LIBC_TARGET_OS}.clock) target_compile_definitions(${lib} PRIVATE TARGET_SUPPORTS_CLOCK) endif() endforeach() diff --git a/libc/test/UnitTest/LibcTest.cpp b/libc/test/UnitTest/LibcTest.cpp index fec45982f3e63..8abf491bebc0b 100644 --- a/libc/test/UnitTest/LibcTest.cpp +++ b/libc/test/UnitTest/LibcTest.cpp @@ -26,6 +26,9 @@ #include "src/time/clock.h" extern "C" clock_t clock() noexcept { return LIBC_NAMESPACE::clock(); } #define LIBC_TEST_USE_CLOCK +#else +#include +extern "C" [[gnu::weak]] clock_t clock() noexcept { return 0; } #endif namespace LIBC_NAMESPACE_DECL {