[libc] 调整文件结构

This commit is contained in:
Meco Man
2022-01-14 23:31:59 -05:00
committed by Bernard Xiong
parent 1a46d7fe92
commit 76cab376ad
21 changed files with 4 additions and 18 deletions

View File

@@ -0,0 +1,4 @@
# NEWLIB (GCC) porting for RT-Thread
https://sourceware.org/newlib/libc.html#Reentrancy

View File

@@ -0,0 +1,31 @@
import os
from building import *
from gcc import *
Import('rtconfig')
src = []
cwd = GetCurrentDir()
group = []
LIBS = ['m'] # link libm
CPPPATH = [cwd]
if rtconfig.PLATFORM == 'gcc':
LIBS += ['c'] # link libc
src += Glob('*.c')
#report newlib version
print('Newlib version:' + GetNewLibVersion(rtconfig))
# identify this is Newlib, and only enable POSIX.1-1990
CPPDEFINES = ['RT_USING_NEWLIB', '_POSIX_C_SOURCE=1']
group = DefineGroup('Compiler', src, depend = [''], CPPPATH = CPPPATH, CPPDEFINES = CPPDEFINES, LIBS = LIBS)
list = os.listdir(cwd)
for d in list:
path = os.path.join(cwd, d)
if os.path.isfile(os.path.join(path, 'SConscript')):
group = group + SConscript(os.path.join(d, 'SConscript'))
Return('group')

View File

@@ -0,0 +1,24 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-09-02 Meco Man First version
*/
#ifndef __FCNTL_H__
#define __FCNTL_H__
#include <sys/_default_fcntl.h>
#ifndef O_DIRECTORY
#define O_DIRECTORY 0x200000
#endif
#ifndef O_BINARY
#define O_BINARY 0x10000
#endif
#endif

View File

@@ -0,0 +1,15 @@
# RT-Thread building script for bridge
import os
from building import *
cwd = GetCurrentDir()
objs = []
list = os.listdir(cwd)
for d in list:
path = os.path.join(cwd, d)
if os.path.isfile(os.path.join(path, 'SConscript')):
objs = objs + SConscript(os.path.join(d, 'SConscript'))
Return('objs')

View File

@@ -0,0 +1,20 @@
from shutil import copy
from building import *
from gcc import *
Import('rtconfig')
src = []
cwd = GetCurrentDir()
CPPPATH = [cwd]
group = []
# sys/select.h does not exist in newlib 2.1.0 or lower version
if rtconfig.PLATFORM == 'gcc' and (CheckHeader(rtconfig, 'sys/select.h') == False):
try:
copy("../../../../common/nogcc/sys/select.h", "./sys/select.h") # copy from 'nogcc/sys/select.h'
except:
pass
group = DefineGroup('Compiler', src, depend = [''], CPPPATH = CPPPATH)
Return('group')

View File

@@ -0,0 +1,2 @@
Newlib 2.2.0 or lower version does not provide `sys/select.h`, and `fd_set` is defined in `sys/types.h`. It will be generated by scons automatically, and **DO NOT** change it.

View File

@@ -0,0 +1,58 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-07-21 Meco Man The first version
*/
#ifndef __SYS_SELECT_H__
#define __SYS_SELECT_H__
#include <rtconfig.h>
#include <sys/types.h>
#include <sys/time.h>
#ifdef _WIN32
#include <winsock.h>
#endif
#ifndef FD_SETSIZE
#define FD_SETSIZE 32
#endif
#ifdef SAL_USING_POSIX
#ifdef FD_SETSIZE
#undef FD_SETSIZE
#endif
#define FD_SETSIZE DFS_FD_MAX
#endif /* SAL_USING_POSIX */
typedef long fd_mask;
#ifndef _WIN32
#ifndef _SYS_TYPES_FD_SET /* MIPS */
#define NBBY 8 /* number of bits in a byte */
#define NFDBITS (sizeof (fd_mask) * NBBY) /* bits per mask */
#ifndef howmany
#define howmany(x,y) (((x)+((y)-1))/(y))
#endif
typedef struct _types_fd_set {
fd_mask fds_bits[howmany(FD_SETSIZE, NFDBITS)];
} _types_fd_set;
#define fd_set _types_fd_set
#define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1L << ((n) % NFDBITS)))
#define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1L << ((n) % NFDBITS)))
#define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1L << ((n) % NFDBITS)))
#define FD_ZERO(p) memset((void*)(p), 0, sizeof(*(p)))
#endif /* _SYS_TYPES_FD_SET */
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
#endif /* _WIN32 */
#endif /* __SYS_SELECT_H__ */

