/ learning / zig / zora / build.zig
build.zig
 1  const std = @import("std");
 2  const microzig = @import("microzig");
 3  
 4  const MicroBuild = microzig.MicroBuild(.{
 5      .stm32 = true,
 6  });
 7  
 8  pub fn build(b: *std.Build) void {
 9      const optimize = b.standardOptimizeOption(.{});
10      const maybe_example = b.option([]const u8, "example", "only build matching examples");
11  
12      const mz_dep = b.dependency("microzig", .{});
13      const mb = MicroBuild.init(b, mz_dep) orelse return;
14      const stm32 = mb.ports.stm32;
15  
16      const available_examples = [_]Example{
17          // https://github.com/ZigEmbeddedGroup/microzig/blob/e334149663f63dcf0d9d84fbfc0d8abf500324e7/port/stmicro/stm32/src/boards/STM32F3DISCOVERY.zig
18          .{ .target = stm32.boards.stm32f3discovery, .name = "STM32F3discovery", .file = "src/blinky.zig" },
19          .{ .target = stm32.boards.stm32f3discovery, .name = "STM32F303_HTS221", .file = "src/hts221.zig" },
20          .{ .target = stm32.chips.STM32F100RB, .name = "STM32F1xx_semihost", .file = "src/semihosting.zig" }, //QEMU target: stm32vldiscovery
21      };
22  
23      for (available_examples) |example| {
24          // If we specify example, only select the ones that match
25          if (maybe_example) |selected_example|
26              if (!std.mem.containsAtLeast(u8, example.name, 1, selected_example))
27                  continue;
28  
29          // `add_firmware` basically works like addExecutable, but takes a
30          // `microzig.Target` for target instead of a `std.zig.CrossTarget`.
31          //
32          // The target will convey all necessary information on the chip,
33          // cpu and potentially the board as well.
34          const fw = mb.add_firmware(.{
35              .name = example.name,
36              .target = example.target,
37              .optimize = optimize,
38              .root_source_file = b.path(example.file),
39          });
40  
41          // `install_firmware()` is the MicroZig pendant to `Build.installArtifact()`
42          // and allows installing the firmware as a typical firmware file.
43          //
44          // This will also install into `$prefix/firmware` instead of `$prefix/bin`.
45          mb.install_firmware(fw, .{});
46  
47          // For debugging, we also always install the firmware as an ELF file
48          mb.install_firmware(fw, .{ .format = .elf });
49      }
50  }
51  
52  const Example = struct {
53      target: *const microzig.Target,
54      name: []const u8,
55      file: []const u8,
56  };