-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.zig
312 lines (286 loc) · 9.4 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
const std = @import("std");
const builtin = @import("builtin");
pub const LoadOptions = struct {
// firmware file to load
firmware: std.Build.LazyPath,
// try rebooting into BOOTSEL before flashing
force: bool = false,
// reboot into program after flashing
execute: bool = false,
// use sudo if usb device is requires elevated permissions
sudo: bool = false,
// device-selection
bus: ?[]const u8 = null,
address: ?[]const u8 = null,
vid: ?[]const u8 = null,
pid: ?[]const u8 = null,
};
// load a firmware file to the default raspi pico found connected on usb
pub fn load(b: *std.Build, opt: LoadOptions, args: anytype) *std.Build.Step.Run {
const this_dep = b.dependencyFromBuildZig(@This(), args);
const flash_step = std.Build.Step.Run.create(b, "picotool");
if (opt.sudo) {
flash_step.addArg("sudo");
}
flash_step.addFileArg(this_dep.artifact("picotool"));
flash_step.addArg("load");
flash_step.addFileArg(opt.firmware);
if (opt.force) {
flash_step.addArg("--force");
}
if (opt.execute) {
flash_step.addArg("--execute");
}
if (opt.bus) |bus| {
flash_step.addArgs(&.{ "--bus", bus });
}
if (opt.address) |address| {
flash_step.addArgs(&.{ "--address", address });
}
if (opt.vid) |vid| {
flash_step.addArgs(&.{ "--vid", vid });
}
if (opt.pid) |pid| {
flash_step.addArgs(&.{ "--pid", pid });
}
return flash_step;
}
pub fn build(b: *std.Build) void {
const run_step = b.step("run", "run picotool");
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const backup_package_sources = b.option(bool, "package_backup", "package_backup") orelse false;
if (backup_package_sources) {
const global_cache: std.Build.LazyPath = .{ .cwd_relative = b.graph.global_cache_root.path.? };
b.installDirectory(.{
.source_dir = global_cache.path(b, "p"),
.install_dir = .{ .custom = "packages" },
.install_subdir = "",
});
}
const sdk_module = b.addModule("sdk", .{
.root_source_file = b.path("src/sdk.zig"),
.target = target,
.optimize = optimize,
});
_ = sdk_module;
const pico_sdk = b.dependency("pico-sdk", .{
.target = target,
.optimize = optimize,
});
const libusb = b.dependency("libusb", .{
.target = target,
.optimize = b.option(
std.builtin.OptimizeMode,
"optimize-libusb",
"optimize mode for libusb, defaults to ReleaseFast",
) orelse .ReleaseFast,
.@"system-libudev" = false,
});
const binh = b.addExecutable(.{
.name = "binh",
.root_source_file = b.path("src/binh.zig"),
.target = b.host,
.optimize = optimize,
});
b.step("binh", "install binh binary").dependOn(
&b.addInstallArtifact(binh, .{}).step,
);
if (b.lazyDependency("picotool", .{
.target = target,
.optimize = optimize,
})) |picotool_src| {
const generate_headers = b.step(
"generate_headers",
"install the generated binh headers",
);
const rp2350_h = generate_header(
b,
binh,
picotool_src.path("bootrom.end.bin"),
"rp2350_rom",
"rp2350.rom.h",
);
generate_headers.dependOn(&b.addInstallFile(
rp2350_h,
"include/rp2350.rom.h",
).step);
const xip_ram_perms_elf_h = generate_header(
b,
binh,
picotool_src.path("xip_ram_perms/xip_ram_perms.elf"),
"xip_ram_perms_elf",
"xip_ram_perms_elf.h",
);
generate_headers.dependOn(&b.addInstallFile(
xip_ram_perms_elf_h,
"include/xip_ram_perms_elf.h",
).step);
const flash_id_bin_h = generate_header(
b,
binh,
picotool_src.path("picoboot_flash_id/flash_id.bin"),
"flash_id_bin",
"flash_id_bin.h",
);
generate_headers.dependOn(&b.addInstallFile(
flash_id_bin_h,
"include/flash_id_bin.h",
).step);
const data_locs = b.addConfigHeader(.{
.style = .{ .cmake = picotool_src.path("data_locs.template.cpp") },
.include_path = "data_locs.cpp",
}, .{
.DATA_LOCS_VEC = "",
});
//elf2uf2
const elf2uf2 = b.addStaticLibrary(.{
.name = "elf2uf2",
.target = target,
.optimize = optimize,
});
elf2uf2.linkLibCpp();
elf2uf2.addCSourceFiles(.{
.files = &.{"elf2uf2.cpp"},
.root = picotool_src.path("elf2uf2"),
.flags = &cppflags,
});
inline for (.{
"elf",
"errors",
}) |include_path| {
const picotool_path = picotool_src.path(include_path);
elf2uf2.addIncludePath(picotool_path);
}
inline for (.{
"src/common/boot_uf2_headers/include",
}) |include_path| {
const pico_sdk_path = pico_sdk.path(include_path);
elf2uf2.addIncludePath(pico_sdk_path);
}
//picotool
const picotool = b.addExecutable(.{
.name = "picotool",
.target = target,
.optimize = optimize,
});
picotool.linkLibCpp();
picotool.addCSourceFile(.{
.file = data_locs.getOutput(),
.flags = &cppflags,
});
picotool.addCSourceFiles(.{
.files = &.{
"main.cpp",
"otp.cpp",
"xip_ram_perms.cpp",
"bintool/bintool.cpp",
"elf/elf_file.cpp",
"errors/errors.cpp",
"lib/whereami/whereami++.cpp",
"picoboot_connection/picoboot_connection_cxx.cpp",
},
.root = picotool_src.path(""),
.flags = &cppflags,
});
picotool.addCSourceFiles(.{
.files = &.{
"picoboot_connection/picoboot_connection.c",
},
.root = picotool_src.path(""),
.flags = &cflags,
});
inline for (.{
.{ "SYSTEM_VERSION", "\"2.0.0\"" },
.{ "PICOTOOL_VERSION", "\"2.0.0\"" },
.{ "COMPILER_INFO", "\"zig-" ++ builtin.zig_version_string ++ "\"" },
.{ "_CLANG_DISABLE_CRT_DEPRECATION_WARNINGS", "1" },
}) |macro| {
picotool.defineCMacro(macro[0], macro[1]);
}
picotool.defineCMacro("HAS_LIBUSB", "1");
picotool.linkLibrary(libusb.artifact("usb"));
picotool.linkLibrary(elf2uf2);
picotool.addIncludePath(rp2350_h.dirname());
picotool.addIncludePath(xip_ram_perms_elf_h.dirname());
picotool.addIncludePath(flash_id_bin_h.dirname());
inline for (.{
"",
"bintool",
"elf",
"elf2uf2",
"errors",
"lib/nlohmann_json/single_include",
"lib/whereami",
"otp_header_parser",
"picoboot_connection",
}) |include_path| {
const picotool_path = picotool_src.path(include_path);
picotool.addIncludePath(picotool_path);
}
inline for (.{
"src/common/boot_picobin_headers/include",
"src/common/boot_picoboot_headers/include",
"src/common/boot_uf2_headers/include",
"src/common/pico_binary_info/include",
"src/common/pico_usb_reset_interface_headers/include",
"src/host/pico_platform/include",
"src/rp2_common/boot_bootrom_headers/include",
"src/rp2_common/pico_stdio_usb/include",
"src/rp2350/hardware_regs/include",
}) |include_path| {
const pico_sdk_path = pico_sdk.path(include_path);
picotool.addIncludePath(pico_sdk_path);
}
b.installArtifact(picotool);
const run_picotool = b.addRunArtifact(picotool);
if (b.args) |args| {
run_picotool.addArgs(args);
}
run_step.dependOn(&run_picotool.step);
const udev_rules = b.addInstallFile(
picotool_src.path("udev/99-picotool.rules"),
"etc/udev/rules.d/99-picotool.rules",
);
b.step("udev", "install the raspberry udev rules").dependOn(&udev_rules.step);
}
}
pub const cppflags = .{
"-std=c++23",
} ++ commonflags;
pub const cflags = .{
"-std=c23",
// "-pedantic",
} ++ commonflags;
pub const commonflags = .{
"-fsanitize=undefined",
"-fsanitize=bounds",
"-Wall",
"-Wextra",
"-g",
"-Werror",
"-Wno-delete-non-abstract-non-virtual-dtor",
"-Wno-enum-enum-conversion",
"-Wno-format",
"-Wno-newline-eof",
"-Wno-sign-compare",
"-Wno-unsequenced",
"-Wno-unused-but-set-variable",
"-Wno-unused-const-variable",
"-Wno-unused-function",
"-Wno-unused-parameter",
"-Wno-unused-variable",
"-Wno-zero-length-array",
};
pub fn generate_header(
b: *std.Build,
binh: *std.Build.Step.Compile,
input: std.Build.LazyPath,
name: []const u8,
out_basename: []const u8,
) std.Build.LazyPath {
const run_step = b.addRunArtifact(binh);
run_step.addFileArg(input);
run_step.addArg(name);
return run_step.addOutputFileArg(out_basename);
}