[libc][musl] support arm-linux-musleabi toolchain

目前基本功能已经可以在QEMU上跑起来,但是pthread和内核signal选中会报冲突。先合并一版本,后续解决这些问题。
This commit is contained in:
Man, Jianting (Meco) 2022-11-22 21:40:50 -05:00 committed by GitHub
parent fc83546cf5
commit bdd663a33f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 247 additions and 126 deletions

View File

@ -26,56 +26,45 @@ with open(automac_h_fn, 'w') as f:
f.write(header + get_mac_address() + end)
# toolchains options
ARCH='arm'
CPU='cortex-a'
CROSS_TOOL='gcc'
if os.getenv('RTT_CC'):
CROSS_TOOL = os.getenv('RTT_CC')
# only support GNU GCC compiler.
ARCH ='arm'
CPU ='cortex-a'
CROSS_TOOL = 'gcc'
PLATFORM = 'gcc'
EXEC_PATH = r'/usr/bin'
if os.getenv('RTT_EXEC_PATH'):
EXEC_PATH = os.getenv('RTT_EXEC_PATH')
BUILD = 'debug'
EXEC_PATH = os.getenv('RTT_EXEC_PATH') or r'/usr/bin'
BUILD = 'debug'
if PLATFORM == 'gcc':
# toolchains
PREFIX = 'arm-none-eabi-'
CC = PREFIX + 'gcc'
CXX = PREFIX + 'g++'
AS = PREFIX + 'gcc'
AR = PREFIX + 'ar'
LINK = PREFIX + 'gcc'
PREFIX = os.getenv('RTT_CC_PREFIX') or 'arm-none-eabi-'
CC = PREFIX + 'gcc'
CXX = PREFIX + 'g++'
AS = PREFIX + 'gcc'
AR = PREFIX + 'ar'
LINK = PREFIX + 'gcc'
TARGET_EXT = 'elf'
SIZE = PREFIX + 'size'
SIZE = PREFIX + 'size'
OBJDUMP = PREFIX + 'objdump'
OBJCPY = PREFIX + 'objcopy'
STRIP = PREFIX + 'strip'
OBJCPY = PREFIX + 'objcopy'
STRIP = PREFIX + 'strip'
CFPFLAGS = ' -msoft-float'
AFPFLAGS = ' -mfloat-abi=softfp -mfpu=neon'
DEVICE = ' -march=armv7-a -mtune=cortex-a7 -ftree-vectorize -ffast-math -funwind-tables -fno-strict-aliasing'
DEVICE = ' -march=armv7-a -marm -msoft-float'
CFLAGS = DEVICE + ' -Wall -Werror'
AFLAGS = ' -c' + DEVICE + ' -x assembler-with-cpp -D__ASSEMBLY__ -I.'
LINK_SCRIPT = 'link.lds'
LFLAGS = DEVICE + ' -nostartfiles -Wl,--gc-sections,-Map=rtthread.map,-cref,-u,system_vectors'+\
' -T %s' % LINK_SCRIPT
CPATH = ''
LPATH = ''
# generate debug info in all cases
AFLAGS += ' -gdwarf-2'
CFLAGS += ' -g -gdwarf-2'
CXXFLAGS= DEVICE + CFPFLAGS + ' -Wall'
CFLAGS = DEVICE + CFPFLAGS + ' -Wall -std=gnu99'
AFLAGS = DEVICE + ' -c' + AFPFLAGS + ' -x assembler-with-cpp'
LFLAGS = DEVICE + ' -Wl,--gc-sections,-Map=rtthread.map,-cref,-u,system_vectors -T link.lds' + ' -lsupc++ -lgcc -static'
CPATH = ''
LPATH = ''
if BUILD == 'debug':
CFLAGS += ' -O0'
CFLAGS += ' -O0 -gdwarf-2'
CXXFLAGS += ' -O0 -gdwarf-2'
AFLAGS += ' -gdwarf-2'
else:
CFLAGS += ' -O2'
CXXFLAGS = CFLAGS + ' -Woverloaded-virtual -fno-exceptions -fno-rtti'
CFLAGS += ' -Os'
CXXFLAGS += ' -Os'
CXXFLAGS += ' -Woverloaded-virtual -fno-exceptions -fno-rtti'
M_CFLAGS = CFLAGS + ' -mlong-calls -fPIC '
M_CXXFLAGS = CXXFLAGS + ' -mlong-calls -fPIC'
@ -83,5 +72,6 @@ if PLATFORM == 'gcc':
' -shared -fPIC -nostartfiles -nostdlib -static-libgcc'
M_POST_ACTION = STRIP + ' -R .hash $TARGET\n' + SIZE + ' $TARGET \n'
DUMP_ACTION = OBJDUMP + ' -D -S $TARGET > rtt.asm\n'
POST_ACTION = OBJCPY + ' -O binary $TARGET rtthread.bin\n' +\
SIZE + ' $TARGET \n'

