-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuild.zig
318 lines (268 loc) · 12.6 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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
const std = @import("std");
const fs = std.fs;
const ztBuild = @import("./ZT/build.zig");
const Builder = std.build.Builder;
pub const filedlgPkg = std.build.Pkg{ .name = "filedialog", .path = std.build.FileSource{ .path = getRelativePath() ++ "src/pkg/filedialog.zig" }, .dependencies = &[_]std.build.Pkg{ztBuild.imguiPkg} };
pub const myMiniZipPkg = std.build.Pkg{ .name = "myminizip", .path = std.build.FileSource{ .path = getRelativePath() ++ "src/pkg/myminizip.zig" }, .dependencies = &[_]std.build.Pkg{} };
fn getRelativePath() []const u8 {
comptime var src: std.builtin.SourceLocation = @src();
return std.fs.path.dirname(src.file).? ++ std.fs.path.sep_str;
}
pub fn build(b: *Builder) void {
const target = b.standardTargetOptions(.{});
b.cache_root = "zig-cache";
// declare optional other zig executables here
// format is: executable_name, path_to_zig_file
const examples = [_][2][]const u8{
[_][]const u8{ "slides", "src/main.zig" },
};
for (examples) |example, i| {
createExe(b, target, example[0], example[1]) catch unreachable;
// first element in the list is added as "run" so "zig build run" works
if (i == 0) createExe(b, target, "run", example[1]) catch unreachable;
}
}
/// creates an exe with all the required dependencies
fn createExe(b: *Builder, target: std.zig.CrossTarget, name: []const u8, source: []const u8) !void {
var exe = b.addExecutable(name, source);
comptime var path = getRelativePath();
exe.setBuildMode(b.standardReleaseOptions());
exe.setOutputDir(std.fs.path.join(b.allocator, &[_][]const u8{ b.cache_root, "bin" }) catch unreachable);
exe.setTarget(target);
// libfiledialog
exe.linkLibrary(filedialogLibrary(exe));
exe.addPackage(filedlgPkg);
// zlib - for libpng
exe.linkLibrary(addZlib(exe));
// libpng
// add include dir to be able to include the libPng headers in the package's zig file:
exe.addIncludeDir(path ++ "./src/dep/libpng-1.6.37");
const libPng = try addLibPng(exe);
exe.linkLibrary(libPng);
// my minizip
const libMyMiniZip = try addLibMyMiniZip(exe);
exe.linkLibrary(libMyMiniZip);
exe.addPackage(myMiniZipPkg);
ztBuild.link(exe);
const run_cmd = exe.run();
const exe_step = b.step(name, b.fmt("run {s}.zig", .{name}));
exe_step.dependOn(&run_cmd.step);
b.default_step.dependOn(&exe.step);
b.installArtifact(exe);
addBinaryContent("assets") catch unreachable;
}
// Filedialog
pub fn filedialogLibrary(exe: *std.build.LibExeObjStep) *std.build.LibExeObjStep {
comptime var path = getRelativePath();
var b = exe.builder;
var target = exe.target;
var filedialog = b.addStaticLibrary("filedialog", null);
filedialog.linkLibC();
filedialog.linkSystemLibrary("c++");
// Generate flags.
var flagContainer = std.ArrayList([]const u8).init(std.heap.page_allocator);
if (b.is_release) flagContainer.append("-Os") catch unreachable;
flagContainer.append("-Wno-return-type-c-linkage") catch unreachable;
flagContainer.append("-fno-sanitize=undefined") catch unreachable;
// Link libraries.
if (target.isWindows()) {
filedialog.linkSystemLibrary("winmm");
filedialog.linkSystemLibrary("user32");
filedialog.linkSystemLibrary("imm32");
filedialog.linkSystemLibrary("gdi32");
}
if (target.isDarwin()) {
// !! Mac TODO
// Here we need to add the include the system libs needed for mac filedialog
// probably none
}
// Include dirs.
filedialog.addIncludeDir(path ++ "src/dep/filedialog");
if (target.isWindows()) {
filedialog.addIncludeDir(path ++ "src/dep/filedialog/dirent");
}
filedialog.addIncludeDir(path ++ "src/dep/filedialog/stb");
filedialog.addIncludeDir(path ++ "ZT/src/dep/cimgui/imgui");
// Add C
filedialog.addCSourceFiles(&.{
path ++ "src/dep/filedialog/ImGuiFileDialog.cpp",
}, flagContainer.items);
return filedialog;
}
pub fn addZlib(exe: *std.build.LibExeObjStep) *std.build.LibExeObjStep {
comptime var path = getRelativePath();
var b = exe.builder;
var target = exe.target;
var libz = b.addStaticLibrary("z", null);
libz.linkLibC();
// Generate flags.
var flagContainer = std.ArrayList([]const u8).init(std.heap.page_allocator);
if (b.is_release) flagContainer.append("-Os") catch unreachable;
flagContainer.append("-Wno-return-type-c-linkage") catch unreachable;
flagContainer.append("-fno-sanitize=undefined") catch unreachable;
if (target.isDarwin()) {
// !! Mac TODO
// probably nothing
}
// Include dirs.
libz.addIncludeDir(path ++ "src/dep/zlib-1.2.12");
// Add C
libz.addCSourceFiles(&.{
path ++ "src/dep/zlib-1.2.12/adler32.c",
path ++ "src/dep/zlib-1.2.12/crc32.c",
path ++ "src/dep/zlib-1.2.12/deflate.c",
path ++ "src/dep/zlib-1.2.12/infback.c",
path ++ "src/dep/zlib-1.2.12/inffast.c",
path ++ "src/dep/zlib-1.2.12/inflate.c",
path ++ "src/dep/zlib-1.2.12/inftrees.c",
path ++ "src/dep/zlib-1.2.12/trees.c",
path ++ "src/dep/zlib-1.2.12/zutil.c",
}, flagContainer.items);
return libz;
}
pub fn addLibPng(exe: *std.build.LibExeObjStep) !*std.build.LibExeObjStep {
comptime var path = getRelativePath();
var b = exe.builder;
var target = exe.target;
var libPng = b.addStaticLibrary("png", null);
libPng.linkLibC();
// Generate flags.
var flagContainer = std.ArrayList([]const u8).init(std.heap.page_allocator);
if (b.is_release) flagContainer.append("-Os") catch unreachable;
flagContainer.append("-Wno-return-type-c-linkage") catch unreachable;
flagContainer.append("-fno-sanitize=undefined") catch unreachable;
if (target.isDarwin()) {
// !! Mac TODO
// probably nothing
}
// Include dirs.
libPng.addIncludeDir(path ++ "src/dep/libpng-1.6.37");
libPng.addIncludeDir(path ++ "src/dep/zlib-1.2.12");
// generate pnglibconf.h from pnglibconf.h.prebuilt
try ensureCopied("src/dep/libpng-1.6.37/scripts", "src/dep/libpng-1.6.37", "pnglibconf.h.prebuilt", "pnglibconf.h");
// Add C
libPng.addCSourceFiles(&.{
path ++ "./src/dep/libpng-1.6.37/png.c",
path ++ "./src/dep/libpng-1.6.37/pngerror.c",
path ++ "./src/dep/libpng-1.6.37/pngget.c",
path ++ "./src/dep/libpng-1.6.37/pngmem.c",
path ++ "./src/dep/libpng-1.6.37/pngpread.c",
path ++ "./src/dep/libpng-1.6.37/pngread.c",
path ++ "./src/dep/libpng-1.6.37/pngrio.c",
path ++ "./src/dep/libpng-1.6.37/pngrtran.c",
path ++ "./src/dep/libpng-1.6.37/pngrutil.c",
path ++ "./src/dep/libpng-1.6.37/pngset.c",
path ++ "./src/dep/libpng-1.6.37/pngtrans.c",
path ++ "./src/dep/libpng-1.6.37/pngwio.c",
path ++ "./src/dep/libpng-1.6.37/pngwrite.c",
path ++ "./src/dep/libpng-1.6.37/pngwtran.c",
path ++ "./src/dep/libpng-1.6.37/pngwutil.c",
}, flagContainer.items);
return libPng;
}
pub fn addLibMyMiniZip(exe: *std.build.LibExeObjStep) !*std.build.LibExeObjStep {
comptime var path = getRelativePath();
var b = exe.builder;
var target = exe.target;
var libMyMiniZip = b.addStaticLibrary("myminizip", null);
libMyMiniZip.linkLibC();
// Generate flags.
var flagContainer = std.ArrayList([]const u8).init(std.heap.page_allocator);
if (b.is_release) flagContainer.append("-Os") catch unreachable;
flagContainer.append("-Wno-return-type-c-linkage") catch unreachable;
flagContainer.append("-fno-sanitize=undefined") catch unreachable;
if (target.isDarwin()) {
// !! Mac TODO
// probably nothing
}
// Include dirs.
libMyMiniZip.addIncludeDir(path ++ "src/dep/zlib-1.2.12");
libMyMiniZip.addIncludeDir(path ++ "src/dep/zlib-1.2.12/contrib/minizip");
// Add C
libMyMiniZip.addCSourceFiles(&.{
path ++ "./src/dep/zlib-1.2.12/contrib/minizip/zip.c",
path ++ "./src/dep/zlib-1.2.12/contrib/minizip/ioapi.c",
path ++ "./src/pkg/myminizip/myminizip.c",
}, flagContainer.items);
return libMyMiniZip;
}
// adaption from the ztBuild version: keep dirname of asset folder
pub fn addBinaryContent(comptime baseContentPath: []const u8) ztBuild.AddContentErrors!void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const zigBin: []const u8 = std.fs.path.join(gpa.allocator(), &[_][]const u8{ "zig-out", "bin" }) catch return error.FolderError;
defer gpa.allocator().free(zigBin);
fs.cwd().makePath(zigBin) catch return error.FolderError;
var sourceFolder: fs.Dir = fs.cwd().openDir(baseContentPath, .{ .iterate = true }) catch return error.FolderError;
defer sourceFolder.close();
var iterator: fs.Dir.Iterator = sourceFolder.iterate();
while (iterator.next() catch return error.FolderError) |target| {
var x: fs.Dir.Entry = target;
if (x.kind == .Directory) {
const source: []const u8 = std.fs.path.join(gpa.allocator(), &[_][]const u8{ baseContentPath, x.name }) catch return error.RecursionError;
const targetFolder: []const u8 = std.fs.path.join(gpa.allocator(), &[_][]const u8{ zigBin, baseContentPath, x.name }) catch return error.RecursionError;
defer gpa.allocator().free(source);
defer gpa.allocator().free(targetFolder);
try innerAddContent(gpa.allocator(), source, targetFolder);
}
if (x.kind == .File) {
const targetFolder: []const u8 = std.fs.path.join(gpa.allocator(), &[_][]const u8{ zigBin, baseContentPath }) catch return error.RecursionError;
try copy(baseContentPath, targetFolder, x.name);
}
}
}
fn innerAddContent(allocator: std.mem.Allocator, folder: []const u8, dest: []const u8) ztBuild.AddContentErrors!void {
var sourceFolder: fs.Dir = fs.cwd().openDir(folder, .{ .iterate = true }) catch return error.FolderError;
defer sourceFolder.close();
var iterator: fs.Dir.Iterator = sourceFolder.iterate();
while (iterator.next() catch return error.FolderError) |target| {
var x: fs.Dir.Entry = target;
if (x.kind == .Directory) {
const source: []const u8 = std.fs.path.join(allocator, &[_][]const u8{ folder, x.name }) catch return error.RecursionError;
const targetFolder: []const u8 = std.fs.path.join(allocator, &[_][]const u8{ dest, x.name }) catch return error.RecursionError;
defer allocator.free(source);
defer allocator.free(targetFolder);
try innerAddContent(allocator, source, targetFolder);
}
if (x.kind == .File) {
try copy(folder, dest, x.name);
}
}
}
fn copy(from: []const u8, to: []const u8, filename: []const u8) ztBuild.AddContentErrors!void {
fs.cwd().makePath(to) catch return error.FolderError;
var source = fs.cwd().openDir(from, .{}) catch return error.FileError;
var dest = fs.cwd().openDir(to, .{}) catch return error.FileError;
var sfile = source.openFile(filename, .{}) catch return error.FileError;
defer sfile.close();
var dfile = dest.openFile(filename, .{}) catch {
source.copyFile(filename, dest, filename, .{}) catch return error.PermissionError;
std.debug.print("COPY: {s}/{s} to {s}/{s}\n", .{ from, filename, to, filename });
return;
};
var sstat = sfile.stat() catch return error.FileError;
var dstat = dfile.stat() catch return error.FileError;
if (sstat.mtime > dstat.mtime) {
dfile.close();
dest.deleteFile(filename) catch return error.PermissionError;
source.copyFile(filename, dest, filename, .{}) catch return error.PermissionError;
std.debug.print("OVERWRITE: {s}/{s} to {s}/{s}\n", .{ from, filename, to, filename });
} else {
defer dfile.close();
std.debug.print("SKIP: {s}/{s}\n", .{ from, filename });
}
}
// this is just for a header file of libpng.
fn ensureCopied(from: []const u8, to: []const u8, filename: []const u8, destfilename: []const u8) !void {
fs.cwd().makePath(to) catch return error.FolderError;
var source = fs.cwd().openDir(from, .{}) catch return error.FileError;
var dest = fs.cwd().openDir(to, .{}) catch return error.FileError;
var sfile = source.openFile(filename, .{}) catch return error.FileError;
defer sfile.close();
// var dfile = dest.openFile(destfilename, .{}) catch {
_ = dest.openFile(destfilename, .{}) catch {
std.debug.print("TRYING: {s}/{s} to {s}/{s}\n", .{ from, filename, to, filename });
source.copyFile(filename, dest, destfilename, .{}) catch return error.PermissionError;
std.debug.print("COPY: {s}/{s} to {s}/{s}\n", .{ from, filename, to, filename });
return;
};
}