This repository has been archived by the owner on Nov 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.zig
98 lines (85 loc) · 3.71 KB
/
build.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const builtin = @import("builtin");
const std = @import("std");
const Builder = @import("std").build.Builder;
// Mostly based on <https://github.com/andrewrk/clashos/blob/master/build.zig>
pub fn build(b: *Builder) !void {
const mode = b.standardReleaseOptions();
const want_gdb = b.option(bool, "gdb", "Build for using gdb with qemu") orelse false;
const arch = builtin.Arch{ .thumb = .v8m_mainline };
// The utility program for creating a CMSE import library
// -------------------------------------------------------
const mkimplib = b.addExecutable("mkimplib", "tools/mkimplib.zig");
mkimplib.setOutputDir("zig-cache");
// The Secure part
// -------------------------------------------------------
const exe_s_name = if (want_gdb) "secure-dbg" else "secure";
const exe_s = b.addExecutable(exe_s_name, "src/secure.zig");
exe_s.setLinkerScriptPath("src/secure/linker.ld");
exe_s.setTarget(arch, .freestanding, .eabi);
exe_s.setBuildMode(mode);
exe_s.addAssemblyFile("src/common/startup.s");
exe_s.setOutputDir("zig-cache");
// TODO: "-mthumb -mfloat-abi=soft -msoft-float -march=armv8-m.main");
// CMSE import library (generated from the Secure binary)
// -------------------------------------------------------
// It includes the absolute addresses of Non-Secure-callable functions
// exported by the Secure code. Usually it's generated by passing the
// `--cmse-implib` option to a supported version of `arm-none-eabi-gcc`, but
// since it might not be available, we use a custom tool to do that.
const implib_path = "zig-cache/secure_implib.s";
var implib_args = std.ArrayList([]const u8).init(b.allocator);
try implib_args.appendSlice([_][]const u8{
mkimplib.getOutputPath(),
exe_s.getOutputPath(),
implib_path,
});
const implib = b.addSystemCommand(implib_args.toSliceConst());
implib.step.dependOn(&exe_s.step);
implib.step.dependOn(&mkimplib.step);
const implib_step = b.step("implib", "Create a CMSE import library");
implib_step.dependOn(&implib.step);
// The Non-Secure part
// -------------------------------------------------------
const exe_ns_name = if (want_gdb) "nonsecure-dbg" else "nonsecure";
const exe_ns = b.addExecutable(exe_ns_name, "src/nonsecure.zig");
exe_ns.setLinkerScriptPath("src/nonsecure/linker.ld");
exe_ns.setTarget(arch, .freestanding, .eabi);
exe_ns.setBuildMode(mode);
exe_ns.addAssemblyFile("src/common/startup.s");
exe_ns.addAssemblyFile(implib_path);
exe_ns.setOutputDir("zig-cache");
exe_ns.step.dependOn(&implib.step);
const exe_both = b.step("build", "Build Secure and Non-Secure executables");
exe_both.dependOn(&exe_s.step);
exe_both.dependOn(&exe_ns.step);
// Launch QEMU
// -------------------------------------------------------
const qemu = b.step("qemu", "Run the program in qemu");
var qemu_args = std.ArrayList([]const u8).init(b.allocator);
const qemu_device_arg = try std.fmt.allocPrint(
b.allocator,
"loader,file={}",
exe_ns.getOutputPath(),
);
try qemu_args.appendSlice([_][]const u8{
"qemu-system-arm",
"-kernel",
exe_s.getOutputPath(),
"-device",
qemu_device_arg,
"-machine",
"mps2-an505",
"-nographic",
"-d",
"guest_errors",
});
if (want_gdb) {
try qemu_args.appendSlice([_][]const u8{ "-S", "-s" });
}
const run_qemu = b.addSystemCommand(qemu_args.toSliceConst());
qemu.dependOn(&run_qemu.step);
run_qemu.step.dependOn(exe_both);
// Default Rule
// -------------------------------------------------------
b.default_step.dependOn(exe_both);
}