-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.zig
78 lines (60 loc) · 1.99 KB
/
build.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const std = @import("std");
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "zmodb",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
exe.linkLibC();
const libmodbus = try buildLibmodbus("libmodbus", b, target, optimize);
const modbusModule = b.addModule("modbus", .{
.root_source_file = b.path("src/modbus/modbus.zig"),
.target = target,
.optimize = optimize,
});
modbusModule.linkLibrary(libmodbus);
exe.root_module.addImport("modbus", modbusModule);
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const runStep = b.step("run", "Run the app");
runStep.dependOn(&run_cmd.step);
}
pub fn buildLibmodbus(
comptime subdir: []const u8,
b: *std.Build,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
) !*std.Build.Step.Compile {
const lib = b.addStaticLibrary(.{
.name = "modbus",
.target = target,
.optimize = optimize,
});
var flags = std.ArrayList([]const u8).init(std.heap.page_allocator);
if (optimize != .Debug) try flags.append("-Os");
try flags.append("-Wno-return-type-c-linkage");
try flags.append("-fno-sanitize=undefined");
try flags.append("-std=gnu23");
try flags.append("-D_GNU_SOURCE");
lib.addIncludePath(b.path(subdir ++ "/src"));
lib.addIncludePath(b.path("src/modbus/"));
lib.addCSourceFiles(.{
.files = &.{
subdir ++ "/src/modbus-data.c",
subdir ++ "/src/modbus-rtu.c",
subdir ++ "/src/modbus-tcp.c",
subdir ++ "/src/modbus.c",
},
.flags = flags.items,
});
lib.linkLibC();
b.installArtifact(lib);
return lib;
}