@@ -26,6 +26,7 @@ pub const chips = struct {
2626 },
2727 },
2828 .hal = hal ,
29+ .binary_post_process = compileWithAvrGcc ,
2930 };
3031};
3132
@@ -40,6 +41,7 @@ pub const boards = struct {
4041 .url = "https://docs.arduino.cc/hardware/nano" ,
4142 .source_file = path ("/src/boards/arduino_nano.zig" ),
4243 },
44+ .binary_post_process = compileWithAvrGcc ,
4345 };
4446
4547 pub const uno_rev3 = .{
@@ -51,11 +53,50 @@ pub const boards = struct {
5153 .url = "https://docs.arduino.cc/hardware/uno-rev3" ,
5254 .source_file = path ("/src/boards/arduino_uno.zig" ),
5355 },
56+ .binary_post_process = compileWithAvrGcc ,
5457 };
5558 };
5659};
5760
58- pub fn build (b : * std.build.Builder ) void {
61+ fn compileWithAvrGcc (b : * std.Build , c_file : std.Build.LazyPath ) std.Build.LazyPath {
62+ const compile_step = @fieldParentPtr (std .Build .CompileStep , "step" , c_file .generated .step );
63+ if (compile_step .target .ofmt != .c ) {
64+ // sanity check: if not building a C file, skip.
65+ return c_file ;
66+ }
67+
68+ const prog = b .findProgram (&.{"avr-gcc" }, &.{}) catch @panic ("Please install avr-gcc!" );
69+
70+ const lib_dir = if (b .zig_lib_dir ) | lib_dir |
71+ lib_dir .getPath (b )
72+ else
73+ b .pathJoin (&.{ std .fs .path .dirname (std .fs .path .dirname (b .zig_exe ).? ).? , "lib" });
74+
75+ const linker_script = compile_step .linker_script .? ;
76+
77+ const compiler = b .addSystemCommand (&.{
78+ prog ,
79+ "-g" , // debug options
80+ "-mmcu=avr5" , // compile for avr5
81+ "-Wno-builtin-declaration-mismatch" , // hide weird zig warnings
82+ "-I" ,
83+ lib_dir ,
84+ "-nostartfiles" , // do not link _start from avrlibc
85+ "-ffreestanding" , // do not link libc
86+ });
87+
88+ compiler .addArg ("-T" );
89+ compiler .addFileArg (linker_script );
90+
91+ compiler .addArg ("-o" );
92+ const elf_file = compiler .addOutputFileArg ("firmware.elf" );
93+
94+ compiler .addFileArg (c_file );
95+
96+ return elf_file ;
97+ }
98+
99+ pub fn build (b : * std.Build ) void {
59100 _ = b ;
60101 // const optimize = b.standardOptimizeOption(.{});
61102 // inline for (@typeInfo(boards).Struct.decls) |decl| {
0 commit comments