-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc] Allow time conversions to compile on bare metal #102014
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
Conversation
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.
@llvm/pr-subscribers-libc Author: Simon Tatham (statham-arm) ChangesThe The only obstacle to this was that the Full diff: https://github.com/llvm/llvm-project/pull/102014.diff 2 Files Affected:
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;
}
|
Can you also include these in the |
Done, in the followup commit I just pushed.
I can try, but I don't think I've had to do that before in this repository! In Github repos that don't enforce squash-and-merge, I'd do it by force-pushing the PR branch. But here I don't know whether I need to do that, or if editing the PR description is enough. If you know which of those will work, please let me know. Otherwise I'll do my best, and manually close the bug afterwards if it doesn't do the right thing. |
You can just add it to the PR description, GitHub will pick it up. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
I have added issue link for you. Feel free to adjust in any way you want. |
The
<time.h>
functionsasctime
,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-styletime_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 seterrno
toEOVERFLOW
, which fails in a bare-metal build because the extra POSIX error values aren't defined, includingEOVERFLOW
. So I've made that assignment conditional onEOVERFLOW
being defined.Fixes #85556