|
| 1 | +const std = @import("std"); |
| 2 | +const microzig = @import("microzig"); |
| 3 | + |
| 4 | +const MicroBuild = microzig.MicroBuild(.{ |
| 5 | + .hc32l110 = 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 examples: []const Example = &.{ |
| 16 | + // .{ .name = "adc", .file = "src/adc.zig" }, |
| 17 | + // .{ .name = "i2c-bus-scan", .file = "src/i2c_bus_scan.zig" }, |
| 18 | + // .{ .name = "pwm", .file = "src/pwm.zig" }, |
| 19 | + // .{ .name = "uart-echo", .file = "src/uart_echo.zig" }, |
| 20 | + // .{ .name = "uart-log", .file = "src/uart_log.zig" }, |
| 21 | + // .{ .name = "spi-master", .file = "src/spi_master.zig" }, |
| 22 | + // .{ .name = "spi-slave", .file = "src/spi_slave.zig" }, |
| 23 | + // .{ .name = "squarewave", .file = "src/squarewave.zig" }, |
| 24 | + // .{ .name = "ws2812", .file = "src/ws2812.zig" }, |
| 25 | + .{ .name = "blinky", .file = "src/blinky.zig" }, |
| 26 | + // .{ .name = "gpio-clock-output", .file = "src/gpio_clock_output.zig" }, |
| 27 | + // .{ .name = "changing-system-clocks", .file = "src/changing_system_clocks.zig" }, |
| 28 | + // .{ .name = "custom-clock-config", .file = "src/custom_clock_config.zig" }, |
| 29 | + // .{ .name = "watchdog-timer", .file = "src/watchdog_timer.zig" }, |
| 30 | + // .{ .name = "interrupts", .file = "src/interrupts.zig" }, |
| 31 | + // .{ .name = "stepper_driver", .file = "src/stepper_driver.zig" }, |
| 32 | + // .{ .name = "stepper_driver_dumb", .file = "src/stepper_driver_dumb.zig" }, |
| 33 | + // .{ .name = "usb-cdc", .file = "src/usb_cdc.zig" }, |
| 34 | + // .{ .name = "dma", .file = "src/dma.zig" }, |
| 35 | + }; |
| 36 | + |
| 37 | + for (examples) |example| { |
| 38 | + // If we specify example, only select the ones that match |
| 39 | + if (maybe_example) |selected_example| |
| 40 | + if (!std.mem.containsAtLeast(u8, example.name, 1, selected_example)) |
| 41 | + continue; |
| 42 | + |
| 43 | + // `add_firmware` basically works like addExecutable, but takes a |
| 44 | + // `microzig.Target` for target instead of a `std.zig.CrossTarget`. |
| 45 | + // |
| 46 | + // The target will convey all necessary information on the chip, |
| 47 | + // cpu and potentially the board as well. |
| 48 | + const firmware = mb.add_firmware(.{ |
| 49 | + .name = example.name, |
| 50 | + .target = mb.ports.hc32l110.chips.hc32l110x4, |
| 51 | + .optimize = optimize, |
| 52 | + .root_source_file = b.path(example.file), |
| 53 | + }); |
| 54 | + // firmware.artifact.want_lto = true; |
| 55 | + |
| 56 | + // `install_firmware()` is the MicroZig pendant to `Build.installArtifact()` |
| 57 | + // and allows installing the firmware as a typical firmware file. |
| 58 | + // |
| 59 | + // This will also install into `$prefix/firmware` instead of `$prefix/bin`. |
| 60 | + mb.install_firmware(firmware, .{}); |
| 61 | + |
| 62 | + // For debugging, we also always install the firmware as an ELF file |
| 63 | + mb.install_firmware(firmware, .{ .format = .elf }); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +const Example = struct { |
| 68 | + name: []const u8, |
| 69 | + file: []const u8, |
| 70 | +}; |
0 commit comments