View File

@@ -0,0 +1,53 @@
/*
* 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 <rtm.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
RTM_EXPORT(strcpy);
RTM_EXPORT(strncpy);
RTM_EXPORT(strlen);
RTM_EXPORT(strcat);
RTM_EXPORT(strstr);
RTM_EXPORT(strchr);
RTM_EXPORT(strcmp);
RTM_EXPORT(strtol);
RTM_EXPORT(strtoul);
RTM_EXPORT(strncmp);
RTM_EXPORT(memcpy);
RTM_EXPORT(memcmp);
RTM_EXPORT(memmove);
RTM_EXPORT(memset);
RTM_EXPORT(memchr);
RTM_EXPORT(putchar);
RTM_EXPORT(puts);
RTM_EXPORT(printf);
RTM_EXPORT(sprintf);
RTM_EXPORT(snprintf);
RTM_EXPORT(fwrite);
#include <setjmp.h>
RTM_EXPORT(longjmp);
RTM_EXPORT(setjmp);
RTM_EXPORT(exit);
RTM_EXPORT(abort);
RTM_EXPORT(rand);
#include <assert.h>
RTM_EXPORT(__assert_func);

View File

@@ -0,0 +1,17 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-02-16 Meco Man The first version
*/
#ifndef _MACHTIME_H_
#define _MACHTIME_H_
#include <rtconfig.h>
#define _CLOCKS_PER_SEC_ RT_TICK_PER_SECOND
#endif /* _MACHTIME_H_ */

View File

