[libc-commits] [libc] [libc] Fix issue with using clock() in hermetic testing (PR #146069)

via libc-commits libc-commits at lists.llvm.org
Fri Jun 27 05:46:15 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: William Huynh (saturn691)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/146069.diff


2 Files Affected:

- (modified) libc/test/UnitTest/CMakeLists.txt (+1-1) 
- (modified) libc/test/UnitTest/LibcTest.cpp (+3) 


``````````diff
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 <time.h>
+extern "C" [[gnu::weak]] clock_t clock() noexcept { return 0; }
 #endif
 
 namespace LIBC_NAMESPACE_DECL {

``````````

</details>


https://github.com/llvm/llvm-project/pull/146069


More information about the libc-commits mailing list