-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathbuild.zig
More file actions
235 lines (202 loc) · 8.58 KB
/
build.zig
File metadata and controls
235 lines (202 loc) · 8.58 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
const std = @import("std");
const manifest = @import("build.zig.zon");
const Imgz = @import("imgz");
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const dynamic = b.option(bool, "dynamic", "Link against libspng, libjpeg and libtiff dynamically") orelse false;
const native_target = b.resolveTargetQuery(.{});
const is_cross_compiling = target.result.cpu.arch != native_target.result.cpu.arch or
target.result.os.tag != native_target.result.os.tag;
const build_options = b.addOptions();
build_options.addOption([]const u8, "version", manifest.version);
const build_options_mod = build_options.createModule();
const lib_mod, const exe = buildOdiff(b, target, optimize, dynamic, build_options_mod);
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
const lib_unit_tests = b.addTest(.{
.root_module = lib_mod,
});
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
const integration_tests_with_io = [_][]const u8{
"src/test_core.zig",
"src/test_io_png.zig",
"src/test_io_bmp.zig",
"src/test_io_jpg.zig",
"src/test_io_tiff.zig",
"src/test_avx.zig",
"src/test_io_webp.zig",
};
const integration_tests_pure_zig = [_][]const u8{
"src/test_color_delta.zig",
"src/test_server.zig",
};
var integration_test_steps = std.array_list.Managed(*std.Build.Step.Run).init(b.allocator);
defer integration_test_steps.deinit();
if (!is_cross_compiling) {
const root_lib = b.addLibrary(.{
.name = "odiff_lib",
.root_module = lib_mod,
.linkage = if (dynamic) .dynamic else .static,
});
for (integration_tests_with_io) |test_path| {
const integration_test = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path(test_path),
.target = target,
.optimize = optimize,
}),
});
integration_test.root_module.addImport("build_options", build_options_mod);
integration_test.linkLibC();
integration_test.linkLibrary(root_lib);
linkDeps(b, target, optimize, dynamic, integration_test.root_module);
const run_integration_test = b.addRunArtifact(integration_test);
integration_test_steps.append(run_integration_test) catch @panic("OOM");
}
for (integration_tests_pure_zig) |test_path| {
const pure_test = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path(test_path),
.target = target,
.optimize = optimize,
}),
});
pure_test.root_module.addImport("build_options", build_options_mod);
pure_test.linkLibC();
pure_test.addCSourceFiles(.{
.files = &.{"src/rvv.c"},
});
const run_pure_test = b.addRunArtifact(pure_test);
integration_test_steps.append(run_pure_test) catch @panic("OOM");
}
}
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_lib_unit_tests.step);
const integration_test_step = b.step("test-integration", "Run integration tests with test images");
// Ensure the main executable is built before running integration tests
integration_test_step.dependOn(b.getInstallStep());
for (integration_test_steps.items) |test_run_step| {
integration_test_step.dependOn(&test_run_step.step);
}
const test_all_step = b.step("test-all", "Run both unit and integration tests");
test_all_step.dependOn(test_step);
test_all_step.dependOn(integration_test_step);
const build_ci_step = b.step("ci", "Build the app for CI");
for (build_targets) |target_query| {
const t = b.resolveTargetQuery(target_query);
_, const odiff_exe = buildOdiff(b, t, optimize, dynamic, build_options_mod);
odiff_exe.root_module.strip = true;
var target_name = try target_query.zigTriple(b.allocator);
if (target_query.cpu_arch == .riscv64 and !target_query.cpu_features_add.isEmpty())
target_name = try std.mem.join(b.allocator, "-", &[_][]const u8{ target_name, "rva23" });
const odiff_output = b.addInstallArtifact(odiff_exe, .{
.dest_dir = .{
.override = .{ .custom = target_name },
},
});
build_ci_step.dependOn(&odiff_output.step);
}
}
fn buildOdiff(
b: *std.Build,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
dynamic: bool,
build_options_mod: *std.Build.Module,
) struct { *std.Build.Module, *std.Build.Step.Compile } {
const lib_mod = b.createModule(.{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
linkDeps(b, target, optimize, dynamic, lib_mod);
var c_flags = std.array_list.Managed([]const u8).init(b.allocator);
defer c_flags.deinit();
c_flags.append("-std=c99") catch @panic("OOM");
c_flags.append("-Wno-nullability-completeness") catch @panic("OOM");
c_flags.append("-DHAVE_SPNG") catch @panic("OOM");
c_flags.append("-DSPNG_STATIC") catch @panic("OOM");
c_flags.append("-DSPNG_SSE=3") catch @panic("OOM");
c_flags.append("-DHAVE_JPEG") catch @panic("OOM");
c_flags.append("-DHAVE_TIFF") catch @panic("OOM");
c_flags.append("-DHAVE_WEBP") catch @panic("OOM");
lib_mod.addCSourceFiles(.{
.files = &.{
"src/rvv.c",
},
.flags = c_flags.items,
});
const exe_mod = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
exe_mod.addImport("odiff_lib", lib_mod);
exe_mod.addImport("build_options", build_options_mod);
lib_mod.addImport("build_options", build_options_mod);
if (target.result.cpu.arch == .x86_64) {
const os_tag = target.result.os.tag;
const fmt: ?[]const u8 = switch (os_tag) {
.linux => "elf64",
.macos => "macho64",
else => null,
};
if (fmt) |nasm_fmt| {
const nasm = b.addSystemCommand(&.{ "nasm", "-f", nasm_fmt, "-o" });
const asm_obj = nasm.addOutputFileArg("vxdiff.o");
nasm.addFileArg(b.path("src/vxdiff.asm"));
lib_mod.addObjectFile(asm_obj);
}
}
const exe = b.addExecutable(.{
.name = "odiff",
.root_module = exe_mod,
});
return .{ lib_mod, exe };
}
pub fn linkDeps(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, dynamic: bool, module: *std.Build.Module) void {
const host_target = b.graph.host.result;
const build_target = target.result;
const is_cross_compiling = host_target.cpu.arch != build_target.cpu.arch or
host_target.os.tag != build_target.os.tag;
if (dynamic and !is_cross_compiling) {
switch (build_target.os.tag) {
.windows => {
std.log.warn("Dynamic linking is not supported on Windows, falling back to static linking", .{});
return linkDeps(b, target, optimize, false, module);
},
else => {
module.linkSystemLibrary("spng", .{});
module.linkSystemLibrary("jpeg", .{});
module.linkSystemLibrary("tiff", .{});
},
}
} else {
Imgz.addToModule(b, module, .{
.target = target,
.optimize = optimize,
.jpeg_turbo = .{},
.spng = .{},
.tiff = .{},
.webp = .{},
}) catch @panic("Failed to link required dependencies, please create an issue on the repo :)");
}
}
const build_targets: []const std.Target.Query = &.{
.{ .cpu_arch = .aarch64, .os_tag = .macos },
.{ .cpu_arch = .aarch64, .os_tag = .linux },
.{ .cpu_arch = .aarch64, .os_tag = .windows },
.{ .cpu_arch = .x86_64, .os_tag = .macos },
.{ .cpu_arch = .x86_64, .os_tag = .linux },
.{ .cpu_arch = .x86_64, .os_tag = .windows },
.{ .cpu_arch = .riscv64, .os_tag = .linux },
.{ .cpu_arch = .riscv64, .os_tag = .linux, .cpu_features_add = std.Target.riscv.featureSet(&.{std.Target.riscv.Feature.rva23u64}) },
};