From 417b260d3a7461b81b962d3d0723b4876a43694b Mon Sep 17 00:00:00 2001 From: Francisco Molina Date: Wed, 2 Mar 2022 10:00:00 +0100 Subject: [PATCH] sys/syscalls: add libc_syscalls_gettimeofday Conditionally implement gettimeofday() if module is included, this avoids including ztimer64 even when not needed --- cpu/native/Kconfig | 1 - cpu/native/Makefile.dep | 13 ++++++++++--- cpu/native/syscalls.c | 5 +++-- makefiles/pseudomodules.inc.mk | 4 ++++ pkg/lua/Makefile.dep | 4 ++++ pkg/tinydtls/Makefile.dep | 4 ++++ pkg/wolfssl/Makefile.dep | 4 ++++ sys/Kconfig | 8 +++++++- sys/Kconfig.newlib | 9 ++++++++- sys/Makefile.dep | 13 +++++++++---- sys/newlib_syscalls_default/syscalls.c | 14 +++----------- tests/cpp11_mutex/Makefile | 1 + 12 files changed, 57 insertions(+), 23 deletions(-) diff --git a/cpu/native/Kconfig b/cpu/native/Kconfig index 2b7faae06f05a..268185bd0aa12 100644 --- a/cpu/native/Kconfig +++ b/cpu/native/Kconfig @@ -25,7 +25,6 @@ config CPU_ARCH_NATIVE # needed modules select MODULE_PERIPH if TEST_KCONFIG - select MODULE_ZTIMER64_XTIMER_COMPAT if MODULE_ZTIMER_XTIMER_COMPAT config CPU_CORE_NATIVE bool diff --git a/cpu/native/Makefile.dep b/cpu/native/Makefile.dep index 2beef965a0a63..a66d3ef4e83ea 100644 --- a/cpu/native/Makefile.dep +++ b/cpu/native/Makefile.dep @@ -40,9 +40,16 @@ ifneq (,$(filter socket_zep,$(USEMODULE))) endif endif -ifneq (,$(filter ztimer_xtimer_compat,$(USEMODULE))) - # requires 64bit for syscalls - USEMODULE += ztimer64_xtimer_compat +ifneq (,$(filter libc_syscalls_gettimeofday,$(USEMODULE))) + USEMODULE += xtimer + ifneq (,$(filter ztimer_xtimer_compat,$(USEMODULE))) + # requires 64bit timestamps + USEMODULE += ztimer64_xtimer_compat + endif +endif + +ifneq (,$(filter tinydtls lua wolfssl,$(USEPKG))) + USEMODULE += libc_syscalls_gettimeofday endif USEMODULE += periph diff --git a/cpu/native/syscalls.c b/cpu/native/syscalls.c index bb60137ef6622..89327f7274c39 100644 --- a/cpu/native/syscalls.c +++ b/cpu/native/syscalls.c @@ -42,6 +42,7 @@ #include "xtimer.h" #include "stdio_base.h" +#include "kernel_defines.h" #include "native_internal.h" #define ENABLE_DEBUG 0 @@ -474,10 +475,10 @@ int getpid(void) return -1; } -#ifdef MODULE_XTIMER +#if (IS_USED(MODULE_LIBC_SYSCALLS_GETTIMEOFDAY)) int _gettimeofday(struct timeval *tp, void *restrict tzp) { - (void) tzp; + (void)tzp; uint64_t now = xtimer_now_usec64(); tp->tv_sec = now / US_PER_SEC; tp->tv_usec = now - tp->tv_sec; diff --git a/makefiles/pseudomodules.inc.mk b/makefiles/pseudomodules.inc.mk index fe812ef5c9956..39b15ba32bc65 100644 --- a/makefiles/pseudomodules.inc.mk +++ b/makefiles/pseudomodules.inc.mk @@ -121,6 +121,10 @@ PSEUDOMODULES += log PSEUDOMODULES += log_printfnoformat PSEUDOMODULES += log_color PSEUDOMODULES += lora +## @defgroup pseudomodule_libc_syscalls_gettimeofday libc_syscalls_gettimeofday +## @brief Includes implementation of gettimeofday() +## +PSEUDOMODULES += libc_syscalls_gettimeofday ## @defgroup pseudomodule_mpu_stack_guard mpu_stack_guard ## @brief MPU based stack guard diff --git a/pkg/lua/Makefile.dep b/pkg/lua/Makefile.dep index 74032e30aba2d..1d4174d9101aa 100644 --- a/pkg/lua/Makefile.dep +++ b/pkg/lua/Makefile.dep @@ -17,3 +17,7 @@ FEATURES_BLACKLIST += arch_riscv # - lua/liolib.c:671:38: error: '_IOFBF' undeclared (first use in this function) # - lua/liolib.c:671:46: error: '_IOLBF' undeclared (first use in this function) FEATURES_BLACKLIST += picolibc + +ifneq (,$(filter newlib_syscalls_default,$(USEMODULE))) + USEMODULE += libc_syscalls_gettimeofday +endif diff --git a/pkg/tinydtls/Makefile.dep b/pkg/tinydtls/Makefile.dep index 70e9b16f950f5..ce4de9204d779 100644 --- a/pkg/tinydtls/Makefile.dep +++ b/pkg/tinydtls/Makefile.dep @@ -14,3 +14,7 @@ ifneq (,$(filter sock_dtls,$(USEMODULE))) USEMODULE += tinydtls_sock_dtls USEMODULE += ztimer_usec endif + +ifneq (,$(filter newlib_syscalls_default,$(USEMODULE))) + USEMODULE += libc_syscalls_gettimeofday +endif diff --git a/pkg/wolfssl/Makefile.dep b/pkg/wolfssl/Makefile.dep index 17434f252ab34..cf0e320ab95b6 100644 --- a/pkg/wolfssl/Makefile.dep +++ b/pkg/wolfssl/Makefile.dep @@ -82,5 +82,9 @@ ifneq (,$(filter wolfcrypt_random,$(USEMODULE))) USEMODULE += random endif +ifneq (,$(filter newlib_syscalls_default,$(USEMODULE))) + USEMODULE += libc_syscalls_gettimeofday +endif + # wolfssl is only supported by 32 bit architectures FEATURES_REQUIRED += arch_32bit diff --git a/sys/Kconfig b/sys/Kconfig index ce356f3d6b7f6..aa815db23e385 100644 --- a/sys/Kconfig +++ b/sys/Kconfig @@ -49,9 +49,15 @@ config MODULE_NEWLIB config MODULE_PICOLIBC bool "Picolibc" depends on HAS_PICOLIBC - endchoice +config MODULE_LIBC_SYSCALLS_GETTIMEOFDAY + bool + select MODULE_XTIMER + select MODULE_ZTIMER64_XTIMER_COMPAT if MODULE_ZTIMER_XTIMER_COMPAT + help + Support for gettimeofday() + rsource "Kconfig.newlib" rsource "Kconfig.picolibc" diff --git a/sys/Kconfig.newlib b/sys/Kconfig.newlib index cd1f788a29e38..9fd0878dd5c34 100644 --- a/sys/Kconfig.newlib +++ b/sys/Kconfig.newlib @@ -18,10 +18,17 @@ config MODULE_NEWLIB_SYSCALLS_DEFAULT default y depends on !HAVE_CUSTOM_NEWLIB_SYSCALLS select MODULE_DIV - select MODULE_ZTIMER64_XTIMER_COMPAT if MODULE_ZTIMER_XTIMER_COMPAT help Default implementation of newlib system calls. +config MODULE_LIBC_SYSCALLS_GETTIMEOFDAY + bool + select MODULE_NEWLIB_SYSCALLS_DEFAULT if MODULE_NEWLIB + select MODULE_XTIMER + select MODULE_ZTIMER64_XTIMER_COMPAT if MODULE_ZTIMER_XTIMER_COMPAT + help + Support for gettimeofday() + endif # MODULE_NEWLIB config HAVE_CUSTOM_NEWLIB_SYSCALLS diff --git a/sys/Makefile.dep b/sys/Makefile.dep index beee33facf461..422c693d61508 100644 --- a/sys/Makefile.dep +++ b/sys/Makefile.dep @@ -242,13 +242,18 @@ ifneq (,$(filter newlib,$(USEMODULE))) endif ifneq (,$(filter newlib_syscalls_default,$(USEMODULE))) USEMODULE += div - ifneq (,$(filter ztimer_xtimer_compat,$(USEMODULE))) - # requires 64bit timestamps when using xtimer - USEMODULE += ztimer64_xtimer_compat + ifneq (,$(filter libc_syscalls_gettimeofday,$(USEMODULE))) + USEMODULE += xtimer + ifneq (,$(filter ztimer_xtimer_compat,$(USEMODULE))) + # requires 64bit timestamps + USEMODULE += ztimer64_xtimer_compat + endif endif - endif + endif endif + + ifneq (,$(filter posix_select,$(USEMODULE))) ifneq (,$(filter posix_sockets,$(USEMODULE))) USEMODULE += sock_async diff --git a/sys/newlib_syscalls_default/syscalls.c b/sys/newlib_syscalls_default/syscalls.c index 9ca0379411d31..62dbe934509ed 100644 --- a/sys/newlib_syscalls_default/syscalls.c +++ b/sys/newlib_syscalls_default/syscalls.c @@ -630,24 +630,16 @@ int _kill(pid_t pid, int sig) return -1; } -#ifdef MODULE_XTIMER +#if (IS_USED(MODULE_LIBC_SYSCALLS_GETTIMEOFDAY)) int _gettimeofday_r(struct _reent *r, struct timeval *restrict tp, void *restrict tzp) { - (void) r; - (void) tzp; + (void)tzp; + (void)r; uint64_t now = xtimer_now_usec64(); tp->tv_sec = div_u64_by_1000000(now); tp->tv_usec = now - (tp->tv_sec * US_PER_SEC); return 0; } -#else -int _gettimeofday_r(struct _reent *r, struct timeval *restrict tp, void *restrict tzp) -{ - (void) tp; - (void) tzp; - r->_errno = ENOSYS; - return -1; -} #endif /** diff --git a/tests/cpp11_mutex/Makefile b/tests/cpp11_mutex/Makefile index 6ec1d9fd36151..8857d10f27c42 100644 --- a/tests/cpp11_mutex/Makefile +++ b/tests/cpp11_mutex/Makefile @@ -2,5 +2,6 @@ include ../Makefile.tests_common USEMODULE += cpp11-compat USEMODULE += xtimer +USEMODULE += libc_syscalls_gettimeofday include $(RIOTBASE)/Makefile.include