|
| 1 | +""" |
| 2 | +Utils for CMake |
| 3 | +Author: https://github.com/klivelinux |
| 4 | +""" |
| 5 | + |
| 6 | +import os |
| 7 | +import sys |
| 8 | +import re |
| 9 | +import utils |
| 10 | +import rtconfig |
| 11 | +from utils import _make_path_relative |
| 12 | + |
| 13 | + |
| 14 | +def GenerateCFiles(env,project): |
| 15 | + info = utils.ProjectInfo(env) |
| 16 | + |
| 17 | + ARCH = ".thumb" if rtconfig.CPU in ['cortex-m0', 'cortex-m3', 'cortex-m4', 'cortex-m7','cortex-m23','cortex-m33','cortex-m85'] else ".arm" |
| 18 | + |
| 19 | + CFLAGS = rtconfig.CFLAGS.replace('\\', "/").replace('\"', "\\\"") |
| 20 | + LFLAGS = rtconfig.LFLAGS.replace('\\', "/").replace('\"', "\\\"") |
| 21 | + |
| 22 | + zig_file = open('build.zig', 'w') |
| 23 | + if zig_file: |
| 24 | + zig_file.write("const std = @import(\"std\");\n\n") |
| 25 | + |
| 26 | + zig_file.write("const target = std.zig.CrossTarget{\n") |
| 27 | + zig_file.write(" .cpu_arch = {},\n".format(ARCH)) |
| 28 | + zig_file.write(" .cpu_model = .{{ .explicit = &std.Target.{}.cpu.{} }},\n".format(rtconfig.ARCH, rtconfig.CPU.replace('-', '_'))) |
| 29 | + zig_file.write(" .os_tag = .freestanding,\n") |
| 30 | + zig_file.write(" .abi = .eabi,\n") |
| 31 | + zig_file.write("};\n\n") |
| 32 | + |
| 33 | + zig_file.write("const c_includes = [_][]const u8{\n") |
| 34 | + for i in info['CPPPATH']: |
| 35 | + # use relative path |
| 36 | + path = _make_path_relative(os.getcwd(), i) |
| 37 | + zig_file.write("\t\"{}\",\n".format(path.replace("\\", "/"))) |
| 38 | + zig_file.write("};\n\n") |
| 39 | + |
| 40 | + zig_file.write("const c_sources = [_][]const u8{\n") |
| 41 | + for group in project: |
| 42 | + for f in group['src']: |
| 43 | + # use relative path |
| 44 | + path = _make_path_relative(os.getcwd(), os.path.normpath(f.rfile().abspath)) |
| 45 | + zig_file.write("\t\"{}\",\n".format(path.replace("\\", "/"))) |
| 46 | + zig_file.write("};\n\n") |
| 47 | + |
| 48 | + zig_file.write("const c_flags = [_][]const u8{\n") |
| 49 | + zig_file.write("\t\"-std=c99\",\n") |
| 50 | + zig_file.write("\t\"-ffunction-sections\",\n") |
| 51 | + zig_file.write("\t\"-fdata-sections\",\n") |
| 52 | + # conver CDefines to CFlags |
| 53 | + for i in info['CPPDEFINES']: |
| 54 | + zig_file.write("\t\"-D{}\",\n".format(i)) |
| 55 | + # conver LocalCDefines to CFlags |
| 56 | + for group in project: |
| 57 | + if 'LOCAL_CPPDEFINES' in group and group['LOCAL_CPPDEFINES']: |
| 58 | + for i in group['LOCAL_CPPDEFINES']: |
| 59 | + zig_file.write("\t\"-D{}\",\n".format(i)) |
| 60 | + zig_file.write("};\n\n") |
| 61 | + |
| 62 | + zig_file.write("pub fn build(b: *std.Build) void {\n") |
| 63 | + zig_file.write(" const optimize = .ReleaseSafe;\n\n") |
| 64 | + |
| 65 | + zig_file.write(" const elf = b.addExecutable(.{\n") |
| 66 | + zig_file.write(" .name = \"rtthread.elf\",\n") |
| 67 | + zig_file.write(" .target = b.resolveTargetQuery(target),\n") |
| 68 | + zig_file.write(" .optimize = optimize,\n") |
| 69 | + zig_file.write(" .strip = false,\n") |
| 70 | + zig_file.write(" });\n\n") |
| 71 | + zig_file.write(" elf.entry = .{ .symbol_name = \"Reset_Handler\" };\n\n") |
| 72 | + |
| 73 | + zig_file.write(" elf.addCSourceFiles(.{ .files = &c_sources, .flags = &c_flags });\n") |
| 74 | + zig_file.write(" for (c_includes) |include| {\n") |
| 75 | + zig_file.write(" elf.addIncludePath(b.path(include));\n") |
| 76 | + zig_file.write(" }\n\n") |
| 77 | + |
| 78 | + # find link script in rtconfig.LFLAGS |
| 79 | + LINK_SCRIPT = re.search(r'-T\s*(\S+)', LFLAGS) |
| 80 | + zig_file.write(" elf.setLinkerScript(b.path(\"{}\"));\n".format(LINK_SCRIPT.group(1))) |
| 81 | + |
| 82 | + zig_file.write(" const copy_elf = b.addInstallArtifact(elf, .{});\n") |
| 83 | + zig_file.write(" b.default_step.dependOn(©_elf.step);\n\n") |
| 84 | + |
| 85 | + zig_file.write(" const bin = b.addObjCopy(elf.getEmittedBin(), .{ .format = .bin });\n") |
| 86 | + zig_file.write(" bin.step.dependOn(&elf.step);\n") |
| 87 | + |
| 88 | + zig_file.write(" const copy_bin = b.addInstallBinFile(bin.getOutput(), \"rtthread.bin\");\n") |
| 89 | + zig_file.write(" b.default_step.dependOn(©_bin.step);\n") |
| 90 | + zig_file.write("}\n") |
| 91 | + zig_file.close() |
| 92 | + |
| 93 | + return |
| 94 | + |
| 95 | +def ZigBuildProject(env,project): |
| 96 | + print('Update setting files for build.zig...') |
| 97 | + GenerateCFiles(env,project) |
| 98 | + print('Done!') |
| 99 | + |
| 100 | + return |
0 commit comments