-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.zig
298 lines (268 loc) · 10.8 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
const std = @import("std");
const sokol = @import("sokol");
const sdl = @import("sdl");
const Child = std.process.Child;
var mod_zi: *std.Build.Module = undefined;
fn sdkPath(comptime suffix: []const u8) []const u8 {
if (suffix[0] != '/') @compileError("relToPath requires an absolute path!");
return comptime blk: {
const root_dir = std.fs.path.dirname(@src().file) orelse ".";
break :blk root_dir ++ suffix;
};
}
pub const PlatformAndRenderer = enum {
sdl,
sdl_soft,
sokol,
};
pub const PlatformCreateOptions = struct {
platform_renderer: PlatformAndRenderer = .sdl,
target: std.Build.ResolvedTarget = undefined,
optimize: std.builtin.OptimizeMode = undefined,
};
fn getPlatformModule(b: *std.Build, options: PlatformCreateOptions) *std.Build.Module {
const target = options.target;
const optimize = options.optimize;
// create common module
const mod_common = b.createModule(.{
.root_source_file = .{ .cwd_relative = sdkPath("/src/zimpact/common.zig") },
.target = target,
.optimize = optimize,
});
var mod_platform: *std.Build.Module = undefined;
switch (options.platform_renderer) {
.sdl_soft => {
const sdl_sdk = sdl.init(b, "");
// SDL platform module
mod_platform = b.createModule(.{
.root_source_file = .{ .cwd_relative = sdkPath("/src/zimpact/platform_sdl_soft.zig") },
.target = target,
.optimize = optimize,
});
mod_platform.addImport("sdl", sdl_sdk.getNativeModule());
},
.sdl => {
const sdl_sdk = sdl.init(b, "");
// SDL platform module
mod_platform = b.createModule(.{
.root_source_file = .{ .cwd_relative = sdkPath("/src/zimpact/platform_sdl.zig") },
.target = target,
.optimize = optimize,
});
mod_platform.addImport("sdl", sdl_sdk.getNativeModule());
},
.sokol => {
// sokol platform module
mod_platform = b.createModule(.{
.root_source_file = .{ .cwd_relative = sdkPath("/src/zimpact/platform_sokol.zig") },
.target = target,
.optimize = optimize,
});
const dep_sokol = b.dependency("sokol", .{
.target = target,
.optimize = optimize,
});
mod_platform.addImport("sokol", dep_sokol.module("sokol"));
},
}
mod_platform.addImport("common", mod_common);
return mod_platform;
}
pub fn getZimpactModule(b: *std.Build, options: PlatformCreateOptions) *std.Build.Module {
var mod_platform: *std.Build.Module = undefined;
mod_platform = getPlatformModule(b, .{
.platform_renderer = options.platform_renderer,
.target = options.target,
.optimize = options.optimize,
});
// zimpact module
mod_zi = b.addModule("zimpact", .{
.root_source_file = .{ .cwd_relative = sdkPath("/src/zimpact/zimpact.zig") },
.target = options.target,
.optimize = options.optimize,
});
mod_zi.addImport("platform", mod_platform);
return mod_zi;
}
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const platform = b.option([]const u8, "platform", "Plaftorm to use: sdl, sdl_soft or sokol") orelse "sdl";
var platform_renderer: PlatformAndRenderer = .sdl;
if (target.result.isWasm()) {
platform_renderer = .sdl;
} else if (std.mem.eql(u8, platform, "sokol")) {
platform_renderer = .sokol;
} else if (std.mem.eql(u8, platform, "sdl_soft")) {
platform_renderer = .sdl_soft;
}
const sdl_sdk = sdl.init(b, null);
_ = getZimpactModule(b, .{
.optimize = optimize,
.target = target,
.platform_renderer = platform_renderer,
});
// build lib
const lib = b.addStaticLibrary(.{
.name = "zimpact",
.root_source_file = b.path("src/zimpact/zimpact.zig"),
.target = target,
.optimize = optimize,
});
b.installArtifact(lib);
// build docs
const docs = b.addInstallDirectory(.{
.source_dir = lib.getEmittedDocs(),
.install_dir = .prefix,
.install_subdir = "docs",
});
var docs_step = b.step("docs", "Build docs");
docs_step.dependOn(&docs.step);
b.getInstallStep().dependOn(docs_step);
const asset_dir = "samples/zdrop/assets";
const assets_step = try buildAssets(b, asset_dir);
// build Z Drop sample
const sample: []const u8 = "zdrop";
if (!target.result.isWasm()) {
// for native platforms, build into a regular executable
const exe = b.addExecutable(.{
.name = sample,
.root_source_file = b.path(b.fmt("samples/{s}/main.zig", .{sample})),
.target = target,
.optimize = optimize,
});
if (platform_renderer == .sdl or platform_renderer == .sdl_soft) {
sdl_sdk.link(exe, .dynamic);
}
exe.root_module.addImport("zimpact", mod_zi);
const install_exe = b.addInstallArtifact(exe, .{});
install_exe.step.dependOn(assets_step);
b.getInstallStep().dependOn(&install_exe.step);
const run_cmd = b.addRunArtifact(exe);
const run_step = b.step(b.fmt("run", .{}), b.fmt("Run {s}.zig example", .{sample}));
run_cmd.step.dependOn(&install_exe.step);
run_step.dependOn(&run_cmd.step);
if (b.args) |args| {
run_cmd.addArgs(args);
}
} else {
try buildWeb(b, .{
.root_source_file = b.path("samples/zdrop/main.zig"),
.target = target,
.optimize = optimize,
.assets_step = assets_step,
.mod_zi = mod_zi,
.output_name = "zdrop",
});
}
}
fn convert(b: *std.Build, tool: *std.Build.Step.Compile, input: []const u8, output: []const u8) *std.Build.Step.InstallFile {
const tool_step = b.addRunArtifact(tool);
tool_step.addFileArg(b.path(input));
const out = tool_step.addOutputFileArg(std.fs.path.basename(output));
// b.getInstallStep().dependOn(&b.addInstallBinFile(out, output).step);
return b.addInstallBinFile(out, output);
}
pub const BuildWebOptions = struct {
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
root_source_file: ?std.Build.LazyPath = null,
assets_step: *std.Build.Step,
mod_zi: *std.Build.Module,
output_name: []const u8,
};
// for web builds, the Zig code needs to be built into a library and linked with the Emscripten linker
pub fn buildWeb(b: *std.Build, options: BuildWebOptions) !void {
const dep_sokol = b.dependency("sokol", .{
.target = options.target,
.optimize = options.optimize,
});
const sample = b.addStaticLibrary(.{
.name = options.output_name,
.target = options.target,
.optimize = options.optimize,
.root_source_file = options.root_source_file,
});
sample.root_module.addImport("zimpact", options.mod_zi);
sample.root_module.addImport("sokol", dep_sokol.module("sokol"));
// create a build step which invokes the Emscripten linker
const emsdk = dep_sokol.builder.dependency("emsdk", .{});
const link_step = try sokol.emLinkStep(b, .{
.lib_main = sample,
.target = options.target,
.optimize = options.optimize,
.emsdk = emsdk,
.use_webgl2 = true,
.use_emmalloc = true,
.use_filesystem = true,
.shell_file_path = sdkPath("/web/shell.html"),
.extra_args = &.{ "-sUSE_OFFSET_CONVERTER=1", "--preload-file", "zig-out/bin/assets@assets" },
});
// ...and a special run step to start the web build output via 'emrun'
const run = sokol.emRunStep(b, .{ .name = options.output_name, .emsdk = emsdk });
run.step.dependOn(options.assets_step);
run.step.dependOn(&link_step.step);
b.step("run", "Run zdrop").dependOn(&run.step);
}
pub fn buildAssets(b: *std.Build, asset_dir: []const u8) !*std.Build.Step {
// build qoiconv executable
const qoiconv_exe = b.addExecutable(.{
.name = "qoiconv",
.target = b.host,
.optimize = .ReleaseFast,
});
qoiconv_exe.linkLibC();
qoiconv_exe.addCSourceFile(.{
.file = .{ .cwd_relative = sdkPath("/tools/qoiconv.c") },
.flags = &[_][]const u8{"-std=c99"},
});
const qoiconv_step = b.step("qoiconv", "Build qoiconv");
qoiconv_step.dependOn(&qoiconv_exe.step);
// build qoaconv executable
const qoaconv_exe = b.addExecutable(.{
.name = "qoaconv",
.target = b.host,
.optimize = .ReleaseFast,
});
qoaconv_exe.linkLibC();
qoaconv_exe.addCSourceFile(.{
.file = .{ .cwd_relative = sdkPath("/tools/qoaconv.c") },
.flags = &[_][]const u8{"-std=c99"},
});
const qoaconv_step = b.step("qoaconv", "Build qoaconv");
qoaconv_step.dependOn(&qoaconv_exe.step);
// convert the assets and install them
const assets_step = b.step("assets", "Build assets");
assets_step.dependOn(qoiconv_step);
assets_step.dependOn(qoaconv_step);
if (std.fs.cwd().openDir(asset_dir, .{ .iterate = true })) |dir| {
var walker = try dir.walk(b.allocator);
defer walker.deinit();
while (try walker.next()) |assets_file| {
switch (assets_file.kind) {
.directory => {},
.file => {
const ext = std.fs.path.extension(assets_file.path);
const file = std.fs.path.stem(assets_file.basename);
const input = b.fmt("{s}/{s}", .{ asset_dir, assets_file.path });
const out_dir = std.fs.path.dirname(assets_file.path);
if (std.mem.eql(u8, ext, ".png")) {
// convert .png to .qoi
const output = if (out_dir) |d| b.fmt("assets/{s}/{s}.qoi", .{ d, file }) else b.fmt("assets/{s}.qoi", .{file});
assets_step.dependOn(&convert(b, qoiconv_exe, input, output).step);
} else if (std.mem.eql(u8, ext, ".wav")) {
// convert .wav to .qoa
const output = if (out_dir) |d| b.fmt("assets/{s}/{s}.qoa", .{ d, file }) else b.fmt("assets/{s}.qoa", .{file});
assets_step.dependOn(&convert(b, qoaconv_exe, input, output).step);
} else {
// just copy the asset
const output = if (out_dir) |d| b.fmt("assets/{s}/{s}{s}", .{ d, file, ext }) else b.fmt("assets/{s}{s}", .{ file, ext });
assets_step.dependOn(&b.addInstallFileWithDir(b.path(input), .bin, output).step);
}
},
else => {},
}
}
} else |_| {}
return assets_step;
}