|
| 1 | +const std = @import("std"); |
| 2 | +const Dependency = std.Build.Dependency; |
| 3 | + |
| 4 | +pub const Format = @import("src/dfu.zig").Format; |
| 5 | +pub const Options = @import("src/dfu.zig").Options; |
| 6 | + |
| 7 | +/// Create a DFU file from an ELF file. |
| 8 | +/// For DfuSe format, converts ELF directly. |
| 9 | +/// For standard DFU format, uses objcopy to extract binary first. |
| 10 | +pub fn from_elf(dep: *Dependency, elf_file: std.Build.LazyPath, opts: Options) std.Build.LazyPath { |
| 11 | + const b = dep.builder; |
| 12 | + |
| 13 | + return switch (opts.format) { |
| 14 | + .dfuse => blk: { |
| 15 | + const elf2dfuse = dep.artifact("elf2dfuse"); |
| 16 | + const run = b.addRunArtifact(elf2dfuse); |
| 17 | + |
| 18 | + run.addArgs(&.{ "--vendor-id", b.fmt("0x{x:0>4}", .{opts.vendor_id}) }); |
| 19 | + run.addArgs(&.{ "--product-id", b.fmt("0x{x:0>4}", .{opts.product_id}) }); |
| 20 | + run.addArgs(&.{ "--device-id", b.fmt("0x{x:0>4}", .{opts.device_id}) }); |
| 21 | + run.addArg("--elf-path"); |
| 22 | + run.addFileArg(elf_file); |
| 23 | + run.addArg("--output-path"); |
| 24 | + break :blk run.addOutputFileArg("output.dfu"); |
| 25 | + }, |
| 26 | + .standard => blk: { |
| 27 | + // Use objcopy to extract binary from ELF |
| 28 | + const objcopy = b.addObjCopy(elf_file, .{ |
| 29 | + .format = .bin, |
| 30 | + }); |
| 31 | + const bin_file = objcopy.getOutput(); |
| 32 | + |
| 33 | + // Then convert binary to DFU |
| 34 | + break :blk from_bin(dep, bin_file, opts); |
| 35 | + }, |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +/// Create a standard DFU file from a raw binary file. |
| 40 | +pub fn from_bin(dep: *Dependency, bin_file: std.Build.LazyPath, opts: Options) std.Build.LazyPath { |
| 41 | + const b = dep.builder; |
| 42 | + const bin2dfu = dep.artifact("bin2dfu"); |
| 43 | + const run = b.addRunArtifact(bin2dfu); |
| 44 | + |
| 45 | + run.addArgs(&.{ "--vendor-id", b.fmt("0x{x:0>4}", .{opts.vendor_id}) }); |
| 46 | + run.addArgs(&.{ "--product-id", b.fmt("0x{x:0>4}", .{opts.product_id}) }); |
| 47 | + run.addArgs(&.{ "--device-id", b.fmt("0x{x:0>4}", .{opts.device_id}) }); |
| 48 | + run.addArg("--bin-path"); |
| 49 | + run.addFileArg(bin_file); |
| 50 | + run.addArg("--output-path"); |
| 51 | + return run.addOutputFileArg("output.dfu"); |
| 52 | +} |
| 53 | + |
| 54 | +pub fn build(b: *std.Build) void { |
| 55 | + const optimize = b.standardOptimizeOption(.{}); |
| 56 | + const target = b.standardTargetOptions(.{}); |
| 57 | + |
| 58 | + const dfu_mod = b.addModule("dfu", .{ |
| 59 | + .root_source_file = b.path("src/dfu.zig"), |
| 60 | + }); |
| 61 | + |
| 62 | + const dfuse_mod = b.createModule(.{ |
| 63 | + .root_source_file = b.path("src/dfuse.zig"), |
| 64 | + .imports = &.{ |
| 65 | + .{ .name = "dfu", .module = dfu_mod }, |
| 66 | + }, |
| 67 | + }); |
| 68 | + |
| 69 | + const elf2dfuse = b.addExecutable(.{ |
| 70 | + .name = "elf2dfuse", |
| 71 | + .root_module = b.createModule(.{ |
| 72 | + .root_source_file = b.path("src/elf2dfuse.zig"), |
| 73 | + .target = target, |
| 74 | + .optimize = optimize, |
| 75 | + .imports = &.{ |
| 76 | + .{ .name = "dfuse", .module = dfuse_mod }, |
| 77 | + }, |
| 78 | + }), |
| 79 | + }); |
| 80 | + b.installArtifact(elf2dfuse); |
| 81 | + |
| 82 | + const bin2dfu = b.addExecutable(.{ |
| 83 | + .name = "bin2dfu", |
| 84 | + .root_module = b.createModule(.{ |
| 85 | + .root_source_file = b.path("src/bin2dfu.zig"), |
| 86 | + .target = target, |
| 87 | + .optimize = optimize, |
| 88 | + .imports = &.{ |
| 89 | + .{ .name = "dfu", .module = dfu_mod }, |
| 90 | + }, |
| 91 | + }), |
| 92 | + }); |
| 93 | + b.installArtifact(bin2dfu); |
| 94 | + |
| 95 | + const main_tests = b.addTest(.{ |
| 96 | + .root_module = b.createModule(.{ |
| 97 | + .root_source_file = b.path("src/dfu.zig"), |
| 98 | + .target = target, |
| 99 | + }), |
| 100 | + }); |
| 101 | + const run_tests = b.addRunArtifact(main_tests); |
| 102 | + const test_step = b.step("test", "Run tests"); |
| 103 | + test_step.dependOn(&run_tests.step); |
| 104 | +} |
0 commit comments