From 51d92d9500018d67682bb1fb2aa69ce906742d19 Mon Sep 17 00:00:00 2001 From: guozhanxin Date: Tue, 17 Dec 2024 23:10:36 +0800 Subject: [PATCH 1/2] [tools] add zig target --- tools/building.py | 7 +++- tools/zigbuild.py | 100 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 tools/zigbuild.py diff --git a/tools/building.py b/tools/building.py index 66bbb672372..61fd040ff46 100644 --- a/tools/building.py +++ b/tools/building.py @@ -163,7 +163,8 @@ def PrepareBuilding(env, root_directory, has_libcpu=False, remove_components = [ 'cmake-armclang':('keil', 'armclang'), 'xmake':('gcc', 'gcc'), 'codelite' : ('gcc', 'gcc'), - 'esp-idf': ('gcc', 'gcc')} + 'esp-idf': ('gcc', 'gcc'), + 'zig':('gcc', 'gcc')} tgt_name = GetOption('target') if tgt_name: @@ -932,6 +933,10 @@ def GenTargetProject(program = None): from esp_idf import ESPIDFProject ESPIDFProject(Env, Projects) + if GetOption('target') == 'zig': + from zigbuild import ZigBuildProject + ZigBuildProject(Env, Projects) + def EndBuilding(target, program = None): from mkdist import MkDist diff --git a/tools/zigbuild.py b/tools/zigbuild.py new file mode 100644 index 00000000000..8811639d340 --- /dev/null +++ b/tools/zigbuild.py @@ -0,0 +1,100 @@ +""" +Utils for CMake +Author: https://github.com/klivelinux +""" + +import os +import sys +import re +import utils +import rtconfig +from utils import _make_path_relative + + +def GenerateCFiles(env,project): + info = utils.ProjectInfo(env) + + ARCH = ".thumb" if rtconfig.CPU in ['cortex-m0', 'cortex-m3', 'cortex-m4', 'cortex-m7','cortex-m23','cortex-m33','cortex-m85'] else ".arm" + + CFLAGS = rtconfig.CFLAGS.replace('\\', "/").replace('\"', "\\\"") + LFLAGS = rtconfig.LFLAGS.replace('\\', "/").replace('\"', "\\\"") + + zig_file = open('build.zig', 'w') + if zig_file: + zig_file.write("const std = @import(\"std\");\n\n") + + zig_file.write("const target = std.zig.CrossTarget{\n") + zig_file.write(" .cpu_arch = {},\n".format(ARCH)) + zig_file.write(" .cpu_model = .{{ .explicit = &std.Target.{}.cpu.{} }},\n".format(rtconfig.ARCH, rtconfig.CPU.replace('-', '_'))) + zig_file.write(" .os_tag = .freestanding,\n") + zig_file.write(" .abi = .eabi,\n") + zig_file.write("};\n\n") + + zig_file.write("const c_includes = [_][]const u8{\n") + for i in info['CPPPATH']: + # use relative path + path = _make_path_relative(os.getcwd(), i) + zig_file.write("\t\"{}\",\n".format(path.replace("\\", "/"))) + zig_file.write("};\n\n") + + zig_file.write("const c_sources = [_][]const u8{\n") + for group in project: + for f in group['src']: + # use relative path + path = _make_path_relative(os.getcwd(), os.path.normpath(f.rfile().abspath)) + zig_file.write("\t\"{}\",\n".format(path.replace("\\", "/"))) + zig_file.write("};\n\n") + + zig_file.write("const c_flags = [_][]const u8{\n") + zig_file.write("\t\"-std=c99\",\n") + zig_file.write("\t\"-ffunction-sections\",\n") + zig_file.write("\t\"-fdata-sections\",\n") + # conver CDefines to CFlags + for i in info['CPPDEFINES']: + zig_file.write("\t\"-D{}\",\n".format(i)) + # conver LocalCDefines to CFlags + for group in project: + if 'LOCAL_CPPDEFINES' in group and group['LOCAL_CPPDEFINES']: + for i in group['LOCAL_CPPDEFINES']: + zig_file.write("\t\"-D{}\",\n".format(i)) + zig_file.write("};\n\n") + + zig_file.write("pub fn build(b: *std.Build) void {\n") + zig_file.write(" const optimize = .ReleaseSafe;\n\n") + + zig_file.write(" const elf = b.addExecutable(.{\n") + zig_file.write(" .name = \"rtthread.elf\",\n") + zig_file.write(" .target = b.resolveTargetQuery(target),\n") + zig_file.write(" .optimize = optimize,\n") + zig_file.write(" .strip = false,\n") + zig_file.write(" });\n\n") + zig_file.write(" elf.entry = .{ .symbol_name = \"Reset_Handler\" };\n\n") + + zig_file.write(" elf.addCSourceFiles(.{ .files = &c_sources, .flags = &c_flags });\n") + zig_file.write(" for (c_includes) |include| {\n") + zig_file.write(" elf.addIncludePath(b.path(include));\n") + zig_file.write(" }\n\n") + + # find link script in rtconfig.LFLAGS + LINK_SCRIPT = re.search(r'-T\s*(\S+)', LFLAGS) + zig_file.write(" elf.setLinkerScript(b.path(\"{}\"));\n".format(LINK_SCRIPT.group(1))) + + zig_file.write(" const copy_elf = b.addInstallArtifact(elf, .{});\n") + zig_file.write(" b.default_step.dependOn(©_elf.step);\n\n") + + zig_file.write(" const bin = b.addObjCopy(elf.getEmittedBin(), .{ .format = .bin });\n") + zig_file.write(" bin.step.dependOn(&elf.step);\n") + + zig_file.write(" const copy_bin = b.addInstallBinFile(bin.getOutput(), \"rtthread.bin\");\n") + zig_file.write(" b.default_step.dependOn(©_bin.step);\n") + zig_file.write("}\n") + zig_file.close() + + return + +def ZigBuildProject(env,project): + print('Update setting files for build.zig...') + GenerateCFiles(env,project) + print('Done!') + + return From c877bd288ef5ae34b04791a6e5c03adad3b6c5da Mon Sep 17 00:00:00 2001 From: guozhanxin Date: Tue, 17 Dec 2024 23:13:39 +0800 Subject: [PATCH 2/2] [kernel] change size_t to rt_size_t --- src/kservice.c | 2 +- src/thread.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/kservice.c b/src/kservice.c index 5a59345b3e6..167f94a6a72 100644 --- a/src/kservice.c +++ b/src/kservice.c @@ -448,7 +448,7 @@ rt_weak rt_err_t rt_backtrace_formatted_print(rt_ubase_t *buffer, long buflen) { rt_kprintf("please use: addr2line -e rtthread.elf -a -f"); - for (size_t i = 0; i < buflen && buffer[i] != 0; i++) + for (rt_size_t i = 0; i < buflen && buffer[i] != 0; i++) { rt_kprintf(" 0x%lx", (rt_ubase_t)buffer[i]); } diff --git a/src/thread.c b/src/thread.c index d9816b52776..994e04d6f5e 100644 --- a/src/thread.c +++ b/src/thread.c @@ -829,7 +829,7 @@ rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg) { rt_uint8_t cpu; - cpu = (rt_uint8_t)(size_t)arg; + cpu = (rt_uint8_t)(rt_size_t)arg; return rt_sched_thread_bind_cpu(thread, cpu); }