Repository navigation
Expand file tree
/
Copy pathbuild.zig
More file actions
304 lines (253 loc) · 11.6 KB
/
Copy pathbuild.zig
File metadata and controls
304 lines (253 loc) · 11.6 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
const std = @import("std");
const chip = @import("src/chip/chip.zig");
const ObjCopyExternal = @import("ObjCopyExternal.zig");
pub const Target = struct {
chip: chip.Chip,
// Override the default linker script for the chip.
linker_script: ?std.Build.LazyPath = null,
};
pub const FirmwareOptions = struct {
name: []const u8,
root_source_file: std.Build.LazyPath,
target: Target,
optimize: std.builtin.OptimizeMode = .ReleaseSmall,
/// Override the default config options.
/// Use `createConfigOptions(b, name, target.chip)` for default and add options to it.
config_options: ?*std.Build.Step.Options = null,
/// If set to false, allows to get a stack trace on panic with `ReleaseSmall` optimization
/// at the cost of increased output binary size.
omit_frame_pointer: ?bool = null,
};
pub fn addFirmwareTest(app_builder: *std.Build, dep_maybe: ?*std.Build.Dependency, native_target: std.Build.ResolvedTarget, options: FirmwareOptions) *std.Build.Step.Compile {
const ch32_builder = if (dep_maybe) |dep| dep.builder else app_builder;
const root_mod = createModules(ch32_builder, native_target, options);
const firmware = ch32_builder.addTest(.{
.name = options.name,
.root_module = root_mod.import_table.get("app") orelse @panic("module 'app' not found in import table"),
.test_runner = .{ .path = ch32_builder.path("test_runner.zig"), .mode = .simple },
});
return firmware;
}
pub fn addFirmware(app_builder: *std.Build, dep_maybe: ?*std.Build.Dependency, options: FirmwareOptions) *std.Build.Step.Compile {
const ch32_builder = if (dep_maybe) |dep| dep.builder else app_builder;
const target = ch32_builder.resolveTargetQuery(options.target.chip.target());
const root_mod = createModules(ch32_builder, target, options);
const firmware = ch32_builder.addExecutable(.{
.name = options.name,
.linkage = .static,
.root_module = root_mod,
});
firmware.bundle_compiler_rt = true;
firmware.link_gc_sections = true;
firmware.link_function_sections = true;
firmware.link_data_sections = true;
firmware.linker_script = options.target.linker_script orelse ch32_builder.path("src/ld").join(ch32_builder.allocator, options.target.chip.linkScript(ch32_builder)) catch @panic("OOM");
// For emit elf with debug info.
firmware.root_module.strip = false;
return firmware;
}
pub fn createModules(b: *std.Build, target: std.Build.ResolvedTarget, options: FirmwareOptions) *std.Build.Module {
const config_options = options.config_options orelse createConfigOptions(b, options.name, options.target.chip);
const config_mod = config_options.createModule();
const svd_mod = svdModule(b, target, options.target.chip.asSeries().svdName());
const hal_mod = halModule(b, target);
hal_mod.addImport("config", config_mod);
hal_mod.addImport("svd", svd_mod);
const app_mod = b.createModule(.{
.root_source_file = options.root_source_file,
.target = target,
.optimize = options.optimize,
.omit_frame_pointer = options.omit_frame_pointer,
.single_threaded = true,
.imports = &.{
.{ .name = "config", .module = config_mod },
.{ .name = "svd", .module = svd_mod },
.{ .name = "hal", .module = hal_mod },
},
});
const root_mod = b.createModule(.{
.root_source_file = b.path("src/ch32.zig"),
.target = target,
.optimize = options.optimize,
.omit_frame_pointer = options.omit_frame_pointer,
.single_threaded = true,
.imports = &.{
.{ .name = "config", .module = config_mod },
.{ .name = "svd", .module = svd_mod },
.{ .name = "hal", .module = hal_mod },
.{ .name = "app", .module = app_mod },
},
});
app_mod.addImport("ch32", root_mod);
return root_mod;
}
const ChipOption = struct {
series: chip.Series,
model: chip.Model,
class: chip.Class,
fn from(c: chip.Chip) ChipOption {
return .{
.series = c.asSeries(),
.model = c.asModel(),
.class = c.asClass(),
};
}
};
pub fn createConfigOptions(
b: *std.Build,
name: []const u8,
c: chip.Chip,
) *std.Build.Step.Options {
const config_options = b.addOptions();
config_options.addOption([]const u8, "name", name);
config_options.addOption(ChipOption, "chip", ChipOption.from(c));
return config_options;
}
fn svdModule(b: *std.Build, target: std.Build.ResolvedTarget, svd_name: []const u8) *std.Build.Module {
const svd_path = b.path("src/svd").join(b.allocator, b.fmt("{s}.zig", .{svd_name})) catch @panic("OOM");
const module = b.createModule(.{
.root_source_file = svd_path,
.target = target,
.single_threaded = true,
});
return module;
}
fn halModule(b: *std.Build, target: std.Build.ResolvedTarget) *std.Build.Module {
const module = b.createModule(.{
.root_source_file = b.path("src/hal/hal.zig"),
.target = target,
.single_threaded = true,
});
return module;
}
const FirmwareFormat = union(enum) {
elf,
bin,
@"asm",
fn ext(format: FirmwareFormat) []const u8 {
return switch (format) {
.elf => "elf",
.bin => "bin",
.@"asm" => "s",
};
}
};
pub const InstallFirmwareOptions = struct {
format: FirmwareFormat = .bin,
install_dir: std.Build.InstallDir = .{ .custom = "firmware" },
};
pub fn installFirmware(b: *std.Build, compile: *std.Build.Step.Compile, options: InstallFirmwareOptions) std.Build.LazyPath {
const basename = b.fmt("{s}.{s}", .{ compile.name, options.format.ext() });
const path = switch (options.format) {
.elf => compile.getEmittedBin(),
// Due to a bug in Zig ObjCopy 0.15.1, external objcopy will have to be called.
// .bin => b.addObjCopy(compile.getEmittedBin(), .{
// .basename = basename,
// .format = .bin,
// }).getOutput(),
.bin => ObjCopyExternal.create(b, compile.getEmittedBin(), .{
.basename = basename,
.format = .bin,
}).getOutput(),
.@"asm" => compile.getEmittedAsm(),
};
const install = b.addInstallFileWithDir(path, .{ .custom = "firmware" }, basename);
b.getInstallStep().dependOn(&install.step);
return path;
}
const FirmwareSize = struct {
step: std.Build.Step,
source: std.Build.LazyPath,
};
pub fn printFirmwareSize(b: *std.Build, file: std.Build.LazyPath) void {
const size = b.allocator.create(FirmwareSize) catch @panic("OOM");
size.* = .{
.step = std.Build.Step.init(.{
.id = .install_file,
.name = b.fmt("print {s} size", .{file.getDisplayName()}),
.owner = b,
.makeFn = makeFirmwareSize,
}),
.source = file,
};
file.addStepDependencies(&size.step);
b.getInstallStep().dependOn(&size.step);
}
fn makeFirmwareSize(step: *std.Build.Step, options: std.Build.Step.MakeOptions) !void {
_ = options;
const b = step.owner;
const self: *FirmwareSize = @fieldParentPtr("step", step);
const path = self.source.getPath3(b, step);
const stat = path.root_dir.handle.statFile(path.subPathOrDot()) catch |err| {
return step.fail("unable to stat file from '{f}': {s}", .{
path, @errorName(err),
});
};
const size = stat.size;
std.log.info("{s} size: {d} bytes", .{ path.basename(), size });
}
pub fn addMinichlink(b: *std.Build, dep_maybe: ?*std.Build.Dependency, step: *std.Build.Step) void {
const ch32_builder = if (dep_maybe) |dep| dep.builder else b;
const minichlink_dep = ch32_builder.dependency("minichlink", .{});
const minichlink = minichlink_dep.artifact("minichlink");
if (b.args) |args| {
const minichlink_run = ch32_builder.addRunArtifact(minichlink);
minichlink_run.setCwd(.{ .cwd_relative = "." });
minichlink_run.addArgs(args);
step.dependOn(&minichlink_run.step);
} else {
const minichlink_install = b.addInstallArtifact(minichlink, .{});
step.dependOn(&minichlink_install.step);
}
}
pub fn build(b: *std.Build) void {
const native_target = b.standardTargetOptions(.{});
const targets: []const Target = &.{
.{ .chip = .{ .series = .ch32v003 } },
// .{ .chip = .{ .series = .ch32v103 } },
.{ .chip = .{ .series = .ch32v20x } },
.{ .chip = .{ .series = .ch32v30x } },
};
// ┌──────────────────────────────────────────────────────────┐
// │ Build │
// └──────────────────────────────────────────────────────────┘
const fw = addFirmware(b, null, .{
.name = "build",
.target = .{ .chip = .{ .series = .ch32v30x } },
.root_source_file = b.path("src/main.zig"),
});
_ = installFirmware(b, fw, .{});
// ┌──────────────────────────────────────────────────────────┐
// │ Test │
// └──────────────────────────────────────────────────────────┘
const test_step = b.step("test", "Run platform-independent tests");
for (targets) |target| {
const hal_test = addFirmwareTest(b, null, native_target, .{
.name = target.chip.string(),
.root_source_file = b.path("src/hal/hal.zig"),
.target = target,
.optimize = .Debug,
});
const hal_unit_tests_run = b.addRunArtifact(hal_test);
test_step.dependOn(&hal_unit_tests_run.step);
const core_test = addFirmwareTest(b, null, native_target, .{
.name = target.chip.string(),
.root_source_file = b.path("src/ch32.zig"),
.target = target,
.optimize = .Debug,
});
const core_unit_tests_run = b.addRunArtifact(core_test);
test_step.dependOn(&core_unit_tests_run.step);
}
// ┌──────────────────────────────────────────────────────────┐
// │ Clean │
// └──────────────────────────────────────────────────────────┘
const clean_step = b.step("clean", "Clean up");
clean_step.dependOn(&b.addRemoveDirTree(.{ .cwd_relative = b.install_path }).step);
clean_step.dependOn(&b.addRemoveDirTree(.{ .cwd_relative = b.pathFromRoot(".zig-cache") }).step);
// ┌──────────────────────────────────────────────────────────┐
// │ minichlink │
// └──────────────────────────────────────────────────────────┘
const minichlink_step = b.step("minichlink", "minichlink");
addMinichlink(b, null, minichlink_step);
}