View File

@ -762,7 +762,7 @@ RTM_EXPORT(telldir);
* @param d the directory stream.
* @param offset the offset in directory stream.
*/
void seekdir(DIR *d, off_t offset)
void seekdir(DIR *d, long offset)
{
struct dfs_fd *fd;

View File

@ -12,7 +12,7 @@
#ifndef __SYS_SELECT_H__
#define __SYS_SELECT_H__
#include <rtthread.h>
#include <rtconfig.h>
#include <sys/types.h>
#include <sys/time.h>

View File

@ -14,12 +14,11 @@
#ifdef __cplusplus
extern "C" {
#endif
#endif /* __cplusplus */
#include <stdint.h>
#include <sys/types.h>
/* sigev_notify values
NOTE: P1003.1c/D10, p. 34 adds SIGEV_THREAD. */
@ -54,7 +53,6 @@ struct siginfo
{
uint16_t si_signo;
uint16_t si_code;
union sigval si_value;
};
typedef struct siginfo siginfo_t;
@ -216,17 +214,16 @@ int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
#define SIGUSR2 31 /* user defined signal 2 */
#define NSIG 32 /* signal 0 implied */
#ifndef _SIGNAL_H_
#if defined(RT_USING_NEWLIBC)
/* Some applications take advantage of the fact that <sys/signal.h>
* and <signal.h> are equivalent in glibc. Allow for that here. */
#include <signal.h>
#endif
#endif
#endif /* defined(RT_USING_NEWLIBC) */
#endif /* __ARMCC_VERSION */
#ifdef __cplusplus
}
#endif
#endif /* __cplusplus */
#endif
#endif /* __SYS_SIGNAL_H__ */

View File

@ -22,7 +22,7 @@ typedef __time64_t time_t;
#ifdef __cplusplus
extern "C" {
#endif
#endif /* __cplusplus */
#undef CLOCKS_PER_SEC
#define CLOCKS_PER_SEC RT_TICK_PER_SECOND
@ -79,22 +79,21 @@ int stime(const time_t *t);
time_t timegm(struct tm * const t);
int gettimeofday(struct timeval *tv, struct timezone *tz);
int settimeofday(const struct timeval *tv, const struct timezone *tz);
#if defined(__ARMCC_VERSION) || defined (__ICCARM__)
#if defined(__ARMCC_VERSION) || defined (__ICCARM__) || defined(_WIN32)
struct tm *gmtime_r(const time_t *timep, struct tm *r);
struct tm* localtime_r(const time_t* t, struct tm* r);
char* asctime_r(const struct tm *t, char *buf);
char *ctime_r(const time_t * tim_p, char * result);
#elif defined(_WIN32)
struct tm* gmtime_r(const time_t* timep, struct tm* r);
struct tm* gmtime(const time_t* t);
struct tm* localtime_r(const time_t* t, struct tm* r);
#endif /* defined(__ARMCC_VERSION) || defined (__ICCARM__) || defined(_WIN32) */
#ifdef _WIN32
struct tm* gmtime(const time_t* t);
struct tm* localtime(const time_t* t);
time_t mktime(struct tm* const t);
char* asctime_r(const struct tm* t, char* buf);
char* ctime_r(const time_t* tim_p, char* result);
char* ctime(const time_t* tim_p);
time_t time(time_t* t);
#endif
#endif /* _WIN32 */
#ifdef RT_USING_POSIX_DELAY
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
@ -112,20 +111,21 @@ int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
#ifndef CLOCK_REALTIME
#define CLOCK_REALTIME 1
#endif
#endif /* CLOCK_REALTIME */
#define CLOCK_CPUTIME_ID 2
#ifndef CLOCK_PROCESS_CPUTIME_ID
#define CLOCK_PROCESS_CPUTIME_ID CLOCK_CPUTIME_ID
#endif
#endif /* CLOCK_PROCESS_CPUTIME_ID */
#ifndef CLOCK_THREAD_CPUTIME_ID
#define CLOCK_THREAD_CPUTIME_ID CLOCK_CPUTIME_ID
#endif
#endif /* CLOCK_THREAD_CPUTIME_ID */
#ifndef CLOCK_MONOTONIC
#define CLOCK_MONOTONIC 4
#endif
#endif /* CLOCK_MONOTONIC */
#endif /* defined(RT_USING_POSIX_CLOCK) || defined (RT_USING_POSIX_TIMER) */
#ifdef RT_USING_POSIX_CLOCK
@ -142,8 +142,7 @@ int timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid);
int timer_delete(timer_t timerid);
int timer_getoverrun(timer_t timerid);
int timer_gettime(timer_t timerid, struct itimerspec *its);
int timer_settime(timer_t timerid, int flags, const struct itimerspec *value,
struct itimerspec *ovalue);
int timer_settime(timer_t timerid, int flags, const struct itimerspec *value, struct itimerspec *ovalue);
#endif /* RT_USING_POSIX_TIMER */
/* timezone */
@ -153,6 +152,6 @@ int8_t tz_is_dst(void);
#ifdef __cplusplus
}
#endif
#endif /* __cplusplus */
#endif /* _SYS_TIME_H_ */

