Here is the full solution to everything is anyone comes through here with similar needs:
build.zig
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// module for users of this library
const ecm_module = b.addModule("ecm", .{
.root_source_file = b.path("src/root.zig"),
});
// CLI tool
const cli_tool = b.addExecutable(.{
.name = "ecm-cli",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
cli_tool.root_module.addImport("ecm", ecm_module);
const flags = b.dependency("flags", .{
.target = target,
.optimize = optimize,
});
cli_tool.root_module.addImport("flags", flags.module("flags"));
b.installArtifact(cli_tool);
// CLI tool unit tests
const cli_tool_unit_tests = b.addTest(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
const run_cli_tool_unit_tests = b.addRunArtifact(cli_tool_unit_tests);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_cli_tool_unit_tests.step);
// example
const example_step = b.step("example", "Build example");
const example = b.addExecutable(.{
.name = "example",
.target = target,
.optimize = optimize,
.root_source_file = b.path("example/main.zig"),
});
example.root_module.addImport("ecm", ecm_module);
// using addInstallArtifact here so it only installs for the example step
const example_install = b.addInstallArtifact(example, .{});
example_step.dependOn(&example_install.step);
}
build.zig.zon
.{
.name = "ecm",
.version = "0.0.0",
.minimum_zig_version = "0.13.0",
.dependencies = .{
.flags = .{
.url = "https://github.com/n0s4/flags/archive/refs/tags/v0.7.0.tar.gz",
.hash = "12206c2e1a9d8c1597a2b98064085c1f6207b31dfa3ccfaff2e54726c39db41d27ea",
},
},
.paths = .{
"build.zig",
"build.zig.zon",
"src",
"LICENSE",
"example",
},
}
example/main.zig
const std = @import("std");
const nic = @import("ecm").nic;
const MainDevice = @import("ecm").MainDevice;
pub const std_options = .{
.log_level = .warn,
};
pub fn main() !void {
var port = try nic.Port.init("enx00e04c68191a");
defer port.deinit();
var main_device = MainDevice.init(
&port,
.{ .timeout_recv_us = 2000 },
);
try main_device.scan();
}
src/root.zig
pub const nic = @import("nic.zig");
pub const MainDevice = @import("maindevice.zig").MainDevice;
src/main.zig
const std = @import("std");
const flags = @import("flags");
const ecm = @import("ecm");
const nic = ecm.nic;
const MainDevice = ecm.MainDevice;
pub const std_options = .{
// Set the log level to info
.log_level = .warn,
};
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
var args = try std.process.argsWithAllocator(gpa.allocator());
defer args.deinit();
const parsed_args = flags.parse(&args, zecm, .{});
try std.json.stringify(........
and my repo: GitHub - kj4tmp/zecm: An EtherCAT MainDevice Written in Pure Zig