-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.zig
121 lines (98 loc) · 3.3 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
const std = @import("std");
const internals = @import("microzig-internal");
pub const port = struct {
pub const rp2 = @import("microzig-rp2");
};
pub fn build(b: *std.Build) void {
const rp2_enable = b.option(bool, "rp2", "Enables the Raspberrypi RP2 port") orelse false;
if (rp2_enable) {
if (b.lazyDependency("microzig-rp2", .{})) |rp2_dep| {
const rp2_port = internals.Port.from_dependency(rp2_dep);
_ = rp2_port;
}
}
// TODO: Add shared/common modules here:
// Tools are just public executables artifacts:
{
const exe = b.addExecutable(.{
.name = "regz",
.target = b.graph.host,
.root_source_file = b.path("tools/regz.zig"),
});
b.installArtifact(exe);
}
{
const exe = b.addExecutable(.{
.name = "uf2",
.target = b.graph.host,
.root_source_file = b.path("tools/uf2.zig"),
});
b.installArtifact(exe);
}
{
const exe = b.addExecutable(.{
.name = "picotool",
.target = b.graph.host,
.root_source_file = b.path("tools/picotool.zig"),
});
b.installArtifact(exe);
}
}
pub fn init(b: *std.Build, dep: *std.Build.Dependency) *MicroZig {
const microzig = dep.builder.allocator.create(MicroZig) catch @panic("out of memory");
microzig.* = .{
.creating_builder = b,
.dep = dep,
};
return microzig;
}
pub const MicroZig = struct {
creating_builder: *std.Build,
dep: *std.Build.Dependency,
pub fn get_tool_exe(mz: *MicroZig, tool: Tool) *std.Build.Step.Compile {
return mz.dep.artifact(@tagName(tool));
}
pub fn add_embedded_executable(mz: *MicroZig, options: EmbeddedExecutable.CreateOptions) *EmbeddedExecutable {
const target = mz.resolve_target(null, options.target);
const core_mod = mz.dep.builder.createModule(.{
.root_source_file = mz.dep.builder.path("core/microzig.zig"),
});
core_mod.addImport("microzig-target", target.module);
const exe: *EmbeddedExecutable = mz.dep.builder.allocator.create(EmbeddedExecutable) catch @panic("out of memory");
exe.* = .{
.artifact = mz.creating_builder.addExecutable(.{
.name = options.name,
.target = target.chip.cpu.target,
.optimize = options.optimize,
.root_source_file = options.root_source_file,
}),
};
exe.artifact.root_module.addImport("microzig", core_mod);
return exe;
}
pub fn install_artifact(mz: *MicroZig, exe: *EmbeddedExecutable) void {
mz.creating_builder.installArtifact(exe.artifact);
}
fn resolve_target(mz: *MicroZig, port_hint: ?*internals.Port, alias: *const internals.TargetAlias) *const internals.Target {
_ = mz;
_ = alias;
_ = port_hint;
@panic("not done");
}
};
pub const EmbeddedExecutable = struct {
pub const CreateOptions = struct {
name: []const u8,
target: *const internals.TargetAlias,
optimize: std.builtin.OptimizeMode,
root_source_file: std.Build.LazyPath,
};
artifact: *std.Build.Step.Compile,
};
pub const Tool = enum {
regz,
uf2,
zcom,
picotool,
// ...
};