Implements board-support info extraction that inspects (and validates) the build.zig file for BSPs

wch-ch32v003
Felix "xq" Queißner 9 months ago
parent 0c4e82e697
commit 6eccd8fd95

@ -47,6 +47,10 @@ pub const chips = struct {
};
};
pub const boards = struct {
// empty right now
};
pub fn build(b: *std.Build) void {
_ = b;
// const optimize = b.standardOptimizeOption(.{});

@ -11,8 +11,7 @@ fn root() []const u8 {
const build_root = root();
pub const chips = struct {
pub const atsamd51j19 = .{
pub const chip_atsamd51j19 = .{
.name = "ATSAMD51J19A",
.url = "https://www.microchip.com/en-us/product/ATSAMD51J19A",
.cpu = .cortex_m4,
@ -26,4 +25,14 @@ pub const chips = struct {
.{ .kind = .flash, .offset = 0x00804000, .length = 512 }, // NVM User Row
},
};
pub const chips = struct {
pub const atsamd51j19 = .{
.preferred_format = .elf,
.chip = chip_atsamd51j19,
};
};
pub const boards = struct {
// empty right now
};

@ -1,5 +1,5 @@
const std = @import("std");
const microzig = @import("root").dependencies.imports.microzig; // HACK: Please import MicroZig always under the name `microzig`. Otherwise the RP2040 module will fail to be properly imported.
const microzig = @import("microzig");
fn root() []const u8 {
return comptime (std.fs.path.dirname(@src().file) orelse ".");

@ -1,5 +1,5 @@
const std = @import("std");
const microzig = @import("root").dependencies.imports.microzig; // HACK: Please import MicroZig always under the name `microzig`. Otherwise the RP2040 module will fail to be properly imported.
const microzig = @import("microzig");
fn root() []const u8 {
return comptime (std.fs.path.dirname(@src().file) orelse ".");

@ -86,6 +86,7 @@ for dir in $(find -type f -name microzig-package.json -exec dirname '{}' ';'); d
out_dir=""
out_basename=""
extra_json="{}"
case "${pkg_type}" in
core)
@ -96,6 +97,17 @@ for dir in $(find -type f -name microzig-package.json -exec dirname '{}' ';'); d
board-support)
out_dir="${deploy_target}/board-support/$(dirname "${pkg_name}")"
out_basename="$(basename "${pkg_name}")"
extra_json="$(
zig run \
"${repo_root}/tools/extract-bsp-info.zig" \
--cache-dir "${repo_root}/zig-cache" \
--deps bsp,microzig \
--mod "bsp:microzig:${dir}/build.zig" \
--mod "microzig:uf2:${repo_root}/core/build.zig" \
--mod "uf2::${repo_root}/tools/lib/dummy_uf2.zig" \
)"
;;
*)
@ -132,7 +144,8 @@ for dir in $(find -type f -name microzig-package.json -exec dirname '{}' ';'); d
--arg fhash "${file_hash}" \
--arg fsize "${file_size}" \
--argjson pkg "${pkg_info}" \
'. + {
--argjson extra "${extra_json}" \
'. + $extra + {
version: $vers,
created: {
unix: $ts_unix,
@ -152,6 +165,5 @@ for dir in $(find -type f -name microzig-package.json -exec dirname '{}' ';'); d
ln -s "${all_files_dir}/${out_fullname}" "${out_name}"
ln -s "${all_files_dir}/${out_fullmeta}" "${out_meta}"
)
)
done

@ -40,6 +40,3 @@ fi
# https://stackoverflow.com/a/53083343
tar -caf "${output_file}" $(git ls-files -- . ':!:microzig-package.json')
)
# echo "included files:"
# tar -tf "${output_file}"

@ -0,0 +1,102 @@
//!
//! A tool that extracs which chips and boards are avaiilable from a board support package
//! and validates that the declarations conform
//!
const std = @import("std");
const bsp = @import("bsp");
const microzig = @import("microzig");
const JsonTarget = struct {
id: []const u8,
output_format: ?[]const u8,
features: struct {
hal: bool,
},
memory: struct {
flash: u64,
ram: u64,
},
cpu: []const u8,
chip: []const u8,
chip_url: ?[]const u8,
board: ?[]const u8,
board_url: ?[]const u8,
};
fn renderMicroZigTarget(stream: anytype, key: []const u8, target: microzig.Target) !void {
var jtarget = JsonTarget{
.id = key,
.output_format = if (target.preferred_format) |fmt| @tagName(fmt) else null,
.features = .{
.hal = (target.hal != null),
},
.cpu = @tagName(target.chip.cpu),
.chip = target.chip.name,
.chip_url = target.chip.url,
.board = null,
.board_url = null,
.memory = .{
.flash = 0,
.ram = 0,
},
};
if (target.board) |brd| {
jtarget.board = brd.name;
jtarget.board_url = brd.url;
}
for (target.chip.memory_regions) |reg| {
switch (reg.kind) {
.flash => jtarget.memory.flash += reg.length,
.ram => jtarget.memory.flash += reg.length,
else => {},
}
}
try std.json.stringify(jtarget, .{}, stream);
}
fn renderTargetArray(stream: anytype, comptime array: type) !void {
inline for (comptime std.meta.declarations(array), 0..) |fld, i| {
if (i > 0) try stream.writeAll(",");
const target = comptime @field(array, fld.name);
if (@TypeOf(target) == type) {
// recurse
try renderTargetArray(stream, target);
} else {
try renderMicroZigTarget(
stream,
fld.name,
target,
);
}
}
}
pub fn main() !void {
var stdout = std.io.getStdOut().writer();
try stdout.writeAll("{ \"board-support\": {");
try stdout.writeAll("\"chips\":[");
try renderTargetArray(stdout, bsp.chips);
try stdout.writeAll("],\"boards\":[");
try renderTargetArray(stdout, bsp.boards);
try stdout.writeAll("]}}");
}

@ -0,0 +1,5 @@
//!
//! Only required for the BSP info tooling, fakes everything we need to make the build work
//!
pub const FamilyId = enum { RP2040 };
Loading…
Cancel
Save