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

via libc-commits libc-commits at lists.llvm.org
Wed Aug 7 01:11:35 PDT 2024


Author: Simon Tatham
Date: 2024-08-07T09:11:31+01:00
New Revision: efd71d921396c71adb2362d91fd9cdfbac21abc2

URL: https://github.com/llvm/llvm-project/commit/efd71d921396c71adb2362d91fd9cdfbac21abc2
DIFF: https://github.com/llvm/llvm-project/commit/efd71d921396c71adb2362d91fd9cdfbac21abc2.diff

LOG: [libc] Allow time conversions to compile on bare metal (#102014)

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.

Fixes #85556.

Added: 
    

Modified: 
    libc/config/baremetal/arm/entrypoints.txt
    libc/config/baremetal/riscv/entrypoints.txt
    libc/src/time/time_utils.h

Removed: 
    


################################################################################
diff  --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index 8f0e1300ec2400..d9b0fd8d065862 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.
diff time
+    libc.src.time.gmtime
+    libc.src.time.gmtime_r
+    libc.src.time.mktime
 
     # internal entrypoints
     libc.startup.baremetal.init

diff  --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index 5d2b6fe87a17e5..60d3070c963a05 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.
diff time
+    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 106870af72997c..47f55f7d389122 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;
 }
 


        


More information about the libc-commits mailing list