-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.zig
297 lines (260 loc) · 12.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
const std = @import("std");
const fs = std.fs;
const exampleList = @import("src/examples.zig").exampleList;
pub const APP_NAME = "raylib-zig-examples";
const raylibSrc = "src/raylib/raylib/src/";
const rayguiSrc = "src/raygui/raygui/src/";
const raylibBindingSrc = "src/raylib/";
const rayguiBindingSrc = "src/raygui/";
pub fn build(b: *std.Build) !void {
// 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 mode = b.standardOptimizeOption(.{});
const exampleNr = b.option(usize, "example", try exampleDescription(b)) orelse 1;
try writeExampleFile(exampleNr);
switch (target.result.os.tag) {
.wasi, .emscripten => {
const emscriptenSrc = "src/raylib/emscripten/";
const webCachedir = "zig-cache/web/";
const webOutdir = "zig-out/web/";
std.log.info("building for emscripten\n", .{});
if (b.sysroot == null) {
std.log.err("\n\nUSAGE: Please build with 'zig build -Doptimize=ReleaseSmall -Dtarget=wasm32-wasi --sysroot \"$EMSDK/upstream/emscripten\"'\n\n", .{});
return error.SysRootExpected;
}
const lib = b.addStaticLibrary(.{
.name = APP_NAME,
.root_source_file = std.Build.LazyPath.relative("src/web.zig"),
.optimize = mode,
.target = target,
});
lib.addIncludePath(.{ .path = raylibSrc });
lib.addIncludePath(.{ .path = rayguiSrc });
const emcc_file = switch (b.host.result.os.tag) {
.windows => "emcc.bat",
else => "emcc",
};
const emar_file = switch (b.host.result.os.tag) {
.windows => "emar.bat",
else => "emar",
};
const emranlib_file = switch (b.host.result.os.tag) {
.windows => "emranlib.bat",
else => "emranlib",
};
const emcc_path = try fs.path.join(b.allocator, &.{ b.sysroot.?, emcc_file });
defer b.allocator.free(emcc_path);
const emranlib_path = try fs.path.join(b.allocator, &.{ b.sysroot.?, emranlib_file });
defer b.allocator.free(emranlib_path);
const emar_path = try fs.path.join(b.allocator, &.{ b.sysroot.?, emar_file });
defer b.allocator.free(emar_path);
const include_path = try fs.path.join(b.allocator, &.{ b.sysroot.?, "cache", "sysroot", "include" });
defer b.allocator.free(include_path);
fs.cwd().makePath(webCachedir) catch {};
fs.cwd().makePath(webOutdir) catch {};
const warnings = ""; //-Wall
const rcoreO = b.addSystemCommand(&.{ emcc_path, "-Os", warnings, "-c", raylibSrc ++ "rcore.c", "-o", webCachedir ++ "rcore.o", "-Os", warnings, "-DPLATFORM_WEB", "-DGRAPHICS_API_OPENGL_ES2" });
const rshapesO = b.addSystemCommand(&.{ emcc_path, "-Os", warnings, "-c", raylibSrc ++ "rshapes.c", "-o", webCachedir ++ "rshapes.o", "-Os", warnings, "-DPLATFORM_WEB", "-DGRAPHICS_API_OPENGL_ES2" });
const rtexturesO = b.addSystemCommand(&.{ emcc_path, "-Os", warnings, "-c", raylibSrc ++ "rtextures.c", "-o", webCachedir ++ "rtextures.o", "-Os", warnings, "-DPLATFORM_WEB", "-DGRAPHICS_API_OPENGL_ES2" });
const rtextO = b.addSystemCommand(&.{ emcc_path, "-Os", warnings, "-c", raylibSrc ++ "rtext.c", "-o", webCachedir ++ "rtext.o", "-Os", warnings, "-DPLATFORM_WEB", "-DGRAPHICS_API_OPENGL_ES2" });
const rmodelsO = b.addSystemCommand(&.{ emcc_path, "-Os", warnings, "-c", raylibSrc ++ "rmodels.c", "-o", webCachedir ++ "rmodels.o", "-Os", warnings, "-DPLATFORM_WEB", "-DGRAPHICS_API_OPENGL_ES2" });
const utilsO = b.addSystemCommand(&.{ emcc_path, "-Os", warnings, "-c", raylibSrc ++ "utils.c", "-o", webCachedir ++ "utils.o", "-Os", warnings, "-DPLATFORM_WEB" });
const raudioO = b.addSystemCommand(&.{ emcc_path, "-Os", warnings, "-c", raylibSrc ++ "raudio.c", "-o", webCachedir ++ "raudio.o", "-Os", warnings, "-DPLATFORM_WEB" });
const libraylibA = b.addSystemCommand(&.{
emar_path,
"rcs",
webCachedir ++ "libraylib.a",
webCachedir ++ "rcore.o",
webCachedir ++ "rshapes.o",
webCachedir ++ "rtextures.o",
webCachedir ++ "rtext.o",
webCachedir ++ "rmodels.o",
webCachedir ++ "utils.o",
webCachedir ++ "raudio.o",
});
const emranlib = b.addSystemCommand(&.{
emranlib_path,
webCachedir ++ "libraylib.a",
});
libraylibA.step.dependOn(&rcoreO.step);
libraylibA.step.dependOn(&rshapesO.step);
libraylibA.step.dependOn(&rtexturesO.step);
libraylibA.step.dependOn(&rtextO.step);
libraylibA.step.dependOn(&rmodelsO.step);
libraylibA.step.dependOn(&utilsO.step);
libraylibA.step.dependOn(&raudioO.step);
emranlib.step.dependOn(&libraylibA.step);
//only build raylib if not already there
_ = fs.cwd().statFile(webCachedir ++ "libraylib.a") catch {
lib.step.dependOn(&emranlib.step);
};
lib.defineCMacro("__EMSCRIPTEN__", null);
lib.defineCMacro("PLATFORM_WEB", null);
std.log.info("emscripten include path: {s}", .{include_path});
lib.addIncludePath(.{ .path = include_path });
lib.addIncludePath(.{ .path = emscriptenSrc });
lib.addIncludePath(.{ .path = raylibBindingSrc });
lib.addIncludePath(.{ .path = rayguiBindingSrc });
lib.addIncludePath(.{ .path = raylibSrc });
lib.addIncludePath(.{ .path = rayguiSrc });
lib.addIncludePath(.{ .path = raylibSrc ++ "extras/" });
lib.root_module.addAnonymousImport("raylib", .{ .root_source_file = .{ .path = raylibBindingSrc ++ "raylib.zig" } });
lib.root_module.addAnonymousImport("raygui", .{
.root_source_file = .{ .path = rayguiBindingSrc ++ "raygui.zig" },
.imports = &.{
.{ .name = "raylib", .module = lib.root_module.import_table.get("raylib").? },
},
});
const libraryOutputFolder = "zig-out/lib/";
// this installs the lib (libraylib-zig-examples.a) to the `libraryOutputFolder` folder
b.installArtifact(lib);
const shell = switch (mode) {
.Debug => emscriptenSrc ++ "shell.html",
else => emscriptenSrc ++ "minshell.html",
};
const emcc = b.addSystemCommand(&.{
emcc_path,
"-o",
webOutdir ++ "game.html",
emscriptenSrc ++ "entry.c",
raylibBindingSrc ++ "marshal.c",
rayguiBindingSrc ++ "raygui_marshal.c",
libraryOutputFolder ++ "lib" ++ APP_NAME ++ ".a",
"-I.",
"-I" ++ raylibSrc,
"-I" ++ rayguiSrc,
"-I" ++ emscriptenSrc,
"-I" ++ raylibBindingSrc,
"-I" ++ rayguiBindingSrc,
"-L.",
"-L" ++ webCachedir,
"-L" ++ libraryOutputFolder,
"-lraylib",
"-l" ++ APP_NAME,
"--shell-file",
shell,
"-DPLATFORM_WEB",
"-DRAYGUI_IMPLEMENTATION",
"-sUSE_GLFW=3",
"-sWASM=1",
"-sALLOW_MEMORY_GROWTH=1",
"-sWASM_MEM_MAX=512MB", //going higher than that seems not to work on iOS browsers ¯\_(ツ)_/¯
"-sTOTAL_MEMORY=512MB",
"-sABORTING_MALLOC=0",
"-sASYNCIFY",
"-sFORCE_FILESYSTEM=1",
"-sASSERTIONS=1",
"--memory-init-file",
"0",
"--preload-file",
"assets",
"--source-map-base",
"-O1",
"-Os",
// "-sLLD_REPORT_UNDEFINED",
"-sERROR_ON_UNDEFINED_SYMBOLS=0",
// optimizations
"-O1",
"-Os",
// "-sUSE_PTHREADS=1",
// "--profiling",
// "-sTOTAL_STACK=128MB",
// "-sMALLOC='emmalloc'",
// "--no-entry",
"-sEXPORTED_FUNCTIONS=['_malloc','_free','_main', '_emsc_main','_emsc_set_window_size']",
"-sEXPORTED_RUNTIME_METHODS=ccall,cwrap",
});
emcc.step.dependOn(&lib.step);
b.getInstallStep().dependOn(&emcc.step);
//-------------------------------------------------------------------------------------
std.log.info("\n\nOutput files will be in {s}\n---\ncd {s}\npython -m http.server\n---\n\nbuilding...", .{ webOutdir, webOutdir });
},
else => {
std.log.info("building for desktop\n", .{});
const exe = b.addExecutable(.{
.name = APP_NAME,
.root_source_file = std.Build.LazyPath.relative("src/desktop.zig"),
.optimize = mode,
.target = target,
});
const raylib = @import("src/raylib/build.zig");
const raygui = @import("src/raygui/build.zig");
raylib.addTo(b, exe, target.query, mode, .{});
raygui.addTo(b, exe, target.query, mode);
switch (target.result.os.tag) {
.macos => {
exe.linkFramework("Foundation");
exe.linkFramework("Cocoa");
exe.linkFramework("OpenGL");
exe.linkFramework("CoreAudio");
exe.linkFramework("CoreVideo");
exe.linkFramework("IOKit");
},
.linux => {
exe.addLibraryPath(.{ .path = "/usr/lib64/" });
exe.linkSystemLibrary("GL");
exe.linkSystemLibrary("rt");
exe.linkSystemLibrary("dl");
exe.linkSystemLibrary("m");
exe.linkSystemLibrary("X11");
},
else => {},
}
exe.linkLibC();
b.installArtifact(exe);
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);
},
}
}
fn writeExampleFile(exampleNr: usize) !void {
var tmp: [4096]u8 = undefined;
var fba = std.heap.FixedBufferAllocator.init(&tmp);
for (exampleList, 1..) |example, i| {
if (exampleNr == i) {
var load_example = try std.fs.cwd().createFile("src/load_example.zig", .{});
defer load_example.close();
try load_example.writeAll(try std.fmt.allocPrint(fba.allocator(), "pub const name = \"{s}\";\n", .{example}));
return;
}
}
}
fn exampleDescription(b: *std.Build) ![]const u8 {
const buf: []u8 = try b.allocator.alloc(u8, 4096);
var tmp: [4096]u8 = undefined;
var fba = std.heap.FixedBufferAllocator.init(&tmp);
const Context = struct {
index: usize,
buf: []u8,
allocator: std.mem.Allocator,
};
const Writer = std.io.Writer(*Context, error{OutOfMemory}, (struct {
fn write(ctx: *Context, bytes: []const u8) error{OutOfMemory}!usize {
if (ctx.buf.len < ctx.index + bytes.len) {
return error.OutOfMemory;
}
std.mem.copyForwards(u8, ctx.buf[ctx.index..], bytes);
ctx.index += bytes.len;
return bytes.len;
}
}).write);
var ctx: Context = .{ .index = 0, .buf = buf, .allocator = fba.allocator() };
var w: Writer = .{ .context = &ctx };
try w.writeAll("Select which example should be built\n\nExamples:\n--------------------------------------------------\n");
for (exampleList, 0..) |example, i| {
defer fba.reset();
try w.writeAll(try std.fmt.allocPrint(fba.allocator(), "{d}:\t{s}\n", .{ i + 1, example }));
}
try w.writeAll("--------------------------------------------------\n\n");
return w.context.buf[0..w.context.index];
}