-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathbuild.zig
240 lines (211 loc) · 9.32 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
const std = @import("std");
const builtin = @import("builtin");
const glfw = @import("mach_glfw");
const gpu = @import("mach_gpu");
pub fn build(b: *std.Build) !void {
const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{});
const mach_gpu_dep = b.dependency("mach_gpu", .{
.target = target,
.optimize = optimize,
});
const sysgpu_dep = b.dependency("mach_sysgpu", .{
.target = target,
.optimize = optimize,
});
const module = b.addModule("mach-core", .{
.root_source_file = .{ .path = "src/main.zig" },
.imports = &.{
.{ .name = "mach-gpu", .module = mach_gpu_dep.module("mach-gpu") },
.{ .name = "mach-sysgpu", .module = sysgpu_dep.module("mach-sysgpu") },
},
});
if (target.result.cpu.arch == .wasm32) {
const sysjs_dep = b.dependency("mach_sysjs", .{
.target = target,
.optimize = optimize,
});
module.addImport("mach-sysjs", sysjs_dep.module("mach-sysjs"));
} else {
const mach_glfw_dep = b.dependency("mach_glfw", .{ .target = target, .optimize = optimize });
const gamemode_dep = b.dependency("mach_gamemode", .{ .target = target, .optimize = optimize });
const x11_headers = b.dependency("x11_headers", .{ .target = target, .optimize = optimize });
const wayland_headers = b.dependency("wayland_headers", .{ .target = target, .optimize = optimize });
module.addImport("mach-glfw", mach_glfw_dep.module("mach-glfw"));
module.addImport("mach-gamemode", gamemode_dep.module("mach-gamemode"));
module.linkLibrary(x11_headers.artifact("x11-headers"));
module.linkLibrary(wayland_headers.artifact("wayland-headers"));
module.addCSourceFile(.{ .file = .{ .path = "src/platform/wayland/wayland.c" } });
const test_platform = b.option(App.Platform, "test_platform", "The mach core platform to use when running tests");
const main_tests = b.addTest(.{
.name = "core-tests",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
var iter = module.import_table.iterator();
while (iter.next()) |e| {
main_tests.root_module.addImport(e.key_ptr.*, e.value_ptr.*);
}
link(b, main_tests, &main_tests.root_module);
b.installArtifact(main_tests);
const platform_options = b.addOptions();
platform_options.addOption(App.Platform, "platform", test_platform orelse App.Platform.fromTarget(target.result));
main_tests.root_module.addOptions("platform_options", platform_options);
const test_step = b.step("test", "run tests");
test_step.dependOn(&b.addRunArtifact(main_tests).step);
// TODO: autodoc segfaults the build if we have this enabled
// https://github.com/hexops/mach/issues/1145
//
// const install_docs = b.addInstallDirectory(.{
// .source_dir = main_tests.getEmittedDocs(),
// .install_dir = .prefix, // default build output prefix, ./zig-out
// .install_subdir = "docs",
// });
// const docs_step = b.step("docs", "Generate API docs");
// docs_step.dependOn(&install_docs.step);
}
try @import("build_examples.zig").build(b, optimize, target, module);
}
fn sdkPath(comptime suffix: []const u8) []const u8 {
if (suffix[0] != '/') @compileError("suffix must be an absolute path");
return comptime blk: {
const root_dir = std.fs.path.dirname(@src().file) orelse ".";
break :blk root_dir ++ suffix;
};
}
pub const App = struct {
b: *std.Build,
name: []const u8,
compile: *std.Build.Step.Compile,
install: *std.Build.Step.InstallArtifact,
run: *std.Build.Step.Run,
platform: Platform,
res_dirs: ?[]const []const u8,
watch_paths: ?[]const []const u8,
pub const Platform = enum {
glfw,
x11,
wayland,
web,
pub fn fromTarget(target: std.Target) Platform {
if (target.cpu.arch == .wasm32) return .web;
return .glfw;
}
};
pub fn init(
app_builder: *std.Build,
core_builder: *std.Build,
options: struct {
name: []const u8,
src: []const u8,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
custom_entrypoint: ?[]const u8 = null,
deps: ?[]const std.Build.Module.Import = null,
res_dirs: ?[]const []const u8 = null,
watch_paths: ?[]const []const u8 = null,
mach_core_mod: ?*std.Build.Module = null,
platform: ?Platform,
},
) !App {
const target = options.target.result;
const platform = options.platform orelse Platform.fromTarget(target);
var imports = std.ArrayList(std.Build.Module.Import).init(app_builder.allocator);
const mach_core_mod = options.mach_core_mod orelse app_builder.dependency("mach_core", .{
.target = options.target,
.optimize = options.optimize,
}).module("mach-core");
try imports.append(.{
.name = "mach-core",
.module = mach_core_mod,
});
if (options.deps) |app_deps| try imports.appendSlice(app_deps);
const app_module = app_builder.createModule(.{
.root_source_file = .{ .path = options.src },
.imports = try imports.toOwnedSlice(),
});
//Tell mach-core about the chosen platform
const platform_options = app_builder.addOptions();
platform_options.addOption(Platform, "platform", platform);
mach_core_mod.addOptions("platform_options", platform_options);
const compile = blk: {
if (platform == .web) {
// wasm libraries should go into zig-out/www/
app_builder.lib_dir = app_builder.fmt("{s}/www", .{app_builder.install_path});
const lib = app_builder.addStaticLibrary(.{
.name = options.name,
.root_source_file = .{ .path = options.custom_entrypoint orelse sdkPath("/src/platform/wasm/entrypoint.zig") },
.target = options.target,
.optimize = options.optimize,
});
lib.rdynamic = true;
break :blk lib;
} else {
const exe = app_builder.addExecutable(.{
.name = options.name,
.root_source_file = .{ .path = options.custom_entrypoint orelse sdkPath("/src/platform/native_entrypoint.zig") },
.target = options.target,
.optimize = options.optimize,
});
// TODO(core): figure out why we need to disable LTO: https://github.com/hexops/mach/issues/597
exe.want_lto = false;
break :blk exe;
}
};
compile.root_module.addImport("mach-core", mach_core_mod);
compile.root_module.addImport("app", app_module);
// Installation step
app_builder.installArtifact(compile);
const install = app_builder.addInstallArtifact(compile, .{});
if (options.res_dirs) |res_dirs| {
for (res_dirs) |res| {
const install_res = app_builder.addInstallDirectory(.{
.source_dir = .{ .path = res },
.install_dir = install.dest_dir.?,
.install_subdir = std.fs.path.basename(res),
.exclude_extensions = &.{},
});
install.step.dependOn(&install_res.step);
}
}
if (platform == .web) {
inline for (.{ sdkPath("/src/platform/wasm/mach.js"), @import("mach_sysjs").getJSPath() }) |js| {
const install_js = app_builder.addInstallFileWithDir(
.{ .path = js },
std.Build.InstallDir{ .custom = "www" },
std.fs.path.basename(js),
);
install.step.dependOn(&install_js.step);
}
}
// Link dependencies
if (platform != .web) {
link(core_builder, compile, &compile.root_module);
}
const run = app_builder.addRunArtifact(compile);
run.step.dependOn(&install.step);
return .{
.b = app_builder,
.compile = compile,
.install = install,
.run = run,
.name = options.name,
.platform = platform,
.res_dirs = options.res_dirs,
.watch_paths = options.watch_paths,
};
}
};
pub fn link(core_builder: *std.Build, step: *std.Build.Step.Compile, mod: *std.Build.Module) void {
gpu.link(core_builder.dependency("mach_gpu", .{
.target = step.root_module.resolved_target orelse core_builder.host,
.optimize = step.root_module.optimize.?,
}).builder, step, mod, .{}) catch unreachable;
}
comptime {
const supported_zig = std.SemanticVersion.parse("0.12.0-dev.2063+804cee3b9") catch unreachable;
if (builtin.zig_version.order(supported_zig) != .eq) {
@compileError(std.fmt.comptimePrint("unsupported Zig version ({}). Required Zig version 2024.1.0-mach: https://machengine.org/about/nominated-zig/#202410-mach", .{builtin.zig_version}));
}
}