forked from stlink-org/stlink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.zig
341 lines (308 loc) · 10.6 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
const std = @import("std");
const Build = std.Build;
const LazyPath = Build.LazyPath;
const c_flags = &.{
"-fno-sanitize-trap=undefined",
};
pub fn init(b: *Build, dependency_name: []const u8) *STLink {
const st = b.allocator.create(STLink) catch @panic("OOM");
st.* = STLink{
.b = b,
.self = b.dependency(dependency_name, .{ .optimize = .ReleaseSafe }),
};
return st;
}
pub fn build(b: *Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const libusb_dep = b.dependency("libusb", .{
.target = target,
.optimize = optimize,
});
const ubsan_runtime_dep = b.dependency("ubsan-runtime", .{
.target = target,
.optimize = optimize,
});
const libusb = libusb_dep.artifact("usb");
const ubsan_runtime = ubsan_runtime_dep.artifact("ubsan-runtime");
const version = b.addConfigHeader(.{
.style = .{
.cmake = .{ .path = "inc/version.h.in" },
},
}, .{
.PROJECT_VERSION = "1.7.0",
.PROJECT_VERSION_MAJOR = 1,
.PROJECT_VERSION_MINOR = 7,
.PROJECT_VERSION_PATCH = 0,
});
const stlink_chips_dir = b.fmt("\"{s}\"", .{b.pathFromRoot("config/chips")});
const stlink = b.addStaticLibrary(.{
.name = "stlink",
.target = target,
.optimize = optimize,
.link_libc = true,
});
stlink.addCSourceFiles(&.{
"src/stlink-lib/calculate.c",
"src/stlink-lib/chipid.c",
"src/stlink-lib/common.c",
"src/stlink-lib/common_flash.c",
"src/stlink-lib/flash_loader.c",
"src/stlink-lib/helper.c",
"src/stlink-lib/lib_md5.c",
"src/stlink-lib/logging.c",
"src/stlink-lib/map_file.c",
"src/stlink-lib/md5.c",
"src/stlink-lib/option_bytes.c",
"src/stlink-lib/read_write.c",
"src/stlink-lib/sg.c",
"src/stlink-lib/usb.c",
}, c_flags);
switch (target.getOsTag()) {
.macos => {
stlink.defineCMacro("STLINK_HAVE_SYS_TIME_H", null);
stlink.defineCMacro("STLINK_HAVE_SYS_MMAN_H", null);
stlink.defineCMacro("STLINK_HAVE_DIRENT_H", null);
stlink.linkFramework("CoreFoundation");
stlink.linkFramework("IOKit");
},
.windows => {
stlink.defineCMacro("STLINK_HAVE_SYS_TIME_H", null);
stlink.addCSourceFiles(&.{
"src/win32/sys_time.c",
"src/win32/getopt/getopt.c",
"src/win32/mmap.c",
"src/win32/win32_socket.c",
}, c_flags);
stlink.addIncludePath(.{ .path = "src/win32" });
stlink.linkSystemLibrary("wsock32");
stlink.linkSystemLibrary("ws2_32");
},
.linux => {
stlink.defineCMacro("STLINK_HAVE_SYS_TIME_H", null);
stlink.defineCMacro("STLINK_HAVE_SYS_MMAN_H", null);
stlink.defineCMacro("STLINK_HAVE_DIRENT_H", null);
},
else => {},
}
stlink.addIncludePath(.{ .path = "inc" });
stlink.addIncludePath(.{ .path = "src/stlink-lib" });
stlink.defineCMacro("STLINK_CHIPS_DIR", stlink_chips_dir);
stlink.addConfigHeader(version);
stlink.linkLibrary(libusb);
b.installArtifact(stlink);
const st_flash = b.addExecutable(.{
.name = "st-flash",
.target = target,
.optimize = optimize,
.link_libc = true,
});
st_flash.addCSourceFiles(&.{
"src/st-flash/flash.c",
"src/st-flash/flash_opts.c",
}, c_flags);
switch (target.getOsTag()) {
.macos => {
st_flash.defineCMacro("STLINK_HAVE_SYS_MMAN_H", null);
},
.windows => {
st_flash.addIncludePath(.{ .path = "src/win32" });
},
else => {},
}
st_flash.addIncludePath(.{ .path = "inc" });
st_flash.addIncludePath(.{ .path = "src/stlink-lib" });
st_flash.defineCMacro("STLINK_CHIPS_DIR", stlink_chips_dir);
st_flash.addConfigHeader(version);
st_flash.linkLibrary(stlink);
st_flash.linkLibrary(libusb);
st_flash.linkLibrary(ubsan_runtime);
b.installArtifact(st_flash);
const st_info = b.addExecutable(.{
.name = "st-info",
.target = target,
.optimize = optimize,
.link_libc = true,
});
st_info.addCSourceFiles(&.{
"src/st-info/info.c",
}, c_flags);
st_info.addIncludePath(.{ .path = "inc" });
st_info.addIncludePath(.{ .path = "src/stlink-lib" });
st_info.defineCMacro("STLINK_CHIPS_DIR", stlink_chips_dir);
st_info.addConfigHeader(version);
st_info.linkLibrary(stlink);
st_info.linkLibrary(libusb);
st_info.linkLibrary(ubsan_runtime);
b.installArtifact(st_info);
const st_util = b.addExecutable(.{
.name = "st-util",
.target = target,
.optimize = optimize,
.link_libc = true,
});
st_util.addCSourceFiles(&.{
"src/st-util/gdb-remote.c",
"src/st-util/gdb-server.c",
"src/st-util/semihosting.c",
}, c_flags);
if (target.getOsTag() == .windows)
st_util.addIncludePath(.{ .path = "src/win32" });
st_util.addIncludePath(.{ .path = "inc" });
st_util.addIncludePath(.{ .path = "src/stlink-lib" });
st_util.defineCMacro("STLINK_CHIPS_DIR", stlink_chips_dir);
st_util.addConfigHeader(version);
st_util.linkLibrary(stlink);
st_util.linkLibrary(libusb);
st_util.linkLibrary(ubsan_runtime);
b.installArtifact(st_util);
const st_trace = b.addExecutable(.{
.name = "st-trace",
.target = target,
.optimize = optimize,
.link_libc = true,
});
st_trace.addCSourceFiles(&.{
"src/st-trace/trace.c",
}, c_flags);
st_trace.addIncludePath(.{ .path = "inc" });
st_trace.addIncludePath(.{ .path = "src/stlink-lib" });
st_trace.defineCMacro("STLINK_CHIPS_DIR", stlink_chips_dir);
st_trace.addConfigHeader(version);
st_trace.linkLibrary(stlink);
st_trace.linkLibrary(libusb);
st_trace.linkLibrary(ubsan_runtime);
b.installArtifact(st_trace);
}
pub const STLink = struct {
b: *Build,
self: *Build.Dependency,
pub const Format = enum {
binary,
ihex,
};
pub const Area = enum {
main,
system,
otp,
optcr,
optcr1,
option,
option_boot_add,
};
pub const FlashOptions = union(enum) {
read: ReadWriteOptions,
write: ReadWriteOptions,
erase: struct {
debug: bool = false,
connect_under_reset: bool = false,
hot_plug: bool = false,
/// in KHz
freq: ?u32 = null,
serial: ?u32 = null,
},
reset: struct {
debug: bool = false,
/// in KHz
freq: ?u32 = null,
serial: ?u32 = null,
},
pub const ReadWriteOptions = struct {
debug: bool = false,
reset: bool = false,
connect_under_reset: bool = false,
hot_plug: bool = false,
opt: bool = false,
serial: ?u32 = null,
format: Format = .binary,
flash: ?u32 = null,
/// in KHz
freq: ?u32 = null,
area: Area = .main,
path: ?*Build.Step.Compile = null,
addr: ?u32 = null,
size: ?u32 = null,
};
};
fn add_read_write_opts(b: *Build, run: *Build.RunStep, opts: FlashOptions.ReadWriteOptions, cmd: []const u8) void {
if (opts.debug) run.addArg("--debug");
if (opts.reset) run.addArg("--reset");
if (opts.connect_under_reset) run.addArg("--connect-under-reset");
if (opts.hot_plug) run.addArg("--hot-plug");
if (opts.opt) run.addArg("--opt");
if (opts.serial) |s| run.addArg(b.fmt("--serial=0x{x}", .{s}));
run.addArg(b.fmt("--format={s}", .{switch (opts.format) {
.binary => "binary",
.ihex => "ihex",
}}));
if (opts.flash) |f| run.addArg(b.fmt("--flash=0x{x}", .{f}));
if (opts.freq) |f| run.addArg(b.fmt("--freq={x}", .{f}));
run.addArg(b.fmt("--area={s}", .{switch (opts.area) {
.main => "main",
.system => "system",
.otp => "otp",
.optcr => "optcr",
.optcr1 => "optcr1",
.option => "option",
.option_boot_add => "option_boot_add",
}}));
run.addArg(cmd);
if (opts.path) |path| {
const raw_elf = path.getEmittedBin();
const objcopy = b.addObjCopy(raw_elf, .{
.basename = path.name,
.format = switch (opts.format) {
.binary => .bin,
.ihex => .hex,
},
});
run.addFileArg(objcopy.getOutput());
}
if (opts.addr) |addr|
run.addArg(b.fmt("0x{x}", .{addr}));
if (opts.size) |size|
run.addArg(b.fmt("0x{x}", .{size}));
}
pub fn flash(stlink: *STLink, opts: FlashOptions) *Build.RunStep {
const b = stlink.b;
const st_flash = stlink.self.artifact("st-flash");
const run = stlink.b.addRunArtifact(st_flash);
switch (opts) {
.read => |o| add_read_write_opts(b, run, o, "read"),
.write => |o| add_read_write_opts(b, run, o, "write"),
.erase => |o| {
if (o.debug) run.addArg("--debug");
if (o.connect_under_reset) run.addArg("--connect-under-reset");
if (o.hot_plug) run.addArg("--hot-plug");
if (o.freq) |f| run.addArg(b.fmt("--freq={}", .{f}));
if (o.serial) |s| run.addArg(b.fmt("--serial=0x{x}", .{s}));
run.addArg("erase");
},
.reset => |o| {
if (o.debug) run.addArg("--debug");
if (o.freq) |f| run.addArg(b.fmt("--freq={}", .{f}));
if (o.serial) |s| run.addArg(b.fmt("--serial=0x{x}", .{s}));
run.addArg("reset");
},
}
return run;
}
pub const InfoOptions = struct {};
pub fn info(stlink: *STLink, opts: InfoOptions) *Build.RunStep {
_ = stlink;
_ = opts;
@panic("TODO: st-info arguments");
}
pub const UtilOptions = struct {};
pub fn util(stlink: *STLink, opts: UtilOptions) *Build.RunStep {
_ = stlink;
_ = opts;
@panic("TODO: st-util arguments");
}
pub const TraceOptions = struct {};
pub fn trace(stlink: *STLink, opts: TraceOptions) *Build.RunStep {
_ = stlink;
_ = opts;
@panic("TODO: st-trace arguments");
}
};