Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

saturn691
Copy link
Contributor

@saturn691 saturn691 commented Jun 27, 2025

Part of #145349. Some targets (like baremetal) don't implement clock(). However, they may later be overridden (e.g. by semihosting libraries), therefore, 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.

Part of llvm#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.
@saturn691 saturn691 requested a review from jhuber6 June 27, 2025 12:45
@llvmbot llvmbot added the libc label Jun 27, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 27, 2025

@llvm/pr-subscribers-libc

Author: William Huynh (saturn691)

Changes

Part of #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 --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 {

@petrhosek
Copy link
Member

Can you clarify who would override the weak definition in your scenario?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants