diff options
| author | 192.168.1.134 <[email protected]> | 2021-11-08 15:02:29 +0800 |
|---|---|---|
| committer | 192.168.1.134 <[email protected]> | 2021-11-08 15:02:29 +0800 |
| commit | 85da2247c050ff16842eba5927fc34a06564c5dc (patch) | |
| tree | d489fbe3078ed99096210087be80759dd515d703 /components/libc | |
| parent | 078c3c5991e3024664fe7a51878874e02bf107dc (diff) | |
| parent | 9e7d7ae52544fd8e84f18734fd4216fc7659d9c9 (diff) | |
Merge branch 'master' of https://github.com/RT-Thread/rt-thread
Diffstat (limited to 'components/libc')
24 files changed, 368 insertions, 653 deletions
diff --git a/components/libc/Kconfig b/components/libc/Kconfig index 812b1ce48c..b2af15e841 100644 --- a/components/libc/Kconfig +++ b/components/libc/Kconfig @@ -2,18 +2,49 @@ menu "POSIX layer and C standard library" config RT_USING_LIBC bool "Enable libc APIs from toolchain" - select RT_USING_DFS - select RT_USING_POSIX default y +if RT_USING_LIBC + config RT_LIBC_USING_TIME + default y + + config RT_LIBC_USING_FILEIO + bool "Enable libc with file operation, eg.fopen/fwrite/fread/getchar" + select RT_USING_DFS + select RT_USING_POSIX + default n + + config RT_USING_MODULE + bool "Enable dynamic module with dlopen/dlsym/dlclose feature" + default n + + if RT_USING_MODULE + config RT_USING_CUSTOM_DLMODULE + bool "Enable load dynamic module by custom" + default n + endif +endif + +if RT_USING_LIBC != y + config RT_LIBC_USING_TIME + bool "Enable time functions without compiler's libc" + default y +endif + +config RT_LIBC_DEFAULT_TIMEZONE + depends on (RT_LIBC_USING_TIME || RT_USING_LIBC) + int "Set the default time zone (UTC+)" + range -12 12 + default 8 + config RT_USING_PTHREADS bool "Enable pthreads APIs" default n if RT_USING_PTHREADS config PTHREAD_NUM_MAX - int "Maximum number of pthreads" - default 8 + int "Maximum number of pthreads" + default 8 endif if RT_USING_DFS @@ -42,32 +73,4 @@ if RT_USING_DFS endif -if RT_USING_LIBC - config RT_LIBC_USING_TIME - default y - - config RT_USING_MODULE - bool "Enable dynamic module with dlopen/dlsym/dlclose feature" - default n - - if RT_USING_MODULE - config RT_USING_CUSTOM_DLMODULE - bool "Enable load dynamic module by custom" - default n - endif - -endif - -if RT_USING_LIBC != y - config RT_LIBC_USING_TIME - bool "Enable time functions without compiler's libc" - default y -endif - -config RT_LIBC_DEFAULT_TIMEZONE - depends on (RT_LIBC_USING_TIME || RT_USING_LIBC) - int "Set the default time zone (UTC+)" - range -12 12 - default 8 - endmenu diff --git a/components/libc/compilers/armlibc/SConscript b/components/libc/compilers/armlibc/SConscript index 52f7a6d93a..bf1a299304 100644 --- a/components/libc/compilers/armlibc/SConscript +++ b/components/libc/compilers/armlibc/SConscript @@ -1,21 +1,12 @@ from building import * Import('rtconfig') -src = Glob('*.c') + Glob('*.cpp') -cwd = GetCurrentDir() +src = Glob('*.c') group = [] -CPPPATH = [cwd] CPPDEFINES = ['RT_USING_ARM_LIBC'] -if GetDepend('RT_USING_DFS') == False: - SrcRemove(src, ['stdio.c']) - -if GetDepend('RT_USING_MODULE') == False: - SrcRemove(src, ['libc_syms.c']) - if rtconfig.PLATFORM == 'armcc' or rtconfig.PLATFORM == 'armclang': - group = DefineGroup('libc', src, depend = ['RT_USING_LIBC'], - CPPPATH = CPPPATH, CPPDEFINES = CPPDEFINES) + group = DefineGroup('libc', src, depend = ['RT_USING_LIBC'], CPPDEFINES = CPPDEFINES) Return('group') diff --git a/components/libc/compilers/armlibc/libc.c b/components/libc/compilers/armlibc/libc.c deleted file mode 100644 index 889c687309..0000000000 --- a/components/libc/compilers/armlibc/libc.c +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2017/10/15 bernard the first version - */ -#include <fcntl.h> -#include <rtthread.h> -#include "libc.h" - -#ifdef RT_USING_PTHREADS -#include <pthread.h> -#endif - -int libc_system_init(void) -{ -#if defined(RT_USING_DFS) & defined(RT_USING_DFS_DEVFS) - rt_device_t dev_console; - - dev_console = rt_console_get_device(); - if (dev_console) - { - #if defined(RT_USING_POSIX) - libc_stdio_set_console(dev_console->parent.name, O_RDWR); - #else - libc_stdio_set_console(dev_console->parent.name, O_WRONLY); - #endif - } -#endif - -#if defined RT_USING_PTHREADS && !defined RT_USING_COMPONENTS_INIT - pthread_system_init(); -#endif - - return 0; -} -INIT_COMPONENT_EXPORT(libc_system_init); diff --git a/components/libc/compilers/armlibc/libc.h b/components/libc/compilers/armlibc/libc.h deleted file mode 100644 index 5c1613728e..0000000000 --- a/components/libc/compilers/armlibc/libc.h +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2017/10/15 bernard the first version - */ -#ifndef __RTT_LIBC_H__ -#define __RTT_LIBC_H__ - -#ifdef __cplusplus -extern "C" { -#endif - -int libc_system_init(void); -int libc_stdio_get_console(void); -int libc_stdio_set_console(const char* device_name, int mode); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/components/libc/compilers/armlibc/stdio.c b/components/libc/compilers/armlibc/stdio.c deleted file mode 100644 index 7dd9dbfe3f..0000000000 --- a/components/libc/compilers/armlibc/stdio.c +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2017/10/15 bernard implement stdio for armcc. - */ - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include <rtthread.h> -#include "libc.h" - -#if defined(RT_USING_DFS) && defined(RT_USING_DFS_DEVFS) -#include <dfs_posix.h> - -#define STDIO_DEVICE_NAME_MAX 32 - -static int std_fd = -1; - -int libc_stdio_set_console(const char* device_name, int mode) -{ - int fd; - char name[STDIO_DEVICE_NAME_MAX]; - - snprintf(name, sizeof(name) - 1, "/dev/%s", device_name); - name[STDIO_DEVICE_NAME_MAX - 1] = '\0'; - - fd = open(name, mode, 0); - if (fd >= 0) - { - if (std_fd >= 0) - { - close(std_fd); - } - std_fd = fd; - } - - return std_fd; -} - -int libc_stdio_get_console(void) -{ - return std_fd; -} - -#endif diff --git a/components/libc/compilers/armlibc/syscalls.c b/components/libc/compilers/armlibc/syscalls.c index ccdcfc86ea..755ffef1a6 100644 --- a/components/libc/compilers/armlibc/syscalls.c +++ b/components/libc/compilers/armlibc/syscalls.c @@ -15,14 +15,14 @@ * 2020-02-14 Meco Man implement _sys_tmpnam() */ -#include <string.h> #include <rt_sys.h> - #include <rtthread.h> +#include <string.h> +#include <fcntl.h> +#include <unistd.h> +#include <sys/stat.h> +#ifdef RT_USING_POSIX #include "libc.h" - -#ifdef RT_USING_DFS -#include <dfs_posix.h> #endif #define DBG_TAG "armlibc.syscalls" @@ -55,7 +55,7 @@ const char __stderr_name[] = "STDERR"; */ FILEHANDLE _sys_open(const char *name, int openmode) { -#ifdef RT_USING_DFS +#ifdef RT_USING_POSIX int fd; int mode = O_RDONLY; #endif @@ -68,8 +68,8 @@ FILEHANDLE _sys_open(const char *name, int openmode) if (strcmp(name, __stderr_name) == 0) return (STDERR); -#ifndef RT_USING_DFS - return -1; +#ifndef RT_USING_POSIX + return 0; /* error */ #else /* Correct openmode from fopen to open */ if (openmode & OPEN_PLUS) @@ -99,21 +99,22 @@ FILEHANDLE _sys_open(const char *name, int openmode) fd = open(name, mode, 0); if (fd < 0) - return -1; + return 0; /* error */ else return fd; -#endif +#endif /* RT_USING_POSIX */ } int _sys_close(FILEHANDLE fh) { -#ifndef RT_USING_DFS - return 0; -#else - if (fh <= STDERR) return 0; +#ifdef RT_USING_POSIX + if (fh <= STDERR) + return 0; /* error */ return close(fh); -#endif +#else + return 0; +#endif /* RT_USING_POSIX */ } /* @@ -143,13 +144,11 @@ int _sys_close(FILEHANDLE fh) */ int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode) { -#ifdef RT_USING_DFS +#ifdef RT_USING_POSIX int size; -#endif if (fh == STDIN) { -#ifdef RT_USING_POSIX if (libc_stdio_get_console() < 0) { LOG_W("Do not invoke standard output before initializing libc"); @@ -157,25 +156,20 @@ int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode) } size = read(STDIN_FILENO, buf, len); return len - size; -#else - /* no stdin */ - return -1; -#endif } else if ((fh == STDOUT) || (fh == STDERR)) { - return -1; + return 0; /* error */ } -#ifndef RT_USING_DFS - return 0; -#else size = read(fh, buf, len); if (size >= 0) return len - size; else - return -1; -#endif + return 0; /* error */ +#else + return 0; /* error */ +#endif /* RT_USING_POSIX */ } /* @@ -185,15 +179,12 @@ int _sys_read(FILEHANDLE fh, unsigned char *buf, unsigned len, int mode) */ int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode) { -#ifdef RT_USING_DFS +#ifdef RT_USING_POSIX int size; -#endif +#endif /* RT_USING_POSIX */ if ((fh == STDOUT) || (fh == STDERR)) { -#if !defined(RT_USING_CONSOLE) || !defined(RT_USING_DEVICE) - return 0; -#else #ifdef RT_USING_POSIX if (libc_stdio_get_console() < 0) { @@ -202,31 +193,29 @@ int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode) } size = write(STDOUT_FILENO, buf, len); return len - size; -#else +#elif defined(RT_USING_CONSOLE) if (rt_console_get_device()) { rt_device_write(rt_console_get_device(), -1, buf, len); - return 0; } - return -1; -#endif -#endif + return 0; /* error */ +#endif /* RT_USING_POSIX */ } else if (fh == STDIN) { - return -1; + return 0; /* error */ } -#ifndef RT_USING_DFS - return 0; -#else +#ifdef RT_USING_POSIX size = write(fh, buf, len); if (size >= 0) return len - size; else - return -1; -#endif + return 0; /* error */ +#else + return 0; +#endif /* RT_USING_POSIX */ } /* @@ -235,16 +224,15 @@ int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode) */ int _sys_seek(FILEHANDLE fh, long pos) { +#ifdef RT_USING_POSIX if (fh < STDERR) - return -1; - -#ifndef RT_USING_DFS - return -1; -#else + return 0; /* error */ /* position is relative to the start of file fh */ return lseek(fh, pos, 0); -#endif +#else + return 0; /* error */ +#endif /* RT_USING_POSIX */ } /** @@ -270,7 +258,7 @@ void _ttywrch(int ch) c = (char)ch; rt_kprintf(&c); -#endif +#endif /* RT_USING_CONSOLE */ } /* for exit() and abort() */ @@ -289,17 +277,17 @@ RT_WEAK void _sys_exit(int return_code) */ long _sys_flen(FILEHANDLE fh) { -#ifdef RT_USING_DFS +#ifdef RT_USING_POSIX struct stat stat; if (fh < STDERR) - return -1; + return 0; /* error */ fstat(fh, &stat); return stat.st_size; #else - return -1; -#endif + return 0; +#endif /* RT_USING_POSIX */ } int _sys_istty(FILEHANDLE fh) @@ -312,11 +300,11 @@ int _sys_istty(FILEHANDLE fh) int remove(const char *filename) { -#ifndef RT_USING_DFS - return -1; -#else +#ifdef RT_USING_POSIX return unlink(filename); -#endif +#else + return 0; /* error */ +#endif /* RT_USING_POSIX */ } #ifdef __MICROLIB @@ -324,11 +312,15 @@ int remove(const char *filename) int fputc(int c, FILE *f) { +#ifdef RT_USING_CONSOLE char ch[2] = {0}; ch[0] = c; rt_kprintf(&ch[0]); return 1; +#else + return 0; /* error */ +#endif /* RT_USING_CONSOLE */ } int fgetc(FILE *f) @@ -339,13 +331,13 @@ int fgetc(FILE *f) if (libc_stdio_get_console() < 0) { LOG_W("Do not invoke standard output before initializing libc"); - return -1; + return 0; } if(read(STDIN_FILENO, &ch, 1) == 1) return ch; -#endif - - return -1; +#endif /* RT_USING_POSIX */ + return 0; /* error */ } -#endif + +#endif /* __MICROLIB */ diff --git a/components/libc/compilers/common/SConscript b/components/libc/compilers/common/SConscript index b7a6626a36..504bf84e56 100644 --- a/components/libc/compilers/common/SConscript +++ b/components/libc/compilers/common/SConscript @@ -1,5 +1,4 @@ from building import * - Import('rtconfig') src = [] @@ -8,13 +7,13 @@ group = [] CPPPATH = [cwd] CPPDEFINES = [] +if rtconfig.CROSS_TOOL == 'keil': + CPPDEFINES += ['__CLK_TCK=RT_TICK_PER_SECOND'] + if GetDepend('RT_USING_LIBC'): src += Glob('*.c') elif GetDepend('RT_LIBC_USING_TIME'): - src += Glob('time.c') - -if rtconfig.CROSS_TOOL == 'keil': - CPPDEFINES += ['__CLK_TCK=RT_TICK_PER_SECOND'] + src += ['time.c'] group = DefineGroup('libc', src, depend = [], CPPPATH = CPPPATH, CPPDEFINES = CPPDEFINES) diff --git a/components/libc/compilers/dlib/SConscript b/components/libc/compilers/dlib/SConscript index 75a5812fab..39707abc61 100644 --- a/components/libc/compilers/dlib/SConscript +++ b/components/libc/compilers/dlib/SConscript @@ -1,26 +1,20 @@ from building import * - Import('rtconfig') src = Glob('*.c') -cwd = GetCurrentDir() group = [] -CPPPATH = [cwd] CPPDEFINES = ['RT_USING_DLIBC'] if rtconfig.PLATFORM == 'iar': - - if GetDepend('RT_USING_DFS'): + if GetDepend('RT_USING_POSIX'): from distutils.version import LooseVersion from iar import IARVersion CPPDEFINES = CPPDEFINES + ['_DLIB_FILE_DESCRIPTOR'] - if LooseVersion(IARVersion()) < LooseVersion("8.20.1"): CPPDEFINES = CPPDEFINES + ['_DLIB_THREAD_SUPPORT'] - group = DefineGroup('libc', src, depend = ['RT_USING_LIBC'], - CPPPATH = CPPPATH, CPPDEFINES = CPPDEFINES) + group = DefineGroup('libc', src, depend = ['RT_USING_LIBC'], CPPDEFINES = CPPDEFINES) Return('group') diff --git a/components/libc/compilers/dlib/libc.c b/components/libc/compilers/dlib/libc.c deleted file mode 100644 index f540ac955f..0000000000 --- a/components/libc/compilers/dlib/libc.c +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2017/10/15 bernard the first version - */ -#include <fcntl.h> -#include <rtthread.h> -#include "libc.h" - -#ifdef RT_USING_PTHREADS -#include <pthread.h> -#endif - -int libc_system_init(void) -{ -#if defined(RT_USING_DFS) && defined(RT_USING_DFS_DEVFS) - rt_device_t dev_console; - - dev_console = rt_console_get_device(); - if (dev_console) - { - #if defined(RT_USING_POSIX) - libc_stdio_set_console(dev_console->parent.name, O_RDWR); - #else - libc_stdio_set_console(dev_console->parent.name, O_WRONLY); - #endif - } -#endif - -#if defined (RT_USING_PTHREADS) && !defined (RT_USING_COMPONENTS_INIT) - pthread_system_init(); -#endif - - return 0; -} -INIT_COMPONENT_EXPORT(libc_system_init); - diff --git a/components/libc/compilers/dlib/stdio.c b/components/libc/compilers/dlib/stdio.c deleted file mode 100644 index ed3f5dfef9..0000000000 --- a/components/libc/compilers/dlib/stdio.c +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2017/10/15 bernard implement stdio for IAR dlib. - */ - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include <rtthread.h> -#include "libc.h" - -#if defined(RT_USING_DFS) && defined(RT_USING_DFS_DEVFS) -#include <dfs_posix.h> - -#define STDIO_DEVICE_NAME_MAX 32 - -static int std_fd = -1; -int libc_stdio_set_console(const char* device_name, int mode) -{ - int fd; - char name[STDIO_DEVICE_NAME_MAX]; - - snprintf(name, sizeof(name) - 1, "/dev/%s", device_name); - name[STDIO_DEVICE_NAME_MAX - 1] = '\0'; - - fd = open(name, mode, 0); - if (fd >= 0) - { - if (std_fd >= 0) - { - close(std_fd); - } - std_fd = fd; - } - - return std_fd; -} - -int libc_stdio_get_console(void) { - return std_fd; -} - -#endif diff --git a/components/libc/compilers/dlib/syscall_close.c b/components/libc/compilers/dlib/syscall_close.c index 7ed1cc2f2d..2897c2418a 100644 --- a/components/libc/compilers/dlib/syscall_close.c +++ b/components/libc/compilers/dlib/syscall_close.c @@ -8,10 +8,8 @@ * 2015-01-28 Bernard first version */ #include <rtthread.h> -#ifdef RT_USING_DFS -#include <dfs_posix.h> -#endif #include <yfuns.h> +#include <unistd.h> #pragma module_name = "?__close" int __close(int handle) @@ -20,10 +18,9 @@ int __close(int handle) handle == _LLIO_STDERR || handle == _LLIO_STDIN) return _LLIO_ERROR; - -#ifdef RT_USING_DFS +#ifdef RT_USING_POSIX return close(handle); #else - return 0; -#endif + return _LLIO_ERROR; +#endif /* RT_USING_POSIX */ } diff --git a/components/libc/compilers/dlib/syscall_lseek.c b/components/libc/compilers/dlib/syscall_lseek.c index 47a069bf0a..4ea55ef1c3 100644 --- a/components/libc/compilers/dlib/syscall_lseek.c +++ b/components/libc/compilers/dlib/syscall_lseek.c @@ -8,10 +8,8 @@ * 2015-01-28 Bernard first version */ #include <rtthread.h> -#ifdef RT_USING_DFS -#include <dfs_posix.h> -#endif #include <yfuns.h> +#include <unistd.h> #pragma module_name = "?__lseek" long __lseek(int handle, long offset, int whence) @@ -20,10 +18,9 @@ long __lseek(int handle, long offset, int whence) handle == _LLIO_STDERR || handle == _LLIO_STDIN) return _LLIO_ERROR; - -#ifdef RT_USING_DFS +#ifdef RT_USING_POSIX return lseek(handle, offset, whence); #else return _LLIO_ERROR; -#endif +#endif /* RT_USING_POSIX */ } diff --git a/components/libc/compilers/dlib/syscall_open.c b/components/libc/compilers/dlib/syscall_open.c index 37bfcecb47..8fb3c3a11f 100644 --- a/components/libc/compilers/dlib/syscall_open.c +++ b/components/libc/compilers/dlib/syscall_open.c @@ -10,17 +10,13 @@ #include <rtthread.h> #include <yfuns.h> -#ifdef RT_USING_DFS -#include <dfs_posix.h> -#endif +#include <fcntl.h> #pragma module_name = "?__open" int __open(const char *filename, int mode) { -#ifndef RT_USING_DFS - return _LLIO_ERROR; -#else +#ifdef RT_USING_POSIX int handle; int open_mode = O_RDONLY; @@ -70,5 +66,7 @@ int __open(const char *filename, int mode) return _LLIO_ERROR; return handle; -#endif +#else + return _LLIO_ERROR; +#endif /* RT_USING_POSIX */ } diff --git a/components/libc/compilers/dlib/syscall_read.c b/components/libc/compilers/dlib/syscall_read.c index ae980be6ca..74cedb966a 100644 --- a/components/libc/compilers/dlib/syscall_read.c +++ b/components/libc/compilers/dlib/syscall_read.c @@ -9,11 +9,11 @@ */ #include <rtthread.h> -#ifdef RT_USING_DFS -#include <dfs_posix.h> -#endif #include <yfuns.h> +#include <unistd.h> +#ifdef RT_USING_POSIX #include "libc.h" +#endif #define DBG_TAG "dlib.syscall_read" #define DBG_LVL DBG_INFO @@ -22,32 +22,26 @@ #pragma module_name = "?__read" size_t __read(int handle, unsigned char *buf, size_t len) { -#ifdef RT_USING_DFS +#ifdef RT_USING_POSIX int size; -#endif if (handle == _LLIO_STDIN) { -#ifdef RT_USING_POSIX if (libc_stdio_get_console() < 0) { LOG_W("Do not invoke standard input before initializing libc"); return 0; } return read(STDIN_FILENO, buf, len); -#else - return _LLIO_ERROR; -#endif } else if ((handle == _LLIO_STDOUT) || (handle == _LLIO_STDERR)) { return _LLIO_ERROR; } -#ifndef RT_USING_DFS - return _LLIO_ERROR; -#else size = read(handle, buf, len); return size; -#endif +#else + return _LLIO_ERROR; +#endif /* RT_USING_POSIX */ } diff --git a/components/libc/compilers/dlib/syscall_remove.c b/components/libc/compilers/dlib/syscall_remove.c index 7d74236f4c..05bfbb67ae 100644 --- a/components/libc/compilers/dlib/syscall_remove.c +++ b/components/libc/compilers/dlib/syscall_remove.c @@ -8,17 +8,15 @@ * 2015-01-28 Bernard first version */ #include <rtthread.h> -#ifdef RT_USING_DFS -#include <dfs_posix.h> -#endif #include <yfuns.h> +#include <unistd.h> #pragma module_name = "?remove" int remove(const char *val) { -#ifndef RT_USING_DFS - return -1; -#else +#ifdef RT_USING_POSIX return unlink(val); -#endif +#else + return -1; +#endif /* RT_USING_POSIX */ } diff --git a/components/libc/compilers/dlib/syscall_write.c b/components/libc/compilers/dlib/syscall_write.c index ffee51882d..ab011c1096 100644 --- a/components/libc/compilers/dlib/syscall_write.c +++ b/components/libc/compilers/dlib/syscall_write.c @@ -9,11 +9,11 @@ */ #include <rtthread.h> -#ifdef RT_USING_DFS -#include <dfs_posix.h> -#endif #include <yfuns.h> +#include <unistd.h> +#ifdef RT_USING_POSIX #include "libc.h" +#endif #define DBG_TAG "dlib.syscall_write" #define DBG_LVL DBG_INFO @@ -23,16 +23,12 @@ size_t __write(int handle, const unsigned char *buf, size_t len) { -#ifdef RT_USING_DFS +#ifdef RT_USING_POSIX int size; -#endif +#endif /* RT_USING_POSIX */ if ((handle == _LLIO_STDOUT) || (handle == _LLIO_STDERR)) { -#ifndef RT_USING_CONSOLE - return _LLIO_ERROR; -#else - #ifdef RT_USING_POSIX if (libc_stdio_get_console() < 0) { @@ -40,7 +36,7 @@ size_t __write(int handle, const unsigned char *buf, size_t len) return 0; } return write(STDOUT_FILENO, (void*)buf, len); -#else +#elif defined(RT_USING_CONSOLE) rt_device_t console_device; console_device = rt_console_get_device(); @@ -50,19 +46,19 @@ size_t __write(int handle, const unsigned char *buf, size_t len) } return len; -#endif -#endif +#else + return _LLIO_ERROR; +#endif /* RT_USING_POSIX */ } else if (handle == _LLIO_STDIN) { return _LLIO_ERROR; } -#ifndef RT_USING_DFS - return _LLIO_ERROR; -#else +#ifdef RT_USING_POSIX size = write(handle, buf, len); return size; -#endif +#else + return _LLIO_ERROR; +#endif /* RT_USING_POSIX */ } - diff --git a/components/libc/compilers/gcc/newlib/SConscript b/components/libc/compilers/gcc/newlib/SConscript index e0ee482747..ae50098dec 100644 --- a/components/libc/compilers/gcc/newlib/SConscript +++ b/components/libc/compilers/gcc/newlib/SConscript @@ -4,21 +4,14 @@ Import('rtconfig') src = [] cwd = GetCurrentDir() group = [] -LIBS = ['m'] +LIBS = ['m'] # link libm CPPDEFINES = ['RT_USING_NEWLIB'] CPPPATH = [cwd] if rtconfig.PLATFORM == 'gcc': if GetDepend('RT_USING_LIBC'): - # link with libc and libm: - # libm is a frequently used lib. Newlib is compiled with -ffunction-sections in - # recent GCC tool chains. The linker would just link in the functions that have - # been referenced. So setting this won't result in bigger text size. - LIBS += ['c'] - + LIBS += ['c'] # link libc src += Glob('*.c') - if GetDepend('RT_USING_MODULE') == False: - SrcRemove(src, ['libc_syms.c']) else: src += ['syscalls.c'] diff --git a/components/libc/compilers/gcc/newlib/libc.c b/components/libc/compilers/gcc/newlib/libc.c deleted file mode 100644 index 9834a9a015..0000000000 --- a/components/libc/compilers/gcc/newlib/libc.c +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2017/10/15 bernard the first version - */ -#include <rtthread.h> -#include <stdio.h> -#include <stdlib.h> -#include <fcntl.h> -#include <sys/time.h> - -#include "libc.h" - -#ifdef RT_USING_PTHREADS -#include <pthread.h> -#endif - -int _EXFUN(putenv,(char *__string)); - -int libc_system_init(void) -{ -#if defined(RT_USING_DFS) & defined(RT_USING_DFS_DEVFS) & defined(RT_USING_CONSOLE) - rt_device_t dev_console; - - dev_console = rt_console_get_device(); - if (dev_console) - { - #if defined(RT_USING_POSIX) - libc_stdio_set_console(dev_console->parent.name, O_RDWR); - #else - libc_stdio_set_console(dev_console->parent.name, O_WRONLY); - #endif - } - - /* set PATH and HOME */ - putenv("PATH=/bin"); - putenv("HOME=/home"); -#endif - -#if defined RT_USING_PTHREADS && !defined RT_USING_COMPONENTS_INIT - pthread_system_init(); -#endif - - return 0; -} -INIT_COMPONENT_EXPORT(libc_system_init); diff --git a/components/libc/compilers/gcc/newlib/libc.h b/components/libc/compilers/gcc/newlib/libc.h deleted file mode 100644 index c470698aa6..0000000000 --- a/components/libc/compilers/gcc/newlib/libc.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2017/10/15 bernard the first version - */ -#ifndef __RTT_LIBC_H__ -#define __RTT_LIBC_H__ - -#ifndef _EXFUN -#define _EXFUN(name, proto) name proto -#endif - -#ifdef __cplusplus -extern "C" { -#endif -int libc_system_init(void); -int libc_stdio_get_console(void); -int libc_stdio_set_console(const char* device_name, int mode); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/components/libc/compilers/gcc/newlib/stdio.c b/components/libc/compilers/gcc/newlib/stdio.c deleted file mode 100644 index 1875d50e3f..0000000000 --- a/components/libc/compilers/gcc/newlib/stdio.c +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (c) 2006-2021, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2017/10/15 bernard the first version - */ -#include <stdio.h> -#include <stdlib.h> -#include <fcntl.h> -#include <rtthread.h> -#include "libc.h" - -#define STDIO_DEVICE_NAME_MAX 32 - -int _EXFUN(fileno, (FILE *)); - -static FILE* std_console = NULL; - -int libc_stdio_set_console(const char* device_name, int mode) -{ - FILE *fp; - char name[STDIO_DEVICE_NAME_MAX]; - char *file_mode; - - snprintf(name, sizeof(name) - 1, "/dev/%s", device_name); - name[STDIO_DEVICE_NAME_MAX - 1] = '\0'; - - if (mode == O_RDWR) file_mode = "r+"; - else if (mode == O_WRONLY) file_mode = "wb"; - else file_mode = "rb"; - - fp = fopen(name, file_mode); - if (fp) - { - setvbuf(fp, NULL, _IONBF, 0); - - if (std_console) - { - fclose(std_console); - std_console = NULL; - } - std_console = fp; - - if (mode == O_RDWR) - { - _GLOBAL_REENT->_stdin = std_console; - } - else - { - _GLOBAL_REENT->_stdin = NULL; - } - - if (mode == O_RDONLY) - { - _GLOBAL_REENT->_stdout = NULL; - _GLOBAL_REENT->_stderr = NULL; - } - else - { - _GLOBAL_REENT->_stdout = std_console; - _GLOBAL_REENT->_stderr = std_console; - } - - _GLOBAL_REENT->__sdidinit = 1; - } - - if (std_console) return fileno(std_console); - - return -1; -} - -int libc_stdio_get_console(void) -{ - if (std_console) - return fileno(std_console); - else - return -1; -} diff --git a/components/libc/compilers/gcc/newlib/syscalls.c b/components/libc/compilers/gcc/newlib/syscalls.c index f5a2c16ecc..f95e695a85 100644 --- a/components/libc/compilers/gcc/newlib/syscalls.c +++ b/components/libc/compilers/gcc/newlib/syscalls.c @@ -13,9 +13,19 @@ #include <reent.h> #include <rtthread.h> +#include <stdio.h> +#include <string.h> #include <stddef.h> +#include <fcntl.h> #include <unistd.h> #include <sys/errno.h> +#include <sys/stat.h> +#ifdef RT_USING_POSIX +#include "libc.h" +#endif +#ifdef RT_USING_MODULE +#include <dlmodule.h> +#endif #define DBG_TAG "newlib.syscalls" #define DBG_LVL DBG_INFO @@ -83,18 +93,8 @@ void __libc_init_array(void) } #ifdef RT_USING_LIBC -#include <reent.h> -#include <stdio.h> -#include "libc.h" -#ifdef RT_USING_DFS -#include <dfs_posix.h> -#endif -#ifdef RT_USING_MODULE -#include <dlmodule.h> -#endif /* Reentrant versions of system calls. */ - #ifndef _REENT_ONLY int *__errno () { @@ -109,39 +109,29 @@ int _getpid_r(struct _reent *ptr) int _close_r(struct _reent *ptr, int fd) { -#ifndef RT_USING_DFS - /* return "not supported" */ - ptr->_errno = ENOTSUP; - return -1; -#else return close(fd); -#endif } int _execve_r(struct _reent *ptr, const char * name, char *const *argv, char *const *env) { - /* return "not supported" */ ptr->_errno = ENOTSUP; return -1; } int _fcntl_r(struct _reent *ptr, int fd, int cmd, int arg) { - /* return "not supported" */ ptr->_errno = ENOTSUP; return -1; } int _fork_r(struct _reent *ptr) { - /* return "not supported" */ ptr->_errno = ENOTSUP; return -1; } int _fstat_r(struct _reent *ptr, int fd, struct stat *pstat) { - /* return "not supported" */ ptr->_errno = ENOTSUP; return -1; } @@ -160,67 +150,73 @@ int _isatty_r(struct _reent *ptr, int fd) int _kill_r(struct _reent *ptr, int pid, int sig) { - /* return "not supported" */ ptr->_errno = ENOTSUP; return -1; } int _link_r(struct _reent *ptr, const char *old, const char *new) { - /* return "not supported" */ ptr->_errno = ENOTSUP; return -1; } -_off_t _lseek_r(struct _reent *ptr, int fd, _off_t pos, int whence) +int _wait_r(struct _reent *ptr, int *status) { -#ifndef RT_USING_DFS - /* return "not supported" */ ptr->_errno = ENOTSUP; return -1; -#else +} + +mode_t umask(mode_t mask) +{ + return 022; +} + +int flock(int fd, int operation) +{ + return 0; +} + +_off_t _lseek_r(struct _reent *ptr, int fd, _off_t pos, int whence) +{ +#ifdef RT_USING_POSIX _off_t rc; rc = lseek(fd, pos, whence); return rc; -#endif +#else + ptr->_errno = ENOTSUP; + return -1; +#endif /* RT_USING_POSIX */ } int _mkdir_r(struct _reent *ptr, const char *name, int mode) { -#ifndef RT_USING_DFS - /* return "not supported" */ - ptr->_errno = ENOTSUP; - return -1; -#else +#ifdef RT_USING_POSIX int rc; rc = mkdir(name, mode); return rc; -#endif +#else + ptr->_errno = ENOTSUP; + return -1; +#endif /* RT_USING_POSIX */ } int _open_r(struct _reent *ptr, const char *file, int flags, int mode) { -#ifndef RT_USING_DFS - /* return "not supported" */ - ptr->_errno = ENOTSUP; - return -1; -#else +#ifdef RT_USING_POSIX int rc; - rc = open(file, flags, mode); return rc; -#endif +#else + ptr->_errno = ENOTSUP; + return -1; +#endif /* RT_USING_POSIX */ } _ssize_t _read_r(struct _reent *ptr, int fd, void *buf, size_t nbytes) { -#ifndef RT_USING_DFS - /* return "not supported" */ - ptr->_errno = ENOTSUP; - return -1; -#else +#ifdef RT_USING_POSIX _ssize_t rc; if (libc_stdio_get_console() < 0 && fd == STDIN_FILENO) { @@ -229,74 +225,51 @@ _ssize_t _read_r(struct _reent *ptr, int fd, void *buf, size_t nbytes) } rc = read(fd, buf, nbytes); return rc; -#endif +#else + ptr->_errno = ENOTSUP; + return -1; +#endif /* RT_USING_POSIX */ } int _rename_r(struct _reent *ptr, const char *old, const char *new) { -#ifndef RT_USING_DFS - /* return "not supported" */ - ptr->_errno = ENOTSUP; - return -1; -#else +#ifdef RT_USING_POSIX int rc; rc = rename(old, new); return rc; -#endif +#else + ptr->_errno = ENOTSUP; + return -1; +#endif /* RT_USING_POSIX */ } int _stat_r(struct _reent *ptr, const char *file, struct stat *pstat) { -#ifndef RT_USING_DFS - /* return "not supported" */ - ptr->_errno = ENOTSUP; - return -1; -#else +#ifdef RT_USING_POSIX int rc; rc = stat(file, pstat); return rc; -#endif -} - -int _unlink_r(struct _reent *ptr, const char *file) -{ -#ifndef RT_USING_DFS - /* return "not supported" */ +#else ptr->_errno = ENOTSUP; return -1; -#else - return unlink(file); -#endif +#endif /* RT_USING_POSIX */ } -int _wait_r(struct _reent *ptr, int *status) +int _unlink_r(struct _reent *ptr, const char *file) { - /* return "not supported" */ +#ifdef RT_USING_POSIX + return unlink(file); +#else ptr->_errno = ENOTSUP; return -1; +#endif /* RT_USING_POSIX */ } _ssize_t _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes) { -#ifndef RT_USING_DFS -#if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE) - if (STDOUT_FILENO == fd) - { - rt_device_t console; - - console = rt_console_get_device(); - if (console) return rt_device_write(console, -1, buf, nbytes); - } - - return 0; -#else - /* return "not supported" */ - ptr->_errno = ENOTSUP; - return -1; -#endif /*RT_USING_DEVICE*/ -#else +#ifdef RT_USING_POSIX _ssize_t rc; if (libc_stdio_get_console() < 0 && fd == STDOUT_FILENO) { @@ -305,7 +278,18 @@ _ssize_t _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes) } rc = write(fd, buf, nbytes); return rc; -#endif +#elif defined(RT_USING_CONSOLE) + if (STDOUT_FILENO == fd) + { + rt_device_t console; + + console = rt_console_get_device(); + if (console) + return rt_device_write(console, -1, buf, nbytes); + } +#endif /* RT_USING_POSIX */ + ptr->_errno = ENOTSUP; + return -1; } /* for exit() and abort() */ @@ -316,16 +300,6 @@ __attribute__ ((noreturn)) void _exit (int status) while(1); } -mode_t umask(mode_t mask) -{ - return 022; -} - -int flock(int fd, int operation) -{ - return 0; -} - /* These functions are implemented and replaced by the 'common/time.c' file int _gettimeofday_r(struct _reent *ptr, struct timeval *__tp, void *__tzp); diff --git a/components/libc/posix/src/SConscript b/components/libc/posix/src/SConscript index 5640181ac5..f338b52cec 100644 --- a/components/libc/posix/src/SConscript +++ b/components/libc/posix/src/SConscript @@ -1,10 +1,13 @@ -# RT-Thread building script for component - from building import * +Import('rtconfig') -cwd = GetCurrentDir() src = Glob('*.c') +cwd = GetCurrentDir() +CPPPATH = [cwd] + +if rtconfig.ARCH == 'sim': # the vs simulator does not support select and poll + SrcRemove(src, ['select.c', 'poll.c']) -group = DefineGroup('POSIX', src, depend = ['RT_USING_POSIX']) +group = DefineGroup('POSIX', src, depend = ['RT_USING_POSIX'], CPPPATH = CPPPATH) Return('group') diff --git a/components/libc/posix/src/libc.c b/components/libc/posix/src/libc.c new file mode 100644 index 0000000000..068719d5d6 --- /dev/null +++ b/components/libc/posix/src/libc.c @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2017/10/15 bernard the first version + */ +#include <rtthread.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <fcntl.h> +#include <sys/time.h> +#include "libc.h" +#include <stdio.h> +#include <stdlib.h> +#ifdef RT_USING_PTHREADS +#include <pthread.h> +#endif + +int libc_system_init(void) +{ +#ifdef RT_USING_POSIX + rt_device_t dev_console; + + dev_console = rt_console_get_device(); + if (dev_console) + { + libc_stdio_set_console(dev_console->parent.name, O_RDWR); + } +#endif /* RT_USING_POSIX */ + +#if defined RT_USING_PTHREADS && !defined RT_USING_COMPONENTS_INIT + pthread_system_init(); +#endif + + return 0; +} +INIT_COMPONENT_EXPORT(libc_system_init); + +#ifdef RT_USING_POSIX + +#if defined(RT_USING_LIBC) && defined(RT_USING_NEWLIB) +#define STDIO_DEVICE_NAME_MAX 32 +static FILE* std_console = NULL; +int libc_stdio_set_console(const char* device_name, int mode) +{ + FILE *fp; + char name[STDIO_DEVICE_NAME_MAX]; + char *file_mode; + + snprintf(name, sizeof(name) - 1, "/dev/%s", device_name); + name[STDIO_DEVICE_NAME_MAX - 1] = '\0'; + + if (mode == O_RDWR) + { + file_mode = "r+"; + } + else if (mode == O_WRONLY) + { + file_mode = "wb"; + } + else + { + file_mode = "rb"; + } + + fp = fopen(name, file_mode); + if (fp) + { + setvbuf(fp, NULL, _IONBF, 0); + + if (std_console) + { + fclose(std_console); + std_console = NULL; + } + std_console = fp; + + if (mode == O_RDWR) + { + _GLOBAL_REENT->_stdin = std_console; + } + else + { + _GLOBAL_REENT->_stdin = NULL; + } + + if (mode == O_RDONLY) + { + _GLOBAL_REENT->_stdout = NULL; + _GLOBAL_REENT->_stderr = NULL; + } + else + { + _GLOBAL_REENT->_stdout = std_console; + _GLOBAL_REENT->_stderr = std_console; + } + + _GLOBAL_REENT->__sdidinit = 1; + } + + if (std_console) + return fileno(std_console); + + return -1; +} + +int libc_stdio_get_console(void) +{ + if (std_console) + return fileno(std_console); + else + return -1; +} + +#else +#define STDIO_DEVICE_NAME_MAX 32 +static int std_fd = -1; +int libc_stdio_set_console(const char* device_name, int mode) +{ + int fd; + char name[STDIO_DEVICE_NAME_MAX]; + + snprintf(name, sizeof(name) - 1, "/dev/%s", device_name); + name[STDIO_DEVICE_NAME_MAX - 1] = '\0'; + + fd = open(name, mode, 0); + if (fd >= 0) + { + if (std_fd >= 0) + { + close(std_fd); + } + std_fd = fd; + } + + return std_fd; +} + +int libc_stdio_get_console(void) { + return std_fd; +} +#endif /* defined(RT_USING_LIBC) && defined(RT_USING_NEWLIB) */ +#endif /* RT_USING_POSIX */ diff --git a/components/libc/compilers/dlib/libc.h b/components/libc/posix/src/libc.h index 9643c1161f..f81380bf78 100644 --- a/components/libc/compilers/dlib/libc.h +++ b/components/libc/posix/src/libc.h @@ -7,17 +7,20 @@ * Date Author Notes * 2017/10/15 bernard the first version */ - #ifndef __RTT_LIBC_H__ #define __RTT_LIBC_H__ +#include <rtconfig.h> + #ifdef __cplusplus extern "C" { #endif int libc_system_init(void); +#ifdef RT_USING_POSIX int libc_stdio_get_console(void); int libc_stdio_set_console(const char* device_name, int mode); +#endif /* RT_USING_POSIX */ #ifdef __cplusplus } |
