Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions tools/WCS.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,9 +426,6 @@ def main():
# Print A Nice Message With Each Function and the WCS
print_all_fxns(call_graph)




def ThreadStackStaticAnalysis(env):
print('Start thread stack static analysis...')

Expand Down
120 changes: 22 additions & 98 deletions tools/building.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
# 2024-04-21 Bernard Add toolchain detection in sdk packages
# 2025-01-05 Bernard Add logging as Env['log']
# 2025-03-02 ZhaoCake Add MkDist_Strip
# 2025-01-05 Assistant Refactor SCons PreProcessor patch to independent class

import os
import sys
Expand All @@ -39,90 +40,14 @@
from utils import _make_path_relative
from mkdist import do_copy_file
from options import AddOptions
from preprocessor import create_preprocessor_instance
from win32spawn import Win32Spawn

BuildOptions = {}
Projects = []
Rtt_Root = ''
Env = None

# SCons PreProcessor patch
def start_handling_includes(self, t=None):
"""
Causes the PreProcessor object to start processing #import,
#include and #include_next lines.

This method will be called when a #if, #ifdef, #ifndef or #elif
evaluates True, or when we reach the #else in a #if, #ifdef,
#ifndef or #elif block where a condition already evaluated
False.

"""
d = self.dispatch_table
p = self.stack[-1] if self.stack else self.default_table

for k in ('import', 'include', 'include_next', 'define'):
d[k] = p[k]

def stop_handling_includes(self, t=None):
"""
Causes the PreProcessor object to stop processing #import,
#include and #include_next lines.

This method will be called when a #if, #ifdef, #ifndef or #elif
evaluates False, or when we reach the #else in a #if, #ifdef,
#ifndef or #elif block where a condition already evaluated True.
"""
d = self.dispatch_table
d['import'] = self.do_nothing
d['include'] = self.do_nothing
d['include_next'] = self.do_nothing
d['define'] = self.do_nothing

PatchedPreProcessor = SCons.cpp.PreProcessor
PatchedPreProcessor.start_handling_includes = start_handling_includes
PatchedPreProcessor.stop_handling_includes = stop_handling_includes

class Win32Spawn:
def spawn(self, sh, escape, cmd, args, env):
# deal with the cmd build-in commands which cannot be used in
# subprocess.Popen
if cmd == 'del':
for f in args[1:]:
try:
os.remove(f)
except Exception as e:
print('Error removing file: ' + e)
return -1
return 0

import subprocess

newargs = ' '.join(args[1:])
cmdline = cmd + " " + newargs

# Make sure the env is constructed by strings
_e = dict([(k, str(v)) for k, v in env.items()])

# Windows(tm) CreateProcess does not use the env passed to it to find
# the executables. So we have to modify our own PATH to make Popen
# work.
old_path = os.environ['PATH']
os.environ['PATH'] = _e['PATH']

try:
proc = subprocess.Popen(cmdline, env=_e, shell=False)
except Exception as e:
print('Error in calling command:' + cmdline.split(' ')[0])
print('Exception: ' + os.strerror(e.errno))
if (os.strerror(e.errno) == "No such file or directory"):
print ("\nPlease check Toolchains PATH setting.\n")

return e.errno
finally:
os.environ['PATH'] = old_path

return proc.wait()

def PrepareBuilding(env, root_directory, has_libcpu=False, remove_components = []):

