[libc-commits] [libc] [libc] Allow time conversions to compile on bare metal (PR #102014)

Simon Tatham via libc-commits libc-commits at lists.llvm.org
Tue Aug 6 03:13:35 PDT 2024


https://github.com/statham-arm updated https://github.com/llvm/llvm-project/pull/102014

>From 28d20576b965c2c5b459dea56a362683462b55e4 Mon Sep 17 00:00:00 2001
From: Simon Tatham <simon.tatham at arm.com>
Date: Mon, 5 Aug 2024 16:55:34 +0100
Subject: [PATCH 1/2] [libc] Allow time conversions to compile on bare metal

The `<time.h>` functions `asctime`, `gmtime`, `mktime` and their `_r`
variants are purely computational functions which convert between well
defined time representations and/or strings. There's no reason these
shouldn't be available in bare-metal builds of the library as well as
hosted ones: even if the library has no way to find out the time in
POSIX format, it might still see POSIX-style `time_t` values in input
data (e.g. network protocols) and need to interpret them.

The only obstacle to this was that the `out_of_range()` helper
function set `errno` to `EOVERFLOW`, which fails in a bare-metal build
because the extra POSIX error values aren't defined, including
`EOVERFLOW`. So I've made that assignment conditional on `EOVERFLOW`
being defined.
---
 libc/config/baremetal/arm/entrypoints.txt | 5 +++++
 libc/src/time/time_utils.h                | 5 +++++
 2 files changed, 10 insertions(+)

diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index b083745d5e082..d0f7bdc974252 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -201,7 +201,12 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdlib.strtoull
 
     # time.h entrypoints
+    libc.src.time.asctime
+    libc.src.time.asctime_r
     libc.src.time.difftime
+    libc.src.time.gmtime
+    libc.src.time.gmtime_r
+    libc.src.time.mktime
 
     # internal entrypoints
     libc.startup.baremetal.init
diff --git a/libc/src/time/time_utils.h b/libc/src/time/time_utils.h
index 106870af72997..47f55f7d38912 100644
--- a/libc/src/time/time_utils.h
+++ b/libc/src/time/time_utils.h
@@ -92,7 +92,12 @@ extern int64_t update_from_seconds(int64_t total_seconds, struct tm *tm);
 
 // POSIX.1-2017 requires this.
 LIBC_INLINE time_t out_of_range() {
+#ifdef EOVERFLOW
+  // For non-POSIX uses of the standard C time functions, where EOVERFLOW is
+  // not defined, it's OK not to set errno at all. The plain C standard doesn't
+  // require it.
   libc_errno = EOVERFLOW;
+#endif
   return TimeConstants::OUT_OF_RANGE_RETURN_VALUE;
 }
 

>From 2c56486cbc9935fffcf86ca06d9a503104a38ae4 Mon Sep 17 00:00:00 2001
From: Simon Tatham <simon.tatham at arm.com>
Date: Tue, 6 Aug 2024 09:18:16 +0100
Subject: [PATCH 2/2] Review feedback: repeat entrypoints.txt changes for
 RISC-V

---
 libc/config/baremetal/riscv/entrypoints.txt | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index 8cdbbdee59d51..61d57c85b4782 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -197,7 +197,12 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdlib.strtoull
 
     # time.h entrypoints
+    libc.src.time.asctime
+    libc.src.time.asctime_r
     libc.src.time.difftime
+    libc.src.time.gmtime
+    libc.src.time.gmtime_r
+    libc.src.time.mktime
 
     # internal entrypoints
     libc.startup.baremetal.init



More information about the libc-commits mailing list