-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
60 lines (48 loc) · 1.91 KB
/
Copy pathbuild.zig
File metadata and controls
60 lines (48 loc) · 1.91 KB
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
const std = @import("std");
pub fn build(b: *std.Build) void {
// For embedded systems with limited flash, prefer ReleaseSmall
const optimize = b.standardOptimizeOption(.{
.preferred_optimize_mode = .ReleaseSmall,
});
// Force ReleaseSmall for this embedded target if no option specified
const actual_optimize = if (optimize == .Debug) .ReleaseSmall else optimize;
const target = b.resolveTargetQuery(.{
.cpu_arch = .thumb,
.os_tag = .freestanding,
.abi = .eabi,
.cpu_model = .{ .explicit = &std.Target.arm.cpu.cortex_m3 },
});
const exe = b.addExecutable(.{
.name = "hello",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = actual_optimize,
}),
});
// Link script for memory layout
exe.setLinkerScript(b.path("src/linker.ld"));
// Add startup code
exe.addAssemblyFile(b.path("src/startup.s"));
// Disable stack protector for embedded
exe.root_module.stack_protector = false;
// Strip symbols for smaller binary
exe.root_module.strip = true;
// Build options for memory constraints
const options = b.addOptions();
options.addOption(u32, "flash_size", 16 * 1024);
options.addOption(u32, "ram_size", 4 * 1024);
exe.root_module.addOptions("build_options", options);
b.installArtifact(exe);
// Create a raw binary for flashing
const bin = b.addObjCopy(exe.getEmittedBin(), .{
.format = .bin,
});
const install_bin = b.addInstallBinFile(bin.getOutput(), "hello.bin");
b.getInstallStep().dependOn(&install_bin.step);
// Size analysis step
const size_cmd = b.addSystemCommand(&.{ "arm-none-eabi-size", "-A", "-x" });
size_cmd.addArtifactArg(exe);
const size_step = b.step("size", "Display the size of each section");
size_step.dependOn(&size_cmd.step);
}