View File

@ -0,0 +1,30 @@
import os
from building import *
from gcc import *
Import('rtconfig')
group = []
libc_name, libc_version = GetGCCLibcNameVersion(rtconfig)
if libc_name == 'musl':
print('Musl version: ' + libc_version)
cwd = GetCurrentDir()
src = Glob('*.c')
CPPPATH = [cwd]
CPPDEFINES = ['RT_USING_MUSLLIBC', 'RT_USING_LIBC']
LIBS = ['c', 'gcc']
LINKFLAGS = ' --specs=kernel.specs'
AddDepend(['RT_USING_MUSLLIBC', 'RT_USING_LIBC'])
group = group + DefineGroup('Compiler', src, depend = [''], CPPPATH = CPPPATH, LINKFLAGS = LINKFLAGS, 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,20 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-11-18 Meco Man first version
*/
#include <rtthread.h>
int *__errno_location(void)
{
return _rt_errno();
}
int *___errno_location(void)
{
return _rt_errno();
}

View File

@ -3,28 +3,27 @@ from building import *
from gcc import *
Import('rtconfig')
src = []
cwd = GetCurrentDir()
group = []
LIBS = []
CPPPATH = [cwd]
if rtconfig.PLATFORM in ['gcc']:
LIBS += ['c', 'm'] # link libc and libm
src += Glob('*.c')
libc_name, libc_version = GetGCCLibcNameVersion(rtconfig)
#report newlib version
print('Newlib version:' + GetNewLibVersion(rtconfig))
if libc_name == 'newlib':
print('Newlib version: ' + libc_version)
# identify this is Newlib, and only enable POSIX.1-1990
CPPDEFINES = ['RT_USING_NEWLIBC', 'RT_USING_LIBC', '_POSIX_C_SOURCE=1']
cwd = GetCurrentDir()
src = Glob('*.c')
CPPPATH = [cwd]
CPPDEFINES = ['RT_USING_NEWLIBC', 'RT_USING_LIBC', '_POSIX_C_SOURCE=1'] # identify this is Newlib, and only enable POSIX.1-1990
LIBS = ['c', 'm'] # link libc and libm
AddDepend(['RT_USING_NEWLIBC', 'RT_USING_LIBC'])
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'))
group = 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

@ -18,6 +18,8 @@
#include <sys/errno.h>
#include "libc.h"
#define STDIO_DEVICE_NAME_MAX 32
int libc_system_init(void)
{
#ifdef RT_USING_POSIX_STDIO
@ -34,7 +36,7 @@ int libc_system_init(void)
INIT_COMPONENT_EXPORT(libc_system_init);
#if defined(RT_USING_POSIX_STDIO) && defined(RT_USING_NEWLIBC)
#define STDIO_DEVICE_NAME_MAX 32
static FILE* std_console = NULL;
int libc_stdio_set_console(const char* device_name, int mode)
{
@ -42,7 +44,7 @@ int libc_stdio_set_console(const char* device_name, int mode)
char name[STDIO_DEVICE_NAME_MAX];
char *file_mode;
snprintf(name, sizeof(name) - 1, "/dev/%s", device_name);
rt_snprintf(name, sizeof(name) - 1, "/dev/%s", device_name);
name[STDIO_DEVICE_NAME_MAX - 1] = '\0';
if (mode == O_RDWR)
@ -107,15 +109,71 @@ int libc_stdio_get_console(void)
return -1;
}
#elif defined(RT_USING_POSIX_STDIO) && defined(RT_USING_MUSLLIBC)
static FILE* std_console = NULL;
int sys_dup2(int oldfd, int new);
int libc_stdio_set_console(const char* device_name, int mode)
{
FILE *fp;
char name[STDIO_DEVICE_NAME_MAX];
char *file_mode;
rt_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 (std_console)
{
int fd = fileno(std_console);
// /* set fd (0, 1, 2) */
// sys_dup2(fd, 0);
// sys_dup2(fd, 1);
// sys_dup2(fd, 2);
return fd;
}
return -1;
}
int libc_stdio_get_console(void)
{
int ret = -1;
if (std_console)
{
ret = fileno(std_console);
}
return ret;
}
#elif defined(RT_USING_POSIX_STDIO)
#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);
rt_snprintf(name, sizeof(name) - 1, "/dev/%s", device_name);
name[STDIO_DEVICE_NAME_MAX - 1] = '\0';
fd = open(name, mode, 0);

View File

@ -550,8 +550,9 @@ typedef struct rt_timer *rt_timer_t;
#ifdef RT_USING_SIGNALS
#include <sys/signal.h>
typedef unsigned long rt_sigset_t;
typedef void (*rt_sighandler_t)(int signo);
typedef siginfo_t rt_siginfo_t;
typedef void (*rt_sighandler_t)(int signo);
#define RT_SIG_MAX 32
#endif /* RT_USING_SIGNALS */

View File

@ -69,7 +69,6 @@ def CheckHeader(rtconfig, filename):
def GetNewLibVersion(rtconfig):
version = 'unknown'
root = GetGCCRoot(rtconfig)
if CheckHeader(rtconfig, '_newlib_version.h'): # get version from _newlib_version.h file
f = open(os.path.join(root, 'include', '_newlib_version.h'), 'r')
if f:
@ -86,6 +85,33 @@ def GetNewLibVersion(rtconfig):
f.close()
return version
# FIXME: it's not very good
def CheckMUSLLibc(rtconfig):
if 'musl' in rtconfig.PREFIX:
return True
return False
# FIXME: there is no musl version or musl macros can be found officially
def GetMuslVersion(rtconfig):
version = 'unknown'
# root = GetGCCRoot(rtconfig)
# print(root)
return version
# return libc name and version
def GetGCCLibcNameVersion(rtconfig):
if rtconfig.PLATFORM != 'gcc':
return ('unknown', 'unknown')
newlib_version = GetNewLibVersion(rtconfig)
if newlib_version != 'unknown':
return ('newlib', newlib_version) # libc: newlib, version: newlib_version
elif CheckMUSLLibc(rtconfig) == True:
GetMuslVersion(rtconfig)
return ('musl', 'unknown') #libc: musl, version: unknown
else:
return ('unknown', 'unknown') # libc: unknown, version: unknown
def GCCResult(rtconfig, str):
import subprocess
@ -173,43 +199,44 @@ def GCCResult(rtconfig, str):
return result
def GenerateGCCConfig(rtconfig):
str = ''
cc_header = ''
cc_header += '#ifndef CCONFIG_H__\n'
cc_header += '#define CCONFIG_H__\n'
cc_header += '/* Automatically generated file; DO NOT EDIT. */\n'
cc_header += '/* compiler configure file for RT-Thread in GCC*/\n\n'
# str = ''
# cc_header = ''
# cc_header += '#ifndef CCONFIG_H__\n'
# cc_header += '#define CCONFIG_H__\n'
# cc_header += '/* Automatically generated file; DO NOT EDIT. */\n'
# cc_header += '/* compiler configure file for RT-Thread in GCC*/\n\n'
if CheckHeader(rtconfig, 'newlib.h'):
str += '#include <newlib.h>\n'
cc_header += '#define HAVE_NEWLIB_H 1\n'
cc_header += '#define LIBC_VERSION "newlib %s"\n\n' % GetNewLibVersion(rtconfig)
# if CheckHeader(rtconfig, 'newlib.h'):
# str += '#include <newlib.h>\n'
# cc_header += '#define HAVE_NEWLIB_H 1\n'
# cc_header += '#define LIBC_VERSION "newlib %s"\n\n' % GetNewLibVersion(rtconfig)
if CheckHeader(rtconfig, 'sys/signal.h'):
str += '#include <sys/signal.h>\n'
cc_header += '#define HAVE_SYS_SIGNAL_H 1\n'
if CheckHeader(rtconfig, 'sys/select.h'):
str += '#include <sys/select.h>\n'
cc_header += '#define HAVE_SYS_SELECT_H 1\n'
if CheckHeader(rtconfig, 'pthread.h'):
str += "#include <pthread.h>\n"
cc_header += '#define HAVE_PTHREAD_H 1\n'
# if CheckHeader(rtconfig, 'sys/signal.h'):
# str += '#include <sys/signal.h>\n'
# cc_header += '#define HAVE_SYS_SIGNAL_H 1\n'
# if CheckHeader(rtconfig, 'sys/select.h'):
# str += '#include <sys/select.h>\n'
# cc_header += '#define HAVE_SYS_SELECT_H 1\n'
# if CheckHeader(rtconfig, 'pthread.h'):
# str += "#include <pthread.h>\n"
# cc_header += '#define HAVE_PTHREAD_H 1\n'
# if CheckHeader(rtconfig, 'sys/dirent.h'):
# str += '#include <sys/dirent.h>\n'
# # if CheckHeader(rtconfig, 'sys/dirent.h'):
# # str += '#include <sys/dirent.h>\n'
# add some common features
str += 'const char* version = __VERSION__;\n'
str += 'const int iso_c_visible = __ISO_C_VISIBLE;\n'
str += '\n#ifdef HAVE_INITFINI_ARRAY\n'
str += 'const int init_fini_array = HAVE_INITFINI_ARRAY;\n'
str += '#endif\n'
# # add some common features
# str += 'const char* version = __VERSION__;\n'
# str += 'const int iso_c_visible = __ISO_C_VISIBLE;\n'
# str += '\n#ifdef HAVE_INITFINI_ARRAY\n'
# str += 'const int init_fini_array = HAVE_INITFINI_ARRAY;\n'
# str += '#endif\n'
cc_header += '\n'
cc_header += GCCResult(rtconfig, str)
cc_header += '\n#endif\n'
# cc_header += '\n'
# cc_header += GCCResult(rtconfig, str)
# cc_header += '\n#endif\n'
cc_file = open('cconfig.h', 'w')
if cc_file:
cc_file.write(cc_header)
cc_file.close()
# cc_file = open('cconfig.h', 'w')
# if cc_file:
# cc_file.write(cc_header)
# cc_file.close()
pass