Repository navigation
Expand file tree
/
Copy pathbuild.zig
More file actions
283 lines (247 loc) · 11.2 KB
/
Copy pathbuild.zig
File metadata and controls
283 lines (247 loc) · 11.2 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// Build option: enable opencv_contrib modules
const enable_contrib = b.option(bool, "contrib", "Enable opencv_contrib modules (default: false)") orelse false;
// Build options module (passed to Zig source as @import("config"))
const config = b.addOptions();
config.addOption(bool, "enable_contrib", enable_contrib);
// ── Export for downstream consumers ──────────────────────────
// Build the C wrapper library (also used internally below)
const c_wrapper = buildCWrapper(b, target, optimize, enable_contrib);
// Export the Zig module — downstream users just call:
// exe.root_module.addImport("zopencv", dep.module("zopencv"));
// System library linking propagates transitively via the C wrapper.
const zopencv_mod = b.addModule("zopencv", .{
.root_source_file = b.path("src/main.zig"),
});
zopencv_mod.addOptions("config", config);
zopencv_mod.addIncludePath(b.path("c_api"));
zopencv_mod.linkLibrary(c_wrapper);
// ── Export for downstream consumers ──────────────────────────
// Create examples
const examples = [_]struct {
name: []const u8,
path: []const u8,
desc: []const u8,
}{
.{ .name = "hello_opencv", .path = "examples/hello_opencv.zig", .desc = "Print OpenCV version" },
.{ .name = "image_io", .path = "examples/image_io.zig", .desc = "Read and write images" },
.{ .name = "video_capture", .path = "examples/video_capture.zig", .desc = "Capture and process video file" },
.{ .name = "face_detection", .path = "examples/face_detection.zig", .desc = "Detect faces using Haar cascades" },
.{ .name = "feature_matching", .path = "examples/feature_matching.zig", .desc = "Feature detection and matching" },
.{ .name = "image_processing_pipeline", .path = "examples/image_processing_pipeline.zig", .desc = "Complete image processing pipeline" },
.{ .name = "realtime_features", .path = "examples/realtime_features.zig", .desc = "Real-time feature detection" },
.{ .name = "background_subtraction", .path = "examples/background_subtraction.zig", .desc = "Background subtraction demo" },
.{ .name = "photo_demo", .path = "examples/photo_demo.zig", .desc = "Photo module demo" },
.{ .name = "edge_detection", .path = "examples/edge_detection.zig", .desc = "Canny, Sobel, and Laplacian edge detection" },
.{ .name = "drawing", .path = "examples/drawing.zig", .desc = "Drawing primitives demo" },
.{ .name = "color_spaces", .path = "examples/color_spaces.zig", .desc = "Color space conversions (HSV, Lab, Gray)" },
.{ .name = "morphology", .path = "examples/morphology.zig", .desc = "Morphological operations demo" },
.{ .name = "threshold", .path = "examples/threshold.zig", .desc = "Thresholding comparison (binary, Otsu, adaptive)" },
.{ .name = "contours", .path = "examples/contours.zig", .desc = "Contour detection and analysis" },
.{ .name = "histogram", .path = "examples/histogram.zig", .desc = "Histogram equalization and visualization" },
.{ .name = "template_matching", .path = "examples/template_matching.zig", .desc = "Template matching demo" },
.{ .name = "aruco_demo", .path = "examples/aruco_demo.zig", .desc = "ArUco marker generation and detection" },
.{ .name = "img_hash_demo", .path = "examples/img_hash_demo.zig", .desc = "Perceptual image hashing" },
.{ .name = "camera_capture", .path = "examples/camera_capture.zig", .desc = "Live camera with edge detection" },
.{ .name = "video_writer", .path = "examples/video_writer.zig", .desc = "Read, process, and write video" },
.{ .name = "optical_flow", .path = "examples/optical_flow.zig", .desc = "Dense optical flow (Farneback)" },
};
const examples_step = b.step("examples", "Build all examples");
for (examples) |ex| {
const exe = b.addExecutable(.{
.name = ex.name,
.root_module = b.createModule(.{
.root_source_file = b.path(ex.path),
.target = target,
.optimize = optimize,
}),
});
b.installArtifact(exe);
// ZigCV pattern: link FIRST, then addAsPackage
linkzOpenCV(b, exe, enable_contrib);
addzOpenCVPackage(exe, config);
const install_exe = b.addInstallArtifact(exe, .{});
const example_step = b.step(
b.fmt("example-{s}", .{ex.name}),
ex.desc,
);
example_step.dependOn(&install_exe.step);
examples_step.dependOn(&install_exe.step);
const run_exe = b.addRunArtifact(exe);
if (b.args) |args| {
run_exe.addArgs(args);
}
const run_step = b.step(
b.fmt("run-{s}", .{ex.name}),
b.fmt("Run {s} example", .{ex.name}),
);
run_step.dependOn(&run_exe.step);
}
// Create unit tests (all tests combined)
const unit_tests = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path("test/test_runner.zig"),
.target = target,
.optimize = optimize,
}),
});
linkzOpenCV(b, unit_tests, enable_contrib);
addzOpenCVPackage(unit_tests, config);
const run_tests = b.addRunArtifact(unit_tests);
const test_step = b.step("test", "Run all tests (unit + integration, add -Dcontrib=true for contrib)");
test_step.dependOn(&run_tests.step);
// Unit tests only (fast, no I/O)
const unit_only = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path("test/unit_runner.zig"),
.target = target,
.optimize = optimize,
}),
});
linkzOpenCV(b, unit_only, enable_contrib);
addzOpenCVPackage(unit_only, config);
const run_unit = b.addRunArtifact(unit_only);
const unit_step = b.step("test-unit", "Run unit tests only (fast, no I/O)");
unit_step.dependOn(&run_unit.step);
// Integration tests only (uses real images/video)
const integration_only = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path("test/integration_runner.zig"),
.target = target,
.optimize = optimize,
}),
});
linkzOpenCV(b, integration_only, enable_contrib);
addzOpenCVPackage(integration_only, config);
const run_integration = b.addRunArtifact(integration_only);
const integration_step = b.step("test-integration", "Run integration tests (uses real images/video)");
integration_step.dependOn(&run_integration.step);
// Contrib tests only (always builds with contrib enabled)
const contrib_config = b.addOptions();
contrib_config.addOption(bool, "enable_contrib", true);
const contrib_only = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path("test/contrib_runner.zig"),
.target = target,
.optimize = optimize,
}),
});
linkzOpenCV(b, contrib_only, true);
addzOpenCVPackage(contrib_only, contrib_config);
const run_contrib = b.addRunArtifact(contrib_only);
const contrib_step = b.step("test-contrib", "Run contrib tests only");
contrib_step.dependOn(&run_contrib.step);
}
// ZigCV pattern: separate function to add package
fn addzOpenCVPackage(exe: *std.Build.Step.Compile, config: *std.Build.Step.Options) void {
const owner = exe.step.owner;
const module = owner.createModule(.{
.root_source_file = owner.path("src/main.zig"),
// NO target, NO optimize!
});
// Add include paths to the module
module.addIncludePath(owner.path("c_api"));
// Pass config options to the zopencv module
module.addOptions("config", config);
exe.root_module.addImport("zopencv", module);
}
// ZigCV pattern: separate function to link
fn linkzOpenCV(b: *std.Build, exe: *std.Build.Step.Compile, enable_contrib: bool) void {
const target = exe.root_module.resolved_target.?;
const optimize = exe.root_module.optimize.?;
// Build C wrapper as static library
const c_wrapper = buildCWrapper(b, target, optimize, enable_contrib);
// Add include paths to exe FIRST (before module import)
exe.root_module.addIncludePath(b.path("c_api"));
exe.root_module.linkLibrary(c_wrapper);
linkOpenCV(exe.root_module);
}
fn buildCWrapper(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, enable_contrib: bool) *std.Build.Step.Compile {
const lib = b.addLibrary(.{
.name = "opencv_wrapper",
.linkage = .static,
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
}),
});
// Core OpenCV C++ sources (always compiled)
const core_sources = [_][]const u8{
"c_api/core.cpp",
"c_api/imgproc.cpp",
"c_api/imgcodecs.cpp",
"c_api/videoio.cpp",
"c_api/highgui.cpp",
"c_api/features2d.cpp",
"c_api/objdetect.cpp",
"c_api/photo.cpp",
"c_api/video.cpp",
"c_api/calib3d.cpp",
"c_api/dnn.cpp",
"c_api/ml.cpp",
"c_api/stitching.cpp",
"c_api/flann.cpp",
};
// Contrib C++ sources (only compiled with -Dcontrib=true)
const contrib_sources = [_][]const u8{
"c_api/contrib/tracking.cpp",
"c_api/contrib/aruco.cpp",
"c_api/contrib/face.cpp",
"c_api/contrib/text.cpp",
"c_api/contrib/xfeatures2d.cpp",
"c_api/contrib/ximgproc.cpp",
"c_api/contrib/xphoto.cpp",
"c_api/contrib/img_hash.cpp",
"c_api/contrib/bgsegm.cpp",
"c_api/contrib/dnn_superres.cpp",
"c_api/contrib/bioinspired.cpp",
"c_api/contrib/optflow.cpp",
"c_api/contrib/saliency.cpp",
};
const cxx_flags = [_][]const u8{
"-std=c++17",
"-Wall",
"-Wextra",
"-Wno-unused-parameter",
};
for (core_sources) |src| {
lib.root_module.addCSourceFile(.{
.file = b.path(src),
.flags = &cxx_flags,
});
}
if (enable_contrib) {
for (contrib_sources) |src| {
lib.root_module.addCSourceFile(.{
.file = b.path(src),
.flags = &cxx_flags,
});
}
}
lib.root_module.addIncludePath(b.path("c_api"));
lib.root_module.link_libcpp = true;
linkOpenCV(lib.root_module);
return lib;
}
pub fn linkOpenCV(module: *std.Build.Module) void {
module.linkSystemLibrary("opencv4", .{});
const target_os = module.resolved_target.?.result.os.tag;
switch (target_os) {
.linux => {
module.linkSystemLibrary("stdc++", .{});
module.linkSystemLibrary("m", .{});
module.linkSystemLibrary("pthread", .{});
},
.macos => {
module.linkSystemLibrary("c++", .{});
module.linkFramework("Accelerate", .{});
},
.windows => {
module.linkSystemLibrary("stdc++", .{});
},
else => {},
}
}