|
| 1 | +const std = @import("std"); |
| 2 | +const microzig = @import("microzig"); |
| 3 | + |
| 4 | +const MicroBuild = microzig.MicroBuild(.{ |
| 5 | + .mcx = 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 | + |
| 15 | + const frdm_mcxa153 = mb.ports.mcx.chips.mcxa153; |
| 16 | + |
| 17 | + const available_examples = [_]Example{ |
| 18 | + .{ .name = "blinky", .target = frdm_mcxa153, .file = "src/blinky.zig" }, |
| 19 | + .{ .name = "gpio_input", .target = frdm_mcxa153, .file = "src/gpio_input.zig" }, |
| 20 | + }; |
| 21 | + |
| 22 | + for (available_examples) |example| { |
| 23 | + // If we specify example, only select the ones that match |
| 24 | + if (maybe_example) |selected_example| |
| 25 | + if (!std.mem.containsAtLeast(u8, example.name, 1, selected_example)) |
| 26 | + continue; |
| 27 | + |
| 28 | + // `add_firmware` basically works like addExecutable, but takes a |
| 29 | + // `microzig.Target` for target instead of a `std.zig.CrossTarget`. |
| 30 | + // |
| 31 | + // The target will convey all necessary information on the chip, |
| 32 | + // cpu and potentially the board as well. |
| 33 | + const firmware = mb.add_firmware(.{ |
| 34 | + .name = example.name, |
| 35 | + .target = example.target, |
| 36 | + .optimize = optimize, |
| 37 | + .root_source_file = b.path(example.file), |
| 38 | + }); |
| 39 | + |
| 40 | + // `install_firmware()` is the MicroZig pendant to `Build.installArtifact()` |
| 41 | + // and allows installing the firmware as a typical firmware file. |
| 42 | + // |
| 43 | + // This will also install into `$prefix/firmware` instead of `$prefix/bin`. |
| 44 | + mb.install_firmware(firmware, .{}); |
| 45 | + |
| 46 | + // For debugging, we also always install the firmware as an ELF file |
| 47 | + mb.install_firmware(firmware, .{ .format = .elf }); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +const Example = struct { |
| 52 | + target: *const microzig.Target, |
| 53 | + name: []const u8, |
| 54 | + file: []const u8, |
| 55 | +}; |
0 commit comments