Skip to content

Commit

Permalink
Build system bug fix (#292)
Browse files Browse the repository at this point in the history
* fix build system bug

* fixed examples and package-test

* fix `package-test` compile errors

* better comments
  • Loading branch information
tact1m4n3 authored Nov 22, 2024
1 parent 1421f56 commit 80d5b66
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 21 deletions.
48 changes: 38 additions & 10 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ const std = @import("std");
const Build = std.Build;
const LazyPath = Build.LazyPath;

const internals = @import("microzig/build-internals");
const internals = @import("build-internals");
pub const Target = internals.Target;
pub const Chip = internals.Chip;
pub const HardwareAbstractionLayer = internals.HardwareAbstractionLayer;
pub const Board = internals.Board;
pub const BinaryFormat = internals.BinaryFormat;
pub const MemoryRegion = internals.MemoryRegion;

const regz = @import("microzig/tools/regz");
const regz = @import("tools/regz");

const port_list: []const struct {
name: [:0]const u8,
Expand Down Expand Up @@ -102,6 +102,31 @@ pub const PortCache = blk: {
var port_cache: PortCache = .{};

/// The MicroZig build system.
///
/// # Example usage:
/// ```zig
/// const std = @import("std");
/// const microzig = @import("microzig");
///
/// const MicroBuild = microzig.MicroBuild(.{
/// .rp2xxx = true,
/// });
///
/// pub fn build(b: *std.Build) void {
/// const optimize = b.standardOptimizeOption(.{});
///
/// const mz_dep = b.dependency("microzig", .{});
/// const mb = MicroBuild.init(b, mz_dep) orelse return;
///
/// const fw = mb.add_firmware(.{
/// .name = "test",
/// .root_source_file = b.path("src/main.zig"),
/// .target = mb.ports.rp2xxx.boards.raspberrypi.pico,
/// .optimize = optimize,
/// });
/// mb.install_firmware(fw, .{});
/// }
/// ```
pub fn MicroBuild(port_select: PortSelect) type {
return struct {
const SelectedPorts = blk: {
Expand Down Expand Up @@ -141,6 +166,9 @@ pub fn MicroBuild(port_select: PortSelect) type {
ports: SelectedPorts,

const InitReturnType = blk: {
// TODO: idk if this is idiomatic
@setEvalBranchQuota(2000);

var ok = true;
for (port_list) |port| {
if (@field(port_select, port.name)) {
Expand All @@ -154,16 +182,16 @@ pub fn MicroBuild(port_select: PortSelect) type {
}
};

/// Initializes the microzig build system.
// TODO: should we call this `create`?
pub fn init(b: *Build, dep: *Build.Dependency) InitReturnType {
/// Initializes the microzig build system. Returns null when there are ports
/// that haven't been fetched yet (it uses lazy dependencies internally).
pub fn init(b: *Build, dep: *Build.Dependency) ?InitReturnType {
if (InitReturnType == noreturn) {
inline for (port_list) |port| {
if (@field(port_select, port.name)) {
_ = dep.builder.lazyDependency(port.dep_name, .{});
}
}
std.process.exit(0);
return null;
}

var ports: SelectedPorts = undefined;
Expand Down Expand Up @@ -234,7 +262,7 @@ pub fn MicroBuild(port_select: PortSelect) type {

/// Additional patches the user may apply to the generated register
/// code. This does not override the chip's existing patches.
patches: []const regz.Patch = .{},
patches: []const regz.patch.Patch = &.{},
};

fn serialize_patches(b: *Build, patches: []const regz.patch.Patch) []const u8 {
Expand Down Expand Up @@ -302,17 +330,17 @@ pub fn MicroBuild(port_select: PortSelect) type {
regz_run.addArg("--output_path"); // Write to a file
const zig_file = regz_run.addOutputFileArg("chip.zig");

var patches = std.ArrayList(regz.Patch).init(b.allocator);
var patches = std.ArrayList(regz.patch.Patch).init(b.allocator);

// From chip definition
patches.appendSlice(target.chip.patches) catch @panic("OOM");

// From user invoking `add_firmware`
patches.appendSlice(options.patches) catch @panic("OOM");

if (patches.len > 0) {
if (patches.items.len > 0) {
// write patches to file
const patch_ndjson = serialize_patches(b, patches);
const patch_ndjson = serialize_patches(b, patches.items);
const write_file_step = b.addWriteFiles();
const patch_file = write_file_step.add("patch.ndjson", patch_ndjson);

Expand Down
2 changes: 1 addition & 1 deletion examples/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const example_dep_names: []const []const u8 = &.{
"nxp/lpc",
"raspberrypi/rp2xxx",
"stmicro/stm32",
"wch/ch32",
// "wch/ch32",
};

pub fn build(b: *std.Build) void {
Expand Down
2 changes: 1 addition & 1 deletion examples/gigadevice/gd32/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn build(b: *std.Build) void {
const optimize = b.standardOptimizeOption(.{});

const mz_dep = b.dependency("microzig", .{});
const mb = MicroBuild.init(b, mz_dep);
const mb = MicroBuild.init(b, mz_dep) orelse return;

const available_examples = [_]Example{
.{ .target = mb.ports.gd32.chips.gd32vf103xb, .name = "gd32vf103xb", .file = "src/empty.zig" },
Expand Down
2 changes: 1 addition & 1 deletion examples/microchip/atsam/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn build(b: *std.Build) void {
const optimize = b.standardOptimizeOption(.{});

const mz_dep = b.dependency("microzig", .{});
const mb = MicroBuild.init(b, mz_dep);
const mb = MicroBuild.init(b, mz_dep) orelse return;

const available_examples = [_]Example{
.{ .target = mb.ports.atsam.chips.atsamd51j19, .name = "atsamd51j19-blinky", .file = "src/blinky.zig" },
Expand Down
2 changes: 1 addition & 1 deletion examples/microchip/avr/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn build(b: *std.Build) void {
const optimize = b.standardOptimizeOption(.{});

const mz_dep = b.dependency("microzig", .{});
const mb = MicroBuild.init(b, mz_dep);
const mb = MicroBuild.init(b, mz_dep) orelse return;

const available_examples = [_]Example{
.{ .target = mb.ports.avr.boards.arduino.nano, .name = "arduino-nano_blinky", .file = "src/blinky.zig" },
Expand Down
2 changes: 1 addition & 1 deletion examples/nordic/nrf5x/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn build(b: *std.Build) void {
const optimize = b.standardOptimizeOption(.{});

const mz_dep = b.dependency("microzig", .{});
const mb = MicroBuild.init(b, mz_dep);
const mb = MicroBuild.init(b, mz_dep) orelse return;

const available_examples = [_]Example{
.{ .target = mb.ports.nrf5x.boards.nordic.nrf52840_dongle, .name = "nrf52480-dongle_blinky", .file = "src/blinky.zig" },
Expand Down
2 changes: 1 addition & 1 deletion examples/nxp/lpc/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn build(b: *std.Build) void {
const optimize = b.standardOptimizeOption(.{});

const mz_dep = b.dependency("microzig", .{});
const mb = MicroBuild.init(b, mz_dep);
const mb = MicroBuild.init(b, mz_dep) orelse return;

const available_examples = [_]Example{
.{ .target = mb.ports.lpc.boards.mbed.lpc1768, .name = "mbed-lpc1768_blinky", .file = "src/blinky.zig" },
Expand Down
2 changes: 1 addition & 1 deletion examples/raspberrypi/rp2xxx/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn build(b: *std.Build) void {
const optimize = b.standardOptimizeOption(.{});

const mz_dep = b.dependency("microzig", .{});
const mb = MicroBuild.init(b, mz_dep);
const mb = MicroBuild.init(b, mz_dep) orelse return;

const rp2040_only_examples: []const Example = &.{
// RaspberryPi Boards:
Expand Down
2 changes: 1 addition & 1 deletion examples/stmicro/stm32/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn build(b: *std.Build) void {
const optimize = b.standardOptimizeOption(.{});

const mz_dep = b.dependency("microzig", .{});
const mb = MicroBuild.init(b, mz_dep);
const mb = MicroBuild.init(b, mz_dep) orelse return;

const available_examples = [_]Example{
.{ .target = mb.ports.stm32.chips.STM32F103C8, .name = "STM32F103C8", .file = "src/blinky.zig" },
Expand Down
6 changes: 3 additions & 3 deletions tools/package-test/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ pub fn build(b: *std.Build) void {
const optimize = b.standardOptimizeOption(.{});

const mz_dep = b.dependency("microzig", .{});
const mb = MicroBuild.init(b, mz_dep);
const mb = MicroBuild.init(b, mz_dep) orelse return;

const examples: []const Example = &.{
.{ .target = mb.ports.rp2xxx.boards.raspberrypi.pico, .name = "rp2xxx" },
.{ .target = mb.ports.gd32.boards.sipeed.longan_nano, .name = "gd32" },
.{ .target = mb.ports.atsam.chips.atsamd51j19, .name = "atsam" },
.{ .target = mb.ports.avr.boards.arduino.nano, .name = "avr" },
.{ .target = mb.ports.nrf5x.boards.nordic.nrf52840_dongle, .name = "nrf5x" },
.{ .target = mb.ports.lpc.boards.nordic.mbed.lpc1768, .name = "lpc" },
.{ .target = mb.ports.lpc.boards.mbed.lpc1768, .name = "lpc" },
.{ .target = mb.ports.stm32.boards.stm32f3discovery, .name = "stm32" },
};

Expand All @@ -40,6 +40,6 @@ pub fn build(b: *std.Build) void {
}

const Example = struct {
target: *microzig.Target,
target: *const microzig.Target,
name: []const u8,
};
1 change: 1 addition & 0 deletions tools/package-test/build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
.paths = .{
"build.zig",
"build.zig.zon",
"src",
},
}

0 comments on commit 80d5b66

Please sign in to comment.