-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.zig
120 lines (98 loc) · 3.61 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const std = @import("std");
const Config = @import("modules/build/Config.zig");
const BUILD_CONFIG = "build_config";
fn currentDate(b: *std.Build) []const u8 {
const out = b.run(&.{ "date", "+\"%Y-%m-%d\"" });
return out[1 .. out.len - 2]; // output is wrapped in quotes, remove them
}
pub fn build(b: *std.Build) !void {
// *** Build configuration ***
const config = Config.fromArgs(b);
const halconf = config.hal.configHeader(b);
const target = b.resolveTargetQuery(.{
.cpu_arch = .thumb,
.cpu_model = .{ .explicit = &std.Target.arm.cpu.cortex_m7 },
.cpu_features_add = std.Target.arm.featureSet(&.{
// FIXME: which one is correct?
.vfp4d16,
// .fp_armv8d16,
}),
.os_tag = .freestanding,
.abi = .eabihf,
});
const optimize: std.builtin.OptimizeMode = .ReleaseSmall;
// *** Entry point ***
const start = b.addExecutable(.{
.name = b.fmt("{s}.elf", .{config.program.name}), // STM32CubeProgrammer does not like the lack of extension
.root_source_file = b.path("modules/common/start.zig"),
.target = target,
.optimize = optimize,
.strip = false,
.error_tracing = true,
});
start.setLinkerScript(b.path(b.fmt("ld/{s}.ld", .{config.program.name})));
// *** Dependencies ***
const libc_dep = b.dependency(
config.libc.dependency,
.{
.target = target,
.optimize = optimize,
},
);
const libc_lib = libc_dep.artifact(config.libc.artifact);
const hal_dep = b.dependency("hal", .{});
const hal_module = hal_dep.module("hal");
const rtt_dep = b.dependency("rtt", .{}).module("rtt");
const zfat_dep = b.dependency(
"zfat",
.{
.target = target,
.optimize = optimize,
.@"static-rtc" = currentDate(b),
.@"no-libc" = true,
},
);
const zfat_module = zfat_dep.module("zfat");
// *** zig code ***
const program_module = b.addModule(
"program",
.{
.root_source_file = b.path(
b.fmt("modules/{s}/main.zig", .{config.program.name}),
),
},
);
const logging_module = b.addModule(
"logging",
.{
.root_source_file = b.path("modules/logging/logging.zig"),
},
);
// *** Expose build config to code ***
const options = config.addOptions(b);
options.addOption(bool, "has_zfat", true);
const options_module = options.createModule();
// *** Glue together (sorted alphabetically just because) ***
program_module.addImport("hal", hal_module);
program_module.addImport(BUILD_CONFIG, options_module);
hal_module.addConfigHeader(halconf);
hal_module.linkLibrary(libc_lib);
libc_lib.link_gc_sections = true;
libc_lib.link_data_sections = true;
libc_lib.link_function_sections = true;
logging_module.addImport("fatfs", zfat_module);
logging_module.addImport("hal", hal_module);
logging_module.addImport(BUILD_CONFIG, options_module);
logging_module.addImport("rtt", rtt_dep);
start.linkLibrary(libc_lib);
start.root_module.addImport("program", program_module);
start.root_module.addImport("hal", hal_module);
start.root_module.addImport("logging", logging_module);
start.root_module.addImport(BUILD_CONFIG, options_module);
start.step.dependOn(&halconf.step); // FIXME: remove hack
zfat_module.linkLibrary(libc_lib);
// otherwise it gets optimized away
start.forceUndefinedSymbol("vector_table");
// *** Output ***
b.installArtifact(start);
}