-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
200 lines (177 loc) · 7.34 KB
/
build.zig
File metadata and controls
200 lines (177 loc) · 7.34 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
const std = @import("std");
const RAYLIB_VERSION = "5.5";
const RAYLIB_URL = "https://github.com/raysan5/raylib/archive/refs/tags/5.5.tar.gz";
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const os_tag = target.result.os.tag;
const is_windows = os_tag == .windows;
const is_macos = os_tag == .macos;
const raylib_root = b.pathFromRoot("deps/raylib-" ++ RAYLIB_VERSION);
const raylib_header = b.pathJoin(&.{ raylib_root, "src", "raylib.h" });
// Upstream raylib 5.5 has a build.zig that uses APIs removed in Zig 0.15
// (ArrayList.init, defineCMacro). Bypass it entirely — fetch the source
// tarball ourselves and compile the C files directly.
ensureRaylib(b, raylib_header) catch |err| {
std.debug.print("failed to fetch raylib: {}\n", .{err});
std.process.exit(1);
};
const sqlite_dep = b.dependency("sqlite", .{});
// ── raylib static lib ─────────────────────────────────────────────────────
const raylib = b.addLibrary(.{
.name = "raylib",
.linkage = .static,
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
}),
});
raylib.root_module.addCSourceFiles(.{
.root = .{ .cwd_relative = raylib_root },
.files = &.{
"src/rcore.c",
"src/rshapes.c",
"src/rtextures.c",
"src/rtext.c",
"src/rmodels.c",
"src/raudio.c",
"src/utils.c",
"src/rglfw.c",
},
.flags = &.{
"-std=gnu99",
"-D_GNU_SOURCE",
"-DPLATFORM_DESKTOP",
"-DPLATFORM_DESKTOP_GLFW",
"-DGRAPHICS_API_OPENGL_33",
"-Wno-missing-braces",
"-Wno-unused-value",
"-Wno-unused-parameter",
"-Wno-unused-but-set-variable",
"-Wno-implicit-function-declaration",
"-fno-strict-aliasing",
},
});
raylib.root_module.addIncludePath(.{ .cwd_relative = b.pathJoin(&.{ raylib_root, "src" }) });
raylib.root_module.addIncludePath(.{ .cwd_relative = b.pathJoin(&.{ raylib_root, "src", "external" }) });
raylib.root_module.addIncludePath(.{ .cwd_relative = b.pathJoin(&.{ raylib_root, "src", "external", "glfw", "include" }) });
if (is_windows) {
raylib.root_module.linkSystemLibrary("opengl32", .{});
raylib.root_module.linkSystemLibrary("gdi32", .{});
raylib.root_module.linkSystemLibrary("winmm", .{});
raylib.root_module.linkSystemLibrary("shell32", .{});
raylib.root_module.linkSystemLibrary("user32", .{});
} else if (is_macos) {
raylib.root_module.linkFramework("OpenGL", .{});
raylib.root_module.linkFramework("Cocoa", .{});
raylib.root_module.linkFramework("IOKit", .{});
raylib.root_module.linkFramework("CoreAudio", .{});
raylib.root_module.linkFramework("CoreVideo", .{});
} else {
raylib.root_module.linkSystemLibrary("GL", .{});
raylib.root_module.linkSystemLibrary("m", .{});
raylib.root_module.linkSystemLibrary("pthread", .{});
raylib.root_module.linkSystemLibrary("dl", .{});
raylib.root_module.linkSystemLibrary("rt", .{});
raylib.root_module.linkSystemLibrary("X11", .{});
}
// ── sqlite3 static lib ────────────────────────────────────────────────────
const sqlite = b.addLibrary(.{
.name = "sqlite3",
.linkage = .static,
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
}),
});
sqlite.root_module.addCSourceFile(.{
.file = sqlite_dep.path("sqlite3.c"),
.flags = &.{
"-DSQLITE_THREADSAFE=1",
"-Wno-unused-but-set-variable",
"-Wno-unused-parameter",
"-Wno-unused-function",
},
});
sqlite.root_module.addIncludePath(sqlite_dep.path(""));
// ── understudy executable ─────────────────────────────────────────────────
const exe = b.addExecutable(.{
.name = "understudy",
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
}),
});
exe.root_module.addCSourceFiles(.{
.files = &.{
"src/main.c",
"src/canvas.c",
"src/toolbar.c",
"src/tools.c",
"src/ui.c",
"src/db.c",
},
.flags = &.{
"-std=c11",
"-Wall",
"-Wextra",
"-Wno-unused-parameter",
"-Wno-missing-field-initializers",
},
});
if (is_macos) {
exe.root_module.addCSourceFile(.{
.file = b.path("src/clipboard_mac.m"),
.flags = &.{ "-std=c11", "-Wall" },
});
} else if (is_windows) {
exe.root_module.addCSourceFile(.{
.file = b.path("src/clipboard_win.c"),
.flags = &.{ "-std=c11", "-Wall" },
});
}
exe.root_module.addIncludePath(.{ .cwd_relative = b.pathJoin(&.{ raylib_root, "src" }) });
exe.root_module.addIncludePath(sqlite_dep.path(""));
exe.root_module.linkLibrary(raylib);
exe.root_module.linkLibrary(sqlite);
if (is_windows) {
exe.root_module.linkSystemLibrary("user32", .{});
exe.root_module.linkSystemLibrary("gdi32", .{});
exe.root_module.linkSystemLibrary("shell32", .{});
exe.root_module.linkSystemLibrary("winmm", .{});
exe.root_module.linkSystemLibrary("opengl32", .{});
exe.subsystem = .Console;
} else if (is_macos) {
exe.root_module.linkFramework("Cocoa", .{});
exe.root_module.linkFramework("IOKit", .{});
exe.root_module.linkFramework("OpenGL", .{});
exe.root_module.linkFramework("CoreVideo", .{});
exe.root_module.linkFramework("CoreAudio", .{});
}
b.installArtifact(exe);
const run_step = b.step("run", "Run Understudy");
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| run_cmd.addArgs(args);
run_step.dependOn(&run_cmd.step);
}
fn ensureRaylib(b: *std.Build, marker: []const u8) !void {
std.fs.accessAbsolute(marker, .{}) catch {
std.fs.cwd().makePath("deps") catch {};
const tarball = "deps/raylib-" ++ RAYLIB_VERSION ++ ".tar.gz";
std.debug.print("fetching raylib {s}\n", .{RAYLIB_VERSION});
try runCmd(b, &.{ "curl", "-L", "--fail", "--silent", "--show-error", "-o", tarball, RAYLIB_URL });
try runCmd(b, &.{ "tar", "-xzf", tarball, "-C", "deps" });
std.fs.cwd().deleteFile(tarball) catch {};
};
}
fn runCmd(b: *std.Build, argv: []const []const u8) !void {
var child = std.process.Child.init(argv, b.allocator);
child.stderr_behavior = .Inherit;
child.stdout_behavior = .Inherit;
const term = try child.spawnAndWait();
if (term != .Exited or term.Exited != 0) return error.CommandFailed;
}