Skip to content
This repository was archived by the owner on Feb 17, 2024. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub const chips = struct {
},
},
.hal = hal,
.binary_post_process = compileWithAvrGcc,
};
};

Expand All @@ -40,6 +41,7 @@ pub const boards = struct {
.url = "https://docs.arduino.cc/hardware/nano",
.source_file = path("/src/boards/arduino_nano.zig"),
},
.binary_post_process = compileWithAvrGcc,
};

pub const uno_rev3 = .{
Expand All @@ -51,11 +53,50 @@ pub const boards = struct {
.url = "https://docs.arduino.cc/hardware/uno-rev3",
.source_file = path("/src/boards/arduino_uno.zig"),
},
.binary_post_process = compileWithAvrGcc,
};
};
};

pub fn build(b: *std.build.Builder) void {
fn compileWithAvrGcc(b: *std.Build, c_file: std.Build.LazyPath) std.Build.LazyPath {
const compile_step = @fieldParentPtr(std.Build.CompileStep, "step", c_file.generated.step);
if (compile_step.target.ofmt != .c) {
// sanity check: if not building a C file, skip.
return c_file;
}

const prog = b.findProgram(&.{"avr-gcc"}, &.{}) catch @panic("Please install avr-gcc!");

const lib_dir = if (b.zig_lib_dir) |lib_dir|
lib_dir.getPath(b)
else
b.pathJoin(&.{ std.fs.path.dirname(std.fs.path.dirname(b.zig_exe).?).?, "lib" });

const linker_script = compile_step.linker_script.?;

const compiler = b.addSystemCommand(&.{
prog,
"-g", // debug options
"-mmcu=avr5", // compile for avr5
"-Wno-builtin-declaration-mismatch", // hide weird zig warnings
"-I",
lib_dir,
"-nostartfiles", // do not link _start from avrlibc
"-ffreestanding", // do not link libc
});

compiler.addArg("-T");
compiler.addFileArg(linker_script);

compiler.addArg("-o");
const elf_file = compiler.addOutputFileArg("firmware.elf");

compiler.addFileArg(c_file);

return elf_file;
}

pub fn build(b: *std.Build) void {
_ = b;
// const optimize = b.standardOptimizeOption(.{});
// inline for (@typeInfo(boards).Struct.decls) |decl| {
Expand Down