Skip to content

improve libc time and MSVC simulator #5775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Apr 7, 2022
20 changes: 19 additions & 1 deletion bsp/simulator/drivers/SConscript
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
import sys
import os
from building import *
Import('rtconfig')

cwd = GetCurrentDir()
src = Glob('*.c')
LIBS = []
LIBPATH = []
CPPPATH = [cwd]
CPPDEFINES = []

if rtconfig.CROSS_TOOL == 'msvc':
CPPDEFINES += \
[
# avoid to conflict with the inherent STDC in VS
'_CRT_DECLARE_NONSTDC_NAMES=0',
# errno macro redefinition
'_CRT_ERRNO_DEFINED',
# time.h conflicts
'_CRT_NO_TIME_T',
# disable deprecation of unsafe functions, such as strncpy
'_CRT_SECURE_NO_WARNINGS',
# RT_VESRION conflicts in winuser.h
'NORESOURCE',
]

# remove no need file.
if GetDepend('PKG_USING_GUIENGINE') == False:
Expand All @@ -30,7 +47,8 @@ if GetDepend('RT_USING_DFS') == False or GetDepend('RT_USING_MODULE') == False:
if sys.platform[0:5]=="linux": #check whether under linux
SrcRemove(src, ['module_win32.c', 'dfs_win32.c'])

group = DefineGroup('Drivers', src, depend = [''], CPPPATH = CPPPATH, LIBS=LIBS, LIBPATH=LIBPATH)
group = DefineGroup('Drivers', src, depend = [''],
CPPPATH = CPPPATH, LIBS=LIBS, LIBPATH=LIBPATH, CPPDEFINES=CPPDEFINES)

list = os.listdir(cwd)
for item in list:
Expand Down
20 changes: 4 additions & 16 deletions bsp/simulator/rtconfig_project.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,9 @@

#define RT_HEAP_SIZE (1024*1024*8)

#if defined(_MSC_VER)
#define NORESOURCE /* RT_VESRION in winuser.h */
#define _CRT_ERRNO_DEFINED /* errno macro redefinition */
#define _INC_WTIME_INL /* dfs_elm.c time.h conflicts with wtime.inl */
#define _INC_TIME_INL /* dfs_elm.c time.h conflicts with wtime.inl */
#define _CRT_DECLARE_NONSTDC_NAMES 0 /* avoid to conflict with the inherent STDC in VS */

#ifdef _MSC_VER
/* disable some warning in MSC */
#pragma warning(disable:4273) /* to ignore: warning C4273: inconsistent dll linkage */
#pragma warning(disable:4312) /* to ignore: warning C4312: 'type cast' : conversion from 'rt_uint32_t' to 'rt_uint32_t *' */
#pragma warning(disable:4311) /* to ignore: warning C4311: 'type cast' : pointer truncation from 'short *__w64 ' to 'long' */
#pragma warning(disable:4996) /* to ignore: warning C4996: The POSIX name for this item is deprecated. */
#pragma warning(disable:4267) /* to ignore: warning C4267: conversion from 'size_t' to 'rt_size_t', possible loss of data */
#pragma warning(disable:4244) /* to ignore: warning C4244: '=' : conversion from '__w64 int' to 'rt_size_t', possible loss of data */

#endif /* end of _MSC_VER */
// #pragma warning(disable:4273) /* to ignore: warning C4273: inconsistent dll linkage */
#endif /* _MSC_VER */

#endif
#endif /* RTCONFIG_PROJECT_H__ */
11 changes: 9 additions & 2 deletions components/libc/compilers/common/extension/SConscript
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from building import *

Import('rtconfig')

src = []
Expand All @@ -9,6 +9,13 @@ group = []

src += Glob('*.c')

if rtconfig.PLATFORM != 'gcc' or rtconfig.ARCH == 'sim':
if rtconfig.PLATFORM != 'gcc':
group = DefineGroup('Compiler', src, depend = [''], CPPPATH = CPPPATH)

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')
4 changes: 4 additions & 0 deletions components/libc/compilers/common/extension/fcntl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Because of the history issue, flags in fcntl.h, such as O_CREAT, have difference types of value. Some OS use hex flags and others use octal flags.

In terms of RT-Thread, Keil, IAR and MSVC use octal flags, which is located in the `tcntl/octal` folder; newlib uses hex flags; musl uses octal flags.

15 changes: 15 additions & 0 deletions components/libc/compilers/common/extension/fcntl/SConscript
Original file line number Diff line number Diff line change
@@ -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')
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from building import *
Import('rtconfig')

src = []
cwd = GetCurrentDir()
CPPPATH = [cwd]
group = []

if rtconfig.PLATFORM == 'armcc' or\
rtconfig.PLATFORM == 'armclang' or\
rtconfig.PLATFORM == 'iar' or\
rtconfig.CROSS_TOOL == 'msvc':
group = DefineGroup('Compiler', src, depend = [''], CPPPATH = CPPPATH)
Return('group')
14 changes: 8 additions & 6 deletions components/libc/compilers/common/sys/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
#include <time.h>
#ifdef _WIN32
#include <winsock.h> /* for struct timeval */
#endif
#include <corecrt.h> /* for __time64_t */
typedef __time64_t time_t;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个地方要留意

get_timeval 和 set_timeval 里面有这样一段代码

static int set_timeval(struct timeval *tv)
{
    rst = rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &tv->tv_sec);
    rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIMEVAL, tv);
}

vs 上 struct timeval 中的 tv_sec 类型是 long,RTC 驱动那边,会当成 time_t * 进行赋值,会造成不致命的内存越界

// 这种形式会写穿 tv_usec,不致命
struct timeval {
        long    tv_sec;         /* seconds */
        long    tv_usec;        /* and microseconds */
};

// 这种形式会写穿后面的内存,致命
struct timeval {
        long    tv_usec;        /* and microseconds */
        long    tv_sec;         /* seconds */
};

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

struct timeval {
        long    tv_usec;        /* and microseconds */
        long    tv_sec;         /* seconds */
};

这种倒过来的形式在newlib vs都没有 都是正着的
time64可以先放着,因为后续都要升级到64位了,要应对2038的问题。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

看来2038的问题不只是time_t 32改64这么简单,相关的时间结构体 long都得改成longlong

#endif /* _WIN32 */

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -50,17 +52,17 @@ struct timeval
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* and microseconds */
};
#endif
#endif /* !defined(_TIMEVAL_DEFINED) && !defined(_WIN32) */

#if !(defined(__GNUC__) && !defined(__ARMCC_VERSION)/*GCC*/) && \
!(defined(__ICCARM__) && (__VER__ >= 8010001)) && \
!defined(_WIN32)
#if defined(__ARMCC_VERSION) || defined(_WIN32) || (defined(__ICCARM__) && (__VER__ >= 8010001))
struct timespec
{
time_t tv_sec; /* seconds */
long tv_nsec; /* and nanoseconds */
};
#endif /* defined(__ARMCC_VERSION) || defined(_WIN32) || (defined(__ICCARM__) && (__VER__ >= 8010001)) */

#if !(defined(__GNUC__) && !defined(__ARMCC_VERSION)/*GCC*/)
/*
* Structure defined by POSIX.1b to be like a itimerval, but with
* timespecs. Used in the timer_*() system calls.
Expand All @@ -70,7 +72,7 @@ struct itimerspec
struct timespec it_interval;
struct timespec it_value;
};
#endif
#endif /* !(defined(__GNUC__) && !defined(__ARMCC_VERSION)) */

int stime(const time_t *t);
time_t timegm(struct tm * const t);
Expand Down