-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.zig
105 lines (84 loc) · 3.34 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
const std = @import("std");
const builtin = @import("builtin");
const uf2 = @import("uf2/src/main.zig");
const rp2040 = @import("rp2040/build.zig");
const microzig = @import("microzig/src/main.zig");
const Builder = std.build.Builder;
const Step = std.build.Step;
const LibExeObjStep = std.build.LibExeObjStep;
const Message = @import("src/device_info.zig").Message;
fn root() []const u8 {
return (std.fs.path.dirname(@src().file) orelse ".") ++ "/";
}
pub fn addExample(b: *Builder, comptime name: []const u8) !void {
const mode = b.standardReleaseOptions();
const blinky = rp2040.addPiPicoExecutable(
microzig,
b,
name,
"examples/" ++ name ++ ".zig",
.{},
);
blinky.setBuildMode(mode);
blinky.install();
const uf2_step = uf2.Uf2Step.create(blinky, .{
.family_id = .RP2040,
});
uf2_step.install();
}
pub fn build(b: *Builder) !void {
try addExample(b, "blinky");
try addExample(b, "blinky_core1");
}
// TODO: wip
pub const DeviceInfoStep = struct {
step: Step,
builder: *Builder,
exe: *LibExeObjStep,
pub fn create(b: *Builder) *DeviceInfoStep {
var ret = b.allocator.create(DeviceInfoStep) catch @panic("failed to allocate DeviceInfoStep");
ret.* = .{
.step = Step.init(.custom, "device_info", b.allocator, make),
.builder = b,
.exe = b.addExecutable("device_info", root() ++ "src/device_info.zig"),
};
ret.step.dependOn(&ret.exe.step);
if (builtin.os.tag == .linux) {
ret.exe.linkLibC();
// TODO: can we check for the existence of this library?
ret.exe.linkSystemLibrary("libudev");
}
return ret;
}
fn make(step: *Step) !void {
const device_info = @fieldParentPtr(DeviceInfoStep, "step", step);
std.log.info("exe path: {s}", .{device_info.exe.getOutputSource().generated.getPath()});
const child = try std.ChildProcess.init(&.{
device_info.exe.getOutputSource().generated.getPath(),
}, device_info.builder.allocator);
defer child.deinit();
child.stdin_behavior = .Pipe;
child.stdout_behavior = .Pipe;
try child.spawn();
const writer = child.stdin.?.writer();
_ = writer;
const reader = child.stdout.?.reader();
while (true) {
const line = reader.readUntilDelimiterAlloc(device_info.builder.allocator, '\n', 1024 * 1024) catch |err| switch (err) {
error.EndOfStream => break,
else => return err,
};
defer device_info.builder.allocator.free(line);
var stream = std.json.TokenStream.init(line);
const message = try std.json.parse(Message, &stream, .{ .allocator = device_info.builder.allocator });
if (std.mem.eql(u8, "log", message.tag)) {
std.log.info("{s}", .{message.payload});
} else if (std.mem.eql(u8, "found", message.tag)) {
std.log.info("found pi pico with device path: {s}", .{message.payload});
} else @panic("incorrect message tag");
}
std.log.err("this device discovery thing isn't fully implemented yet, make sure it's mounted and specify that path.", .{});
// could wait instead, but I'm in a bloodthirsty mood
_ = try child.kill();
}
};