@@ -0,0 +1,339 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-02-11 Meco Man remove _gettimeofday_r() and _times_r()
* 2020-02-13 Meco Man re-implement exit() and abort()
* 2020-02-21 Meco Man improve and beautify syscalls
* 2020-02-24 Meco Man fix bug of _isatty_r()
*/
#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_STDIO
#include "libc.h"
#endif /* RT_USING_POSIX_STDIO */
#ifdef RT_USING_MODULE
#include <dlmodule.h>
#endif /* RT_USING_MODULE */
#include <compiler_private.h>
#define DBG_TAG "newlib.syscalls"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#ifdef RT_USING_HEAP /* Memory routine */
void *_malloc_r (struct _reent *ptr, size_t size)
{
void* result;
result = (void*)rt_malloc (size);
if (result == RT_NULL)
{
ptr->_errno = ENOMEM;
}
return result;
}
void *_realloc_r (struct _reent *ptr, void *old, size_t newlen)
{
void* result;
result = (void*)rt_realloc (old, newlen);
if (result == RT_NULL)
{
ptr->_errno = ENOMEM;
}
return result;
}
void *_calloc_r (struct _reent *ptr, size_t size, size_t len)
{
void* result;
result = (void*)rt_calloc (size, len);
if (result == RT_NULL)
{
ptr->_errno = ENOMEM;
}
return result;
}
void _free_r (struct _reent *ptr, void *addr)
{
rt_free (addr);
}
#else
void *
_sbrk_r(struct _reent *ptr, ptrdiff_t incr)
{
LOG_E("Please enable RT_USING_HEAP");
RT_ASSERT(0);
return RT_NULL;
}
#endif /*RT_USING_HEAP*/
void __libc_init_array(void)
{
/* we not use __libc init_aray to initialize C++ objects */
/* __libc_init_array is ARM code, not Thumb; it will cause a hardfault. */
}
/* Reentrant versions of system calls. */
#ifndef _REENT_ONLY
int *__errno ()
{
return _rt_errno();
}
#endif
int _getpid_r(struct _reent *ptr)
{
return 0;
}
int _close_r(struct _reent *ptr, int fd)
{
#ifdef DFS_USING_POSIX
return close(fd);
#else
LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
ptr->_errno = ENOTSUP;
return -1;
#endif /* DFS_USING_POSIX */
}
int _execve_r(struct _reent *ptr, const char * name, char *const *argv, char *const *env)
{
ptr->_errno = ENOTSUP;
return -1;
}
int _fcntl_r(struct _reent *ptr, int fd, int cmd, int arg)
{
ptr->_errno = ENOTSUP;
return -1;
}
int _fork_r(struct _reent *ptr)
{
ptr->_errno = ENOTSUP;
return -1;
}
int _fstat_r(struct _reent *ptr, int fd, struct stat *pstat)
{
ptr->_errno = ENOTSUP;
return -1;
}
int _isatty_r(struct _reent *ptr, int fd)
{
if (fd >=0 && fd < 3)
{
return 1;
}
else
{
return 0;
}
}
int _kill_r(struct _reent *ptr, int pid, int sig)
{
ptr->_errno = ENOTSUP;
return -1;
}
int _link_r(struct _reent *ptr, const char *old, const char *new)
{
ptr->_errno = ENOTSUP;
return -1;
}
int _wait_r(struct _reent *ptr, int *status)
{
ptr->_errno = ENOTSUP;
return -1;
}
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 DFS_USING_POSIX
_off_t rc;
rc = lseek(fd, pos, whence);
return rc;
#else
LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
ptr->_errno = ENOTSUP;
return -1;
#endif /* DFS_USING_POSIX */
}
int _mkdir_r(struct _reent *ptr, const char *name, int mode)
{
#ifdef DFS_USING_POSIX
int rc;
rc = mkdir(name, mode);
return rc;
#else
LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
ptr->_errno = ENOTSUP;
return -1;
#endif /* DFS_USING_POSIX */
}
int _open_r(struct _reent *ptr, const char *file, int flags, int mode)
{
#ifdef DFS_USING_POSIX
int rc;
rc = open(file, flags, mode);
return rc;
#else
LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
ptr->_errno = ENOTSUP;
return -1;
#endif /* DFS_USING_POSIX */
}
_ssize_t _read_r(struct _reent *ptr, int fd, void *buf, size_t nbytes)
{
#ifdef DFS_USING_POSIX
_ssize_t rc;
if (fd == STDIN_FILENO)
{
#ifdef RT_USING_POSIX_STDIO
if (libc_stdio_get_console() < 0)
{
LOG_W("Do not invoke standard input before initializing Compiler");
return 0;
}
#else
LOG_W("%s: %s", __func__, _WARNING_WITHOUT_STDIO);
ptr->_errno = ENOTSUP;
return -1;
#endif /* RT_USING_POSIX_STDIO */
}
else if (fd == STDOUT_FILENO || fd == STDERR_FILENO)
{
ptr->_errno = ENOTSUP;
return -1;
}
rc = read(fd, buf, nbytes);
return rc;
#else
LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
ptr->_errno = ENOTSUP;
return -1;
#endif /* DFS_USING_POSIX */
}
int _rename_r(struct _reent *ptr, const char *old, const char *new)
{
#ifdef DFS_USING_POSIX
int rc;
rc = rename(old, new);
return rc;
#else
LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
ptr->_errno = ENOTSUP;
return -1;
#endif /* DFS_USING_POSIX */
}
int _stat_r(struct _reent *ptr, const char *file, struct stat *pstat)
{
#ifdef DFS_USING_POSIX
int rc;
rc = stat(file, pstat);
return rc;
#else
LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
ptr->_errno = ENOTSUP;
return -1;
#endif /* DFS_USING_POSIX */
}
int _unlink_r(struct _reent *ptr, const char *file)
{
#ifdef DFS_USING_POSIX
return unlink(file);
#else
LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
ptr->_errno = ENOTSUP;
return -1;
#endif /* DFS_USING_POSIX */
}
_ssize_t _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes)
{
#ifdef DFS_USING_POSIX
_ssize_t rc;
#endif /* DFS_USING_POSIX */
if (fd == STDOUT_FILENO || fd == STDERR_FILENO)
{
#if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
rt_device_t console;
console = rt_console_get_device();
if (console)
return rt_device_write(console, -1, buf, nbytes);
#else
ptr->_errno = ENOTSUP;
return -1;
#endif /* defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE) */
}
else if (fd == STDIN_FILENO)
{
ptr->_errno = ENOTSUP;
return -1;
}
#ifdef DFS_USING_POSIX
rc = write(fd, buf, nbytes);
return rc;
#else
LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS);
ptr->_errno = ENOTSUP;
return -1;
#endif /* DFS_USING_POSIX */
}
/* for exit() and abort() */
__attribute__ ((noreturn)) void _exit (int status)
{
extern void __rt_libc_exit(int status);
__rt_libc_exit(status);
while(1);
}
/*
These functions are implemented and replaced by the 'common/time.c' file
int _gettimeofday_r(struct _reent *ptr, struct timeval *__tp, void *__tzp);
_CLOCK_T_ _times_r(struct _reent *ptr, struct tms *ptms);
*/