-
-
Notifications
You must be signed in to change notification settings - Fork 215
Expand file tree
/
Copy pathbuild.zig
More file actions
319 lines (272 loc) · 11.8 KB
/
build.zig
File metadata and controls
319 lines (272 loc) · 11.8 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
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
319
const std = @import("std");
fn libName(b: *std.Build, name: []const u8, target: std.Target) []const u8 {
return switch (target.os.tag) {
.windows => b.fmt("{s}.lib", .{name}),
else => b.fmt("lib{s}.a", .{name}),
};
}
fn linkLibraries(b: *std.Build, exe: *std.Build.Step.Compile, useLocalDeps: bool) void {
const target = exe.root_module.resolved_target.?;
const t = target.result;
const optimize = exe.root_module.optimize.?;
const depsLib = b.fmt("cubyz_deps_{s}-{s}-{s}", .{@tagName(t.cpu.arch), @tagName(t.os.tag), switch (t.os.tag) {
.linux => "musl",
.macos => "none",
.windows => "gnu",
else => "none",
}});
const artifactName = libName(b, depsLib, t);
var depsName: []const u8 = b.fmt("cubyz_deps_{s}_{s}", .{@tagName(t.cpu.arch), @tagName(t.os.tag)});
if (useLocalDeps) depsName = "local";
const libsDeps = b.lazyDependency(depsName, .{
.target = target,
.optimize = optimize,
}) orelse {
// Lazy dependencies with a `url` field will fail here the first time.
// build.zig will restart and try again.
std.log.info("Downloading cubyz_deps libraries {s}.", .{depsName});
return;
};
const headersDeps = if (useLocalDeps) libsDeps else b.lazyDependency("cubyz_deps_headers", .{}) orelse {
std.log.info("Downloading cubyz_deps headers {s}.", .{depsName});
return;
};
exe.root_module.addIncludePath(headersDeps.path("include"));
exe.root_module.addObjectFile(libsDeps.path("lib").path(b, artifactName));
const subPath = libsDeps.path("lib").path(b, depsLib);
exe.root_module.addObjectFile(subPath.path(b, libName(b, "glslang", t)));
exe.root_module.addObjectFile(subPath.path(b, libName(b, "MachineIndependent", t)));
exe.root_module.addObjectFile(subPath.path(b, libName(b, "GenericCodeGen", t)));
exe.root_module.addObjectFile(subPath.path(b, libName(b, "glslang-default-resource-limits", t)));
exe.root_module.addObjectFile(subPath.path(b, libName(b, "SPIRV", t)));
exe.root_module.addObjectFile(subPath.path(b, libName(b, "SPIRV-Tools", t)));
exe.root_module.addObjectFile(subPath.path(b, libName(b, "SPIRV-Tools-opt", t)));
if (t.os.tag == .macos) {
const moltenVkLibInstall = b.addInstallFile(subPath.path(b, "libMoltenVK.dylib"), "bin/Cubyz.app/Contents/Frameworks/libMoltenVK.dylib");
const moltenVkJsonInstall = b.addInstallFile(subPath.path(b, "MoltenVK_icd.json"), "bin/Cubyz.app/Contents/Resources/vulkan/icd.d/MoltenVK_icd.json");
exe.step.dependOn(&moltenVkLibInstall.step);
exe.step.dependOn(&moltenVkJsonInstall.step);
const validationLayerLibInstall = b.addInstallFile(subPath.path(b, "libVkLayer_khronos_validation.dylib"), "bin/Cubyz.app/Contents/Frameworks/libVkLayer_khronos_validation.dylib");
const validationLayerJsonInstall = b.addInstallFile(subPath.path(b, "VkLayer_khronos_validation.json"), "bin/Cubyz.app/Contents/Resources/vulkan/explicit_layer.d/VkLayer_khronos_validation.json");
exe.step.dependOn(&validationLayerLibInstall.step);
exe.step.dependOn(&validationLayerJsonInstall.step);
}
if (t.os.tag == .windows) {
exe.root_module.linkSystemLibrary("bcrypt", .{});
exe.root_module.linkSystemLibrary("crypt32", .{});
exe.root_module.linkSystemLibrary("gdi32", .{});
exe.root_module.linkSystemLibrary("opengl32", .{});
exe.root_module.linkSystemLibrary("ws2_32", .{});
} else if (t.os.tag == .macos) {
exe.root_module.linkFramework("Cocoa", .{});
exe.root_module.linkFramework("CoreFoundation", .{});
exe.root_module.linkFramework("IOKit", .{});
exe.root_module.linkFramework("QuartzCore", .{});
} else if (t.os.tag != .linux) {
std.log.err("Unsupported target: {}\n", .{t.os.tag});
}
}
pub fn makeModFeature(io: std.Io, step: *std.Build.Step, name: []const u8) !void {
var featureList: std.ArrayListUnmanaged(u8) = .empty;
defer featureList.deinit(step.owner.allocator);
var modDir = try std.Io.Dir.cwd().openDir(io, "mods", .{.iterate = true});
defer modDir.close(io);
var iterator = modDir.iterate();
while (try iterator.next(io)) |modEntry| {
if (modEntry.kind != .directory) continue;
var mod = try modDir.openDir(io, modEntry.name, .{});
defer mod.close(io);
var featureDir = mod.openDir(io, name, .{.iterate = true}) catch continue;
defer featureDir.close(io);
var featureIterator = featureDir.iterate();
while (try featureIterator.next(io)) |featureEntry| {
if (featureEntry.kind != .file) continue;
if (!std.mem.endsWith(u8, featureEntry.name, ".zig")) continue;
try featureList.appendSlice(step.owner.allocator, step.owner.fmt(
\\pub const @"{s}:{s}" = @import("{s}/{s}/{s}");
\\
,
.{
modEntry.name,
featureEntry.name[0 .. featureEntry.name.len - 4],
modEntry.name,
name,
featureEntry.name,
},
));
}
}
const file_path = step.owner.fmt("mods/{s}.zig", .{name});
try std.Io.Dir.cwd().writeFile(io, .{.data = featureList.items, .sub_path = file_path});
}
pub fn addModFeatureModule(b: *std.Build, exe: *std.Build.Step.Compile, name: []const u8) !void {
const module = b.createModule(.{
.root_source_file = b.path(b.fmt("mods/{s}.zig", .{name})),
.target = exe.root_module.resolved_target,
.optimize = exe.root_module.optimize,
});
module.addImport("main", exe.root_module);
exe.root_module.addImport(name, module);
}
fn addModFeatures(b: *std.Build, exe: *std.Build.Step.Compile) !void {
const step = try b.allocator.create(std.Build.Step);
step.* = std.Build.Step.init(.{
.id = .custom,
.name = "Create Mods",
.owner = b,
.makeFn = makeModFeaturesStep,
});
exe.step.dependOn(step);
try addModFeatureModule(b, exe, "rotation");
}
pub fn makeModFeaturesStep(step: *std.Build.Step, options: std.Build.Step.MakeOptions) anyerror!void {
var io = std.Io.Threaded.init(options.gpa, .{});
defer io.deinit();
try makeModFeature(io.io(), step, "rotation");
}
fn createLaunchConfig(b: *std.Build) !void {
var io = std.Io.Threaded.init(b.allocator, .{});
defer io.deinit();
std.Io.Dir.cwd().access(io.io(), "launchConfig.zon", .{}) catch {
const launchConfig =
\\.{
\\ .cubyzDir = "",
\\ .autoEnterWorld = "",
\\ .headlessServer = false,
\\ // .preferredAuthenticationAlgorithm = .ed25519, // Uncomment and change this if you own a server in an outdated game version where the default algorithm got compromised.
\\}
;
try std.Io.Dir.cwd().writeFile(io.io(), .{
.data = launchConfig,
.sub_path = "launchConfig.zon",
});
};
}
pub fn build(b: *std.Build) !void {
try createLaunchConfig(b);
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const optimize = b.standardOptimizeOption(.{});
const options = b.addOptions();
const isRelease = b.option(bool, "release", "Removes the -dev flag from the version") orelse false;
const version = b.fmt("0.3.0{s}", .{if (isRelease) "" else "-dev"});
if (b.option([]const u8, "version", "used by the CI to check if the git tag and game version match")) |tagVersion| {
const tagVersionUpperbound: usize = std.mem.indexOfScalar(u8, tagVersion, '-') orelse tagVersion.len;
const versionUpperbound: usize = std.mem.indexOfScalar(u8, version, '-') orelse version.len;
const tagParsed = try std.SemanticVersion.parse(tagVersion[0..tagVersionUpperbound]);
const versionParsed = try std.SemanticVersion.parse(version[0..versionUpperbound]);
if (std.SemanticVersion.order(tagParsed, versionParsed) != .eq) {
std.log.err("Provided version {s} does not match version in build.zig: {s}", .{tagVersion, version});
return error.VersionMismatch;
}
}
options.addOption([]const u8, "version", version);
options.addOption(bool, "isTaggedRelease", isRelease);
const useLocalDeps = b.option(bool, "local", "Use local cubyz_deps") orelse false;
const largeAssets = b.dependency("cubyz_large_assets", .{});
b.installDirectory(.{
.source_dir = largeAssets.path("music"),
.install_subdir = "assets/cubyz/music/",
.install_dir = .{.custom = ".."},
});
b.installDirectory(.{
.source_dir = largeAssets.path("fonts"),
.install_subdir = "assets/cubyz/fonts/",
.install_dir = .{.custom = ".."},
});
const mainModule = b.addModule("main", .{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
.link_libcpp = true,
});
const exe = b.addExecutable(.{
.name = "Cubyz",
.root_module = mainModule,
//.sanitize_thread = true,
});
exe.root_module.addOptions("build_options", options);
exe.root_module.addImport("main", mainModule);
try addModFeatures(b, exe);
if (isRelease and target.result.os.tag == .windows) {
exe.subsystem = .Windows;
}
linkLibraries(b, exe, useLocalDeps);
var exeInstallOptions: std.Build.Step.InstallArtifact.Options = .{};
if (target.result.os.tag == .macos) {
exeInstallOptions = .{
.dest_dir = .{.override = .{.custom = "bin/Cubyz.app/Contents/MacOS"}},
};
const plistContents =
\\<?xml version="1.0" encoding="UTF-8"?>
\\<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
\\<plist version="1.0">
\\<dict>
\\ <key>CFBundleIconFile</key>
\\ <string>logo</string>
\\</dict>
\\</plist>
;
const writeFiles = b.addWriteFiles();
const plistPath = writeFiles.add("Info.plist", plistContents);
const plistInstall = b.addInstallFile(plistPath, "bin/Cubyz.app/Contents/Info.plist");
b.getInstallStep().dependOn(&plistInstall.step);
const iconsInstall = b.addInstallFile(b.path("assets/cubyz/logo.icns"), "bin/Cubyz.app/Contents/Resources/logo.icns");
b.getInstallStep().dependOn(&iconsInstall.step);
// NOTE(blackedout): This is to make the Vulkan loader search in (bundle)/Contents/Frameworks to find the libs referenced in the manifest files
exe.root_module.addRPathSpecial("@loader_path/../Frameworks");
}
const installExe = b.addInstallArtifact(exe, exeInstallOptions);
b.getInstallStep().dependOn(&installExe.step);
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 dependencyWithTestRunner = b.lazyDependency("cubyz_test_runner", .{
.target = target,
.optimize = optimize,
}) orelse {
std.log.info("Downloading cubyz_test_runner dependency.", .{});
return;
};
const exe_tests = b.addTest(.{
.root_module = mainModule,
.test_runner = .{.path = dependencyWithTestRunner.path("lib/compiler/test_runner.zig"), .mode = .simple},
});
linkLibraries(b, exe_tests, useLocalDeps);
exe_tests.root_module.addOptions("build_options", options);
exe_tests.root_module.addImport("main", mainModule);
try addModFeatures(b, exe_tests);
const run_exe_tests = b.addRunArtifact(exe_tests);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_exe_tests.step);
// MARK: Formatter
const formatter = b.addExecutable(.{
.name = "CubyzFormatter",
.root_module = b.addModule("format", .{
.root_source_file = b.path("src/formatter/format.zig"),
.target = target,
.optimize = optimize,
}),
});
// ZLS is stupid and cannot detect which executable is the main one, so we add the import everywhere...
formatter.root_module.addOptions("build_options", options);
formatter.root_module.addImport("main", mainModule);
const formatter_install = b.addInstallArtifact(formatter, .{});
const formatter_cmd = b.addRunArtifact(formatter);
formatter_cmd.step.dependOn(&formatter_install.step);
if (b.args) |args| {
formatter_cmd.addArgs(args);
}
const formatter_step = b.step("format", "Check the formatting of the code");
formatter_step.dependOn(&formatter_cmd.step);
}