Skip to content

Commit 50bf59b

Browse files
committed
allow building with latest zig
Zig changed some build related structs and functions. In order to build with the latest mainline some changes are required. Signed-off-by: Tobias Kohlbau <tobias@kohlbau.de>
1 parent 4180534 commit 50bf59b

3 files changed

Lines changed: 40 additions & 44 deletions

File tree

build.zig

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,10 @@ pub fn init(b: *std.Build, dependency_name: []const u8) *MicroZig {
3434

3535
/// This build script validates usage patterns we expect from MicroZig
3636
pub fn build(b: *std.Build) !void {
37-
const uf2_dep = b.dependency("uf2", .{});
38-
3937
const build_test = b.addTest(.{
4038
.root_source_file = .{ .path = "build.zig" },
4139
});
4240

43-
build_test.addAnonymousModule("uf2", .{
44-
.source_file = .{ .cwd_relative = uf2_dep.builder.pathFromRoot("build.zig") },
45-
});
46-
4741
const install_docs = b.addInstallDirectory(.{
4842
.source_dir = build_test.getEmittedDocs(),
4943
.install_dir = .prefix,
@@ -218,10 +212,10 @@ pub const Cpu = struct {
218212
name: []const u8,
219213

220214
/// Source file providing startup code and memory initialization routines.
221-
source_file: std.build.LazyPath,
215+
source_file: std.Build.LazyPath,
222216

223-
/// The compiler target we use to compile all the code.
224-
target: std.zig.CrossTarget,
217+
/// Query to resolve target used to compile all code.
218+
query: std.Target.Query,
225219
};
226220

227221
/// A descriptor for memory regions in a microcontroller.
@@ -321,13 +315,13 @@ pub const LinkerScript = union(enum) {
321315
generated,
322316

323317
/// Externally defined linker script.
324-
source_file: std.build.LazyPath,
318+
source_file: std.Build.LazyPath,
325319
};
326320

327321
/// A compilation target for MicroZig. Provides information about the chip,
328322
/// hal, board and so on.
329323
///
330-
/// This is used instead of `std.zig.CrossTarget` to define a MicroZig Firmware.
324+
/// This is used instead of `std.Target.Query` to define a MicroZig Firmware.
331325
pub const Target = struct {
332326
/// The preferred binary format of this MicroZig target. If `null`, the user must
333327
/// explicitly give the `.format` field during a call to `getEmittedBin()` or installation steps.
@@ -464,7 +458,7 @@ pub fn addFirmware(
464458
.artifact = host_build.addExecutable(.{
465459
.name = options.name,
466460
.optimize = options.optimize,
467-
.target = cpu.target,
461+
.target = host_build.resolveTargetQuery(cpu.query),
468462
.linkage = .static,
469463
.root_source_file = .{ .cwd_relative = mz.self.builder.pathFromRoot("src/start.zig") },
470464
}),
@@ -475,11 +469,11 @@ pub fn addFirmware(
475469

476470
.modules = .{
477471
.microzig = micro_build.createModule(.{
478-
.source_file = .{ .cwd_relative = micro_build.pathFromRoot("src/microzig.zig") },
479-
.dependencies = &.{
472+
.root_source_file = .{ .cwd_relative = micro_build.pathFromRoot("src/microzig.zig") },
473+
.imports = &.{
480474
.{
481475
.name = "config",
482-
.module = micro_build.createModule(.{ .source_file = config.getSource() }),
476+
.module = micro_build.createModule(.{ .root_source_file = config.getSource() }),
483477
},
484478
},
485479
}),
@@ -494,58 +488,60 @@ pub fn addFirmware(
494488
},
495489
};
496490
errdefer fw.output_files.deinit();
491+
// disable default entrypoint and use the one provided by the linker script
492+
fw.artifact.entry = .disabled;
497493

498494
fw.modules.chip = mz.b.createModule(.{
499-
.source_file = chip_source,
500-
.dependencies = &.{
495+
.root_source_file = chip_source,
496+
.imports = &.{
501497
.{ .name = "microzig", .module = fw.modules.microzig },
502498
},
503499
});
504-
fw.modules.microzig.dependencies.put("chip", fw.modules.chip) catch @panic("out of memory");
500+
fw.modules.microzig.addImport("chip", fw.modules.chip);
505501

506502
fw.modules.cpu = mz.b.createModule(.{
507-
.source_file = cpu.source_file,
508-
.dependencies = &.{
503+
.root_source_file = cpu.source_file,
504+
.imports = &.{
509505
.{ .name = "microzig", .module = fw.modules.microzig },
510506
},
511507
});
512-
fw.modules.microzig.dependencies.put("cpu", fw.modules.cpu) catch @panic("out of memory");
508+
fw.modules.microzig.addImport("cpu", fw.modules.cpu);
513509

514510
if (maybe_hal) |hal| {
515511
fw.modules.hal = mz.b.createModule(.{
516-
.source_file = hal.source_file,
517-
.dependencies = &.{
512+
.root_source_file = hal.source_file,
513+
.imports = &.{
518514
.{ .name = "microzig", .module = fw.modules.microzig },
519515
},
520516
});
521-
fw.modules.microzig.dependencies.put("hal", fw.modules.hal.?) catch @panic("out of memory");
517+
fw.modules.microzig.addImport("hal", fw.modules.hal.?);
522518
}
523519

524520
if (maybe_board) |brd| {
525521
fw.modules.board = mz.b.createModule(.{
526-
.source_file = brd.source_file,
527-
.dependencies = &.{
522+
.root_source_file = brd.source_file,
523+
.imports = &.{
528524
.{ .name = "microzig", .module = fw.modules.microzig },
529525
},
530526
});
531-
fw.modules.microzig.dependencies.put("board", fw.modules.board.?) catch @panic("out of memory");
527+
fw.modules.microzig.addImport("board", fw.modules.board.?);
532528
}
533529

534530
fw.modules.app = host_build.createModule(.{
535-
.source_file = options.source_file,
536-
.dependencies = &.{
531+
.root_source_file = options.source_file,
532+
.imports = &.{
537533
.{ .name = "microzig", .module = fw.modules.microzig },
538534
},
539535
});
540536

541537
const umm = mz.dependency("umm-zig", .{}).module("umm");
542-
fw.modules.microzig.dependencies.put("umm", umm) catch @panic("out of memory");
538+
fw.modules.microzig.addImport("umm", umm);
543539

544-
fw.artifact.addModule("app", fw.modules.app);
545-
fw.artifact.addModule("microzig", fw.modules.microzig);
540+
fw.artifact.root_module.addImport("app", fw.modules.app);
541+
fw.artifact.root_module.addImport("microzig", fw.modules.microzig);
546542

547-
fw.artifact.strip = false; // we always want debug symbols, stripping brings us no benefit on embedded
548-
fw.artifact.single_threaded = options.single_threaded orelse fw.target.single_threaded;
543+
fw.artifact.root_module.strip = false; // we always want debug symbols, stripping brings us no benefit on embedded
544+
fw.artifact.root_module.single_threaded = options.single_threaded orelse fw.target.single_threaded;
549545
fw.artifact.bundle_compiler_rt = options.bundle_compiler_rt orelse fw.target.bundle_compiler_rt;
550546

551547
switch (linker_script) {
@@ -778,7 +774,7 @@ pub const cpus = struct {
778774
pub const avr5 = Cpu{
779775
.name = "AVR5",
780776
.source_file = .{ .path = build_root ++ "/src/cpus/avr5.zig" },
781-
.target = std.zig.CrossTarget{
777+
.query = std.Target.Query{
782778
.cpu_arch = .avr,
783779
.cpu_model = .{ .explicit = &std.Target.avr.cpu.avr5 },
784780
.os_tag = .freestanding,
@@ -789,7 +785,7 @@ pub const cpus = struct {
789785
pub const cortex_m0 = Cpu{
790786
.name = "ARM Cortex-M0",
791787
.source_file = .{ .path = build_root ++ "/src/cpus/cortex-m.zig" },
792-
.target = std.zig.CrossTarget{
788+
.query = std.Target.Query{
793789
.cpu_arch = .thumb,
794790
.cpu_model = .{ .explicit = &std.Target.arm.cpu.cortex_m0 },
795791
.os_tag = .freestanding,
@@ -800,7 +796,7 @@ pub const cpus = struct {
800796
pub const cortex_m0plus = Cpu{
801797
.name = "ARM Cortex-M0+",
802798
.source_file = .{ .path = build_root ++ "/src/cpus/cortex-m.zig" },
803-
.target = std.zig.CrossTarget{
799+
.query = std.Target.Query{
804800
.cpu_arch = .thumb,
805801
.cpu_model = .{ .explicit = &std.Target.arm.cpu.cortex_m0plus },
806802
.os_tag = .freestanding,
@@ -811,7 +807,7 @@ pub const cpus = struct {
811807
pub const cortex_m3 = Cpu{
812808
.name = "ARM Cortex-M3",
813809
.source_file = .{ .path = build_root ++ "/src/cpus/cortex-m.zig" },
814-
.target = std.zig.CrossTarget{
810+
.query = std.Target.Query{
815811
.cpu_arch = .thumb,
816812
.cpu_model = .{ .explicit = &std.Target.arm.cpu.cortex_m3 },
817813
.os_tag = .freestanding,
@@ -822,7 +818,7 @@ pub const cpus = struct {
822818
pub const cortex_m4 = Cpu{
823819
.name = "ARM Cortex-M4",
824820
.source_file = .{ .path = build_root ++ "/src/cpus/cortex-m.zig" },
825-
.target = std.zig.CrossTarget{
821+
.query = std.Target.Query{
826822
.cpu_arch = .thumb,
827823
.cpu_model = .{ .explicit = &std.Target.arm.cpu.cortex_m4 },
828824
.os_tag = .freestanding,
@@ -833,7 +829,7 @@ pub const cpus = struct {
833829
pub const riscv32_imac = Cpu{
834830
.name = "RISC-V 32-bit",
835831
.source_file = .{ .path = build_root ++ "/src/cpus/riscv32.zig" },
836-
.target = std.zig.CrossTarget{
832+
.query = std.Target.Query{
837833
.cpu_arch = .riscv32,
838834
.cpu_model = .{ .explicit = &std.Target.riscv.cpu.sifive_e21 },
839835
.os_tag = .freestanding,
@@ -933,7 +929,7 @@ fn generateLinkerScript(b: *std.Build, chip: Chip) !std.Build.LazyPath {
933929
\\
934930
);
935931

936-
switch (cpu.target.getCpuArch()) {
932+
switch (cpu.query.cpu_arch orelse return error.InvalidTarget) {
937933
.arm, .thumb => try writer.writeAll(
938934
\\ .ARM.exidx : {
939935
\\ *(.ARM.exidx* .gnu.linkonce.armexidx.*)

src/cpus/cortex-m.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,12 @@ pub var vector_table: VectorTable = blk: {
128128
if (@typeInfo(interrupts) != .Struct)
129129
@compileLog("root.interrupts must be a struct");
130130

131-
inline for (@typeInfo(interrupts).Struct.decls) |decl| {
131+
for (@typeInfo(interrupts).Struct.decls) |decl| {
132132
const function = @field(interrupts, decl.name);
133133

134134
if (!@hasField(VectorTable, decl.name)) {
135135
var msg: []const u8 = "There is no such interrupt as '" ++ decl.name ++ "'. Declarations in 'interrupts' must be one of:\n";
136-
inline for (std.meta.fields(VectorTable)) |field| {
136+
for (std.meta.fields(VectorTable)) |field| {
137137
if (is_valid_field(field.name)) {
138138
msg = msg ++ " " ++ field.name ++ "\n";
139139
}

src/interrupt.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub fn globally_enabled() bool {
4040
/// Enters a critical section and disables interrupts globally.
4141
/// Call `.leave()` on the return value to restore the previous state.
4242
pub fn enter_critical_section() CriticalSection {
43-
var section = CriticalSection{
43+
const section = CriticalSection{
4444
.enable_on_leave = globally_enabled(),
4545
};
4646
disable_interrupts();

0 commit comments

Comments
 (0)