Repository navigation
Expand file tree
/
Copy pathbuild.zig
More file actions
269 lines (234 loc) · 10.5 KB
/
Copy pathbuild.zig
File metadata and controls
269 lines (234 loc) · 10.5 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
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)));
const translate_c = b.addTranslateC(.{
.root_source_file = b.path("src/c.h"),
.target = target,
.optimize = optimize,
});
translate_c.addIncludePath(headersDeps.path("include"));
exe.root_module.addImport("c", translate_c.createModule());
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("comdlg32", .{});
exe.root_module.linkSystemLibrary("crypt32", .{});
exe.root_module.linkSystemLibrary("gdi32", .{});
exe.root_module.linkSystemLibrary("ole32", .{});
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 addModFeatureModule(b: *std.Build, modFinderStep: *std.Build.Step.Run, exe: *std.Build.Step.Compile, modsFolder: std.Build.LazyPath, name: []const u8) !void {
const module = b.createModule(.{
.root_source_file = try modsFolder.join(b.allocator, b.fmt("{s}.zig", .{name})),
.target = exe.root_module.resolved_target,
.optimize = exe.root_module.optimize,
});
if (exe.kind == .@"test") {
const exe_tests = b.addTest(.{
.root_module = module,
.test_runner = exe.test_runner,
});
const run_exe_tests = b.addRunArtifact(exe_tests);
exe.step.dependOn(&run_exe_tests.step);
}
module.addImport("main", exe.root_module);
exe.step.dependOn(&modFinderStep.step);
exe.root_module.addImport(name, module);
}
fn addModFeatures(b: *std.Build, exe: *std.Build.Step.Compile) !void {
const modFinder = b.addExecutable(.{
.name = "mod_finder",
.root_module = b.createModule(.{
.root_source_file = b.path("scripts/mod_finder.zig"),
.target = b.graph.host,
}),
});
const modFinderStep = b.addRunArtifact(modFinder);
modFinderStep.addDirectoryArg(b.path("mods"));
modFinderStep.addDirectoryArg(b.path("mods"));
try addModFeatureModule(b, modFinderStep, exe, b.path("mods"), "rotations");
}
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,
\\ .threadPoolThreads = null, // Number of threads used by the threadPool, default is number of CPU cores - 1 (to leave room for rendering, audio, networking, server ticks, OS)
\\ // .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 sanitizeThread = b.option(bool, "sanitizeThread", "enables the builtin thread sanitizer");
var version = try std.SemanticVersion.parse("0.5.0");
if (b.option([]const u8, "version", "used by the CI to set the patch version, major and minor must match the ones in build.zig")) |tagVersion| {
const tagParsed = try std.SemanticVersion.parse(tagVersion);
if (tagParsed.major != version.major or tagParsed.minor != version.minor) {
std.log.err("Provided version {s} does not match version in build.zig: {f}", .{tagVersion, version});
return error.VersionMismatch;
}
version.patch = tagParsed.patch;
}
if (!isRelease) version.pre = "dev";
options.addOption([]const u8, "version", b.fmt("{f}", .{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,
.sanitize_thread = sanitizeThread,
});
const exe = b.addExecutable(.{
.name = "Cubyz",
.root_module = mainModule,
.use_llvm = if (sanitizeThread orelse false) true else null,
});
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());
run_cmd.addPassthruArgs();
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);
}