-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.zig
372 lines (305 loc) · 13.1 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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
const std = @import("std");
const TestSuiteConfig = @import("src/testconfig.zig").TestSuiteConfig;
const samples = [_][]const u8{
"math",
};
const avr_target = std.zig.CrossTarget{
.cpu_arch = .avr,
.cpu_model = .{ .explicit = &std.Target.avr.cpu.atmega328p },
.os_tag = .freestanding,
.abi = .none,
};
// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.Build) !void {
// Targets
const test_step = b.step("test", "Run test suite");
const run_step = b.step("run", "Run the app");
const debug_testsuite_step = b.step("debug-testsuite", "Installs all testsuite examples");
const update_testsuite_step = b.step("update-testsuite", "Updates the folder testsuite with the data in testsuite.avr-gcc. Requires avr-gcc to be present!");
// Deps
const args_dep = b.dependency("args", .{});
// Dep modules
const args_module = args_dep.module("args");
// Options
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// Modules
const isa_module = b.createModule(.{
.source_file = .{ .path = "src/shared/isa.zig" },
});
const isa_tables_module = b.createModule(.{
.source_file = generateIsaTables(b, isa_module),
.dependencies = &.{
.{ .name = "isa", .module = isa_module },
},
});
const aviron_module = b.addModule("aviron", .{
.source_file = .{ .path = "src/lib/aviron.zig" },
.dependencies = &.{
.{ .name = "autogen-tables", .module = isa_tables_module },
.{ .name = "isa", .module = isa_module },
},
});
// Main emulator executable
const aviron_exe = b.addExecutable(.{
.name = "aviron",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
aviron_exe.addModule("args", args_module);
aviron_exe.addModule("aviron", aviron_module);
b.installArtifact(aviron_exe);
const run_cmd = b.addRunArtifact(aviron_exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
run_step.dependOn(&run_cmd.step);
// Samples
for (samples) |sample_name| {
const sample = b.addExecutable(.{
.name = sample_name,
.root_source_file = .{ .path = b.fmt("samples/{s}.zig", .{sample_name}) },
.target = avr_target,
.optimize = .ReleaseSmall,
});
sample.bundle_compiler_rt = false;
sample.setLinkerScriptPath(std.build.FileSource{ .path = "linker.ld" });
sample.strip = false;
// install to the prefix:
const install_elf_sample = b.addInstallFile(sample.getEmittedBin(), b.fmt("samples/{s}.elf", .{sample_name}));
b.getInstallStep().dependOn(&install_elf_sample.step);
}
// Test suite:
try addTestSuite(b, test_step, debug_testsuite_step, target, optimize, args_module, aviron_module);
try addTestSuiteUpdate(b, update_testsuite_step);
}
fn addTestSuite(
b: *std.Build,
test_step: *std.Build.Step,
debug_step: *std.Build.Step,
target: std.zig.CrossTarget,
optimize: std.builtin.OptimizeMode,
args_module: *std.build.Module,
aviron_module: *std.build.Module,
) !void {
const unit_tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
test_step.dependOn(&b.addRunArtifact(unit_tests).step);
const testrunner_exe = b.addExecutable(.{
.name = "aviron-test-runner",
.root_source_file = .{ .path = "src/testrunner.zig" },
.target = target,
.optimize = optimize,
});
testrunner_exe.addModule("args", args_module);
testrunner_exe.addModule("aviron", aviron_module);
debug_step.dependOn(&b.addInstallArtifact(testrunner_exe, .{}).step);
{
var walkdir = try b.build_root.handle.openIterableDir("testsuite", .{});
defer walkdir.close();
var walker = try walkdir.walk(b.allocator);
defer walker.deinit();
while (try walker.next()) |entry| {
if (entry.kind != .file)
continue;
const FileAction = union(enum) {
compile,
load,
ignore,
unknown,
};
const extension_to_action = .{
.c = .compile,
.cpp = .compile,
.S = .compile,
.zig = .compile,
.bin = .load,
.elf = .load,
.inc = .ignore,
.h = .ignore,
.json = .ignore,
};
const ext = std.fs.path.extension(entry.basename);
const action: FileAction = inline for (std.meta.fields(@TypeOf(extension_to_action))) |fld| {
const action: FileAction = @field(extension_to_action, fld.name);
if (std.mem.eql(u8, ext, "." ++ fld.name))
break action;
} else .unknown;
const ConfigAndExe = struct {
binary: std.Build.LazyPath,
config: TestSuiteConfig,
};
const cae: ConfigAndExe = switch (action) {
.unknown => std.debug.panic("Unknown test action on file testsuite/{s}, please fix the build script.", .{entry.path}),
.ignore => continue,
.compile => blk: {
var file = try entry.dir.openFile(entry.basename, .{});
defer file.close();
const config = try parseTestSuiteConfig(b, file);
const custom_target = if (config.cpu) |cpu|
std.zig.CrossTarget.parse(.{
.arch_os_abi = "avr-freestanding-eabi",
.cpu_features = cpu,
}) catch @panic(cpu)
else
avr_target;
const test_payload = b.addExecutable(.{
.name = std.fs.path.stem(entry.basename),
.root_source_file = .{ .path = b.fmt("testsuite/{s}", .{entry.path}) },
.target = custom_target,
.optimize = config.optimize,
});
test_payload.bundle_compiler_rt = false;
test_payload.addIncludePath(.{ .path = "testsuite" });
test_payload.setLinkerScriptPath(std.build.FileSource{ .path = "linker.ld" });
test_payload.addAnonymousModule("testsuite", .{
.source_file = .{ .path = "src/libtestsuite/lib.zig" },
});
test_payload.strip = false;
debug_step.dependOn(&b.addInstallFile(
test_payload.getEmittedBin(),
b.fmt("testsuite/{s}/{s}.elf", .{
std.fs.path.dirname(entry.path).?,
std.fs.path.stem(entry.basename),
}),
).step);
break :blk ConfigAndExe{
.binary = test_payload.getEmittedBin(),
.config = config,
};
},
.load => blk: {
const config_path = b.fmt("{s}.json", .{entry.basename});
const config = if (entry.dir.openFile(config_path, .{})) |file| cfg: {
defer file.close();
break :cfg try TestSuiteConfig.load(b.allocator, file);
} else |_| @panic(config_path);
break :blk ConfigAndExe{
.binary = .{ .path = b.fmt("testsuite/{s}", .{entry.path}) },
.config = config,
};
},
};
const write_file = b.addWriteFile("config.json", cae.config.toString(b));
const test_run = b.addRunArtifact(testrunner_exe);
test_run.addArg("--config");
test_run.addFileArg(write_file.files.items[0].getPath());
test_run.addArg("--name");
test_run.addArg(entry.path);
test_run.addFileArg(cae.binary);
test_run.expectExitCode(0);
test_step.dependOn(&test_run.step);
}
}
}
fn addTestSuiteUpdate(b: *std.Build, invoke_step: *std.Build.Step) !void {
const avr_gcc = if (b.findProgram(&.{"avr-gcc"}, &.{})) |path| std.build.LazyPath{
.cwd_relative = path,
} else |_| b.addExecutable(.{
.name = "no-avr-gcc",
.root_source_file = .{ .path = "tools/no-avr-gcc.zig" },
}).getEmittedBin();
{
var walkdir = try b.build_root.handle.openIterableDir("testsuite.avr-gcc", .{});
defer walkdir.close();
var walker = try walkdir.walk(b.allocator);
defer walker.deinit();
while (try walker.next()) |entry| {
if (entry.kind != .file)
continue;
const FileAction = union(enum) {
compile,
ignore,
unknown,
};
const extension_to_action = .{
.c = .compile,
.cpp = .compile,
.S = .compile,
.inc = .ignore,
.h = .ignore,
.json = .ignore,
.md = .ignore,
};
const ext = std.fs.path.extension(entry.basename);
const action: FileAction = inline for (std.meta.fields(@TypeOf(extension_to_action))) |fld| {
const action: FileAction = @field(extension_to_action, fld.name);
if (std.mem.eql(u8, ext, "." ++ fld.name))
break action;
} else .unknown;
switch (action) {
.unknown => std.debug.panic("Unknown test action on file testsuite/{s}, please fix the build script.", .{entry.path}),
.ignore => continue,
.compile => {
var file = try entry.dir.openFile(entry.basename, .{});
defer file.close();
const config = try parseTestSuiteConfig(b, file);
const gcc_invocation = std.Build.Step.Run.create(b, "run avr-gcc");
gcc_invocation.addFileArg(avr_gcc);
gcc_invocation.addArg("-o");
gcc_invocation.addArg(b.fmt("testsuite/{s}/{s}.elf", .{ std.fs.path.dirname(entry.path).?, std.fs.path.stem(entry.basename) }));
gcc_invocation.addArg(b.fmt("-mmcu={s}", .{config.cpu orelse avr_target.cpu_model.explicit.llvm_name orelse @panic("Unknown MCU!")}));
for (config.gcc_flags) |opt| {
gcc_invocation.addArg(opt);
}
gcc_invocation.addArg("-I");
gcc_invocation.addArg("testsuite");
gcc_invocation.addArg(b.fmt("testsuite.avr-gcc/{s}", .{entry.path}));
const write_file = b.addWriteFile("config.json", config.toString(b));
const copy_file = b.addSystemCommand(&.{"cp"}); // todo make this cross-platform!
copy_file.addFileArg(write_file.files.items[0].getPath());
copy_file.addArg(b.fmt("testsuite/{s}/{s}.elf.json", .{ std.fs.path.dirname(entry.path).?, std.fs.path.stem(entry.basename) }));
invoke_step.dependOn(&gcc_invocation.step);
invoke_step.dependOn(©_file.step);
},
}
}
}
}
fn parseTestSuiteConfig(b: *std.Build, file: std.fs.File) !TestSuiteConfig {
var code = std.ArrayList(u8).init(b.allocator);
defer code.deinit();
var line_buffer: [4096]u8 = undefined;
while (true) {
var fbs = std.io.fixedBufferStream(&line_buffer);
file.reader().streamUntilDelimiter(fbs.writer(), '\n', null) catch |err| switch (err) {
error.EndOfStream => break,
else => |e| return e,
};
const line = fbs.getWritten();
if (std.mem.startsWith(u8, line, "//!")) {
try code.appendSlice(line[3..]);
try code.appendSlice("\n");
}
}
const json_text = std.mem.trim(u8, code.items, "\r\n\t ");
if (json_text.len == 0)
return TestSuiteConfig{};
return try std.json.parseFromSliceLeaky(
TestSuiteConfig,
b.allocator,
json_text,
.{
.allocate = .alloc_always,
},
);
}
fn generateIsaTables(b: *std.Build, isa_mod: *std.Build.Module) std.Build.LazyPath {
const generate_tables_exe = b.addExecutable(.{
.name = "aviron-generate-tables",
.root_source_file = .{ .path = "tools/generate-tables.zig" },
.target = .{},
.optimize = .Debug,
});
generate_tables_exe.addModule("isa", isa_mod);
const run = b.addRunArtifact(generate_tables_exe);
const tables_zig_file = run.addOutputFileArg("tables.zig");
return tables_zig_file;
}