-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
libs.zig
215 lines (183 loc) · 6.3 KB
/
libs.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
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
const std = @import("std");
const ArrayList = std.ArrayList;
const LazyPath = std.build.LazyPath;
pub fn addAsPackage(exe: *std.Build.CompileStep) void {
addAsPackageWithCustomName(exe, "zigcv");
}
pub fn addAsPackageWithCustomName(exe: *std.Build.CompileStep, name: []const u8) void {
const owner = exe.step.owner;
var module = std.build.createModule(owner, .{
.source_file = std.Build.FileSource.relative("src/main.zig"),
.dependencies = &.{},
});
exe.addModule(name, module);
}
pub fn link(b: *std.build.Builder, exe: *std.Build.CompileStep) void {
ensureSubmodules(exe);
const target = exe.target;
const mode = exe.optimize;
const builder = exe.step.owner;
const go_src_files = .{
"asyncarray.cpp",
"calib3d.cpp",
"core.cpp",
"dnn.cpp",
"features2d.cpp",
"highgui.cpp",
"imgcodecs.cpp",
"imgproc.cpp",
"objdetect.cpp",
"photo.cpp",
"svd.cpp",
"version.cpp",
"video.cpp",
"videoio.cpp",
};
const cv = builder.addStaticLibrary(std.Build.StaticLibraryOptions{
.name = "opencv",
.target = target,
.optimize = mode,
});
inline for (go_src_files) |file| {
const go_src_dir_path = go_src_dir.getPath(b);
const c_file_path = b.pathJoin(&.{ go_src_dir_path, file });
cv.addCSourceFile(.{
.file = .{ .path = c_file_path },
.flags = c_build_options,
});
}
linkToOpenCV(cv);
exe.linkLibrary(cv);
linkToOpenCV(exe);
}
fn linkToOpenCV(exe: *std.build.CompileStep) void {
const target_os = exe.target.toTarget().os.tag;
exe.addIncludePath(go_src_dir);
exe.addIncludePath(zig_src_dir);
switch (target_os) {
.windows => {
exe.addIncludePath(.{ .path = "c:/msys64/mingw64/include" });
exe.addIncludePath(.{ .path = "c:/msys64/mingw64/include/c++/12.2.0" });
exe.addIncludePath(.{ .path = "c:/msys64/mingw64/include/c++/12.2.0/x86_64-w64-mingw32" });
exe.addLibraryPath(.{ .path = "c:/msys64/mingw64/lib" });
exe.addIncludePath(.{ .path = "c:/opencv/build/install/include" });
exe.addLibraryPath(.{ .path = "c:/opencv/build/install/x64/mingw/staticlib" });
exe.linkSystemLibrary("opencv4");
exe.linkSystemLibrary("stdc++.dll");
exe.linkSystemLibrary("unwind");
exe.linkSystemLibrary("m");
exe.linkSystemLibrary("c");
},
else => {
exe.linkLibCpp();
exe.linkSystemLibrary("opencv4");
exe.linkSystemLibrary("unwind");
exe.linkSystemLibrary("m");
exe.linkSystemLibrary("c");
},
}
}
pub const contrib = struct {
pub fn addAsPackage(exe: *std.build.LibExeObjStep) void {
@This().addAsPackageWithCutsomName(exe, "zigcv_contrib");
}
pub fn addAsPackageWithCutsomName(exe: *std.build.LibExeObjStep, name: []const u8) void {
exe.addPackagePath(name, .{ .path = "src/contrib/main.zig" });
}
pub fn link(b: *std.build.Builder, exe: *std.build.LibExeObjStep) void {
ensureSubmodules(exe);
const target = exe.target;
const optimize = exe.optimize;
const builder = exe.step.owner;
const contrib_dir = b.pathJoin(.{ go_src_dir.getPath(b), "contrib/" });
const contrib_files = .{
"aruco.cpp",
"bgsegm.cpp",
"face.cpp",
"img_hash.cpp",
"tracking.cpp",
"wechat_qrcode.cpp",
"xfeatures2d.cpp",
"ximgproc.cpp",
"xphoto.cpp",
};
const cv_contrib = builder.addStaticLibrary(.{
.name = "opencv_contrib",
.target = target,
.optimize = optimize,
});
cv_contrib.force_pic = true;
for (contrib_files) |file| {
const c_path = b.pathJoin(&.{ contrib_dir, file });
cv_contrib.addCSourceFile(.{
.file = .{ .path = c_path },
.flags = c_build_options,
});
}
cv_contrib.addIncludePath(.{ .path = contrib_dir });
linkToOpenCV(cv_contrib);
exe.linkLibrary(cv_contrib);
linkToOpenCV(exe);
}
};
pub const cuda = struct {
pub fn addAsPackage(exe: *std.build.LibExeObjStep) void {
@This().addAsPackageWithCutsomName(exe, "zigcv_cuda");
}
pub fn addAsPackageWithCutsomName(exe: *std.build.LibExeObjStep, name: []const u8) void {
exe.addPackagePath(name, .{ .path = "src/cuda/main.zig" });
}
pub fn link(b: *std.build.Builder, exe: *std.build.LibExeObjStep) void {
ensureSubmodules(exe);
const target = exe.target;
const mode = exe.build_mode;
const builder = exe.step.owner;
const cuda_dir = b.pathJoin(&.{ go_src_dir.getPath(builder), "cuda/" });
const cuda_files = .{
"arithm.cpp",
"bgsegm.cpp",
"core.cpp",
"cuda.cpp",
"filters.cpp",
"imgproc.cpp",
"objdetect.cpp",
"optflow.cpp",
"warping.cpp",
};
const cv_cuda = builder.addStaticLibrary("opencv_cuda");
cv_cuda.setTarget(target);
cv_cuda.setBuildMode(mode);
cv_cuda.force_pic = true;
for (cuda_files) |file| {
const c_path = b.pathJoin(&.{ cuda_dir, file });
cv_cuda.addCSourceFile(.{
.file = .{ .path = c_path },
.flags = c_build_options,
});
}
cv_cuda.addIncludePath(go_src_dir);
linkToOpenCV(cv_cuda);
exe.linkLibrary(cv_cuda);
linkToOpenCV(exe);
}
};
var ensure_submodule: bool = false;
fn ensureSubmodules(exe: *std.build.LibExeObjStep) void {
const b = exe.step.owner;
if (!ensure_submodule) {
exe.step.dependOn(&b.addSystemCommand(&.{ "git", "submodule", "update", "--init", "--recursive" }).step);
ensure_submodule = true;
}
}
const go_src_dir = (LazyPath{ .path = "libs/gocv/" });
const zig_src_dir = (LazyPath{ .path = "src/" });
const Program = struct {
name: []const u8,
path: []const u8,
desc: []const u8,
};
const c_build_options: []const []const u8 = &.{
"-Wall",
"-Wextra",
"--std=c++11",
};