From e215261ced0de7c92e04dab162d3a79bc7c65bda Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Wed, 8 Jul 2020 10:38:28 -0700 Subject: [PATCH] picolibc: Use most NEWLIB code with picolibc In most places, picolibc and newlib are the same, so use the existing newlib code when compiling with picolibc. Signed-off-by: Keith Packard --- cpu/arm7_common/arm7_init.c | 2 +- cpu/cortexm_common/vectors_cortexm.c | 2 +- examples/filesystem/main.c | 6 +-- sys/log/log_color/log_module.h | 2 +- sys/posix/include/fcntl.h | 2 +- sys/progress_bar/progress_bar.c | 2 +- sys/shell/commands/sc_sntp.c | 2 +- sys/shell/shell.c | 4 +- tests/bloom_bytes/main.c | 2 +- tests/pkg_fatfs_vfs/main.c | 50 +++++++++---------- tests/rng/test.c | 2 +- .../tests-vfs/tests-vfs-mount-constfs.c | 4 +- 12 files changed, 40 insertions(+), 40 deletions(-) diff --git a/cpu/arm7_common/arm7_init.c b/cpu/arm7_common/arm7_init.c index f9619eae9d..c5e4d903a8 100644 --- a/cpu/arm7_common/arm7_init.c +++ b/cpu/arm7_common/arm7_init.c @@ -127,7 +127,7 @@ void bootloader(void) /* cpu specific setup of clocks, peripherals */ cpu_init(); -#ifdef MODULE_NEWLIB +#if defined(MODULE_NEWLIB) || defined(MODULE_PICOLIBC) extern void __libc_init_array(void); __libc_init_array(); #endif diff --git a/cpu/cortexm_common/vectors_cortexm.c b/cpu/cortexm_common/vectors_cortexm.c index 4a751d4bcf..17fd205921 100644 --- a/cpu/cortexm_common/vectors_cortexm.c +++ b/cpu/cortexm_common/vectors_cortexm.c @@ -174,7 +174,7 @@ void reset_handler_default(void) /* initialize the board (which also initiates CPU initialization) */ board_init(); -#if MODULE_NEWLIB +#if MODULE_NEWLIB || MODULE_PICOLIBC /* initialize std-c library (this must be done after board_init) */ extern void __libc_init_array(void); __libc_init_array(); diff --git a/examples/filesystem/main.c b/examples/filesystem/main.c index ba1a28a9d8..759d61c4ab 100644 --- a/examples/filesystem/main.c +++ b/examples/filesystem/main.c @@ -240,9 +240,9 @@ static int _cat(int argc, char **argv) printf("Usage: %s \n", argv[0]); return 1; } - /* With newlib, low-level syscalls are plugged to RIOT vfs + /* With newlib or picolibc, low-level syscalls are plugged to RIOT vfs * on native, open/read/write/close/... are plugged to RIOT vfs */ -#ifdef MODULE_NEWLIB +#if defined(MODULE_NEWLIB) || defined(MODULE_PICOLIBC) FILE *f = fopen(argv[1], "r"); if (f == NULL) { printf("file %s does not exist\n", argv[1]); @@ -276,7 +276,7 @@ static int _tee(int argc, char **argv) return 1; } -#ifdef MODULE_NEWLIB +#if defined(MODULE_NEWLIB) || defined(MODULE_PICOLIBC) FILE *f = fopen(argv[1], "w+"); if (f == NULL) { printf("error while trying to create %s\n", argv[1]); diff --git a/sys/log/log_color/log_module.h b/sys/log/log_color/log_module.h index 4e0942923b..82209c4459 100644 --- a/sys/log/log_color/log_module.h +++ b/sys/log/log_color/log_module.h @@ -110,7 +110,7 @@ static inline void log_write(unsigned level, const char *format, ...) va_end(args); printf(LOG_RESET_ANSI_COLOR_CODE); -#ifdef MODULE_NEWLIB +#if defined(MODULE_NEWLIB) || defined(MODULE_PICOLIBC) /* no fflush on msp430 */ fflush(stdout); #endif diff --git a/sys/posix/include/fcntl.h b/sys/posix/include/fcntl.h index 3cc1a766cf..d7ff4ecae7 100644 --- a/sys/posix/include/fcntl.h +++ b/sys/posix/include/fcntl.h @@ -16,7 +16,7 @@ */ #ifndef DOXYGEN -#if defined(CPU_NATIVE) || MODULE_NEWLIB +#if defined(CPU_NATIVE) || MODULE_NEWLIB || MODULE_PICOLIBC /* If building on native or newlib we need to use the system header instead */ #pragma GCC system_header /* without the GCC pragma above #include_next will trigger a pedantic error */ diff --git a/sys/progress_bar/progress_bar.c b/sys/progress_bar/progress_bar.c index 70ee709885..2bec1c5d8c 100644 --- a/sys/progress_bar/progress_bar.c +++ b/sys/progress_bar/progress_bar.c @@ -72,7 +72,7 @@ void progress_bar_print(char *prefix, char *suffix, uint8_t value) /* show cursor */ printf("\033[?25h"); -#ifdef MODULE_NEWLIB +#if defined(MODULE_NEWLIB) || defined(MODULE_PICOLIBC) fflush(stdout); #endif } diff --git a/sys/shell/commands/sc_sntp.c b/sys/shell/commands/sc_sntp.c index d8337068fb..1cd3886cc6 100644 --- a/sys/shell/commands/sc_sntp.c +++ b/sys/shell/commands/sc_sntp.c @@ -82,7 +82,7 @@ int _ntpdate(int argc, char **argv) puts("Error in synchronization"); return 1; } -#ifdef MODULE_NEWLIB +#if defined(MODULE_NEWLIB) || defined(MODULE_PICOLIBC) struct tm *tm; time_t time = (time_t)(sntp_get_unix_usec() / US_PER_SEC); diff --git a/sys/shell/shell.c b/sys/shell/shell.c index 167cdb6ee4..297344043f 100644 --- a/sys/shell/shell.c +++ b/sys/shell/shell.c @@ -42,11 +42,11 @@ #define BS '\x08' /** ASCII "Backspace" */ #define DEL '\x7f' /** ASCII "Delete" */ -#ifdef MODULE_NEWLIB +#if defined(MODULE_NEWLIB) || defined(MODULE_PICOLIBC) #define flush_if_needed() fflush(stdout) #else #define flush_if_needed() -#endif /* MODULE_NEWLIB */ +#endif /* MODULE_NEWLIB || MODULE_PICOLIBC */ #ifndef SHELL_NO_ECHO #define ECHO_ON 1 diff --git a/tests/bloom_bytes/main.c b/tests/bloom_bytes/main.c index f611e6f4ba..5628cad6b2 100644 --- a/tests/bloom_bytes/main.c +++ b/tests/bloom_bytes/main.c @@ -110,7 +110,7 @@ int main(void) double false_positive_rate = (double) in / (double) lenA; /* Use 'fmt/print_float' to work on all platforms (atmega) * Stdout should be flushed before to prevent garbled output. */ -#ifdef MODULE_NEWLIB +#if defined(MODULE_NEWLIB) || defined(MODULE_PICOLIBC) fflush(stdout); #endif print_float(false_positive_rate, 6); diff --git a/tests/pkg_fatfs_vfs/main.c b/tests/pkg_fatfs_vfs/main.c index 4b6059fe7c..b90b5f3636 100644 --- a/tests/pkg_fatfs_vfs/main.c +++ b/tests/pkg_fatfs_vfs/main.c @@ -325,16 +325,16 @@ static void test_fstat(void) print_test_result("test_stat__umount", vfs_umount(&_test_vfs_mount) == 0); } -#ifdef MODULE_NEWLIB -static void test_newlib(void) +#if defined(MODULE_NEWLIB) || defined(MODULE_PICOLIBC) +static void test_libc(void) { FILE* fl; char buf[sizeof(test_txt) + sizeof(test_txt2)]; - print_test_result("test_newlib__mount", vfs_mount(&_test_vfs_mount) == 0); + print_test_result("test_libc__mount", vfs_mount(&_test_vfs_mount) == 0); /* try to open file that doesn't exist */ fl = fopen(FULL_FNAME_NXIST, "r"); - print_test_result("test_newlib__fopen", fl == NULL); + print_test_result("test_libc__fopen", fl == NULL); if (fl) { fclose(fl); } @@ -342,52 +342,52 @@ static void test_newlib(void) /* create new file write and check content */ remove(FULL_FNAME2); fl = fopen(FULL_FNAME2, "w+"); - print_test_result("test_newlib__fopen_w", fl != NULL); + print_test_result("test_libc__fopen_w", fl != NULL); if (fl) { - print_test_result("test_newlib__fputs_w", fputs(test_txt, fl) >= 0); + print_test_result("test_libc__fputs_w", fputs(test_txt, fl) >= 0); rewind(fl); - print_test_result("test_newlib__fread_w", + print_test_result("test_libc__fread_w", fread(buf, sizeof(*buf), sizeof(buf), fl) > 0); - print_test_result("test_newlib__strcmp_w", strcmp(test_txt, buf) == 0); - print_test_result("test_newlib__fclose_w", fclose(fl) == 0); + print_test_result("test_libc__strcmp_w", strcmp(test_txt, buf) == 0); + print_test_result("test_libc__fclose_w", fclose(fl) == 0); } /* cppcheck-suppress resourceLeak * (reason: cppcheck <2.0 reports a false positive here) */ fl = fopen(FULL_FNAME2, "r"); /* open file RO */ - print_test_result("test_newlib__fopen_r", fl != NULL); + print_test_result("test_libc__fopen_r", fl != NULL); if (fl) { - print_test_result("test_newlib__fclose_r", fclose(fl) == 0); + print_test_result("test_libc__fclose_r", fclose(fl) == 0); } /* remove file */ - print_test_result("test_newlib__remove", remove(FULL_FNAME2) == 0); + print_test_result("test_libc__remove", remove(FULL_FNAME2) == 0); /* append to non existing file */ fl = fopen(FULL_FNAME2, "a"); - print_test_result("test_newlib__fopen_a", fl != NULL); + print_test_result("test_libc__fopen_a", fl != NULL); if (fl) { - print_test_result("test_newlib__fputs_a", fputs(test_txt, fl) >= 0); - print_test_result("test_newlib__fclose_a", fclose(fl) == 0); + print_test_result("test_libc__fputs_a", fputs(test_txt, fl) >= 0); + print_test_result("test_libc__fclose_a", fclose(fl) == 0); } /* append to existing file and check content */ fl = fopen(FULL_FNAME2, "a+"); - print_test_result("test_newlib__fopen_a2", fl != NULL); + print_test_result("test_libc__fopen_a2", fl != NULL); if (fl) { - print_test_result("test_newlib__fputs_a2", fputs(test_txt2, fl) >= 0); + print_test_result("test_libc__fputs_a2", fputs(test_txt2, fl) >= 0); rewind(fl); - print_test_result("test_newlib__fread_a2", + print_test_result("test_libc__fread_a2", fread(buf, sizeof(*buf), sizeof(buf), fl) > 0); - print_test_result("test_newlib__strcmp_a2", + print_test_result("test_libc__strcmp_a2", strncmp(test_txt, buf, strlen(test_txt)) == 0); - print_test_result("test_newlib__strcmp_a2", strncmp(test_txt2, + print_test_result("test_libc__strcmp_a2", strncmp(test_txt2, &buf[strlen(test_txt)], strlen(test_txt2)) == 0); - print_test_result("test_newlib__fclose_a2", fclose(fl) == 0); + print_test_result("test_libc__fclose_a2", fclose(fl) == 0); } - print_test_result("test_newlib__remove", remove(FULL_FNAME2) == 0); + print_test_result("test_libc__remove", remove(FULL_FNAME2) == 0); - print_test_result("test_newlib__umount", vfs_umount(&_test_vfs_mount) == 0); + print_test_result("test_libc__umount", vfs_umount(&_test_vfs_mount) == 0); } #endif @@ -421,8 +421,8 @@ int main(void) test_mkrmdir(); test_create(); test_fstat(); -#ifdef MODULE_NEWLIB - test_newlib(); +#if defined(MODULE_NEWLIB) || defined(MODULE_PICOLIBC) + test_libc(); #endif printf("Test end.\n"); diff --git a/tests/rng/test.c b/tests/rng/test.c index 08a54f32c0..18ecae457a 100644 --- a/tests/rng/test.c +++ b/tests/rng/test.c @@ -389,7 +389,7 @@ void test_entropy(uint32_t samples) /* Use 'fmt/print_float' to work on all platforms (atmega) * Stdout should be flushed before to prevent garbled output. */ printf("Calculated "); -#ifdef MODULE_NEWLIB +#if defined(MODULE_NEWLIB) || defined(MODULE_PICOLIBC) /* no fflush on msp430 */ fflush(stdout); #endif diff --git a/tests/unittests/tests-vfs/tests-vfs-mount-constfs.c b/tests/unittests/tests-vfs/tests-vfs-mount-constfs.c index 4f630ac2be..5cf14271de 100644 --- a/tests/unittests/tests-vfs/tests-vfs-mount-constfs.c +++ b/tests/unittests/tests-vfs/tests-vfs-mount-constfs.c @@ -171,7 +171,7 @@ static void test_vfs_constfs_read_lseek(void) TEST_ASSERT_EQUAL_INT(0, res); } -#if MODULE_NEWLIB || defined(BOARD_NATIVE) +#if MODULE_NEWLIB || MODULE_PICOLIBC || defined(BOARD_NATIVE) static void test_vfs_constfs__posix(void) { int res; @@ -210,7 +210,7 @@ Test *tests_vfs_mount_constfs_tests(void) new_TestFixture(test_vfs_umount__invalid_mount), new_TestFixture(test_vfs_constfs_open), new_TestFixture(test_vfs_constfs_read_lseek), -#if MODULE_NEWLIB || defined(BOARD_NATIVE) +#if MODULE_NEWLIB || MODULE_PICOLIBC || defined(BOARD_NATIVE) new_TestFixture(test_vfs_constfs__posix), #endif };