-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild.zig
More file actions
418 lines (380 loc) · 11.5 KB
/
Copy pathbuild.zig
File metadata and controls
418 lines (380 loc) · 11.5 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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
const std = @import("std");
const builtin = @import("builtin");
const build_zon = @import("build.zig.zon");
const Build = std.Build;
const LazyPath = Build.LazyPath;
const Run = Build.Step.Run;
const Compile = Build.Step.Compile;
const OptimizeMode = std.builtin.OptimizeMode;
const pico_sdk_version = build_zon.version;
pub fn build(b: *Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const data_locs = b.option(
[]const u8,
"data-locs",
"semicolon separated runtime data locations",
) orelse "./;/usr/local/share/picotool";
const ci_step = b.step("ci", "build picotool for ci targets");
const picotool_src = b.dependency("picotool", .{});
const udev_rules = b.addInstallFile(
picotool_src.path("udev/60-picotool.rules"),
"etc/udev/rules.d/60-picotool.rules",
);
b.getInstallStep().dependOn(&udev_rules.step);
ci_step.dependOn(&udev_rules.step);
b.step("udev", "install the raspberry udev rules").dependOn(&udev_rules.step);
const picotool_exe = buildWithOptions(
b,
target,
optimize,
data_locs,
true,
);
b.installArtifact(picotool_exe);
const run_picotool = b.addRunArtifact(picotool_exe);
passthroughArgs(b, run_picotool);
const run_step = b.step("run", "run picotool");
run_step.dependOn(&run_picotool.step);
inline for (&.{
"x86_64-linux-gnu",
"x86_64-linux-musl",
"aarch64-linux-gnu",
"aarch64-linux-musl",
"x86_64-windows-gnu",
"aarch64-windows-gnu",
}) |target_str| {
const ci_target = b.resolveTargetQuery(std.Target.Query.parse(.{ .arch_os_abi = target_str }) catch unreachable);
const exe = buildWithOptions(
b,
ci_target,
optimize,
data_locs,
false,
);
const ci_install = b.addInstallArtifact(exe, .{
.dest_dir = .{ .override = .{ .custom = "bin/" ++ target_str } },
});
ci_step.dependOn(&ci_install.step);
}
}
fn buildWithOptions(
b: *Build,
target: Build.ResolvedTarget,
optimize: OptimizeMode,
data_locs: []const u8,
global_steps: bool,
) *Compile {
const pico_sdk = b.dependency("pico-sdk", .{});
const picotool_src = b.dependency("picotool", .{});
const libusb = b.dependency("libusb", .{
.target = target,
.optimize = optimize,
.@"system-libudev" = false,
});
const binh = b.addExecutable(.{
.name = "binh",
.root_module = b.createModule(.{
.root_source_file = b.path("src/binh.zig"),
.target = b.graph.host,
.optimize = optimize,
}),
});
const xip_ram_perms_elf_h = generate_binh(
b,
binh,
picotool_src.path("xip_ram_perms/xip_ram_perms.elf"),
"xip_ram_perms_elf",
"xip_ram_perms_elf.h",
);
const flash_id_bin_h = generate_binh(
b,
binh,
picotool_src.path("picoboot_flash_id/flash_id.bin"),
"flash_id_bin",
"flash_id_bin.h",
);
const data_locs_vec = b.fmt("{f}", .{
DataLocsFmt.fmt(data_locs),
});
const data_locs_h = b.addConfigHeader(.{
.style = .{ .cmake = picotool_src.path("data_locs.template.cpp") },
.include_path = "data_locs.cpp",
}, .{
.DATA_LOCS_VEC = data_locs_vec,
});
//elf2uf2
const elf2uf2 = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libcpp = true,
});
const libelf2uf2 = b.addLibrary(.{
.name = "elf2uf2",
.root_module = elf2uf2,
});
elf2uf2.addCSourceFiles(.{
.files = &.{"elf2uf2.cpp"},
.root = picotool_src.path("elf2uf2"),
.flags = &cppflags,
});
libelf2uf2.installHeadersDirectory(
picotool_src.path("elf2uf2"),
"",
.{},
);
inline for (.{
"elf",
"errors",
"model",
}) |include_path| {
elf2uf2.addIncludePath(picotool_src.path(include_path));
}
inline for (.{
"src/common/boot_picoboot_headers/include",
"src/common/boot_uf2_headers/include",
"src/host/pico_platform/include",
}) |include_path| {
elf2uf2.addIncludePath(pico_sdk.path(include_path));
}
//oofatfs
const oofatfs = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
});
const liboofatfs = b.addLibrary(.{
.name = "oofatfs",
.root_module = oofatfs,
});
oofatfs.addCSourceFiles(.{
.root = picotool_src.path("lib/oofatfs/src"),
.files = &.{
"ff.c",
"ffunicode.c",
},
.flags = &cflags,
});
oofatfs.addIncludePath(picotool_src.path("lib/oofatfs/src"));
liboofatfs.installHeadersDirectory(
picotool_src.path("lib/oofatfs/src"),
"",
.{},
);
//littlefs
const littlefs = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
});
const liblittlefs = b.addLibrary(.{
.name = "littlefs",
.root_module = littlefs,
});
littlefs.addCSourceFiles(.{
.root = picotool_src.path("lib/littlefs"),
.files = &.{
"lfs.c",
"lfs_util.c",
},
.flags = &cflags,
});
littlefs.addIncludePath(picotool_src.path("lib/littlefs"));
liblittlefs.installHeadersDirectory(
picotool_src.path("lib/littlefs"),
"",
.{},
);
//picotool
const picotool = b.createModule(.{
.target = target,
.optimize = optimize,
});
const picotool_exe = b.addExecutable(.{
.name = "picotool",
.root_module = picotool,
});
picotool.addCSourceFile(.{
.file = data_locs_h.getOutputFile(),
.flags = &cppflags,
});
picotool.addCSourceFiles(.{
.files = &.{
"main.cpp",
"model/model.cpp",
"otp.cpp",
"get_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,
});
const pico_sdk_version_escaped = b.fmt("\"{s}\"", .{pico_sdk_version});
inline for (.{
.{ "SYSTEM_VERSION", pico_sdk_version_escaped },
.{ "PICOTOOL_VERSION", pico_sdk_version_escaped },
.{ "COMPILER_INFO", "\"zig-" ++ builtin.zig_version_string ++ "\"" },
.{ "_CLANG_DISABLE_CRT_DEPRECATION_WARNINGS", "1" },
}) |macro| {
picotool.addCMacro(macro[0], macro[1]);
}
picotool.addCMacro("HAS_LIBUSB", "1");
picotool.linkLibrary(libusb.artifact("usb"));
picotool.linkLibrary(libelf2uf2);
picotool.linkLibrary(liboofatfs);
picotool.linkLibrary(liblittlefs);
const generate_headers = generate_headers: {
if (global_steps) {
b.step("binh", "install binh binary").dependOn(
&b.addInstallArtifact(binh, .{}).step,
);
const generate_headers = b.step(
"generate_headers",
"install the generated binh headers",
);
generate_headers.dependOn(&b.addInstallHeaderFile(
xip_ram_perms_elf_h,
"xip_ram_perms_elf.h",
).step);
generate_headers.dependOn(&b.addInstallHeaderFile(
flash_id_bin_h,
"flash_id_bin.h",
).step);
break :generate_headers generate_headers;
}
break :generate_headers null;
};
inline for (&.{
"rp2350_a2_rom_end",
"rp2350_a3_rom_end",
"rp2350_a4_rom_end",
}) |bin_h_name| {
const model_h_name = bin_h_name ++ ".h";
const model_h = generate_binh(
b,
binh,
picotool_src.path("model/" ++ bin_h_name ++ ".bin"),
bin_h_name,
model_h_name,
);
if (generate_headers) |gh| {
gh.dependOn(&b.addInstallHeaderFile(
model_h,
model_h_name,
).step);
}
picotool.addIncludePath(model_h.dirname());
elf2uf2.addIncludePath(model_h.dirname());
}
picotool.addIncludePath(xip_ram_perms_elf_h.dirname());
picotool.addIncludePath(flash_id_bin_h.dirname());
inline for (.{
"",
"bintool",
"elf",
"errors",
"lib/nlohmann_json/single_include",
"lib/whereami",
"model",
"otp_header_parser",
"picoboot_connection",
}) |include_path| {
picotool.addIncludePath(picotool_src.path(include_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);
}
return picotool_exe;
}
pub const DataLocsFmt = struct {
src: []const u8,
pub fn fmt(src: []const u8) DataLocsFmt {
return .{ .src = src };
}
pub fn format(self: *const @This(), w: *std.Io.Writer) !void {
for (self.src) |c| {
if (c == ';') {
@branchHint(.unlikely);
try w.writeAll("\",\"");
continue;
}
try w.writeAll(&.{c});
}
}
};
pub const cppflags = .{
"-std=c++23",
"-fuse-cxa-atexit",
} ++ commonflags;
pub const cflags = .{
"-std=c23",
"-pedantic",
} ++ commonflags;
pub const commonflags = .{
"-fsanitize=undefined",
"-fsanitize-trap=undefined",
"-fsanitize=bounds",
"-Wall",
"-Wextra",
"-g",
"-Werror",
"-Wno-error=delete-non-abstract-non-virtual-dtor",
"-Wno-error=enum-enum-conversion",
"-Wno-error=format",
"-Wno-error=missing-field-initializers",
"-Wno-error=newline-eof",
"-Wno-error=reorder",
"-Wno-error=sign-compare",
"-Wno-error=unsequenced",
"-Wno-error=unused-but-set-variable",
"-Wno-error=unused-command-line-argument",
"-Wno-error=unused-const-variable",
"-Wno-error=unused-function",
"-Wno-error=unused-parameter",
"-Wno-error=unused-variable",
"-Wno-error=zero-length-array",
};
pub fn generate_binh(
b: *Build,
binh: *Compile,
input: LazyPath,
name: []const u8,
out_basename: []const u8,
) LazyPath {
const run_step = b.addRunArtifact(binh);
run_step.addFileArg(input);
run_step.addArg(name);
return run_step.addOutputFileArg(out_basename);
}
// zig 0.17.0 and 0.16.0 compatible args passthrough function
inline fn passthroughArgs(b: *Build, run: *Run) void {
if (comptime @import("builtin").zig_version.order(std.SemanticVersion.parse("0.16.0") catch unreachable) == .gt) {
run.addPassthruArgs();
} else {
if (b.args) |args| {
for (args) |arg| run.addArg(arg);
}
}
}