global BuildOptions
Expand Down Expand Up @@ -294,7 +219,7 @@ def PrepareBuilding(env, root_directory, has_libcpu=False, remove_components = [
Env.Append(BUILDERS = {'BuildLib': bld})

# parse rtconfig.h to get used component
PreProcessor = PatchedPreProcessor()
PreProcessor = create_preprocessor_instance()
f = open('rtconfig.h', 'r')
contents = f.read()
f.close()
Expand Down Expand Up @@ -433,7 +358,7 @@ def PrepareModuleBuilding(env, root_directory, bsp_directory):
Rtt_Root = root_directory

# parse bsp rtconfig.h to get used component
PreProcessor = PatchedPreProcessor()
PreProcessor = create_preprocessor_instance()
f = open(bsp_directory + '/rtconfig.h', 'r')
contents = f.read()
f.close()
Expand Down Expand Up @@ -877,7 +802,7 @@ def local_group(group, objects):
def GenTargetProject(program = None):

if GetOption('target') in ['mdk', 'mdk4', 'mdk5']:
from keil import MDK2Project, MDK4Project, MDK5Project, ARMCC_Version
from targets.keil import MDK2Project, MDK4Project, MDK5Project, ARMCC_Version

if os.path.isfile('template.uvprojx') and GetOption('target') not in ['mdk4']: # Keil5
MDK5Project(GetOption('project-name') + '.uvprojx', Projects)
Expand All @@ -895,68 +820,68 @@ def GenTargetProject(program = None):
print("Keil-MDK project has generated successfully!")

if GetOption('target') == 'iar':
from iar import IARProject, IARVersion
from targets.iar import IARProject, IARVersion
print("IAR Version: " + IARVersion())
IARProject(GetOption('project-name') + '.ewp', Projects)
print("IAR project has generated successfully!")

if GetOption('target') == 'vs':
from vs import VSProject
from targets.vs import VSProject
VSProject(GetOption('project-name') + '.vcproj', Projects, program)

if GetOption('target') == 'vs2012':
from vs2012 import VS2012Project
from targets.vs2012 import VS2012Project
VS2012Project(GetOption('project-name') + '.vcxproj', Projects, program)

if GetOption('target') == 'cb':
from codeblocks import CBProject
from targets.codeblocks import CBProject
CBProject(GetOption('project-name') + '.cbp', Projects, program)

if GetOption('target') == 'ua':
from ua import PrepareUA
from targets.ua import PrepareUA
PrepareUA(Projects, Rtt_Root, str(Dir('#')))

if GetOption('target') == 'vsc':
from vsc import GenerateVSCode
from targets.vsc import GenerateVSCode
GenerateVSCode(Env)
if GetOption('cmsispack'):
from vscpyocd import GenerateVSCodePyocdConfig
GenerateVSCodePyocdConfig(GetOption('cmsispack'))

if GetOption('target') == 'cdk':
from cdk import CDKProject
from targets.cdk import CDKProject
CDKProject(GetOption('project-name') + '.cdkproj', Projects)

if GetOption('target') == 'ses':
from ses import SESProject
from targets.ses import SESProject
SESProject(Env)

if GetOption('target') == 'makefile':
from makefile import TargetMakefile
from targets.makefile import TargetMakefile
TargetMakefile(Env)

if GetOption('target') == 'eclipse':
from eclipse import TargetEclipse
from targets.eclipse import TargetEclipse
TargetEclipse(Env, GetOption('reset-project-config'), GetOption('project-name'))

if GetOption('target') == 'codelite':
from codelite import TargetCodelite
from targets.codelite import TargetCodelite
TargetCodelite(Projects, program)

if GetOption('target') == 'cmake' or GetOption('target') == 'cmake-armclang':
from cmake import CMakeProject
from targets.cmake import CMakeProject
CMakeProject(Env, Projects, GetOption('project-name'))

if GetOption('target') == 'xmake':
from xmake import XMakeProject
from targets.xmake import XMakeProject
XMakeProject(Env, Projects)

if GetOption('target') == 'esp-idf':
from esp_idf import ESPIDFProject
from targets.esp_idf import ESPIDFProject
ESPIDFProject(Env, Projects)

if GetOption('target') == 'zig':
from zigbuild import ZigBuildProject
from targets.zigbuild import ZigBuildProject
ZigBuildProject(Env, Projects)

def EndBuilding(target, program = None):
Expand Down Expand Up @@ -1069,7 +994,7 @@ def GetVersion():
rtdef = os.path.join(Rtt_Root, 'include', 'rtdef.h')

# parse rtdef.h to get RT-Thread version
prepcessor = PatchedPreProcessor()
prepcessor = create_preprocessor_instance()
f = open(rtdef, 'r')
contents = f.read()
f.close()
Expand Down Expand Up @@ -1109,4 +1034,3 @@ def PackageSConscript(package):
from package import BuildPackage

return BuildPackage(package)

49 changes: 49 additions & 0 deletions tools/hello/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Hello Component

这是一个使用package.json配置的RT-Thread组件示例,展示了如何使用package.json来替代传统的SConscript中DefineGroup的方式。

## 文件结构

```
hello/
├── hello.h # 头文件
├── hello.c # 源文件
├── package.json # 组件配置文件
├── SConscript # 构建脚本
└── README.md # 说明文档
```

## package.json配置说明

package.json文件包含了组件的所有构建信息:

- `name`: 组件名称
- `version`: 版本号
- `description`: 组件描述
- `author`: 作者信息
- `license`: 许可证
- `source_files`: 源文件列表
- `CPPPATH`: 头文件搜索路径
- `CPPDEFINES`: 预定义宏
- `depends`: 依赖的组件

## 使用方法

1. 将hello文件夹复制到你的RT-Thread项目的components目录下
2. 在应用代码中包含头文件:
```c
#include "hello.h"
```
3. 调用hello_world函数:
```c
hello_world(); // 输出: Hello World!
```

## 构建过程

SConscript文件会:
1. 导入package.py模块
2. 调用BuildPackage函数处理package.json
3. 自动创建DefineGroup并返回构建对象

这种方式比传统的SConscript更加简洁和易于维护。
4 changes: 4 additions & 0 deletions tools/hello/SConscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from package import *

objs = BuildPackage()
Return('objs')
22 changes: 22 additions & 0 deletions tools/hello/hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (c) 2025 RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2025-06-21 Bernard First version
*/

#include <rtthread.h>
#include "hello.h"

/**
* @brief Hello world function implementation
*
* This function prints "Hello World!" to the console using rt_kprintf
*/
void hello_world(void)
{
rt_kprintf("Hello World!\n");
}
29 changes: 29 additions & 0 deletions tools/hello/hello.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2025 RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2025-06-21 Bernard First version
*/

#ifndef __HELLO_H__
#define __HELLO_H__

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief Hello world function
*
* This function prints "Hello World!" to the console
*/
void hello_world(void);

#ifdef __cplusplus
}
#endif

#endif /* __HELLO_H__ */
20 changes: 20 additions & 0 deletions tools/hello/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "hello",
"version": "1.0.0",
"description": "Hello World component for RT-Thread",
"author": "RT-Thread Development Team",
"license": "Apache-2.0",
"type": "rt-package",
"source_files": [
"hello.c"
],
"CPPPATH": [
"."
],
"CPPDEFINES": [
"HELLO"
],
"depends": [
""
]
}
Loading